@travetto/manifest 4.0.0-rc.0 → 4.0.0-rc.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/src/util.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { path } from './path';
2
- import { ManifestContext, ManifestRoot } from './types';
3
2
  import { ManifestModuleUtil } from './module';
4
3
  import { ManifestFileUtil } from './file';
5
4
  import { PackageUtil } from './package';
6
5
 
6
+ import type { ManifestContext } from './types/context';
7
+ import type { ManifestRoot } from './types/manifest';
8
+
7
9
  const MANIFEST_FILE = 'manifest.json';
8
10
 
9
11
  /**
@@ -15,29 +17,41 @@ export class ManifestUtil {
15
17
  */
16
18
  static async buildManifest(ctx: ManifestContext): Promise<ManifestRoot> {
17
19
  return {
18
- modules: await ManifestModuleUtil.produceModules(ctx),
19
20
  generated: Date.now(),
20
- ...ctx
21
+ workspace: ctx.workspace,
22
+ build: ctx.build,
23
+ main: ctx.main,
24
+ modules: await ManifestModuleUtil.produceModules(ctx),
21
25
  };
22
26
  }
23
27
 
28
+ /**
29
+ * Produce a manifest location given a current context and a module name
30
+ */
31
+ static getManifestLocation(ctx: ManifestContext, module?: string): string {
32
+ return path.resolve(ctx.workspace.path, ctx.build.outputFolder, 'node_modules', module ?? ctx.workspace.name);
33
+ }
34
+
24
35
  /**
25
36
  * Produce a production manifest from a given manifest
26
37
  */
27
38
  static createProductionManifest(manifest: ManifestRoot): ManifestRoot {
39
+ const prodModules = Object.values(manifest.modules).filter(x => x.prod);
40
+ const prodModNames = new Set([...prodModules.map(x => x.name)]);
28
41
  return {
29
- ...manifest,
30
- // If in prod mode, only include std modules
31
- modules: Object.fromEntries(
32
- Object.values(manifest.modules)
33
- .filter(x => x.prod)
34
- .map(m => [m.name, m])
35
- ),
42
+ generated: manifest.generated,
43
+ workspace: manifest.workspace,
36
44
  build: {
37
45
  ...manifest.build,
38
46
  // Mark output folder/workspace path as portable
39
47
  outputFolder: '$$PRODUCTION$$',
40
- }
48
+ },
49
+ main: manifest.main,
50
+ modules: Object.fromEntries(
51
+ prodModules.map(m => [m.name, Object.assign(m, {
52
+ parents: m.parents.filter(x => prodModNames.has(x))
53
+ })])
54
+ ),
41
55
  };
42
56
  }
43
57
 
@@ -47,7 +61,7 @@ export class ManifestUtil {
47
61
  * @param file
48
62
  * @returns
49
63
  */
50
- static readManifestSync(file: string): { manifest: ManifestRoot, file: string } {
64
+ static readManifestSync(file: string): ManifestRoot {
51
65
  file = path.resolve(file);
52
66
  if (!file.endsWith('.json')) {
53
67
  file = path.resolve(file, MANIFEST_FILE);
@@ -58,7 +72,7 @@ export class ManifestUtil {
58
72
  manifest.build.outputFolder = path.cwd();
59
73
  manifest.workspace.path = path.cwd();
60
74
  }
61
- return { manifest, file };
75
+ return manifest;
62
76
  }
63
77
 
64
78
  /**
@@ -89,7 +103,8 @@ export class ManifestUtil {
89
103
  */
90
104
  static getWorkspaceContext(ctx: ManifestContext): ManifestContext {
91
105
  return ctx.workspace.mono ? {
92
- ...ctx,
106
+ workspace: ctx.workspace,
107
+ build: ctx.build,
93
108
  main: {
94
109
  name: ctx.workspace.name,
95
110
  folder: '',
@@ -106,7 +121,8 @@ export class ManifestUtil {
106
121
  const pkg = PackageUtil.readPackage(modPath);
107
122
 
108
123
  return {
109
- ...ctx,
124
+ workspace: ctx.workspace,
125
+ build: ctx.build,
110
126
  main: {
111
127
  name: pkg.name,
112
128
  folder,
@@ -115,4 +131,43 @@ export class ManifestUtil {
115
131
  }
116
132
  };
117
133
  }
134
+
135
+ /**
136
+ * Efficient lookup for path-based graphs
137
+ */
138
+ static lookupTrie<T>(
139
+ inputs: T[], getPath: (v: T) => string[], validateUnknown?: (pth: string[]) => boolean
140
+ ): (pth: string[]) => T | undefined {
141
+ type TrieNode = { value?: T, subs: Record<string, TrieNode> };
142
+ const root: TrieNode = { subs: {} };
143
+ for (const item of inputs) {
144
+ const pth = getPath(item);
145
+ let node = root;
146
+ for (const sub of pth) {
147
+ if (sub) {
148
+ node = node.subs[sub] ??= { subs: {} };
149
+ }
150
+ }
151
+ node.value = item;
152
+ }
153
+
154
+ return pth => {
155
+ let node = root;
156
+ let value = node.value;
157
+ let i = 0;
158
+
159
+ for (const sub of pth) {
160
+ i += 1;
161
+ if (node) {
162
+ node = node.subs[sub];
163
+ value = node?.value ?? value;
164
+ } else if (validateUnknown && !node && !validateUnknown(pth.slice(0, i))) {
165
+ value = undefined;
166
+ break;
167
+ }
168
+ }
169
+
170
+ return value;
171
+ };
172
+ }
118
173
  }
package/src/types.ts DELETED
@@ -1,140 +0,0 @@
1
- export type NodeModuleType = 'module' | 'commonjs';
2
- export type NodePackageManager = 'yarn' | 'npm';
3
-
4
- export type ManifestModuleFileType = 'typings' | 'ts' | 'js' | 'json' | 'package-json' | 'unknown' | 'fixture' | 'md';
5
- export type ManifestModuleFolderType =
6
- '$root' | '$index' | '$package' |
7
- 'src' | 'bin' | 'support' | 'resources' | 'test' | 'doc' |
8
- 'test/fixtures' | 'support/fixtures' | 'support/resources' |
9
- '$other' | '$transformer';
10
-
11
- export type ManifestModuleRole = 'std' | 'test' | 'doc' | 'compile' | 'build';
12
-
13
- export type ManifestModuleFile = [string, ManifestModuleFileType, number] | [string, ManifestModuleFileType, number, ManifestModuleRole];
14
- export type ManifestDepCore = {
15
- name: string;
16
- main?: boolean;
17
- local?: boolean;
18
- version: string;
19
- prod: boolean;
20
- internal?: boolean;
21
- };
22
- export type ManifestModuleCore = ManifestDepCore & {
23
- sourceFolder: string;
24
- outputFolder: string;
25
- roles: ManifestModuleRole[];
26
- parents: string[];
27
- };
28
-
29
- export type ManifestModule = ManifestModuleCore & {
30
- files: Partial<Record<ManifestModuleFolderType, ManifestModuleFile[]>>;
31
- };
32
-
33
- export type ManifestContext = {
34
- workspace: {
35
- /** Workspace path for module */
36
- path: string;
37
- /** The module name for the workspace root */
38
- name: string;
39
- /** Is the workspace a monorepo? */
40
- mono?: boolean;
41
- /** The module type of the workspace */
42
- type: NodeModuleType;
43
- /** The package manager of the workspace */
44
- manager: NodePackageManager;
45
- };
46
- build: {
47
- /** Compiler folder, relative to workspace */
48
- compilerFolder: string;
49
- /** Compiler module folder */
50
- compilerModuleFolder: string;
51
- /** URL for the compiler server */
52
- compilerUrl: string;
53
- /** Code output folder, relative to workspace */
54
- outputFolder: string;
55
- };
56
- main: {
57
- /** Main module for manifest */
58
- name: string;
59
- /** Folder, relative to workspace for main module */
60
- folder: string;
61
- /** Description of the main module */
62
- description?: string;
63
- /** Version of the main module */
64
- version: string;
65
- };
66
- };
67
-
68
- export type ManifestRoot = ManifestContext & {
69
- generated: number;
70
- modules: Record<string, ManifestModule>;
71
- };
72
-
73
- export type Package = {
74
- name: string;
75
- type?: NodeModuleType;
76
- version: string;
77
- description?: string;
78
- license?: string;
79
- repository?: {
80
- url: string;
81
- directory?: string;
82
- };
83
- author?: {
84
- email?: string;
85
- name?: string;
86
- };
87
- main: string;
88
- homepage?: string;
89
- files?: string[];
90
- bin?: Record<string, string>;
91
- scripts?: Record<string, string>;
92
- engines?: Record<string, string>;
93
- keywords?: string[];
94
-
95
- dependencies?: Record<string, string>;
96
- devDependencies?: Record<string, string>;
97
- peerDependencies?: Record<string, string>;
98
- peerDependenciesMeta?: Record<string, { optional?: boolean }>;
99
- optionalDependencies?: Record<string, string>;
100
- travetto?: {
101
- displayName?: string;
102
- roles?: ManifestModuleRole[];
103
- doc?: {
104
- output?: string[];
105
- root?: string;
106
- baseUrl?: string;
107
- outputs?: string[];
108
- };
109
- build?: Partial<ManifestContext['build']> & {
110
- isolated?: boolean;
111
- withModules?: Record<string, 'main' | true>;
112
- };
113
- };
114
- workspaces?: string[];
115
- private?: boolean;
116
- publishConfig?: { access?: 'restricted' | 'public' };
117
- };
118
-
119
- type OrProm<T> = T | Promise<T>;
120
-
121
- export type PackageVisitReq<T> = { pkg: Package, prod: boolean, sourcePath: string, parent?: T, topLevel?: boolean };
122
- export type PackageVisitor<T> = {
123
- rootPath: string;
124
- init?(req: PackageVisitReq<T>): OrProm<undefined | void | PackageVisitReq<T>[]>;
125
- valid?(req: PackageVisitReq<T>): boolean;
126
- create(req: PackageVisitReq<T>): OrProm<T>;
127
- visit?(req: PackageVisitReq<T>, item: T): OrProm<void>;
128
- complete?(values: Set<T>): OrProm<Set<T> | undefined>;
129
- };
130
-
131
- export type PackageWorkspaceEntry = { name: string, sourcePath: string };
132
-
133
- export type FunctionMetadata = {
134
- id: string;
135
- source: string;
136
- hash?: number;
137
- methods?: Record<string, { hash: number }>;
138
- synthetic?: boolean;
139
- abstract?: boolean;
140
- };