@vltpkg/workspaces 1.3.8 → 1.5.0

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
@@ -81,9 +81,8 @@ type VltProject = {
81
81
 
82
82
  If `vlt.json` has no `workspaces` field, the npm/yarn-style
83
83
  `workspaces` field of the project root's `package.json` is used
84
- instead, so an existing npm or yarn monorepo works without a
85
- `vlt.json`. Only the shapes npm and yarn understand are accepted there
86
- -- a glob string, an array of them, or `{packages: [...]}` (plus
84
+ instead. Only the shapes npm and yarn understand are accepted there --
85
+ a glob string, an array of them, or `{packages: [...]}` (plus
87
86
  yarn-classic's `nohoist`, which is parsed and ignored). Named groups
88
87
  are rejected in `package.json`, because a `package.json` using them
89
88
  would be meaningless to every other package manager.
@@ -92,8 +91,16 @@ Note the precedence is keyed on the _field_, not the file: a
92
91
  `vlt.json` that exists but says nothing about workspaces still falls
93
92
  through to `package.json`. The two are never merged.
94
93
 
95
- In either file, a pattern beginning with `!` excludes the paths it
96
- matches, following npm's semantics: `!a/b` excludes only that
94
+ If neither declares workspaces, the `packages` list of a root
95
+ `pnpm-workspace.yaml` is used (plain string lists only; other keys and
96
+ unsupported YAML are ignored).
97
+
98
+ `vlt install` copies these globs to `vlt.json` (see
99
+ `migratableWSConfig`; opt out with `--no-migrate-workspaces`), and
100
+ `vlt ci` reads `vlt.json` only (`setVltJsonOnly`).
101
+
102
+ In any of these files, a pattern beginning with `!` excludes the paths
103
+ it matches, following npm's semantics: `!a/b` excludes only that
97
104
  directory, `!a/b/**` excludes the subtree, and a later positive
98
105
  pattern overrides an earlier exclusion.
99
106
 
package/dist/index.d.ts CHANGED
@@ -55,26 +55,41 @@ export declare const asManifestWSConfig: (conf: unknown, path: string) => Worksp
55
55
  /**
56
56
  * Which file a project's workspace definitions came from.
57
57
  */
58
- export type WorkspaceConfigSource = 'vlt.json' | 'package.json';
58
+ export type WorkspaceConfigSource = 'vlt.json' | 'package.json' | 'pnpm-workspace.yaml';
59
59
  export type ResolvedWorkspaceConfig = {
60
60
  config: WorkspaceConfigObject;
61
61
  /**
62
- * The file the config came from, or `undefined` when neither
63
- * `vlt.json` nor the root `package.json` declares any workspaces at
64
- * all -- ie, when the project is not a monorepo.
62
+ * The file the config came from, or `undefined` when none of
63
+ * `vlt.json`, the root `package.json` or `pnpm-workspace.yaml`
64
+ * declares any workspaces -- ie, when the project is not a monorepo.
65
65
  */
66
66
  source?: WorkspaceConfigSource;
67
67
  };
68
+ /**
69
+ * Process-wide: when true, {@link resolveWSConfig} reads `vlt.json`
70
+ * only, ignoring `package.json` and `pnpm-workspace.yaml`. Set by
71
+ * `vlt ci`.
72
+ */
73
+ export declare const setVltJsonOnly: (value: boolean) => void;
68
74
  /**
69
75
  * Resolve the effective workspace definitions for a project.
70
76
  *
71
77
  * `vlt.json` wins whenever it has a `workspaces` field; otherwise the
72
78
  * npm/yarn-style `workspaces` field of the project root's
73
- * `package.json` is used. Note the precedence is keyed on the *field*,
79
+ * `package.json` is used, then the `packages` list of a root
80
+ * `pnpm-workspace.yaml`. Note the precedence is keyed on the *field*,
74
81
  * not the file: a `vlt.json` that exists but says nothing about
75
82
  * workspaces still falls through to `package.json`.
83
+ *
84
+ * `vltJsonOnlyArg` skips both fallbacks (default: {@link setVltJsonOnly}).
85
+ */
86
+ export declare const resolveWSConfig: (projectRoot: string, packageJson?: PackageJson, vltJsonOnlyArg?: boolean) => ResolvedWorkspaceConfig;
87
+ /**
88
+ * Globs to save to `vlt.json` for workspaces declared in `package.json`
89
+ * or `pnpm-workspace.yaml`: plain array, in order, `!` kept (yarn
90
+ * `nohoist` already dropped). `undefined` if from `vlt.json` or none.
76
91
  */
77
- export declare const resolveWSConfig: (projectRoot: string, packageJson?: PackageJson) => ResolvedWorkspaceConfig;
92
+ export declare const migratableWSConfig: ({ config, source, }: ResolvedWorkspaceConfig) => string[] | undefined;
78
93
  /**
79
94
  * Split a list of glob patterns into the positive patterns and the
80
95
  * npm-compatible `!`-negated ones, which become ignore patterns.
@@ -99,9 +114,9 @@ export type MonorepoOptions = {
99
114
  */
100
115
  scurry?: PathScurry;
101
116
  /**
102
- * Parsed normalized contents of the workspaces, from either a
103
- * `vlt.json` or a `package.json` file. If set, the file is not read
104
- * again.
117
+ * Parsed normalized contents of the workspaces, from `vlt.json`,
118
+ * `package.json` or `pnpm-workspace.yaml`. If set, the file is not
119
+ * read again.
105
120
  */
106
121
  config?: WorkspaceConfigObject;
107
122
  /**
@@ -130,9 +145,10 @@ export declare class Monorepo {
130
145
  get size(): number;
131
146
  constructor(projectRoot: string, options?: MonorepoOptions);
132
147
  /**
133
- * Load the workspace definitions from vlt.json, or from the root
134
- * package.json when vlt.json declares none, canonicalizing the result
135
- * into the effective `{[group:string]:string[]}` form.
148
+ * Load the workspace definitions from vlt.json, else the root
149
+ * package.json, else pnpm-workspace.yaml (see {@link resolveWSConfig}),
150
+ * canonicalizing the result into the effective
151
+ * `{[group:string]:string[]}` form.
136
152
  *
137
153
  * Eg:
138
154
  * - `"src/*"` => `{packages:["src/*"]}`
@@ -249,8 +265,9 @@ export declare class Monorepo {
249
265
  /**
250
266
  * Convenience method to instantiate and load in one call.
251
267
  * Returns undefined if the project is not a monorepo workspaces root,
252
- * meaning neither `vlt.json` nor the root `package.json` declares any
253
- * workspaces. Otherwise returns the loaded Monorepo.
268
+ * meaning none of `vlt.json`, the root `package.json` or
269
+ * `pnpm-workspace.yaml` declares any workspaces. Otherwise returns
270
+ * the loaded Monorepo.
254
271
  */
255
272
  static maybeLoad(projectRoot: string, options?: MonorepoOptions): Monorepo | undefined;
256
273
  /**
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ import { graphRun, graphRunSync } from '@vltpkg/graph-run';
8
8
  import { minimatch } from 'minimatch';
9
9
  import { basename, posix, resolve } from 'node:path';
10
10
  import { PathScurry } from 'path-scurry';
11
+ import { readPnpmWorkspacePackages } from "./pnpm-workspace.js";
11
12
  /**
12
13
  * Check if an error (potentially wrapped by @vltpkg/error-cause) is
13
14
  * caused by a JSON syntax error. The error chain from PackageJson.read()
@@ -124,30 +125,56 @@ export const asManifestWSConfig = (conf, path) => {
124
125
  }
125
126
  return asWSConfig(conf, path);
126
127
  };
128
+ let vltJsonOnly = false;
129
+ /**
130
+ * Process-wide: when true, {@link resolveWSConfig} reads `vlt.json`
131
+ * only, ignoring `package.json` and `pnpm-workspace.yaml`. Set by
132
+ * `vlt ci`.
133
+ */
134
+ export const setVltJsonOnly = (value) => {
135
+ vltJsonOnly = value;
136
+ };
127
137
  /**
128
138
  * Resolve the effective workspace definitions for a project.
129
139
  *
130
140
  * `vlt.json` wins whenever it has a `workspaces` field; otherwise the
131
141
  * npm/yarn-style `workspaces` field of the project root's
132
- * `package.json` is used. Note the precedence is keyed on the *field*,
142
+ * `package.json` is used, then the `packages` list of a root
143
+ * `pnpm-workspace.yaml`. Note the precedence is keyed on the *field*,
133
144
  * not the file: a `vlt.json` that exists but says nothing about
134
145
  * workspaces still falls through to `package.json`.
146
+ *
147
+ * `vltJsonOnlyArg` skips both fallbacks (default: {@link setVltJsonOnly}).
135
148
  */
136
- export const resolveWSConfig = (projectRoot, packageJson = new PackageJson()) => {
149
+ export const resolveWSConfig = (projectRoot, packageJson = new PackageJson(), vltJsonOnlyArg = vltJsonOnly) => {
137
150
  const fromVltJson = load('workspaces', assertWSConfig);
138
151
  if (fromVltJson !== undefined) {
139
152
  return { config: asWSConfig(fromVltJson), source: 'vlt.json' };
140
153
  }
154
+ if (vltJsonOnlyArg)
155
+ return { config: {} };
141
156
  // maybeRead, because a project root need not have a package.json at
142
157
  // all, and an unreadable one is not this module's error to report.
143
158
  const workspaces = packageJson.maybeRead(projectRoot)?.workspaces;
144
- if (workspaces === undefined)
145
- return { config: {} };
159
+ if (workspaces === undefined) {
160
+ const packages = readPnpmWorkspacePackages(projectRoot);
161
+ return packages ?
162
+ { config: { packages }, source: 'pnpm-workspace.yaml' }
163
+ : { config: {} };
164
+ }
146
165
  return {
147
166
  config: asManifestWSConfig(workspaces, resolve(projectRoot, 'package.json')),
148
167
  source: 'package.json',
149
168
  };
150
169
  };
170
+ /**
171
+ * Globs to save to `vlt.json` for workspaces declared in `package.json`
172
+ * or `pnpm-workspace.yaml`: plain array, in order, `!` kept (yarn
173
+ * `nohoist` already dropped). `undefined` if from `vlt.json` or none.
174
+ */
175
+ export const migratableWSConfig = ({ config, source, }) => source && source !== 'vlt.json' ?
176
+ Object.values(config).flat()
177
+ : undefined;
151
178
  /**
152
179
  * Split a list of glob patterns into the positive patterns and the
153
180
  * npm-compatible `!`-negated ones, which become ignore patterns.
@@ -208,9 +235,10 @@ export class Monorepo {
208
235
  this.load(options.load);
209
236
  }
210
237
  /**
211
- * Load the workspace definitions from vlt.json, or from the root
212
- * package.json when vlt.json declares none, canonicalizing the result
213
- * into the effective `{[group:string]:string[]}` form.
238
+ * Load the workspace definitions from vlt.json, else the root
239
+ * package.json, else pnpm-workspace.yaml (see {@link resolveWSConfig}),
240
+ * canonicalizing the result into the effective
241
+ * `{[group:string]:string[]}` form.
214
242
  *
215
243
  * Eg:
216
244
  * - `"src/*"` => `{packages:["src/*"]}`
@@ -640,8 +668,9 @@ export class Monorepo {
640
668
  /**
641
669
  * Convenience method to instantiate and load in one call.
642
670
  * Returns undefined if the project is not a monorepo workspaces root,
643
- * meaning neither `vlt.json` nor the root `package.json` declares any
644
- * workspaces. Otherwise returns the loaded Monorepo.
671
+ * meaning none of `vlt.json`, the root `package.json` or
672
+ * `pnpm-workspace.yaml` declares any workspaces. Otherwise returns
673
+ * the loaded Monorepo.
645
674
  */
646
675
  static maybeLoad(projectRoot, options = { load: {} }) {
647
676
  if (options.config) {
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The `packages` globs of a `pnpm-workspace.yaml` file. Only a plain
3
+ * block or flow list of strings is read; anything else, or a list
4
+ * without positive globs (after dropping `.` root entries), gives
5
+ * `undefined`. Never throws.
6
+ */
7
+ export declare const parsePnpmWorkspacePackages: (yaml: string) => string[] | undefined;
8
+ /**
9
+ * Read the `packages` globs from `pnpm-workspace.yaml` in the project
10
+ * root, or `undefined` if missing or not parseable.
11
+ */
12
+ export declare const readPnpmWorkspacePackages: (projectRoot: string) => string[] | undefined;
@@ -0,0 +1,150 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { resolve } from 'node:path';
3
+ // a quote opens a scalar, or `#` a comment, only after a separator
4
+ const sep = (prev, flow) => !prev || (flow ? /[\s[,]/ : /\s/).test(prev);
5
+ // end of the quoted scalar opening at `i`, or -1 if not closed
6
+ const skipQuoted = (s, i) => {
7
+ const q = s.charAt(i);
8
+ for (let j = i + 1; j < s.length && s.charAt(j) !== '\n'; j++) {
9
+ const c = s.charAt(j);
10
+ if (q === '"' && c === '\\')
11
+ j++;
12
+ else if (c === q) {
13
+ if (q === "'" && s.charAt(j + 1) === "'")
14
+ j++;
15
+ else
16
+ return j + 1;
17
+ }
18
+ }
19
+ return -1;
20
+ };
21
+ const isQuote = (c) => c === "'" || c === '"';
22
+ const stripComment = (line, flow) => {
23
+ for (let i = 0; i < line.length; i++) {
24
+ const c = line.charAt(i);
25
+ const prev = line.charAt(i - 1);
26
+ if (isQuote(c) && sep(prev, flow)) {
27
+ const end = skipQuoted(line, i);
28
+ if (end < 0)
29
+ break;
30
+ i = end - 1;
31
+ }
32
+ else if (c === '#' && sep(prev, false)) {
33
+ return line.slice(0, i).trimEnd();
34
+ }
35
+ }
36
+ return line.trimEnd();
37
+ };
38
+ // raw items of a `[a, 'b']` flow sequence that may span lines
39
+ const splitFlow = (s) => {
40
+ const items = [];
41
+ let start = 1;
42
+ for (let i = 1; i < s.length; i++) {
43
+ const c = s.charAt(i);
44
+ if (isQuote(c) && sep(s.charAt(i - 1), true)) {
45
+ const end = skipQuoted(s, i);
46
+ if (end < 0)
47
+ return undefined;
48
+ i = end - 1;
49
+ }
50
+ else if (c === '[' || c === '{') {
51
+ return undefined;
52
+ }
53
+ else if (c === ',' || c === ']') {
54
+ const item = s.slice(start, i).trim();
55
+ if (item)
56
+ items.push(item);
57
+ else if (c === ',')
58
+ return undefined;
59
+ if (c === ']') {
60
+ return /^(?:\n|$)/.test(s.slice(i + 1)) ? items : undefined;
61
+ }
62
+ start = i + 1;
63
+ }
64
+ }
65
+ return undefined;
66
+ };
67
+ // raw items of a `- a` block sequence, up to the next top-level key
68
+ const splitBlock = (lines) => {
69
+ const items = [];
70
+ let indent;
71
+ for (const line of lines) {
72
+ const l = stripComment(line, false);
73
+ if (!l.trim())
74
+ continue;
75
+ const m = /^ *- +(?=\S)/.exec(l);
76
+ if (!m) {
77
+ if (/^\S/.test(l) && l !== '-')
78
+ break;
79
+ return undefined;
80
+ }
81
+ const i = l.indexOf('-');
82
+ indent ??= i;
83
+ if (i !== indent)
84
+ return undefined;
85
+ items.push(l.slice(m[0].length));
86
+ }
87
+ return items;
88
+ };
89
+ const scalar = (s, flow) => {
90
+ if (/^'(?:[^']|'')*'$/.test(s)) {
91
+ return s.slice(1, -1).replace(/''/g, "'");
92
+ }
93
+ if (s.startsWith('"')) {
94
+ try {
95
+ // starts with `"`, so a string if it parses at all
96
+ return JSON.parse(s);
97
+ }
98
+ catch {
99
+ return undefined;
100
+ }
101
+ }
102
+ return (/^[-?:,[\]{}#&*!|>'"%@`]/.test(s) ||
103
+ /: |:$|\s['"]/.test(s) ||
104
+ (flow && /[{}\n]/.test(s))) ?
105
+ undefined
106
+ : s;
107
+ };
108
+ /**
109
+ * The `packages` globs of a `pnpm-workspace.yaml` file. Only a plain
110
+ * block or flow list of strings is read; anything else, or a list
111
+ * without positive globs (after dropping `.` root entries), gives
112
+ * `undefined`. Never throws.
113
+ */
114
+ export const parsePnpmWorkspacePackages = (yaml) => {
115
+ const lines = yaml.replace(/^\uFEFF/, '').split(/\r?\n/);
116
+ const keys = lines.flatMap((l, i) => {
117
+ const m = /^(['"]?)packages\1[ \t]*:(?=[ \t]|$)/.exec(l);
118
+ return m ? [{ i, value: l.slice(m[0].length) }] : [];
119
+ });
120
+ const [key, dup] = keys;
121
+ if (!key || dup)
122
+ return undefined;
123
+ const flow = key.value.trimStart().startsWith('[');
124
+ const rest = stripComment(key.value, flow).trim();
125
+ const after = lines.slice(key.i + 1);
126
+ const raw = flow ?
127
+ splitFlow([rest, ...after.map(l => stripComment(l, true))].join('\n'))
128
+ : rest ? undefined
129
+ : splitBlock(after);
130
+ const out = raw?.map(r => scalar(r, flow));
131
+ if (!out?.every(v => v !== undefined))
132
+ return undefined;
133
+ // root is always the main importer, never a workspace
134
+ const globs = out.filter(v => v !== '.' && v !== './');
135
+ return globs.some(g => !g.startsWith('!')) ? globs : undefined;
136
+ };
137
+ /**
138
+ * Read the `packages` globs from `pnpm-workspace.yaml` in the project
139
+ * root, or `undefined` if missing or not parseable.
140
+ */
141
+ export const readPnpmWorkspacePackages = (projectRoot) => {
142
+ let yaml;
143
+ try {
144
+ yaml = readFileSync(resolve(projectRoot, 'pnpm-workspace.yaml'), 'utf8');
145
+ }
146
+ catch {
147
+ return undefined;
148
+ }
149
+ return parsePnpmWorkspacePackages(yaml);
150
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/workspaces",
3
3
  "description": "Utility for working with vlt workspaces",
4
- "version": "1.3.8",
4
+ "version": "1.5.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,13 +13,13 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/dep-id": "1.3.8",
17
- "@vltpkg/error-cause": "1.3.8",
18
- "@vltpkg/graph-run": "1.3.8",
19
- "@vltpkg/package-json": "1.3.8",
20
- "@vltpkg/semver": "1.3.8",
21
- "@vltpkg/types": "1.3.8",
22
- "@vltpkg/vlt-json": "1.3.8",
16
+ "@vltpkg/dep-id": "1.5.0",
17
+ "@vltpkg/error-cause": "1.5.0",
18
+ "@vltpkg/graph-run": "1.5.0",
19
+ "@vltpkg/package-json": "1.5.0",
20
+ "@vltpkg/semver": "1.5.0",
21
+ "@vltpkg/types": "1.5.0",
22
+ "@vltpkg/vlt-json": "1.5.0",
23
23
  "glob": "^13.0.0",
24
24
  "minimatch": "^10.1.1",
25
25
  "path-scurry": "^2.0.1"