@travetto/manifest 5.0.1 → 5.0.2

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/README.md CHANGED
@@ -134,6 +134,7 @@ By default, all paths within the framework are assumed to be in a POSIX style, a
134
134
  [ "test/fixtures/simple.ts", "fixture", 1868155200000, "test" ]
135
135
  ],
136
136
  "src": [
137
+ [ "src/context.ts", "ts", 1868155200000 ],
137
138
  [ "src/delta.ts", "ts", 1868155200000 ],
138
139
  [ "src/dependencies.ts", "ts", 1868155200000 ],
139
140
  [ "src/file.ts", "ts", 1868155200000 ],
@@ -146,10 +147,6 @@ By default, all paths within the framework are assumed to be in a POSIX style, a
146
147
  [ "src/types/context.ts", "ts", 1868155200000 ],
147
148
  [ "src/types/manifest.ts", "ts", 1868155200000 ],
148
149
  [ "src/types/package.ts", "ts", 1868155200000 ]
149
- ],
150
- "bin": [
151
- [ "bin/context.d.ts", "typings", 1868155200000 ],
152
- [ "bin/context.js", "js", 1868155200000 ]
153
150
  ]
154
151
  }
155
152
  }
package/__index__.ts CHANGED
@@ -1,5 +1,4 @@
1
- /// <reference path="../runtime/src/global.d.ts" />
2
-
1
+ export * from './src/context';
3
2
  export * from './src/module';
4
3
  export * from './src/delta';
5
4
  export * from './src/manifest-index';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/manifest",
3
- "version": "5.0.1",
3
+ "version": "5.0.2",
4
4
  "description": "Support for project indexing, manifesting, along with file watching",
5
5
  "keywords": [
6
6
  "path",
@@ -15,7 +15,6 @@
15
15
  "email": "travetto.framework@gmail.com",
16
16
  "name": "Travetto Framework"
17
17
  },
18
- "type": "module",
19
18
  "files": [
20
19
  "__index__.ts",
21
20
  "bin",
@@ -1,25 +1,29 @@
1
- // @ts-check
2
-
3
- /**
4
- * @typedef {import('../src/types/package').Package & { path:string }} Pkg
5
- * @typedef {Pkg & { mono: boolean, manager: 'yarn'|'npm', resolve: (file:string) => string, stripRoot: (file:string)=>string}} Workspace
6
- * @typedef {import('../src/types/context').ManifestContext} ManifestContext
7
- */
8
1
  import { existsSync, readFileSync } from 'node:fs';
9
2
  import path from 'node:path';
10
3
  import { createRequire } from 'node:module';
11
4
 
12
- /** @type {Record<string, Workspace>} */ const WS_ROOT = {};
5
+ import type { Package } from './types/package';
6
+ import type { ManifestContext } from './types/context';
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
+ }>;
16
+
13
17
  const TOOL_FOLDER = '.trv/tool';
14
18
  const COMPILER_FOLDER = '.trv/compiler';
15
19
  const OUTPUT_FOLDER = '.trv/output';
16
20
 
21
+ const WS_ROOT: Record<string, Workspace> = {};
22
+
17
23
  /**
18
24
  * Read package.json or return undefined if missing
19
- * @param {string} dir
20
- * @returns {Pkg|undefined}
21
25
  */
22
- function $readPackage(dir) {
26
+ function readPackage(dir: string): Pkg | undefined {
23
27
  dir = dir.endsWith('.json') ? path.dirname(dir) : dir;
24
28
  try {
25
29
  const v = readFileSync(path.resolve(dir, 'package.json'), 'utf8');
@@ -29,14 +33,12 @@ function $readPackage(dir) {
29
33
 
30
34
  /**
31
35
  * Find package.json for a given folder
32
- * @param {string} dir
33
- * @return {Pkg}
34
36
  */
35
- function $findPackage(dir) {
37
+ function findPackage(dir: string): Pkg {
36
38
  let prev;
37
39
  let pkg, curr = path.resolve(dir);
38
40
  while (!pkg && curr !== prev) {
39
- pkg = $readPackage(curr);
41
+ pkg = readPackage(curr);
40
42
  [prev, curr] = [curr, path.dirname(curr)];
41
43
  }
42
44
  if (!pkg) {
@@ -48,9 +50,8 @@ function $findPackage(dir) {
48
50
 
49
51
  /**
50
52
  * Get workspace root
51
- * @return {Workspace}
52
53
  */
53
- function $resolveWorkspace(base = process.cwd()) {
54
+ function resolveWorkspace(base: string): Workspace {
54
55
  if (base in WS_ROOT) { return WS_ROOT[base]; }
55
56
  let folder = base;
56
57
  let prev;
@@ -59,7 +60,7 @@ function $resolveWorkspace(base = process.cwd()) {
59
60
 
60
61
  while (prev !== folder) {
61
62
  [prev, prevPkg] = [folder, pkg];
62
- pkg = $readPackage(folder) ?? pkg;
63
+ pkg = readPackage(folder) ?? pkg;
63
64
  if (
64
65
  (pkg && (!!pkg.workspaces || !!pkg.travetto?.build?.isolated)) || // if we have a monorepo root, or we are isolated
65
66
  existsSync(path.resolve(folder, '.git')) // we made it to the source repo root
@@ -86,9 +87,8 @@ function $resolveWorkspace(base = process.cwd()) {
86
87
 
87
88
  /**
88
89
  * Get Compiler url
89
- * @param {Workspace} ws
90
90
  */
91
- function $getCompilerUrl(ws) {
91
+ function getCompilerUrl(ws: Workspace): string {
92
92
  // eslint-disable-next-line no-bitwise
93
93
  const port = (Math.abs([...ws.path].reduce((a, b) => (a * 33) ^ b.charCodeAt(0), 5381)) % 29000) + 20000;
94
94
  return `http://localhost:${port}`;
@@ -96,16 +96,14 @@ function $getCompilerUrl(ws) {
96
96
 
97
97
  /**
98
98
  * Resolve module folder
99
- * @param {Workspace} workspace
100
- * @param {string|undefined} folder
101
99
  */
102
- function $resolveModule(workspace, folder) {
100
+ function resolveModule(workspace: Workspace, folder: string): Pkg {
103
101
  let mod;
104
102
  if (!folder && process.env.TRV_MODULE) {
105
103
  mod = process.env.TRV_MODULE;
106
- if (/[.](t|j)sx?$/.test(mod)) { // Rewrite from file to module
104
+ if (/[.][cm]?(t|j)sx?$/.test(mod)) { // Rewrite from file to module
107
105
  try {
108
- process.env.TRV_MODULE = mod = $findPackage(path.dirname(mod)).name;
106
+ process.env.TRV_MODULE = mod = findPackage(path.dirname(mod)).name;
109
107
  } catch {
110
108
  process.env.TRV_MODULE = mod = '';
111
109
  }
@@ -116,7 +114,7 @@ function $resolveModule(workspace, folder) {
116
114
  try {
117
115
  folder = path.dirname(workspace.resolve(`${mod}/package.json`));
118
116
  } catch {
119
- const workspacePkg = $readPackage(workspace.path);
117
+ const workspacePkg = readPackage(workspace.path);
120
118
  if (workspacePkg?.name === mod) {
121
119
  folder = workspace.path;
122
120
  } else {
@@ -125,17 +123,15 @@ function $resolveModule(workspace, folder) {
125
123
  }
126
124
  }
127
125
 
128
- return $findPackage(folder ?? '.');
126
+ return findPackage(folder ?? '.');
129
127
  }
130
128
 
131
129
  /**
132
130
  * Gets build context
133
- * @param {string} [folder]
134
- * @return {ManifestContext}
135
131
  */
136
- export function getManifestContext(folder) {
137
- const workspace = $resolveWorkspace(folder);
138
- const mod = $resolveModule(workspace, folder);
132
+ export function getManifestContext(folder = process.cwd()): ManifestContext {
133
+ const workspace = resolveWorkspace(folder);
134
+ const mod = resolveModule(workspace, folder);
139
135
  const build = workspace.travetto?.build ?? {};
140
136
  const toolFolder = build.toolFolder ?? TOOL_FOLDER;
141
137
 
@@ -150,7 +146,7 @@ export function getManifestContext(folder) {
150
146
  },
151
147
  build: {
152
148
  compilerFolder: build.compilerFolder ?? COMPILER_FOLDER,
153
- compilerUrl: build.compilerUrl ?? $getCompilerUrl(workspace),
149
+ compilerUrl: build.compilerUrl ?? getCompilerUrl(workspace),
154
150
  compilerModuleFolder: workspace.stripRoot(path.dirname(workspace.resolve('@travetto/compiler/package.json'))),
155
151
  outputFolder: build.outputFolder ?? OUTPUT_FOLDER,
156
152
  toolFolder
package/src/delta.ts CHANGED
@@ -12,8 +12,8 @@ type DeltaModule = ManifestModuleCore & { files: Record<string, ManifestModuleFi
12
12
  export type DeltaEvent = { file: string, type: DeltaEventType, module: string, sourceFile: string };
13
13
 
14
14
  const VALID_SOURCE_FOLDERS = new Set<ManifestModuleFolderType>(['bin', 'src', 'test', 'support', '$index', '$package', 'doc']);
15
- const VALID_OUTPUT_TYPE = new Set<ManifestModuleFileType>(['js', 'ts', 'package-json']);
16
- const VALID_SOURCE_TYPE = new Set<ManifestModuleFileType>([...VALID_OUTPUT_TYPE, 'typings']);
15
+ const VALID_SOURCE_TYPE = new Set<ManifestModuleFileType>(['js', 'ts', 'package-json']);
16
+ const VALID_OUTPUT_TYPE = new Set<ManifestModuleFileType>([...VALID_SOURCE_TYPE, 'typings']);
17
17
 
18
18
  const TypedObject: { keys<T = unknown, K extends keyof T = keyof T>(o: T): K[] } & ObjectConstructor = Object;
19
19
 
package/src/module.ts CHANGED
@@ -24,7 +24,9 @@ const INDEX_FILES = new Set(
24
24
  );
25
25
 
26
26
  const STD_TOP_FOLDERS = new Set(['src', 'bin', 'support']);
27
+ const FULL_TOP_FOLDERS = new Set([...STD_TOP_FOLDERS, 'doc', 'test', 'resources']);
27
28
  const STD_TOP_FILES = new Set([...INDEX_FILES, 'package.json']);
29
+ const FULL_TOP_FILES = new Set([...STD_TOP_FILES, 'DOC.tsx', 'README.md', 'LICENSE', 'DOC.html']);
28
30
 
29
31
  const SUPPORT_FILE_MAP: Record<string, ManifestModuleRole> = {
30
32
  transformer: 'compile',
@@ -32,7 +34,7 @@ const SUPPORT_FILE_MAP: Record<string, ManifestModuleRole> = {
32
34
  test: 'test',
33
35
  doc: 'doc',
34
36
  pack: 'build',
35
- FILEld: 'build'
37
+ build: 'build'
36
38
  };
37
39
 
38
40
  const SUPPORT_FILE_RE = new RegExp(`^support[/](?<name>${Object.keys(SUPPORT_FILE_MAP).join('|')})[./]`);
@@ -48,8 +50,8 @@ export class ManifestModuleUtil {
48
50
  /**
49
51
  * Replace a source file's extension with a given value
50
52
  */
51
- static #pathToExtension(inputFile: string, ext: string): string {
52
- return inputFile.replace(/[.][tj]sx?$/, ext);
53
+ static #pathToExtension(sourceFile: string, ext: string): string {
54
+ return sourceFile.replace(/[.][cm]?[tj]sx?$/, ext);
53
55
  }
54
56
 
55
57
  /**
@@ -73,6 +75,9 @@ export class ManifestModuleUtil {
73
75
  path.resolve(ctx.workspace.path, ctx.build.toolFolder),
74
76
  ]);
75
77
 
78
+ const topFolders = full ? FULL_TOP_FOLDERS : STD_TOP_FOLDERS;
79
+ const topFiles = full ? FULL_TOP_FILES : STD_TOP_FILES;
80
+
76
81
  const stack: [string, number][] = [[folder, 0]];
77
82
  while (stack.length) {
78
83
  const popped = stack.pop();
@@ -85,21 +90,23 @@ export class ManifestModuleUtil {
85
90
  // Don't navigate into sub-folders with package.json's
86
91
  if (top !== folder && await fs.stat(`${top}/package.json`).catch(() => false)) {
87
92
  continue;
88
- }
89
- if (exclude.has(top)) {
93
+ } else if (exclude.has(top)) {
90
94
  continue;
91
95
  }
92
96
 
93
97
  for (const sub of await fs.readdir(top)) {
94
- const valid = !sub.startsWith('.') && (depth > 0 || full);
95
- const stat = await fs.stat(`${top}/${sub}`);
98
+ if (sub.startsWith('.') || sub === 'node_modules') {
99
+ continue;
100
+ }
101
+ const fullPath = `${top}/${sub}`;
102
+ const stat = await fs.stat(fullPath);
96
103
  if (stat.isFile()) {
97
- if (valid || STD_TOP_FILES.has(sub)) {
98
- out.push(`${top}/${sub}`);
104
+ if (depth > 0 || topFiles.has(sub)) {
105
+ out.push(fullPath);
99
106
  }
100
107
  } else {
101
- if (!sub.includes('node_modules') && (valid || STD_TOP_FOLDERS.has(sub))) {
102
- stack.push([`${top}/${sub}`, depth + 1]);
108
+ if (depth > 0 || topFolders.has(sub)) {
109
+ stack.push([fullPath, depth + 1]);
103
110
  }
104
111
  }
105
112
  }
@@ -167,7 +174,7 @@ export class ManifestModuleUtil {
167
174
  case 'doc':
168
175
  case 'resources':
169
176
  case 'support': return key;
170
- default: return '$other';
177
+ default: throw new Error(`Unknown folder: ${key}`);
171
178
  }
172
179
  } else if (/^DOC[.]tsx?$/.test(moduleFile)) {
173
180
  return 'doc';
@@ -227,14 +234,20 @@ export class ManifestModuleUtil {
227
234
  /**
228
235
  * Get the output file name for a given input
229
236
  */
230
- static withOutputExtension(inputFile: string): string {
231
- return this.#pathToExtension(inputFile, '.js');
237
+ static withOutputExtension(sourceFile: string): string {
238
+ if (sourceFile.endsWith('.d.ts')) {
239
+ return sourceFile;
240
+ }
241
+ return this.#pathToExtension(sourceFile, '.js');
232
242
  }
233
243
 
234
244
  /**
235
245
  * Get the file without an extension
236
246
  */
237
- static withoutSourceExtension(inputFile: string): string {
238
- return this.#pathToExtension(inputFile, '');
247
+ static withoutSourceExtension(sourceFile: string): string {
248
+ if (sourceFile.endsWith('.d.ts')) {
249
+ return sourceFile;
250
+ }
251
+ return this.#pathToExtension(sourceFile, '');
239
252
  }
240
253
  }
package/src/path.ts CHANGED
@@ -21,6 +21,7 @@ export const path: posix.PlatformPath & {
21
21
  dirname: file => posix.dirname(toPosix(file)),
22
22
  toNative,
23
23
  toPosix,
24
+ matchesGlob: (file, pattern) => posix.matchesGlob(toPosix(file), toPosix(pattern)),
24
25
  ...process.platform === 'win32' ? {
25
26
  resolve: (...args) => toPosix(native.resolve(cwd(), ...args.map(toPosix))),
26
27
  join: (root, ...args) => toPosix(native.join(toPosix(root), ...args.map(toPosix))),
@@ -6,6 +6,6 @@ export type ManifestModuleFolderType =
6
6
  '$root' | '$index' | '$package' |
7
7
  'src' | 'bin' | 'support' | 'resources' | 'test' | 'doc' |
8
8
  'test/fixtures' | 'support/fixtures' | 'support/resources' |
9
- '$other' | '$transformer';
9
+ '$transformer';
10
10
 
11
11
  export type ManifestModuleRole = 'std' | 'test' | 'doc' | 'compile' | 'build';
@@ -37,4 +37,4 @@ export type ManifestContext = {
37
37
  /** Version of the main module */
38
38
  version: string;
39
39
  };
40
- };
40
+ };
package/bin/context.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import type { ManifestContext } from '../src/types/context';
2
-
3
- declare namespace ManifestBootstrap {
4
- /**
5
- * Get Context for building
6
- */
7
- function getManifestContext(folder?: string): ManifestContext;
8
- }
9
-
10
- export = ManifestBootstrap;