@effected/workspaces 0.39.0 → 0.40.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/SourceBoundary.js +262 -6
- package/WorkspaceDiscovery.js +4 -2
- package/WorkspaceMarker.js +47 -0
- package/WorkspaceRoot.js +107 -34
- package/Workspaces.js +6 -3
- package/WorkspacesSync.js +44 -6
- package/index.d.ts +227 -11
- package/index.js +4 -3
- package/internal/causeMessage.js +22 -4
- package/internal/layerRoot.js +18 -5
- package/package.json +11 -11
- package/testing.d.ts +156 -10
- package/testing.js +2 -1
package/SourceBoundary.js
CHANGED
|
@@ -24,9 +24,14 @@ var Offence = class extends Schema.Class("Offence")({
|
|
|
24
24
|
"console",
|
|
25
25
|
"console-stdout",
|
|
26
26
|
"forbidImports",
|
|
27
|
-
"forbidTokens"
|
|
27
|
+
"forbidTokens",
|
|
28
|
+
"forbidImportedNames"
|
|
28
29
|
]),
|
|
29
|
-
/**
|
|
30
|
+
/**
|
|
31
|
+
* What matched: the identifier, the call, the import specifier, the token,
|
|
32
|
+
* or, for `forbidImportedNames`, `<name> from <specifier>` (`*` for an
|
|
33
|
+
* `export * from`).
|
|
34
|
+
*/
|
|
30
35
|
detail: Schema.String
|
|
31
36
|
}) {
|
|
32
37
|
/** `file:line:column rule detail`, the form an assertion message reads best in. */
|
|
@@ -93,8 +98,8 @@ const stdoutWrites = (code) => [...code.matchAll(STDOUT_WRITE)].map((match) => (
|
|
|
93
98
|
at,
|
|
94
99
|
method
|
|
95
100
|
}));
|
|
96
|
-
/** The member
|
|
97
|
-
const
|
|
101
|
+
/** The member directly after the reference at `at` (`console.log` → `"log"`, `console?.log` too) and its offset, or `undefined` when it is not a named member access. */
|
|
102
|
+
const memberAt = (code, at, name) => {
|
|
98
103
|
let j = at + name.length;
|
|
99
104
|
while (j < code.length && /\s/.test(code[j] ?? "")) j++;
|
|
100
105
|
if (code[j] === "?" && code[j + 1] === ".") j++;
|
|
@@ -103,7 +108,161 @@ const memberAfter = (code, at, name) => {
|
|
|
103
108
|
while (j < code.length && /\s/.test(code[j] ?? "")) j++;
|
|
104
109
|
let end = j;
|
|
105
110
|
while (isIdentifierChar(code[end])) end++;
|
|
106
|
-
return end > j ?
|
|
111
|
+
return end > j ? {
|
|
112
|
+
name: code.slice(j, end),
|
|
113
|
+
at: j
|
|
114
|
+
} : void 0;
|
|
115
|
+
};
|
|
116
|
+
/** The member name directly after the reference at `at`, or `undefined` when it is not a named member access. */
|
|
117
|
+
const memberAfter = (code, at, name) => memberAt(code, at, name)?.name;
|
|
118
|
+
/**
|
|
119
|
+
* The tokens of the clause between an `import`/`export` keyword and its
|
|
120
|
+
* `from`: identifiers, `{`, `}`, `,`, `*`, and a string-literal name (as
|
|
121
|
+
* `""`, carrying its value from `literals`, keyed by the opening quote's
|
|
122
|
+
* offset). `undefined` when anything else appears, so the text is not a
|
|
123
|
+
* clause.
|
|
124
|
+
*/
|
|
125
|
+
const clauseTokens = (code, start, end, literals) => {
|
|
126
|
+
const tokens = [];
|
|
127
|
+
let i = start;
|
|
128
|
+
while (i < end) {
|
|
129
|
+
const char = code[i] ?? "";
|
|
130
|
+
if (/\s/.test(char)) i++;
|
|
131
|
+
else if (isIdentifierChar(char)) {
|
|
132
|
+
let j = i;
|
|
133
|
+
while (j < end && isIdentifierChar(code[j])) j++;
|
|
134
|
+
tokens.push({
|
|
135
|
+
text: code.slice(i, j),
|
|
136
|
+
at: i
|
|
137
|
+
});
|
|
138
|
+
i = j;
|
|
139
|
+
} else if (char === "\"" || char === "'") {
|
|
140
|
+
const close = code.indexOf(char, i + 1);
|
|
141
|
+
if (close === -1 || close >= end) return void 0;
|
|
142
|
+
const literal = literals.get(i);
|
|
143
|
+
tokens.push(literal === void 0 ? {
|
|
144
|
+
text: "\"\"",
|
|
145
|
+
at: i
|
|
146
|
+
} : {
|
|
147
|
+
text: "\"\"",
|
|
148
|
+
at: i,
|
|
149
|
+
literal
|
|
150
|
+
});
|
|
151
|
+
i = close + 1;
|
|
152
|
+
} else if ("{},*".includes(char)) {
|
|
153
|
+
tokens.push({
|
|
154
|
+
text: char,
|
|
155
|
+
at: i
|
|
156
|
+
});
|
|
157
|
+
i++;
|
|
158
|
+
} else return;
|
|
159
|
+
}
|
|
160
|
+
return tokens;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* A TypeScript import-equals declaration ending at the `(` of its `require(`:
|
|
164
|
+
* `import m = require(`, `import type m = require(` or `export import m =
|
|
165
|
+
* require(`. Group 1 is the binding.
|
|
166
|
+
*/
|
|
167
|
+
const IMPORT_EQUALS = /(?<![\p{ID_Continue}$.])import\s+(?:type\s+)?([\p{ID_Start}$_][\p{ID_Continue}$\u200c\u200d]*)\s*=\s*require\s*\($/u;
|
|
168
|
+
/** An `import` or `export` keyword that is a statement keyword, not a member or part of a longer name. */
|
|
169
|
+
const CLAUSE_KEYWORD = /(?<![\p{ID_Continue}$.])(import|export)(?![\p{ID_Continue}$])/gu;
|
|
170
|
+
/** The last `import`/`export` keyword before `end` and its offset; `at` is `-1` when there is none. */
|
|
171
|
+
const clauseKeyword = (code, end) => {
|
|
172
|
+
let found = {
|
|
173
|
+
keyword: "import",
|
|
174
|
+
at: -1
|
|
175
|
+
};
|
|
176
|
+
for (const match of code.slice(0, end).matchAll(CLAUSE_KEYWORD)) found = {
|
|
177
|
+
keyword: match[1] === "export" ? "export" : "import",
|
|
178
|
+
at: match.index ?? -1
|
|
179
|
+
};
|
|
180
|
+
return found;
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* The imported names of a `{ ... }` list (`a`, `a as b`, `type a`), and a
|
|
184
|
+
* string-literal name (`"a" as b`, an arbitrary module namespace name) by its
|
|
185
|
+
* value.
|
|
186
|
+
*/
|
|
187
|
+
const namedImports = (tokens) => {
|
|
188
|
+
const names = [];
|
|
189
|
+
let element = [];
|
|
190
|
+
const flush = () => {
|
|
191
|
+
const [first, second] = element;
|
|
192
|
+
const name = first?.text === "type" && second !== void 0 && second.text !== "as" ? second : first;
|
|
193
|
+
if (name !== void 0) {
|
|
194
|
+
if (name.text !== "\"\"") names.push({
|
|
195
|
+
name: name.text,
|
|
196
|
+
at: name.at
|
|
197
|
+
});
|
|
198
|
+
else if (name.literal !== void 0) names.push({
|
|
199
|
+
name: name.literal,
|
|
200
|
+
at: name.at
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
element = [];
|
|
204
|
+
};
|
|
205
|
+
for (const token of tokens) if (token.text === ",") flush();
|
|
206
|
+
else element.push(token);
|
|
207
|
+
flush();
|
|
208
|
+
return names;
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* Where lexed source imports one of `rule.names` from `rule.from`: named and
|
|
212
|
+
* default bindings (a string-literal name included), named re-exports and
|
|
213
|
+
* `export *`, and member accesses on a namespace binding of the module —
|
|
214
|
+
* `import * as m from` or the TypeScript `import m = require(...)`.
|
|
215
|
+
*/
|
|
216
|
+
const importedNameUses = (lexed, rule) => {
|
|
217
|
+
const { code } = lexed;
|
|
218
|
+
const forbidden = new Set(rule.names);
|
|
219
|
+
const found = [];
|
|
220
|
+
const report = (offset, name) => {
|
|
221
|
+
found.push({
|
|
222
|
+
offset,
|
|
223
|
+
detail: `${name} from ${rule.from}`
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
const literalAt = new Map(lexed.literals.map((literal) => [literal.start, literal.value]));
|
|
227
|
+
/** Report every forbidden member access on the namespace binding `binding`. */
|
|
228
|
+
const namespaceMembers = (binding) => {
|
|
229
|
+
for (const reference of references(code, binding)) {
|
|
230
|
+
const member = memberAt(code, reference, binding);
|
|
231
|
+
if (member !== void 0 && forbidden.has(member.name)) report(member.at, member.name);
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
for (const literal of specifierLiterals(lexed)) {
|
|
235
|
+
if (literal.value !== rule.from) continue;
|
|
236
|
+
let j = literal.start - 1;
|
|
237
|
+
while (j >= 0 && /\s/.test(code[j] ?? "")) j--;
|
|
238
|
+
if (code[j] === "(") {
|
|
239
|
+
const binding = IMPORT_EQUALS.exec(code.slice(0, j + 1))?.[1];
|
|
240
|
+
if (binding !== void 0) namespaceMembers(binding);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (code.slice(j - 3, j + 1) !== "from" || isIdentifierChar(code[j - 4])) continue;
|
|
244
|
+
const { keyword, at } = clauseKeyword(code, j - 3);
|
|
245
|
+
if (at === -1) continue;
|
|
246
|
+
const tokens = clauseTokens(code, at + keyword.length, j - 3, literalAt);
|
|
247
|
+
if (tokens === void 0 || tokens.length === 0) continue;
|
|
248
|
+
const rest = tokens[0]?.text === "type" && tokens.length > 1 && tokens[1]?.text !== "," ? tokens.slice(1) : tokens;
|
|
249
|
+
const open = rest.findIndex((token) => token.text === "{");
|
|
250
|
+
const close = rest.findIndex((token) => token.text === "}");
|
|
251
|
+
if (open !== -1 && close > open) {
|
|
252
|
+
for (const { name, at: nameAt } of namedImports(rest.slice(open + 1, close))) if (forbidden.has(name)) report(nameAt, name);
|
|
253
|
+
}
|
|
254
|
+
const star = rest.findIndex((token) => token.text === "*");
|
|
255
|
+
if (keyword === "export") {
|
|
256
|
+
if (star !== -1 && forbidden.size > 0) report(rest[star]?.at ?? at, "*");
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
const first = rest[0];
|
|
260
|
+
if (first !== void 0 && first.text !== "{" && first.text !== "*" && forbidden.has("default")) report(first.at, "default");
|
|
261
|
+
const namespace = star === -1 || rest[star + 1]?.text !== "as" ? void 0 : rest[star + 2];
|
|
262
|
+
if (namespace === void 0 || !isIdentifierChar(namespace.text[0])) continue;
|
|
263
|
+
namespaceMembers(namespace.text);
|
|
264
|
+
}
|
|
265
|
+
return found;
|
|
107
266
|
};
|
|
108
267
|
/**
|
|
109
268
|
* Every offset where `token` appears in `code` as whole text: a token that
|
|
@@ -395,6 +554,87 @@ const FIXTURES = [
|
|
|
395
554
|
rule: { forbidTokens: ["process.env.__PACKAGE_VERSION__"] },
|
|
396
555
|
flagged: true
|
|
397
556
|
},
|
|
557
|
+
{
|
|
558
|
+
name: "forbidImportedNames: an aliased named import",
|
|
559
|
+
source: "import { AppConfig, App as Application } from \"@effected/app\";",
|
|
560
|
+
rule: { forbidImportedNames: {
|
|
561
|
+
from: "@effected/app",
|
|
562
|
+
names: ["App"]
|
|
563
|
+
} },
|
|
564
|
+
flagged: true
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
name: "forbidImportedNames: a member of a namespace import",
|
|
568
|
+
source: "import * as app from \"@effected/app\";\nconst layer = app.App.layer;",
|
|
569
|
+
rule: { forbidImportedNames: {
|
|
570
|
+
from: "@effected/app",
|
|
571
|
+
names: ["App"]
|
|
572
|
+
} },
|
|
573
|
+
flagged: true
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
name: "forbidImportedNames: a named re-export",
|
|
577
|
+
source: "export { type AppStore } from \"@effected/app\";",
|
|
578
|
+
rule: { forbidImportedNames: {
|
|
579
|
+
from: "@effected/app",
|
|
580
|
+
names: ["AppStore"]
|
|
581
|
+
} },
|
|
582
|
+
flagged: true
|
|
583
|
+
},
|
|
584
|
+
{
|
|
585
|
+
name: "forbidImportedNames: an allowed name and an allowed namespace member",
|
|
586
|
+
source: "import { AppConfig } from \"@effected/app\";\nimport * as app from \"@effected/app\";\napp.AppConfig;",
|
|
587
|
+
rule: { forbidImportedNames: {
|
|
588
|
+
from: "@effected/app",
|
|
589
|
+
names: ["App"]
|
|
590
|
+
} },
|
|
591
|
+
flagged: false
|
|
592
|
+
},
|
|
593
|
+
{
|
|
594
|
+
name: "forbidImportedNames: the name from another module, and in a comment",
|
|
595
|
+
source: "import { App } from \"./App.js\"; // import { App } from \"@effected/app\";",
|
|
596
|
+
rule: { forbidImportedNames: {
|
|
597
|
+
from: "@effected/app",
|
|
598
|
+
names: ["App"]
|
|
599
|
+
} },
|
|
600
|
+
flagged: false
|
|
601
|
+
},
|
|
602
|
+
{
|
|
603
|
+
name: "forbidImportedNames: a string-literal import name",
|
|
604
|
+
source: "import { \"App\" as Application } from \"@effected/app\";",
|
|
605
|
+
rule: { forbidImportedNames: {
|
|
606
|
+
from: "@effected/app",
|
|
607
|
+
names: ["App"]
|
|
608
|
+
} },
|
|
609
|
+
flagged: true
|
|
610
|
+
},
|
|
611
|
+
{
|
|
612
|
+
name: "forbidImportedNames: a member of an import-equals binding",
|
|
613
|
+
source: "import app = require(\"@effected/app\");\nconst layer = app.App.layer;",
|
|
614
|
+
rule: { forbidImportedNames: {
|
|
615
|
+
from: "@effected/app",
|
|
616
|
+
names: ["App"]
|
|
617
|
+
} },
|
|
618
|
+
flagged: true
|
|
619
|
+
},
|
|
620
|
+
{
|
|
621
|
+
name: "forbidImportedNames: an allowed member of an import-equals binding",
|
|
622
|
+
source: "import app = require(\"@effected/app\");\nconst config = app.AppConfig;",
|
|
623
|
+
rule: { forbidImportedNames: {
|
|
624
|
+
from: "@effected/app",
|
|
625
|
+
names: ["App"]
|
|
626
|
+
} },
|
|
627
|
+
flagged: false
|
|
628
|
+
},
|
|
629
|
+
{
|
|
630
|
+
name: "forbidImportedNames: a local alias named like a forbidden name",
|
|
631
|
+
source: "import { AppConfig as App } from \"@effected/app\";",
|
|
632
|
+
rule: { forbidImportedNames: {
|
|
633
|
+
from: "@effected/app",
|
|
634
|
+
names: ["App"]
|
|
635
|
+
} },
|
|
636
|
+
flagged: false
|
|
637
|
+
},
|
|
398
638
|
{
|
|
399
639
|
name: "forbidTokens: a private field",
|
|
400
640
|
source: "const v = this.#process.env.__PACKAGE_VERSION__;",
|
|
@@ -440,7 +680,18 @@ const FIXTURES = [
|
|
|
440
680
|
* both words);
|
|
441
681
|
*
|
|
442
682
|
* - `forbidImports: ["node:*"]` does not catch a bare built-in such as
|
|
443
|
-
* `"fs"` (see {@link BoundaryRule})
|
|
683
|
+
* `"fs"` (see {@link BoundaryRule});
|
|
684
|
+
*
|
|
685
|
+
* - `{ forbidImportedNames }` reads static `import` and `export ... from`
|
|
686
|
+
* clauses and member accesses on a namespace binding (an `import * as m` or
|
|
687
|
+
* an `import m = require(...)`). It does not see a
|
|
688
|
+
* name taken from a dynamic `import()` or a plain `require()` call
|
|
689
|
+
* (`const { App } = await import(...)`), a computed member (`m["App"]`), a
|
|
690
|
+
* string-literal name written with an escape (`"\u0041pp"` reads as its
|
|
691
|
+
* escaped text, not `App`), a
|
|
692
|
+
* destructuring of a namespace binding (`const { App } = m`), or a
|
|
693
|
+
* namespace binding passed on whole. Without scope analysis, a local that
|
|
694
|
+
* shadows the namespace binding's name is read as the binding.
|
|
444
695
|
*
|
|
445
696
|
* Assert on {@link SourceBoundary.verifyFixtures} beside your own scan: it
|
|
446
697
|
* proves the scanner you are trusting still flags what it must and spares
|
|
@@ -519,6 +770,11 @@ var SourceBoundary = class SourceBoundary {
|
|
|
519
770
|
detail: member === void 0 ? "console" : `console.${member}`
|
|
520
771
|
});
|
|
521
772
|
}
|
|
773
|
+
else if ("forbidImportedNames" in rule) for (const { offset, detail } of importedNameUses(lexed, rule.forbidImportedNames)) found.push({
|
|
774
|
+
offset,
|
|
775
|
+
rule: "forbidImportedNames",
|
|
776
|
+
detail
|
|
777
|
+
});
|
|
522
778
|
else if ("forbidTokens" in rule) for (const token of new Set(rule.forbidTokens)) for (const offset of tokenOccurrences(lexed.code, token)) found.push({
|
|
523
779
|
offset,
|
|
524
780
|
rule: "forbidTokens",
|
package/WorkspaceDiscovery.js
CHANGED
|
@@ -147,6 +147,8 @@ var WorkspaceDiscovery = class WorkspaceDiscovery extends Context.Service()("@ef
|
|
|
147
147
|
const roots = yield* WorkspaceRoot;
|
|
148
148
|
const fs = yield* FileSystem.FileSystem;
|
|
149
149
|
const path = yield* Path.Path;
|
|
150
|
+
const markers = options?.markers;
|
|
151
|
+
const perCallFind = markers === void 0 ? void 0 : { markers };
|
|
150
152
|
/** Read one `package.json` into the tolerant discovery projection. */
|
|
151
153
|
const readPackage = (root, directory, relativePath) => Effect.gen(function* () {
|
|
152
154
|
const packageJsonPath = path.join(directory, "package.json");
|
|
@@ -269,7 +271,7 @@ var WorkspaceDiscovery = class WorkspaceDiscovery extends Context.Service()("@ef
|
|
|
269
271
|
const rootMemos = /* @__PURE__ */ new Map();
|
|
270
272
|
/** The memoized discovery for the workspace root containing `directory`. */
|
|
271
273
|
const memoIn = Effect.fn("WorkspaceDiscovery.memoIn")(function* (directory) {
|
|
272
|
-
const root = yield* roots.find(directory);
|
|
274
|
+
const root = yield* roots.find(directory, perCallFind);
|
|
273
275
|
const existing = rootMemos.get(root);
|
|
274
276
|
if (existing !== void 0) return yield* existing.memo;
|
|
275
277
|
const [built, invalidateOne] = yield* Effect.cachedInvalidateWithTTL(discoverAt(root), (exit) => Exit.isSuccess(exit) ? Duration.infinity : Duration.zero);
|
|
@@ -355,7 +357,7 @@ var WorkspaceDiscovery = class WorkspaceDiscovery extends Context.Service()("@ef
|
|
|
355
357
|
return [...seen.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
356
358
|
}),
|
|
357
359
|
refreshIn: Effect.fn("WorkspaceDiscovery.refreshIn")(function* (directory) {
|
|
358
|
-
const root = yield* roots.find(directory);
|
|
360
|
+
const root = yield* roots.find(directory, perCallFind);
|
|
359
361
|
const cell = rootMemos.get(root);
|
|
360
362
|
if (cell === void 0) return;
|
|
361
363
|
yield* cell.invalidate;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
// src/WorkspaceMarker.ts
|
|
4
|
+
/**
|
|
5
|
+
* A file that marks a workspace root: a `pnpm-workspace.yaml`, or a
|
|
6
|
+
* `package.json` carrying a `workspaces` field.
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
const WorkspaceMarker = Schema.Literals(["pnpm-workspace.yaml", "package.json"]);
|
|
11
|
+
/**
|
|
12
|
+
* The marker filenames {@link WorkspaceRoot} probes for, in priority order.
|
|
13
|
+
*
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
const WORKSPACE_MARKERS = WorkspaceMarker.literals;
|
|
17
|
+
/**
|
|
18
|
+
* The markers a search probes, in priority order: every marker by default,
|
|
19
|
+
* or the ones `markers` names.
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* An empty or unknown marker list is developer wiring, so it throws: a search
|
|
23
|
+
* that probes nothing would report "no root" for every directory.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
const probedMarkers = (markers) => {
|
|
28
|
+
if (markers === void 0) return WORKSPACE_MARKERS;
|
|
29
|
+
const unknown = markers.filter((marker) => !WORKSPACE_MARKERS.includes(marker));
|
|
30
|
+
if (markers.length === 0 || unknown.length > 0) throw new Error(`markers must name at least one of ${WORKSPACE_MARKERS.join(", ")}, received ${JSON.stringify(markers)}`);
|
|
31
|
+
return WORKSPACE_MARKERS.filter((marker) => markers.includes(marker));
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* A workspace root, and the marker file that made it one.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
var WorkspaceRootMatch = class extends Schema.Class("WorkspaceRootMatch")({
|
|
39
|
+
/** The workspace root directory. */
|
|
40
|
+
root: Schema.String,
|
|
41
|
+
/** Which marker matched. */
|
|
42
|
+
marker: WorkspaceMarker,
|
|
43
|
+
/** The marker file's path: `root` joined with `marker`. */
|
|
44
|
+
path: Schema.String
|
|
45
|
+
}) {};
|
|
46
|
+
|
|
47
|
+
export { WORKSPACE_MARKERS, WorkspaceMarker, WorkspaceRootMatch, probedMarkers };
|
package/WorkspaceRoot.js
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
|
+
import { WorkspaceRootMatch, probedMarkers } from "./WorkspaceMarker.js";
|
|
1
2
|
import { Context, Effect, FileSystem, Layer, Option, Path, Result, Schema } from "effect";
|
|
2
3
|
import { PackageJsonFormat } from "@effected/package-json";
|
|
3
4
|
import { Walker } from "@effected/walker";
|
|
4
5
|
|
|
5
6
|
// src/WorkspaceRoot.ts
|
|
6
|
-
/**
|
|
7
|
-
* The marker filenames {@link WorkspaceRoot} probes for, in priority order.
|
|
8
|
-
*
|
|
9
|
-
* @public
|
|
10
|
-
*/
|
|
11
|
-
const WORKSPACE_MARKERS = ["pnpm-workspace.yaml", "package.json"];
|
|
12
7
|
/** How the not-found message names the `package.json` marker: only one with a `workspaces` field counts. */
|
|
13
8
|
const PACKAGE_JSON_MARKER_PROSE = "package.json with a \"workspaces\" field";
|
|
14
9
|
/**
|
|
@@ -44,21 +39,60 @@ var WorkspaceRootNotFoundError = class extends Schema.TaggedError()("WorkspaceRo
|
|
|
44
39
|
}
|
|
45
40
|
};
|
|
46
41
|
/**
|
|
47
|
-
*
|
|
48
|
-
* `package.json` with a `workspaces` field.
|
|
42
|
+
* Which of `markers` makes `dir` a workspace root, in priority order: a
|
|
43
|
+
* `pnpm-workspace.yaml`, or a `package.json` with a `workspaces` field.
|
|
49
44
|
*
|
|
50
45
|
* A malformed root `package.json` is "not a root", not an error — the ascent
|
|
51
46
|
* continues past it, matching walker's absorption contract.
|
|
52
47
|
*/
|
|
53
|
-
const
|
|
48
|
+
const markerOf = (dir, markers) => Effect.gen(function* () {
|
|
54
49
|
const fs = yield* FileSystem.FileSystem;
|
|
55
50
|
const path = yield* Path.Path;
|
|
56
|
-
if (
|
|
51
|
+
if (markers.includes("pnpm-workspace.yaml")) {
|
|
52
|
+
if (yield* fs.exists(path.join(dir, "pnpm-workspace.yaml")).pipe(Effect.orElseSucceed(() => false))) return Option.some("pnpm-workspace.yaml");
|
|
53
|
+
}
|
|
54
|
+
if (!markers.includes("package.json")) return Option.none();
|
|
57
55
|
const packageJson = path.join(dir, "package.json");
|
|
58
|
-
if (!(yield* fs.exists(packageJson).pipe(Effect.orElseSucceed(() => false)))) return
|
|
56
|
+
if (!(yield* fs.exists(packageJson).pipe(Effect.orElseSucceed(() => false)))) return Option.none();
|
|
59
57
|
const content = yield* fs.readFileString(packageJson).pipe(Effect.orElseSucceed(() => "{}"));
|
|
60
58
|
const parsed = PackageJsonFormat.parseObjectResult(content);
|
|
61
|
-
return Result.isSuccess(parsed) && parsed.success.workspaces !== void 0 && parsed.success.workspaces !== null;
|
|
59
|
+
return Result.isSuccess(parsed) && parsed.success.workspaces !== void 0 && parsed.success.workspaces !== null ? Option.some("package.json") : Option.none();
|
|
60
|
+
});
|
|
61
|
+
/**
|
|
62
|
+
* The ascent both `WorkspaceRoot.find` (through the service) and
|
|
63
|
+
* `WorkspaceRoot.locate` (straight from `R`) run: one implementation, so the
|
|
64
|
+
* two cannot disagree about which root a set of options names. Not spanned —
|
|
65
|
+
* each caller opens its own named span.
|
|
66
|
+
*/
|
|
67
|
+
const locateIn = (cwd, options) => Effect.gen(function* () {
|
|
68
|
+
const path = yield* Path.Path;
|
|
69
|
+
const markers = yield* Effect.try({
|
|
70
|
+
try: () => probedMarkers(options?.markers),
|
|
71
|
+
catch: (error) => error
|
|
72
|
+
}).pipe(Effect.orDie);
|
|
73
|
+
const start = path.resolve(cwd);
|
|
74
|
+
const ceiling = options?.stopAt === void 0 ? void 0 : path.resolve(options.stopAt);
|
|
75
|
+
const chain = yield* Walker.ascend(start, {
|
|
76
|
+
...ceiling === void 0 ? {} : { stopAt: ceiling },
|
|
77
|
+
...options?.maxDepth === void 0 ? {} : { maxDepth: options.maxDepth }
|
|
78
|
+
});
|
|
79
|
+
let matched;
|
|
80
|
+
const found = yield* Walker.findRoot(chain, (dir) => Effect.map(markerOf(dir, markers), (marker) => {
|
|
81
|
+
if (Option.isNone(marker)) return false;
|
|
82
|
+
matched = marker.value;
|
|
83
|
+
return true;
|
|
84
|
+
}));
|
|
85
|
+
if (Option.isNone(found) || matched === void 0) return yield* Effect.fail(new WorkspaceRootNotFoundError({
|
|
86
|
+
searchPath: start,
|
|
87
|
+
markers,
|
|
88
|
+
...ceiling === void 0 ? {} : { stopAt: ceiling }
|
|
89
|
+
}));
|
|
90
|
+
yield* Effect.logDebug("Workspace root found").pipe(Effect.annotateLogs("workspace.root", found.value));
|
|
91
|
+
return WorkspaceRootMatch.make({
|
|
92
|
+
root: found.value,
|
|
93
|
+
marker: matched,
|
|
94
|
+
path: path.join(found.value, matched)
|
|
95
|
+
});
|
|
62
96
|
});
|
|
63
97
|
/** Whether `descendant` is `ancestor` itself or lies beneath it, on POSIX or win32 separators. */
|
|
64
98
|
const isAtOrBelow = (descendant, ancestor) => {
|
|
@@ -102,23 +136,48 @@ var WorkspaceRoot = class WorkspaceRoot extends Context.Service()("@effected/wor
|
|
|
102
136
|
const fs = yield* FileSystem.FileSystem;
|
|
103
137
|
const path = yield* Path.Path;
|
|
104
138
|
const find = Effect.fn("WorkspaceRoot.find")(function* (cwd, options) {
|
|
105
|
-
|
|
106
|
-
const ceiling = options?.stopAt === void 0 ? void 0 : path.resolve(options.stopAt);
|
|
107
|
-
const chain = yield* Walker.ascend(start, {
|
|
108
|
-
...ceiling === void 0 ? {} : { stopAt: ceiling },
|
|
109
|
-
...options?.maxDepth === void 0 ? {} : { maxDepth: options.maxDepth }
|
|
110
|
-
});
|
|
111
|
-
const found = yield* Walker.findRoot(chain, isWorkspaceRoot);
|
|
112
|
-
if (Option.isNone(found)) return yield* Effect.fail(new WorkspaceRootNotFoundError({
|
|
113
|
-
searchPath: start,
|
|
114
|
-
markers: WORKSPACE_MARKERS,
|
|
115
|
-
...ceiling === void 0 ? {} : { stopAt: ceiling }
|
|
116
|
-
}));
|
|
117
|
-
yield* Effect.logDebug("Workspace root found").pipe(Effect.annotateLogs("workspace.root", found.value));
|
|
118
|
-
return found.value;
|
|
139
|
+
return (yield* locateIn(cwd, options)).root;
|
|
119
140
|
});
|
|
120
141
|
return { find: (cwd, options) => find(cwd, options).pipe(Effect.provideService(FileSystem.FileSystem, fs), Effect.provideService(Path.Path, path)) };
|
|
121
142
|
});
|
|
143
|
+
/**
|
|
144
|
+
* The nearest workspace root at or above `cwd` and the marker file that made
|
|
145
|
+
* it one — the Effect form of `locateWorkspaceRootSync`.
|
|
146
|
+
*
|
|
147
|
+
* @remarks
|
|
148
|
+
* The same ascent as the service's `find`, with the same `stopAt`,
|
|
149
|
+
* `maxDepth` and `markers`, answering a {@link WorkspaceRootMatch} whose
|
|
150
|
+
* `path` is the matched file. A tool that rewrites the marker (an `export`
|
|
151
|
+
* writing `pnpm-workspace.yaml`) reads the path from here rather than
|
|
152
|
+
* re-deriving it from the root.
|
|
153
|
+
*
|
|
154
|
+
* It is a static over core `FileSystem` and `Path` in `R`, not a member of
|
|
155
|
+
* {@link WorkspaceRootShape}: a new member would break every hand-written
|
|
156
|
+
* `Layer.succeed(WorkspaceRoot, { find })` double. The consequence is that
|
|
157
|
+
* it never consults a provided `WorkspaceRoot` — a
|
|
158
|
+
* {@link WorkspaceRoot.layerTest} double does not answer it; a test gives it
|
|
159
|
+
* a filesystem (an `@effected/memfs` volume) instead. On the same inputs it
|
|
160
|
+
* agrees with `locateWorkspaceRootSync`, which answers `null` where this
|
|
161
|
+
* fails {@link WorkspaceRootNotFoundError}.
|
|
162
|
+
*
|
|
163
|
+
* @param cwd - The directory to start the ascent from; resolved to an
|
|
164
|
+
* absolute path first.
|
|
165
|
+
* @param options - The ascent bounds and marker filter of `find`.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* import { WorkspaceRoot } from "@effected/workspaces";
|
|
170
|
+
* import { Effect } from "effect";
|
|
171
|
+
*
|
|
172
|
+
* const program = Effect.gen(function* () {
|
|
173
|
+
* const match = yield* WorkspaceRoot.locate("/repo/packages/a", { markers: ["pnpm-workspace.yaml"] });
|
|
174
|
+
* return match.path; // "/repo/pnpm-workspace.yaml"
|
|
175
|
+
* });
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
static locate = Effect.fn("WorkspaceRoot.locate")(function* (cwd, options) {
|
|
179
|
+
return yield* locateIn(cwd, options);
|
|
180
|
+
});
|
|
122
181
|
/** The live layer. */
|
|
123
182
|
static layer = Layer.effect(WorkspaceRoot, WorkspaceRoot.make);
|
|
124
183
|
/**
|
|
@@ -141,19 +200,33 @@ var WorkspaceRoot = class WorkspaceRoot extends Context.Service()("@effected/wor
|
|
|
141
200
|
* `Path.layer` internally, so the requirement never surfaces at their call
|
|
142
201
|
* site.
|
|
143
202
|
*
|
|
203
|
+
* `markers` is honoured the same way: the double's root is marked by
|
|
204
|
+
* `marker` (`"pnpm-workspace.yaml"` unless given), so a `find` whose
|
|
205
|
+
* `markers` leave that marker out fails as the live service would, and a
|
|
206
|
+
* `find` with an empty list dies.
|
|
207
|
+
*
|
|
144
208
|
* `maxDepth` is deliberately NOT modelled: the double does not walk, so it has
|
|
145
209
|
* no depth to cap, and pretending otherwise would encode a fiction. A suite
|
|
146
210
|
* exercising the depth guard wants the live service over a fixture tree.
|
|
147
211
|
*
|
|
148
212
|
* @param root - The root every unbounded `find` resolves to.
|
|
213
|
+
* @param options - `marker`: the marker the double's root carries.
|
|
149
214
|
*/
|
|
150
|
-
static makeTest = (root) => Effect.gen(function* () {
|
|
215
|
+
static makeTest = (root, options) => Effect.gen(function* () {
|
|
151
216
|
const path = yield* Path.Path;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
markers
|
|
155
|
-
|
|
156
|
-
|
|
217
|
+
const marker = options?.marker ?? "pnpm-workspace.yaml";
|
|
218
|
+
return { find: (cwd, findOptions) => Effect.gen(function* () {
|
|
219
|
+
const markers = yield* Effect.try({
|
|
220
|
+
try: () => probedMarkers(findOptions?.markers),
|
|
221
|
+
catch: (error) => error
|
|
222
|
+
}).pipe(Effect.orDie);
|
|
223
|
+
if (findOptions?.stopAt !== void 0 && !isAtOrBelow(root, path.resolve(findOptions.stopAt)) || !markers.includes(marker)) return yield* Effect.fail(new WorkspaceRootNotFoundError({
|
|
224
|
+
searchPath: path.resolve(cwd),
|
|
225
|
+
markers,
|
|
226
|
+
...findOptions?.stopAt === void 0 ? {} : { stopAt: findOptions.stopAt }
|
|
227
|
+
}));
|
|
228
|
+
return root;
|
|
229
|
+
}) };
|
|
157
230
|
});
|
|
158
231
|
/**
|
|
159
232
|
* The test layer: {@link WorkspaceRoot.makeTest} with `Path.layer` provided.
|
|
@@ -184,7 +257,7 @@ var WorkspaceRoot = class WorkspaceRoot extends Context.Service()("@effected/wor
|
|
|
184
257
|
* // program.pipe(Effect.provide(TestRoot), Effect.provide(TestDiscovery))
|
|
185
258
|
* ```
|
|
186
259
|
*/
|
|
187
|
-
static layerTest = (root) => Layer.effect(WorkspaceRoot, WorkspaceRoot.makeTest(root)).pipe(Layer.provide(Path.layer));
|
|
260
|
+
static layerTest = (root, options) => Layer.effect(WorkspaceRoot, WorkspaceRoot.makeTest(root, options)).pipe(Layer.provide(Path.layer));
|
|
188
261
|
};
|
|
189
262
|
|
|
190
|
-
export {
|
|
263
|
+
export { WorkspaceRoot, WorkspaceRootNotFoundError };
|
package/Workspaces.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { layerFindOptions } from "./internal/layerRoot.js";
|
|
1
2
|
import { WorkspaceRoot } from "./WorkspaceRoot.js";
|
|
2
3
|
import { WorkspaceDiscovery } from "./WorkspaceDiscovery.js";
|
|
3
4
|
import { ChangeDetector } from "./ChangeDetector.js";
|
|
@@ -257,7 +258,10 @@ var Workspaces = class Workspaces {
|
|
|
257
258
|
* project-local launcher has to run where the workspace is. `stopAt` caps
|
|
258
259
|
* that ascent exactly as {@link WorkspacesOptions.stopAt} does, and a
|
|
259
260
|
* ceiling with no root at or below it is the same `Option.none()` as no
|
|
260
|
-
* root at all.
|
|
261
|
+
* root at all. `markers` filters the ascent exactly as
|
|
262
|
+
* {@link WorkspacesOptions.markers} does, so a pnpm-only tool's launcher
|
|
263
|
+
* runs from the pnpm workspace root, not from a member that kept
|
|
264
|
+
* `package.json#workspaces`.
|
|
261
265
|
*
|
|
262
266
|
* An omitted `cwd` is `"."`, which `WorkspaceRoot.find` resolves through
|
|
263
267
|
* the injected core `Path` on every call — this layer reads no `process`
|
|
@@ -298,8 +302,7 @@ var Workspaces = class Workspaces {
|
|
|
298
302
|
const detector = yield* PackageManagerDetector;
|
|
299
303
|
return { context: Effect.gen(function* () {
|
|
300
304
|
const cwd = options?.cwd ?? ".";
|
|
301
|
-
const
|
|
302
|
-
const root = yield* roots.find(cwd, stopAt === void 0 ? void 0 : { stopAt }).pipe(Effect.asSome, Effect.orElseSucceed(Option.none));
|
|
305
|
+
const root = yield* roots.find(cwd, layerFindOptions(options)).pipe(Effect.asSome, Effect.orElseSucceed(Option.none));
|
|
303
306
|
if (Option.isNone(root)) return Option.none();
|
|
304
307
|
const detected = yield* detector.detect(root.value).pipe(Effect.asSome, Effect.catchTag("PackageManagerDetectionError", () => Effect.succeed(Option.none())), Effect.mapError((cause) => new LocalExecError({
|
|
305
308
|
directory: root.value,
|