@travetto/manifest 5.0.5 → 5.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/context.ts +38 -126
package/package.json
CHANGED
package/src/context.ts
CHANGED
|
@@ -5,159 +5,71 @@ import { createRequire } from 'node:module';
|
|
|
5
5
|
import type { Package } from './types/package';
|
|
6
6
|
import type { ManifestContext } from './types/context';
|
|
7
7
|
|
|
8
|
-
type Pkg
|
|
9
|
-
type PathOp = (file: string) => string;
|
|
10
|
-
type Workspace = Pkg<{
|
|
11
|
-
mono: boolean;
|
|
12
|
-
manager: 'yarn' | 'npm';
|
|
13
|
-
resolve: PathOp;
|
|
14
|
-
stripRoot: PathOp;
|
|
15
|
-
}>;
|
|
8
|
+
type Pkg = Package & { path: string };
|
|
16
9
|
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
10
|
+
// eslint-disable-next-line no-bitwise
|
|
11
|
+
const toPort = (pth: string): number => (Math.abs([...pth].reduce((a, b) => (a * 33) ^ b.charCodeAt(0), 5381)) % 29000) + 20000;
|
|
12
|
+
const toPosix = (pth: string): string => pth.replaceAll('\\', '/');
|
|
13
|
+
const readPackage = (file: string): Pkg => ({ ...JSON.parse(readFileSync(file, 'utf8')), path: toPosix(path.dirname(file)) });
|
|
21
14
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
*/
|
|
27
|
-
function readPackage(dir: string): Pkg | undefined {
|
|
28
|
-
dir = dir.endsWith('.json') ? path.dirname(dir) : dir;
|
|
29
|
-
try {
|
|
30
|
-
const v = readFileSync(path.resolve(dir, 'package.json'), 'utf8');
|
|
31
|
-
return ({ ...JSON.parse(v), path: path.resolve(dir) });
|
|
32
|
-
} catch { }
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Find package.json for a given folder
|
|
37
|
-
*/
|
|
38
|
-
function findPackage(dir: string): Pkg {
|
|
39
|
-
let prev;
|
|
40
|
-
let pkg, curr = path.resolve(dir);
|
|
41
|
-
while (!pkg && curr !== prev) {
|
|
42
|
-
pkg = readPackage(curr);
|
|
43
|
-
[prev, curr] = [curr, path.dirname(curr)];
|
|
44
|
-
}
|
|
45
|
-
if (!pkg) {
|
|
46
|
-
throw new Error('Could not find a package.json');
|
|
47
|
-
} else {
|
|
48
|
-
return pkg;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Get workspace root
|
|
54
|
-
*/
|
|
55
|
-
function resolveWorkspace(base: string = process.cwd()): Workspace {
|
|
56
|
-
if (base in WS_ROOT) {
|
|
57
|
-
return WS_ROOT[base];
|
|
58
|
-
}
|
|
59
|
-
let folder = base;
|
|
60
|
-
let prev: string | undefined;
|
|
15
|
+
/** Find package */
|
|
16
|
+
function findPackage(base: string, pred: (_p?: Pkg) => boolean): Pkg {
|
|
17
|
+
let folder = `${base}/.`;
|
|
18
|
+
let prev: string;
|
|
61
19
|
let pkg: Pkg | undefined;
|
|
62
20
|
|
|
63
|
-
|
|
21
|
+
do {
|
|
64
22
|
prev = folder;
|
|
65
|
-
pkg = readPackage(folder) ?? pkg;
|
|
66
|
-
if (
|
|
67
|
-
(pkg && (!!pkg.workspaces || !!pkg.travetto?.build?.isolated)) || // if we have a monorepo root, or we are isolated
|
|
68
|
-
existsSync(path.resolve(folder, '.git')) // we made it to the source repo root
|
|
69
|
-
) {
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
23
|
folder = path.dirname(folder);
|
|
73
|
-
|
|
24
|
+
const folderPkg = path.resolve(folder, 'package.json');
|
|
25
|
+
pkg = existsSync(folderPkg) ? readPackage(folderPkg) : pkg;
|
|
26
|
+
} while (
|
|
27
|
+
prev !== folder && // Not at root
|
|
28
|
+
!pred(pkg) && // Matches criteria
|
|
29
|
+
!existsSync(path.resolve(folder, '.git')) // Not at source root
|
|
30
|
+
);
|
|
74
31
|
|
|
75
32
|
if (!pkg) {
|
|
76
33
|
throw new Error('Could not find a package.json');
|
|
77
34
|
}
|
|
78
35
|
|
|
79
|
-
return
|
|
80
|
-
...pkg,
|
|
81
|
-
name: pkg.name ?? 'untitled',
|
|
82
|
-
type: pkg.type,
|
|
83
|
-
manager: existsSync(path.resolve(pkg.path, 'yarn.lock')) ? 'yarn' : 'npm',
|
|
84
|
-
resolve: createRequire(`${pkg.path}/node_modules`).resolve.bind(null),
|
|
85
|
-
stripRoot: (full) => full === pkg.path ? '' : full.replace(`${pkg.path}/`, ''),
|
|
86
|
-
mono: !!pkg.workspaces
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Get Compiler url
|
|
92
|
-
*/
|
|
93
|
-
function getCompilerUrl(ws: Workspace): string {
|
|
94
|
-
// eslint-disable-next-line no-bitwise
|
|
95
|
-
const port = (Math.abs([...ws.path].reduce((a, b) => (a * 33) ^ b.charCodeAt(0), 5381)) % 29000) + 20000;
|
|
96
|
-
return `http://localhost:${port}`;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Resolve module folder
|
|
101
|
-
*/
|
|
102
|
-
function resolveModule(workspace: Workspace, folder?: string): Pkg {
|
|
103
|
-
let mod;
|
|
104
|
-
if (!folder && process.env.TRV_MODULE) {
|
|
105
|
-
mod = process.env.TRV_MODULE;
|
|
106
|
-
if (/[.][cm]?(t|j)sx?$/.test(mod)) { // Rewrite from file to module
|
|
107
|
-
try {
|
|
108
|
-
process.env.TRV_MODULE = mod = findPackage(path.dirname(mod)).name;
|
|
109
|
-
} catch {
|
|
110
|
-
process.env.TRV_MODULE = mod = '';
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (mod) { // If module provided in lieu of folder
|
|
116
|
-
try {
|
|
117
|
-
folder = path.dirname(workspace.resolve(`${mod}/package.json`));
|
|
118
|
-
} catch {
|
|
119
|
-
const workspacePkg = readPackage(workspace.path);
|
|
120
|
-
if (workspacePkg?.name === mod) {
|
|
121
|
-
folder = workspace.path;
|
|
122
|
-
} else {
|
|
123
|
-
throw new Error(`Unable to resolve location for ${folder}`);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return findPackage(folder ?? '.');
|
|
36
|
+
return pkg;
|
|
129
37
|
}
|
|
130
38
|
|
|
131
39
|
/**
|
|
132
40
|
* Gets build context
|
|
133
41
|
*/
|
|
134
|
-
export function getManifestContext(
|
|
135
|
-
const workspace =
|
|
136
|
-
const mod = resolveModule(workspace, folder);
|
|
42
|
+
export function getManifestContext(root: string = process.cwd()): ManifestContext {
|
|
43
|
+
const workspace = findPackage(root, pkg => !!pkg?.workspaces || !!pkg?.travetto?.build?.isolated);
|
|
137
44
|
const build = workspace.travetto?.build ?? {};
|
|
45
|
+
const resolve = createRequire(path.resolve(workspace.path, 'node_modules')).resolve.bind(null);
|
|
46
|
+
const wsPrefix = `${workspace.path}/`;
|
|
47
|
+
const modPkg = (!!workspace.workspaces && process.env.TRV_MODULE) ?
|
|
48
|
+
readPackage(resolve(`${process.env.TRV_MODULE}/package.json`)) :
|
|
49
|
+
findPackage(root, pkg => !!pkg) ?? workspace;
|
|
138
50
|
|
|
139
51
|
return {
|
|
140
52
|
workspace: {
|
|
141
|
-
name: workspace.name,
|
|
53
|
+
name: workspace.name ?? 'untitled',
|
|
142
54
|
path: workspace.path,
|
|
143
|
-
mono: workspace.
|
|
144
|
-
manager: workspace.
|
|
55
|
+
mono: !!workspace.workspaces,
|
|
56
|
+
manager: existsSync(path.resolve(workspace.path, 'yarn.lock')) ? 'yarn' : 'npm',
|
|
145
57
|
type: workspace.type ?? 'commonjs',
|
|
146
58
|
defaultEnv: workspace.travetto?.defaultEnv ?? 'local'
|
|
147
59
|
},
|
|
148
60
|
build: {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
outputFolder: build.outputFolder ??
|
|
153
|
-
toolFolder: build.toolFolder ??
|
|
154
|
-
typesFolder: build.typesFolder ??
|
|
61
|
+
compilerUrl: build.compilerUrl ?? `http://localhost:${toPort(wsPrefix)}`,
|
|
62
|
+
compilerModuleFolder: toPosix(path.dirname(resolve('@travetto/compiler/package.json'))).replace(wsPrefix, ''),
|
|
63
|
+
compilerFolder: toPosix(build.compilerFolder ?? '.trv/compiler'),
|
|
64
|
+
outputFolder: toPosix(build.outputFolder ?? '.trv/output'),
|
|
65
|
+
toolFolder: toPosix(build.toolFolder ?? '.trv/tool'),
|
|
66
|
+
typesFolder: toPosix(build.typesFolder ?? '.trv/types')
|
|
155
67
|
},
|
|
156
68
|
main: {
|
|
157
|
-
name:
|
|
158
|
-
folder:
|
|
159
|
-
version:
|
|
160
|
-
description:
|
|
69
|
+
name: modPkg.name ?? 'untitled',
|
|
70
|
+
folder: modPkg.path.replace(wsPrefix, ''),
|
|
71
|
+
version: modPkg.version,
|
|
72
|
+
description: modPkg.description
|
|
161
73
|
}
|
|
162
74
|
};
|
|
163
75
|
}
|