@travetto/manifest 7.1.1 → 7.1.3

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,7 +134,7 @@ The context contains:
134
134
  * The location where all compiled code will be stored. Defaults to: `.trv_output`. (*Can be overridden in your [Package JSON](https://docs.npmjs.com/cli/v9/configuring-npm/package-json) in 'travetto.outputFolder'*)
135
135
  * The location where the intermediate compiler will be created. Defaults to: `.trv_compiler`
136
136
  * The location where tooling will be able to write to. Defaults to: `.trv_output`
137
- * Which package manager is in use [Npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) or [Yarn](https://yarnpg.com)
137
+ * Which package manager is in use [Npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)/[Yarn](https://yarnpkg.com)
138
138
  * The main module version
139
139
  * The main module description
140
140
  * The framework version (based on @travetto/manifest)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/manifest",
3
- "version": "7.1.1",
3
+ "version": "7.1.3",
4
4
  "type": "module",
5
5
  "description": "Support for project indexing, manifesting, along with file watching",
6
6
  "keywords": [
package/src/context.ts CHANGED
@@ -2,7 +2,7 @@ import { existsSync, readFileSync } from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { createRequire } from 'node:module';
4
4
 
5
- import type { Package } from './types/package.ts';
5
+ import { type Package, PACKAGE_MANAGERS } from './types/package.ts';
6
6
  import type { ManifestContext } from './types/context.ts';
7
7
 
8
8
  type Pkg = Package & { path: string };
@@ -41,11 +41,17 @@ function findPackage(base: string, pred: (_p?: Pkg) => boolean): Pkg {
41
41
  return pkg;
42
42
  }
43
43
 
44
+ const WORKSPACE_FILES = PACKAGE_MANAGERS.map(x => x.workspaceFile!).filter(Boolean);
45
+
44
46
  /**
45
47
  * Gets build context
46
48
  */
47
49
  export function getManifestContext(root: string = process.cwd()): ManifestContext {
48
- const workspace = findPackage(root, pkg => !!pkg?.workspaces || !!pkg?.travetto?.build?.isolated);
50
+ const workspace = findPackage(root, pkg =>
51
+ !!pkg?.workspaces ||
52
+ !!pkg?.travetto?.build?.isolated ||
53
+ (!!pkg && WORKSPACE_FILES.some(file => existsSync(path.resolve(pkg.path, file))))
54
+ );
49
55
  if (workspace.type !== 'module') {
50
56
  throw new Error('Only ESM modules are supported, package.json must be of type module');
51
57
  }
@@ -63,7 +69,7 @@ export function getManifestContext(root: string = process.cwd()): ManifestContex
63
69
  name: workspace.name ?? 'untitled',
64
70
  path: workspace.path,
65
71
  mono: !!workspace.workspaces,
66
- manager: existsSync(path.resolve(workspace.path, 'yarn.lock')) ? 'yarn' : 'npm'
72
+ manager: PACKAGE_MANAGERS.find(item => existsSync(path.resolve(workspace.path, item.lock)))?.type ?? 'npm'
67
73
  },
68
74
  build: {
69
75
  compilerUrl: build.compilerUrl ?? `http://localhost:${toPort(wsPrefix)}`,
@@ -330,4 +330,9 @@ export class ManifestIndex {
330
330
  const manifestLocation = ManifestUtil.getManifestLocation(moduleContext, moduleName);
331
331
  return ManifestUtil.readManifestSync(manifestLocation);
332
332
  }
333
+
334
+ /** Resolve a package command tied to workspace */
335
+ resolvePackageCommand(cmd: string): string {
336
+ return path.resolve(this.manifest.workspace.path, 'node_modules', '.bin', cmd);
337
+ }
333
338
  }
package/src/package.ts CHANGED
@@ -7,7 +7,6 @@ import { ManifestFileUtil } from './file.ts';
7
7
 
8
8
  import { PackagePathSymbol, type Package, type PackageWorkspaceEntry } from './types/package.ts';
9
9
  import type { ManifestContext } from './types/context.ts';
10
- import type { NodePackageManager } from './types/common.ts';
11
10
 
12
11
  /**
13
12
  * Utilities for querying, traversing and reading package.json files.
@@ -18,12 +17,6 @@ export class PackageUtil {
18
17
  static #cache: Record<string, Package> = {};
19
18
  static #workspaces: Record<string, PackageWorkspaceEntry[]> = {};
20
19
 
21
- static #exec<T>(workingDirectory: string, cmd: string): Promise<T> {
22
- const env = { PATH: process.env.PATH, NODE_PATH: process.env.NODE_PATH };
23
- const text = execSync(cmd, { cwd: workingDirectory, encoding: 'utf8', env, stdio: ['pipe', 'pipe'] }).toString().trim();
24
- return JSON.parse(text);
25
- }
26
-
27
20
  /**
28
21
  * Resolve import given a manifest context
29
22
  */
@@ -80,7 +73,7 @@ export class PackageUtil {
80
73
  modulePath.endsWith('.json') ? modulePath : path.resolve(modulePath, 'package.json'),
81
74
  );
82
75
 
83
- nodePackage.name ??= 'untitled'; // If a package.json (root-only) is missing a name, allows for npx execution
76
+ nodePackage.name ??= 'untitled'; // If a package.json (root-only) is missing a name, allows for execution
84
77
 
85
78
  nodePackage[PackagePathSymbol] = modulePath;
86
79
  return nodePackage;
@@ -102,29 +95,19 @@ export class PackageUtil {
102
95
  try {
103
96
  return this.#workspaces[rootPath] ??= ManifestFileUtil.readAsJsonSync<PackageWorkspaceEntry[]>(cache);
104
97
  } catch {
105
- let out: PackageWorkspaceEntry[];
98
+ let args: string[];
106
99
  switch (ctx.workspace.manager) {
107
100
  case 'yarn':
108
- case 'npm': {
109
- const workspaces = await this.#exec<{ location: string, name: string }[]>(rootPath, 'npm query .workspace');
110
- out = workspaces.map(module => ({ path: path.resolve(ctx.workspace.path, module.location), name: module.name }));
111
- break;
112
- }
101
+ case 'npm': args = ['query', '.workspace']; break;
102
+ case 'pnpm': args = ['ls', '-r', '--depth', '-1', '--json']; break;
113
103
  }
114
- await ManifestFileUtil.bufferedFileWrite(cache, JSON.stringify(out));
115
- return out;
116
- }
117
- }
118
104
 
119
- /**
120
- * Get an install command for a given npm module
121
- */
122
- static getInstallCommand(ctx: { workspace: { manager: NodePackageManager } }, pkg: string, production = false): string {
123
- let install: string;
124
- switch (ctx.workspace.manager) {
125
- case 'npm': install = `npm i ${production ? '' : '--save-dev '}${pkg}`; break;
126
- case 'yarn': install = `yarn add ${production ? '' : '--dev '}${pkg}`; break;
105
+ const env = { PATH: process.env.PATH, NODE_PATH: process.env.NODE_PATH };
106
+ const text = execSync(`${ctx.workspace.manager} ${args.join(' ')}`, { cwd: rootPath, encoding: 'utf8', env, stdio: ['pipe', 'pipe'] });
107
+ const out: PackageWorkspaceEntry[] = JSON.parse(text.trim());
108
+ const filtered = out.map(item => ({ name: item.name, path: item.path }));
109
+ await ManifestFileUtil.bufferedFileWrite(cache, JSON.stringify(filtered));
110
+ return filtered;
127
111
  }
128
- return install;
129
112
  }
130
113
  }
@@ -1,5 +1,3 @@
1
- export type NodePackageManager = 'yarn' | 'npm';
2
-
3
1
  export type ManifestModuleFileType = 'typings' | 'ts' | 'js' | 'json' | 'package-json' | 'unknown' | 'fixture' | 'md';
4
2
  export type ManifestModuleFolderType =
5
3
  '$root' | '$index' | '$package' |
@@ -1,4 +1,4 @@
1
- import type { NodePackageManager } from './common.ts';
1
+ import type { NodePackageManager } from './package.ts';
2
2
 
3
3
  export type ManifestContext = {
4
4
  workspace: {
@@ -3,6 +3,16 @@ import type { ManifestContext } from './context.ts';
3
3
 
4
4
  export const PackagePathSymbol = Symbol.for('@travetto/manifest:package-path');
5
5
 
6
+ const ALL_PACKAGE_MANAGERS = [
7
+ { lock: 'package-lock.json', type: 'npm', title: 'Npm', workspaceFile: undefined, active: true },
8
+ { lock: 'yarn.lock', type: 'yarn', title: 'Yarn', workspaceFile: undefined, active: true },
9
+ { lock: 'pnpm-lock.yaml', type: 'pnpm', workspaceFile: 'pnpm-workspace.yaml', title: 'Pnpm', active: false }
10
+ ] as const;
11
+
12
+ export type NodePackageManager = (typeof ALL_PACKAGE_MANAGERS)[number]['type'];
13
+
14
+ export const PACKAGE_MANAGERS = ALL_PACKAGE_MANAGERS.filter(manager => manager.active);
15
+
6
16
  export type Package = {
7
17
  [PackagePathSymbol]?: string;
8
18
  name: string;