Repository navigation
Expand file tree
/
Copy pathpaths.ts
More file actions
113 lines (95 loc) · 3.39 KB
/
Copy pathpaths.ts
File metadata and controls
113 lines (95 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { join, extname, relative } from 'node:path';
import { fileURLToPath } from 'node:url';
import { ensure_end, strip_end } from '@fuzdev/fuz_util/string.ts';
import { styleText as st } from 'node:util';
import type { PathId } from '@fuzdev/fuz_util/path.ts';
import {
GRO_CONFIG_FILENAME,
GRO_DEV_DIR,
GRO_DIR,
LIB_DIR,
SOURCE_DIR,
SVELTEKIT_DIST_DIRNAME
} from './constants.ts';
/*
A path `id` is an absolute path to the source/.gro/dist directory.
It's the same name that Rollup uses.
*/
/*
`paths` is built from the conventional locations in `./constants.ts`, never from the
SvelteKit `files` config values - reading those costs a full Vite config resolution,
which is too expensive for a module every Gro invocation imports.
*/
export interface Paths {
/** @trailing_slash */
root: string;
/** @trailing_slash */
source: string;
/** @trailing_slash */
lib: string;
/** @trailing_slash */
build: string;
/** @trailing_slash */
build_dev: string;
config: string;
}
export const create_paths = (root_dir: string): Paths => {
// TODO remove reliance on trailing slash towards windows support
const root = ensure_end(root_dir, '/');
return {
root,
source: root + SOURCE_DIR,
lib: root + LIB_DIR,
build: root + GRO_DIR,
build_dev: root + GRO_DEV_DIR,
config: root + GRO_CONFIG_FILENAME
};
};
export const infer_paths = (id: PathId): Paths => (is_gro_id(id) ? gro_paths : paths);
export const is_gro_id = (id: PathId): boolean =>
id.startsWith(gro_paths.root) || gro_paths.root === ensure_end(id, '/');
// '/home/me/app/src/foo/bar/baz.ts' → 'src/foo/bar/baz.ts'
export const to_root_path = (id: PathId, p = infer_paths(id)): string =>
relative(p.root, id) || './';
// '/home/me/app/src/foo/bar/baz.ts' → 'foo/bar/baz.ts'
export const path_id_to_base_path = (path_id: PathId, p = infer_paths(path_id)): string =>
relative(p.source, path_id);
// TODO base_path is an obsolete concept, it was a remnant from forcing `src/`
// 'foo/bar/baz.ts' → '/home/me/app/src/foo/bar/baz.ts'
export const base_path_to_path_id = (base_path: string, p = infer_paths(base_path)): PathId =>
join(p.source, base_path);
export const print_path = (path: string, p = infer_paths(path)): string => {
let final_path =
strip_end(path, '/') === strip_end(GRO_DIST_DIR, '/') ? 'gro' : to_root_path(path, p);
final_path =
final_path === 'gro' ? final_path : final_path[0] === '.' ? final_path : './' + final_path;
return st('gray', final_path);
};
export const replace_extension = (path: string, new_extension: string): string => {
const { length } = extname(path);
return (length === 0 ? path : path.substring(0, path.length - length)) + new_extension;
};
/**
* `Paths` for the user repo.
*/
export const paths = create_paths(process.cwd());
/** @trailing_slash */
export const GRO_PACKAGE_DIR = 'gro/';
// TODO document these conditions with comments
// TODO there's probably a more robust way to do this
const filename = fileURLToPath(import.meta.url);
const gro_package_dir_path = join(
filename,
filename.includes('/gro/src/lib/')
? '../../../'
: filename.includes('/gro/dist/')
? '../../'
: '../'
);
export const IS_THIS_GRO = gro_package_dir_path === paths.root;
/**
* `Paths` for the Gro package being used by the user repo.
*/
export const gro_paths = IS_THIS_GRO ? paths : create_paths(gro_package_dir_path);
/** @trailing_slash */
export const GRO_DIST_DIR = gro_paths.root + SVELTEKIT_DIST_DIRNAME + '/';