@vltpkg/workspaces 1.3.8 → 1.4.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
@@ -92,8 +92,12 @@ Note the precedence is keyed on the _field_, not the file: a
92
92
  `vlt.json` that exists but says nothing about workspaces still falls
93
93
  through to `package.json`. The two are never merged.
94
94
 
95
- In either file, a pattern beginning with `!` excludes the paths it
96
- matches, following npm's semantics: `!a/b` excludes only that
95
+ If neither declares workspaces, the `packages` list of a root
96
+ `pnpm-workspace.yaml` is used (plain string lists only; other keys and
97
+ unsupported YAML are ignored).
98
+
99
+ In any of these files, a pattern beginning with `!` excludes the paths
100
+ it matches, following npm's semantics: `!a/b` excludes only that
97
101
  directory, `!a/b/**` excludes the subtree, and a later positive
98
102
  pattern overrides an earlier exclusion.
99
103
 
package/dist/index.d.ts CHANGED
@@ -55,13 +55,13 @@ 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
  };
@@ -70,7 +70,8 @@ export type ResolvedWorkspaceConfig = {
70
70
  *
71
71
  * `vlt.json` wins whenever it has a `workspaces` field; otherwise the
72
72
  * npm/yarn-style `workspaces` field of the project root's
73
- * `package.json` is used. Note the precedence is keyed on the *field*,
73
+ * `package.json` is used, then the `packages` list of a root
74
+ * `pnpm-workspace.yaml`. Note the precedence is keyed on the *field*,
74
75
  * not the file: a `vlt.json` that exists but says nothing about
75
76
  * workspaces still falls through to `package.json`.
76
77
  */
@@ -99,9 +100,9 @@ export type MonorepoOptions = {
99
100
  */
100
101
  scurry?: PathScurry;
101
102
  /**
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.
103
+ * Parsed normalized contents of the workspaces, from `vlt.json`,
104
+ * `package.json` or `pnpm-workspace.yaml`. If set, the file is not
105
+ * read again.
105
106
  */
106
107
  config?: WorkspaceConfigObject;
107
108
  /**
@@ -130,9 +131,10 @@ export declare class Monorepo {
130
131
  get size(): number;
131
132
  constructor(projectRoot: string, options?: MonorepoOptions);
132
133
  /**
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.
134
+ * Load the workspace definitions from vlt.json, else the root
135
+ * package.json, else pnpm-workspace.yaml (see {@link resolveWSConfig}),
136
+ * canonicalizing the result into the effective
137
+ * `{[group:string]:string[]}` form.
136
138
  *
137
139
  * Eg:
138
140
  * - `"src/*"` => `{packages:["src/*"]}`
@@ -249,8 +251,9 @@ export declare class Monorepo {
249
251
  /**
250
252
  * Convenience method to instantiate and load in one call.
251
253
  * 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.
254
+ * meaning none of `vlt.json`, the root `package.json` or
255
+ * `pnpm-workspace.yaml` declares any workspaces. Otherwise returns
256
+ * the loaded Monorepo.
254
257
  */
255
258
  static maybeLoad(projectRoot: string, options?: MonorepoOptions): Monorepo | undefined;
256
259
  /**
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()
@@ -129,7 +130,8 @@ export const asManifestWSConfig = (conf, path) => {
129
130
  *
130
131
  * `vlt.json` wins whenever it has a `workspaces` field; otherwise the
131
132
  * npm/yarn-style `workspaces` field of the project root's
132
- * `package.json` is used. Note the precedence is keyed on the *field*,
133
+ * `package.json` is used, then the `packages` list of a root
134
+ * `pnpm-workspace.yaml`. Note the precedence is keyed on the *field*,
133
135
  * not the file: a `vlt.json` that exists but says nothing about
134
136
  * workspaces still falls through to `package.json`.
135
137
  */
@@ -141,8 +143,12 @@ export const resolveWSConfig = (projectRoot, packageJson = new PackageJson()) =>
141
143
  // maybeRead, because a project root need not have a package.json at
142
144
  // all, and an unreadable one is not this module's error to report.
143
145
  const workspaces = packageJson.maybeRead(projectRoot)?.workspaces;
144
- if (workspaces === undefined)
145
- return { config: {} };
146
+ if (workspaces === undefined) {
147
+ const packages = readPnpmWorkspacePackages(projectRoot);
148
+ return packages ?
149
+ { config: { packages }, source: 'pnpm-workspace.yaml' }
150
+ : { config: {} };
151
+ }
146
152
  return {
147
153
  config: asManifestWSConfig(workspaces, resolve(projectRoot, 'package.json')),
148
154
  source: 'package.json',
@@ -208,9 +214,10 @@ export class Monorepo {
208
214
  this.load(options.load);
209
215
  }
210
216
  /**
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.
217
+ * Load the workspace definitions from vlt.json, else the root
218
+ * package.json, else pnpm-workspace.yaml (see {@link resolveWSConfig}),
219
+ * canonicalizing the result into the effective
220
+ * `{[group:string]:string[]}` form.
214
221
  *
215
222
  * Eg:
216
223
  * - `"src/*"` => `{packages:["src/*"]}`
@@ -640,8 +647,9 @@ export class Monorepo {
640
647
  /**
641
648
  * Convenience method to instantiate and load in one call.
642
649
  * 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.
650
+ * meaning none of `vlt.json`, the root `package.json` or
651
+ * `pnpm-workspace.yaml` declares any workspaces. Otherwise returns
652
+ * the loaded Monorepo.
645
653
  */
646
654
  static maybeLoad(projectRoot, options = { load: {} }) {
647
655
  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.4.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.4.0",
17
+ "@vltpkg/error-cause": "1.4.0",
18
+ "@vltpkg/graph-run": "1.4.0",
19
+ "@vltpkg/package-json": "1.4.0",
20
+ "@vltpkg/semver": "1.4.0",
21
+ "@vltpkg/types": "1.4.0",
22
+ "@vltpkg/vlt-json": "1.4.0",
23
23
  "glob": "^13.0.0",
24
24
  "minimatch": "^10.1.1",
25
25
  "path-scurry": "^2.0.1"