@travetto/manifest 5.0.4 → 5.0.6

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.
Files changed (3) hide show
  1. package/README.md +2 -1
  2. package/package.json +1 -1
  3. package/src/context.ts +40 -127
package/README.md CHANGED
@@ -91,7 +91,8 @@ By default, all paths within the framework are assumed to be in a POSIX style, a
91
91
  "compilerUrl": "http://127.0.0.1:26803",
92
92
  "compilerModuleFolder": "module/compiler",
93
93
  "outputFolder": ".trv/output",
94
- "toolFolder": ".trv/tool"
94
+ "toolFolder": ".trv/tool",
95
+ "typesFolder": ".trv/types"
95
96
  },
96
97
  "main": {
97
98
  "name": "@travetto/manifest",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/manifest",
3
- "version": "5.0.4",
3
+ "version": "5.0.6",
4
4
  "description": "Support for project indexing, manifesting, along with file watching",
5
5
  "keywords": [
6
6
  "path",
package/src/context.ts CHANGED
@@ -5,158 +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<T extends {} = {}> = Package & T & { path: string };
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
- const TOOL_FOLDER = '.trv/tool';
18
- const COMPILER_FOLDER = '.trv/compiler';
19
- const OUTPUT_FOLDER = '.trv/output';
20
- const TYPES_FOLDER = '.trv/types';
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
- const WS_ROOT: Record<string, Workspace> = {};
15
+ /** Find package */
16
+ function findPackage(base: string, pred: (_p?: Pkg) => boolean): Pkg {
17
+ let folder = `${base}/.`;
18
+ let prev: string;
19
+ let pkg: Pkg | undefined;
23
20
 
24
- /**
25
- * Read package.json or return undefined if missing
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) { return WS_ROOT[base]; }
57
- let folder = base;
58
- let prev;
59
- /** @type {Pkg|undefined} */
60
- let prevPkg, pkg;
61
-
62
- while (prev !== folder) {
63
- [prev, prevPkg] = [folder, pkg];
64
- pkg = readPackage(folder) ?? pkg;
65
- if (
66
- (pkg && (!!pkg.workspaces || !!pkg.travetto?.build?.isolated)) || // if we have a monorepo root, or we are isolated
67
- existsSync(path.resolve(folder, '.git')) // we made it to the source repo root
68
- ) {
69
- break;
70
- }
21
+ do {
22
+ prev = folder;
71
23
  folder = path.dirname(folder);
72
- }
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
+ );
73
31
 
74
32
  if (!pkg) {
75
33
  throw new Error('Could not find a package.json');
76
34
  }
77
35
 
78
- return WS_ROOT[base] = {
79
- ...pkg,
80
- name: pkg.name ?? 'untitled',
81
- type: pkg.type,
82
- manager: existsSync(path.resolve(pkg.path, 'yarn.lock')) ? 'yarn' : 'npm',
83
- resolve: createRequire(`${pkg.path}/node_modules`).resolve.bind(null),
84
- stripRoot: (full) => full === pkg.path ? '' : full.replace(`${pkg.path}/`, ''),
85
- mono: !!pkg.workspaces || (!pkg.travetto?.build?.isolated && !!prevPkg) // Workspaces or nested projects
86
- };
87
- }
88
-
89
- /**
90
- * Get Compiler url
91
- */
92
- function getCompilerUrl(ws: Workspace): string {
93
- // eslint-disable-next-line no-bitwise
94
- const port = (Math.abs([...ws.path].reduce((a, b) => (a * 33) ^ b.charCodeAt(0), 5381)) % 29000) + 20000;
95
- return `http://localhost:${port}`;
96
- }
97
-
98
- /**
99
- * Resolve module folder
100
- */
101
- function resolveModule(workspace: Workspace, folder?: string): Pkg {
102
- let mod;
103
- if (!folder && process.env.TRV_MODULE) {
104
- mod = process.env.TRV_MODULE;
105
- if (/[.][cm]?(t|j)sx?$/.test(mod)) { // Rewrite from file to module
106
- try {
107
- process.env.TRV_MODULE = mod = findPackage(path.dirname(mod)).name;
108
- } catch {
109
- process.env.TRV_MODULE = mod = '';
110
- }
111
- }
112
- }
113
-
114
- if (mod) { // If module provided in lieu of folder
115
- try {
116
- folder = path.dirname(workspace.resolve(`${mod}/package.json`));
117
- } catch {
118
- const workspacePkg = readPackage(workspace.path);
119
- if (workspacePkg?.name === mod) {
120
- folder = workspace.path;
121
- } else {
122
- throw new Error(`Unable to resolve location for ${folder}`);
123
- }
124
- }
125
- }
126
-
127
- return findPackage(folder ?? '.');
36
+ return pkg;
128
37
  }
129
38
 
130
39
  /**
131
40
  * Gets build context
132
41
  */
133
- export function getManifestContext(folder?: string): ManifestContext {
134
- const workspace = resolveWorkspace();
135
- const mod = resolveModule(workspace, folder);
42
+ export function getManifestContext(root: string, mod?: string): ManifestContext {
43
+ const workspace = findPackage(root, pkg => !!pkg?.workspaces || !!pkg?.travetto?.build?.isolated);
136
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 = mod ?
48
+ readPackage(resolve(`${mod}/package.json`)) :
49
+ findPackage(root, pkg => !!pkg) ?? workspace;
137
50
 
138
51
  return {
139
52
  workspace: {
140
- name: workspace.name,
53
+ name: workspace.name ?? 'untitled',
141
54
  path: workspace.path,
142
- mono: workspace.mono,
143
- manager: workspace.manager,
55
+ mono: !!workspace.workspaces,
56
+ manager: existsSync(path.resolve(workspace.path, 'yarn.lock')) ? 'yarn' : 'npm',
144
57
  type: workspace.type ?? 'commonjs',
145
58
  defaultEnv: workspace.travetto?.defaultEnv ?? 'local'
146
59
  },
147
60
  build: {
148
- compilerFolder: build.compilerFolder ?? COMPILER_FOLDER,
149
- compilerUrl: build.compilerUrl ?? getCompilerUrl(workspace),
150
- compilerModuleFolder: workspace.stripRoot(path.dirname(workspace.resolve('@travetto/compiler/package.json'))),
151
- outputFolder: build.outputFolder ?? OUTPUT_FOLDER,
152
- toolFolder: build.toolFolder ?? TOOL_FOLDER,
153
- typesFolder: build.typesFolder ?? TYPES_FOLDER
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')
154
67
  },
155
68
  main: {
156
- name: mod.name ?? 'untitled',
157
- folder: workspace.stripRoot(mod.path),
158
- version: mod.version,
159
- description: mod.description
69
+ name: modPkg.name ?? 'untitled',
70
+ folder: modPkg.path.replace(wsPrefix, ''),
71
+ version: modPkg.version,
72
+ description: modPkg.description
160
73
  }
161
74
  };
162
75
  }