@akanjs/devkit 3.2.0-rc.1 → 3.2.0-rc.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +52 -1
  2. package/akanContext.ts +1 -3
  3. package/akanMcpContract.ts +5 -4
  4. package/artifact/storeBootstrap.ts +3 -2
  5. package/codeAgent/skills/akan-scaffold/SKILL.md +2 -1
  6. package/commandDecorators/command.ts +35 -18
  7. package/commandDecorators/commandDecorators.test.ts +75 -3
  8. package/commandDecorators/targetMeta.ts +2 -0
  9. package/constantFieldNames.test.ts +34 -0
  10. package/constantFieldNames.ts +18 -0
  11. package/frontendBuild/buildRouteClient.test.ts +43 -0
  12. package/frontendBuild/clientEntryDiscovery.test.ts +52 -0
  13. package/frontendBuild/clientEntryDiscovery.ts +28 -3
  14. package/frontendBuild/csrArtifactBuilder.test.ts +48 -0
  15. package/frontendBuild/csrArtifactBuilder.ts +9 -3
  16. package/frontendBuild/csrDevBundler.ts +12 -2
  17. package/frontendBuild/csrDevModuleCompiler.test.ts +97 -0
  18. package/frontendBuild/csrDevModuleCompiler.ts +37 -8
  19. package/frontendBuild/csrDevPatcher.test.ts +89 -2
  20. package/frontendBuild/csrDevPatcher.ts +47 -7
  21. package/frontendBuild/csrDevTypes.ts +16 -0
  22. package/frontendBuild/cssCompiler.test.ts +63 -1
  23. package/frontendBuild/devBarrelImports.ts +132 -0
  24. package/frontendBuild/devRegistryBundler.ts +19 -4
  25. package/frontendBuild/mcpAppViewBuilder.test.ts +67 -0
  26. package/frontendBuild/ssrDevBundler.ts +13 -2
  27. package/package.json +3 -3
  28. package/preview/PreviewView.ts +2 -0
  29. package/preview/previewView.test.ts +46 -9
  30. package/transforms/barrelAnalyzer.ts +46 -1
  31. package/transforms/barrelImportsPlugin.ts +61 -45
  32. package/transforms/transforms.test.ts +67 -1
  33. package/workflow/artifacts.ts +17 -0
  34. package/workflow/index.ts +1 -0
  35. package/workflow/plainName.ts +15 -0
  36. package/workflow/plan.ts +55 -21
  37. package/workspaceGraph.test.ts +200 -3
  38. package/workspaceGraph.ts +46 -8
@@ -14,7 +14,12 @@ const slowChromeRuns = PreviewView.isAvailable("chrome") && process.platform !==
14
14
  const withSlowChrome = async (body: string, startSeconds = 3) => {
15
15
  const dir = await mkdtemp(path.join(tmpdir(), "akan-slow-chrome-"));
16
16
  const chrome = path.join(dir, "chrome");
17
- await writeFile(chrome, `#!/bin/sh\nsleep ${startSeconds}\nexec "${PreviewChrome.executable()}" "$@"\n`);
17
+ const chromeLog = path.join(dir, "chrome.log");
18
+ //? Bun.WebView sends the browser's stderr to /dev/null, which would hide why it died.
19
+ await writeFile(
20
+ chrome,
21
+ `#!/bin/sh\nexec 2>>"${chromeLog}"\nsleep ${startSeconds}\nexec "${PreviewChrome.executable()}" "$@"\n`,
22
+ );
18
23
  await chmod(chrome, 0o755);
19
24
  const script = `const { PreviewView } = await import(${JSON.stringify(path.join(import.meta.dir, "PreviewView.ts"))});\n${body}`;
20
25
  //? Chrome is spawned once per process, so only a fresh process takes the slow executable.
@@ -30,7 +35,10 @@ const withSlowChrome = async (body: string, startSeconds = 3) => {
30
35
  new Response(child.stderr).text(),
31
36
  child.exited,
32
37
  ]);
33
- return { code, out: out.trim(), err };
38
+ const chromeErr = await Bun.file(chromeLog)
39
+ .text()
40
+ .catch(() => "");
41
+ return { code, out: out.trim(), log: `${err}\nChrome's stderr:\n${chromeErr}` };
34
42
  } finally {
35
43
  child.kill();
36
44
  await rm(dir, { recursive: true, force: true });
@@ -112,12 +120,12 @@ describe("PreviewView", () => {
112
120
  async () => {
113
121
  const server = serve("<!doctype html><title>Up</title><body>up</body>");
114
122
  try {
115
- const { code, out, err } = await withSlowChrome(
123
+ const { code, out, log } = await withSlowChrome(
116
124
  `const probe = await PreviewView.probe("http://localhost:${server.port}/", { engine: "chrome", timeoutMs: 1_500 });
117
125
  console.log(probe.text);`,
118
126
  );
119
- expect(out || err).toBe("up");
120
- expect(code).toBe(0);
127
+ expect(out, log).toBe("up");
128
+ expect(code, log).toBe(0);
121
129
  } finally {
122
130
  server.stop(true);
123
131
  }
@@ -128,7 +136,7 @@ console.log(probe.text);`,
128
136
  test.skipIf(!slowChromeRuns)(
129
137
  "gives a later page only its own bound for a browser that missed its start, and recovers once it is up",
130
138
  async () => {
131
- const { code, out, err } = await withSlowChrome(
139
+ const { code, out, log } = await withSlowChrome(
132
140
  `const open = () =>
133
141
  PreviewView.probe("about:blank", { engine: "chrome", startTimeoutMs: 1_500, timeoutMs: 1_000 }).then(
134
142
  () => "opened",
@@ -137,7 +145,7 @@ console.log(probe.text);`,
137
145
  console.log(await open());
138
146
  console.log(await open());
139
147
  let late = await open();
140
- for (let tries = 0; late !== "opened" && tries < 100; tries++) late = await open();
148
+ for (let tries = 0; late.startsWith("Timed out starting") && tries < 100; tries++) late = await open();
141
149
  console.log(late);
142
150
  Bun.WebView.closeAll();
143
151
  await Bun.sleep(500);
@@ -145,7 +153,7 @@ console.log(await open());
145
153
  Bun.WebView.closeAll();`,
146
154
  6,
147
155
  );
148
- expect(out || err).toBe(
156
+ expect(out, log).toBe(
149
157
  [
150
158
  "Timed out starting the chrome browser after 1.5 s.",
151
159
  "Timed out starting the chrome browser after 1 s.",
@@ -153,7 +161,36 @@ Bun.WebView.closeAll();`,
153
161
  "Timed out starting the chrome browser after 1.5 s.",
154
162
  ].join("\n"),
155
163
  );
156
- expect(code).toBe(0);
164
+ expect(code, log).toBe(0);
165
+ },
166
+ browserTestMs,
167
+ );
168
+
169
+ test.skipIf(!slowChromeRuns)(
170
+ "gives a browser killed after it missed its start the full bound for its next start",
171
+ async () => {
172
+ const { code, out, log } = await withSlowChrome(
173
+ `const open = () =>
174
+ PreviewView.probe("about:blank", { engine: "chrome", startTimeoutMs: 1_500, timeoutMs: 1_000 }).then(
175
+ () => "opened",
176
+ (error) => error.message,
177
+ );
178
+ console.log(await open());
179
+ const killed = open();
180
+ Bun.WebView.closeAll();
181
+ console.error(await killed);
182
+ await Bun.sleep(500);
183
+ console.log(await open());
184
+ Bun.WebView.closeAll();`,
185
+ 6,
186
+ );
187
+ expect(out, log).toBe(
188
+ [
189
+ "Timed out starting the chrome browser after 1.5 s.",
190
+ "Timed out starting the chrome browser after 1.5 s.",
191
+ ].join("\n"),
192
+ );
193
+ expect(code, log).toBe(0);
157
194
  },
158
195
  browserTestMs,
159
196
  );
@@ -1,3 +1,4 @@
1
+ import fs from "node:fs";
1
2
  import path from "node:path";
2
3
  import { Logger } from "akanjs/common";
3
4
  import { parseNamedList } from "./moduleSyntax";
@@ -41,6 +42,8 @@ export class BarrelAnalyzer {
41
42
  readonly #logger = new Logger("BarrelAnalyzer");
42
43
  readonly #opts: BarrelAnalyzerOptions;
43
44
  readonly #cache = new Map<string, Promise<BarrelExportMap | null>>();
45
+ readonly #read = new Map<string, Set<string>>();
46
+ readonly #realDirs = new Map<string, string>();
44
47
  readonly #tsTranspiler = new Bun.Transpiler({ loader: "ts" });
45
48
  readonly #tsxTranspiler = new Bun.Transpiler({ loader: "tsx" });
46
49
 
@@ -56,12 +59,44 @@ export class BarrelAnalyzer {
56
59
  return promise;
57
60
  }
58
61
 
62
+ async filesOf(pkgName: string): Promise<string[]> {
63
+ await this.analyze(pkgName);
64
+ return [...(this.#read.get(pkgName) ?? [])];
65
+ }
66
+
67
+ //? A file beside one the analysis read counts too: created, it can take over a star export's specifier (`Foo.tsx`
68
+ //? beside `Foo/index.tsx`) with no read file changed. Both sides by realpath: a watcher names a symlinked lib's
69
+ //? files by their real path, where the walk went through the link.
70
+ forget(files: string[]): Map<string, Promise<BarrelExportMap | null>> {
71
+ const dirs = new Set(files.map((file) => realDirOf(file)));
72
+ const forgotten = new Map<string, Promise<BarrelExportMap | null>>();
73
+ for (const [pkgName, read] of this.#read) {
74
+ const analysis = this.#cache.get(pkgName);
75
+ if (!analysis || ![...read].some((file) => dirs.has(this.#realDirOf(file)))) continue;
76
+ forgotten.set(pkgName, analysis);
77
+ this.#cache.delete(pkgName);
78
+ this.#read.delete(pkgName);
79
+ }
80
+ return forgotten;
81
+ }
82
+
83
+ #realDirOf(file: string): string {
84
+ let dir = this.#realDirs.get(file);
85
+ if (dir === undefined) {
86
+ dir = realDirOf(file);
87
+ this.#realDirs.set(file, dir);
88
+ }
89
+ return dir;
90
+ }
91
+
59
92
  async #analyzeSafe(pkgName: string): Promise<BarrelExportMap | null> {
60
93
  try {
61
94
  const pkg = await this.#opts.resolvePackage(pkgName);
62
95
  if (!pkg) return null;
63
96
  const map: BarrelExportMap = new Map();
64
- await this.#walk(pkg.entryFile, pkg, map, new Set());
97
+ const read = new Set<string>();
98
+ this.#read.set(pkgName, read);
99
+ await this.#walk(pkg.entryFile, pkg, map, read);
65
100
  return map;
66
101
  } catch (err) {
67
102
  this.#logger.error(`analyze failed for ${pkgName}: ${(err as Error).message}`);
@@ -170,6 +205,16 @@ export class BarrelAnalyzer {
170
205
  }
171
206
  }
172
207
 
208
+ const realDirOf = (file: string): string => {
209
+ const dir = path.dirname(path.resolve(file));
210
+ try {
211
+ return fs.realpathSync.native(dir);
212
+ } catch {
213
+ // A directory removed with the file keeps its own path.
214
+ return dir;
215
+ }
216
+ };
217
+
173
218
  const isRelative = (spec: string): boolean => {
174
219
  return spec.startsWith("./") || spec.startsWith("../") || spec === "." || spec === "..";
175
220
  };
@@ -4,7 +4,7 @@ import ts from "typescript";
4
4
  import { StoreBootstrap } from "../artifact/storeBootstrap";
5
5
  import type { App } from "../commandDecorators";
6
6
  import { BarrelAnalyzer, type BarrelExportMap, type PackageEntry } from "./barrelAnalyzer";
7
- import { loaderFor, type NamedItem, parseNamedList } from "./moduleSyntax";
7
+ import { loaderFor, type NamedItem } from "./moduleSyntax";
8
8
 
9
9
  export interface BarrelImportsPluginOptions {
10
10
  /** Absolute paths whose content is returned unchanged (e.g. node_modules). */
@@ -234,10 +234,22 @@ const resolveFileCandidate = async (candidate: string): Promise<string | null> =
234
234
 
235
235
  const MACRO_ATTR_RE = /with\s*\{\s*type\s*:\s*["']macro["']\s*\}/;
236
236
 
237
+ export interface RewriteBarrelImportsOptions {
238
+ /** Each replacement takes one line plus the newlines the statement spanned, so later lines keep their numbers. */
239
+ keepLines?: boolean;
240
+ specifierOf?: (subpath: string) => string;
241
+ /** Called for each import of a listed barrel with the value names whose rewrite reads its export map. */
242
+ onImport?: (barrel: string, names: string[]) => void;
243
+ }
244
+
245
+ export const workspaceBarrels = (barrels: readonly string[]): string[] =>
246
+ barrels.filter((barrel) => /^@(?:apps|libs)\//.test(barrel));
247
+
237
248
  export const rewriteBarrelImports = async (
238
249
  source: string,
239
250
  barrels: string[],
240
251
  analyzer: BarrelAnalyzer,
252
+ { keepLines = false, specifierOf = (subpath: string) => subpath, onImport }: RewriteBarrelImportsOptions = {},
241
253
  ): Promise<string | null> => {
242
254
  // Sound pre-filter before the costly parse: a static import cannot name a specifier absent from the text.
243
255
  if (!barrels.some((barrel) => source.includes(barrel))) return null;
@@ -251,20 +263,32 @@ export const rewriteBarrelImports = async (
251
263
  const stmt = statements[i];
252
264
  if (!stmt) continue;
253
265
  if (!barrels.includes(stmt.specifier)) continue;
266
+ const names = valueNamesOf(stmt.clause);
267
+ if (names.length > 0) onImport?.(stmt.specifier, names);
254
268
  const map = await analyzer.analyze(stmt.specifier);
255
269
  if (!map || map.size === 0) continue;
256
- const replacement = rewriteSingleStatement(stmt, map);
257
- if (replacement === null) continue;
270
+ const lines = rewriteSingleStatement(stmt, map, specifierOf);
271
+ if (lines === null) continue;
272
+ const replacement = keepLines
273
+ ? `${lines.join(" ")}${"\n".repeat(source.slice(stmt.start, stmt.end).split("\n").length - 1)}`
274
+ : lines.join("\n");
258
275
  out = out.slice(0, stmt.start) + replacement + out.slice(stmt.end);
259
276
  changed = true;
260
277
  }
261
278
  return changed ? out : null;
262
279
  };
263
280
 
281
+ interface ParsedClause {
282
+ defaultImport?: string;
283
+ namespaceImport?: string;
284
+ named?: NamedItem[];
285
+ typeOnly: boolean;
286
+ }
287
+
264
288
  interface ImportStatement {
265
289
  start: number;
266
290
  end: number;
267
- clause: string;
291
+ clause: ParsedClause | null;
268
292
  specifier: string;
269
293
  }
270
294
 
@@ -286,52 +310,42 @@ const findImportStatements = (source: string): ImportStatement[] => {
286
310
  statements.push({
287
311
  start: statement.getStart(sourceFile),
288
312
  end: statement.end,
289
- clause: source.slice(importClause.getStart(sourceFile), importClause.end).trim(),
313
+ clause: clauseOf(importClause),
290
314
  specifier: statement.moduleSpecifier.text,
291
315
  });
292
316
  }
293
317
  return statements;
294
318
  };
295
319
 
296
- interface ParsedClause {
297
- defaultImport?: string;
298
- namespaceImport?: string;
299
- named?: NamedItem[];
300
- typeOnly: boolean;
301
- }
302
-
303
- const parseImportClause = (clause: string): ParsedClause | null => {
304
- let rest = clause.trim();
305
- let typeOnly = false;
306
- if (rest.startsWith("type ")) {
307
- typeOnly = true;
308
- rest = rest.slice(5).trim();
309
- }
310
- const parsed: ParsedClause = { typeOnly };
311
- const commaMatch = /^(\w+)\s*,\s*(.+)$/.exec(rest);
312
- if (commaMatch) {
313
- parsed.defaultImport = commaMatch[1];
314
- rest = commaMatch[2] ?? "";
315
- } else if (/^\w+$/.test(rest)) {
316
- parsed.defaultImport = rest;
317
- return parsed;
318
- }
319
- if (rest.startsWith("*")) {
320
- const ns = /^\*\s+as\s+(\w+)$/.exec(rest);
321
- if (!ns) return null;
322
- parsed.namespaceImport = ns[1];
323
- return parsed;
324
- }
325
- if (rest.startsWith("{")) {
326
- const close = rest.indexOf("}");
327
- if (close === -1) return null;
328
- parsed.named = parseNamedList(rest.slice(1, close));
320
+ const clauseOf = (importClause: ts.ImportClause): ParsedClause | null => {
321
+ const parsed: ParsedClause = { typeOnly: importClause.phaseModifier === ts.SyntaxKind.TypeKeyword };
322
+ if (importClause.name) parsed.defaultImport = importClause.name.text;
323
+ const bindings = importClause.namedBindings;
324
+ if (!bindings) return parsed;
325
+ if (ts.isNamespaceImport(bindings)) return { ...parsed, namespaceImport: bindings.name.text };
326
+ const named: NamedItem[] = [];
327
+ for (const element of bindings.elements) {
328
+ if (element.propertyName && !ts.isIdentifier(element.propertyName)) return null;
329
+ named.push({
330
+ imported: (element.propertyName ?? element.name).text,
331
+ local: element.name.text,
332
+ isType: element.isTypeOnly,
333
+ });
329
334
  }
330
- return parsed;
335
+ return { ...parsed, named };
331
336
  };
332
337
 
333
- const rewriteSingleStatement = (stmt: ImportStatement, map: BarrelExportMap): string | null => {
334
- const clause = parseImportClause(stmt.clause);
338
+ const valueNamesOf = (clause: ParsedClause | null): string[] => {
339
+ if (!clause || clause.namespaceImport || clause.typeOnly) return [];
340
+ return (clause.named ?? []).filter((item) => !item.isType).map((item) => item.imported);
341
+ };
342
+
343
+ const rewriteSingleStatement = (
344
+ stmt: ImportStatement,
345
+ map: BarrelExportMap,
346
+ specifierOf: (subpath: string) => string,
347
+ ): string[] | null => {
348
+ const { clause } = stmt;
335
349
  if (!clause) return null;
336
350
  // Namespace imports need the whole barrel — cannot rewrite safely.
337
351
  if (clause.namespaceImport) return null;
@@ -353,7 +367,7 @@ const rewriteSingleStatement = (stmt: ImportStatement, map: BarrelExportMap): st
353
367
  continue;
354
368
  }
355
369
  if (target.namespace) {
356
- namespaceLines.push(`import * as ${item.local} from "${target.subpath}";`);
370
+ namespaceLines.push(`import * as ${item.local} from ${JSON.stringify(specifierOf(target.subpath))};`);
357
371
  continue;
358
372
  }
359
373
  const list = rewrites.get(target.subpath) ?? [];
@@ -364,7 +378,9 @@ const rewriteSingleStatement = (stmt: ImportStatement, map: BarrelExportMap): st
364
378
  if (rewrites.size === 0 && namespaceLines.length === 0) return null;
365
379
 
366
380
  //? An app or lib client barrel's import-time side effects are its client runtime and store, not its module UI.
367
- const lines: string[] = [...(StoreBootstrap.ofClientBarrel(stmt.specifier, map) ?? [])];
381
+ const lines = (StoreBootstrap.ofClientBarrel(stmt.specifier, map) ?? []).map(
382
+ (specifier) => `import ${JSON.stringify(specifierOf(specifier))};`,
383
+ );
368
384
 
369
385
  if (clause.defaultImport || remaining.length > 0) {
370
386
  const parts: string[] = [];
@@ -374,11 +390,11 @@ const rewriteSingleStatement = (stmt: ImportStatement, map: BarrelExportMap): st
374
390
  }
375
391
 
376
392
  for (const [subpath, items] of rewrites) {
377
- lines.push(`import { ${items.map(serializeNamedItem).join(", ")} } from "${subpath}";`);
393
+ lines.push(`import { ${items.map(serializeNamedItem).join(", ")} } from ${JSON.stringify(specifierOf(subpath))};`);
378
394
  }
379
395
  lines.push(...namespaceLines);
380
396
 
381
- return lines.join("\n");
397
+ return lines;
382
398
  };
383
399
 
384
400
  const serializeNamedItem = (item: NamedItem): string => {
@@ -1,4 +1,5 @@
1
1
  import { describe, expect, test } from "bun:test";
2
+ import fs from "node:fs";
2
3
  import path from "node:path";
3
4
  import { ApplicationBuildReporter } from "../applicationBuildReporter";
4
5
  import { tempDirs, writeText as write } from "../testHelpers";
@@ -262,6 +263,27 @@ describe("BarrelAnalyzer and rewriteBarrelImports", () => {
262
263
  expect(await rewriteBarrelImports(source, ["@libs/util/common"], analyzer)).toBeNull();
263
264
  });
264
265
 
266
+ test("forgets an analysis when a file beside one it read changes, under the link or the real path", async () => {
267
+ const root = await makeTempRoot();
268
+ await write(path.join(root, "real/ui/index.ts"), 'export * from "./Foo";\n');
269
+ await write(path.join(root, "real/ui/Foo.ts"), "export const Foo = 1;\n");
270
+ await write(path.join(root, "real/other/Bar.ts"), "export const Bar = 1;\n");
271
+ fs.symlinkSync(path.join(root, "real"), path.join(root, "linked"));
272
+ const pkgDir = path.join(root, "linked/ui");
273
+ const analyzer = new BarrelAnalyzer({
274
+ resolvePackage: async () => ({ pkgName: "@scope/ui", entryFile: path.join(pkgDir, "index.ts"), pkgDir }),
275
+ });
276
+ const analyzed = async () => {
277
+ await analyzer.analyze("@scope/ui");
278
+ return analyzer;
279
+ };
280
+
281
+ expect([...(await analyzed()).forget([path.join(root, "real/other/Bar.ts")]).keys()]).toEqual([]);
282
+ expect([...(await analyzed()).forget([path.join(root, "real/ui/Foo.ts")]).keys()]).toEqual(["@scope/ui"]);
283
+ expect([...(await analyzed()).forget([path.join(pkgDir, "Created.ts")]).keys()]).toEqual(["@scope/ui"]);
284
+ expect([...analyzer.forget([path.join(pkgDir, "Foo.ts")]).keys()]).toEqual([]);
285
+ });
286
+
265
287
  test("rewrites flattenable named imports and preserves default, type, and unknown imports", async () => {
266
288
  const analyzer = fakeAnalyzer([
267
289
  ["A", { subpath: "@scope/pkg/leaf", originalName: "A" }],
@@ -287,6 +309,27 @@ describe("BarrelAnalyzer and rewriteBarrelImports", () => {
287
309
  expect(await rewriteBarrelImports('import { A } from "@other/pkg";', ["@scope/pkg"], analyzer)).toBeNull();
288
310
  });
289
311
 
312
+ test("reads every name of an import whose braces hold comments, aliases and type-only names", async () => {
313
+ const analyzer = fakeAnalyzer([
314
+ ["A", { subpath: "@scope/pkg/leaf", originalName: "A" }],
315
+ ["st", { subpath: "@scope/pkg/store", originalName: "st" }],
316
+ ]);
317
+ const rewrite = async (source: string) => await rewriteBarrelImports(source, ["@scope/pkg"], analyzer);
318
+ const narrowed = 'import { A } from "@scope/pkg/leaf";\nimport { st } from "@scope/pkg/store";\n';
319
+
320
+ expect(await rewrite('import {\n A, // the leaf\n st,\n} from "@scope/pkg";\n')).toBe(narrowed);
321
+ expect(await rewrite('import { A, /* the store */ st } from "@scope/pkg";\n')).toBe(narrowed);
322
+ expect(await rewrite('import { type Shape, A as Leaf, st as store } from "@scope/pkg";\n')).toBe(
323
+ [
324
+ 'import { type Shape } from "@scope/pkg";',
325
+ 'import { A as Leaf } from "@scope/pkg/leaf";',
326
+ 'import { st as store } from "@scope/pkg/store";',
327
+ "",
328
+ ].join("\n"),
329
+ );
330
+ expect(await rewrite('import { "A" as quoted } from "@scope/pkg";\n')).toBeNull();
331
+ });
332
+
290
333
  test("keeps a client barrel's runtime and store, never its module UI, when flattening an app or lib import", async () => {
291
334
  const root = await makeTempRoot();
292
335
  for (const sys of ["apps/demo", "libs/kit"]) {
@@ -308,13 +351,15 @@ describe("BarrelAnalyzer and rewriteBarrelImports", () => {
308
351
  }
309
352
  await write(path.join(root, "libs/hand/client.ts"), 'export { thing } from "./lib/thing";\n');
310
353
  await write(path.join(root, "libs/hand/lib/thing.ts"), "export const thing = 1;\n");
354
+ await write(path.join(root, "libs/half/client.ts"), 'export { st } from "./lib/st";\n');
355
+ await write(path.join(root, "libs/half/lib/st.ts"), "export const st = {};\n");
311
356
  const analyzer = new BarrelAnalyzer({
312
357
  resolvePackage: await createTsconfigPackageResolver({
313
358
  workspace: { workspaceRoot: root },
314
359
  getTsConfig: async () => ({ compilerOptions: { paths: { "@apps/*": ["./apps/*"], "@libs/*": ["./libs/*"] } } }),
315
360
  } as never),
316
361
  });
317
- const barrels = ["@apps/demo/client", "@libs/kit/client", "@libs/hand/client"];
362
+ const barrels = ["@apps/demo/client", "@libs/kit/client", "@libs/hand/client", "@libs/half/client"];
318
363
  const rewrite = async (source: string) => await rewriteBarrelImports(source, barrels, analyzer);
319
364
 
320
365
  expect(await rewrite('import { Memo, st } from "@apps/demo/client";\n')).toBe(
@@ -348,6 +393,27 @@ describe("BarrelAnalyzer and rewriteBarrelImports", () => {
348
393
  expect(await rewrite('import { thing } from "@libs/hand/client";\n')).toBe(
349
394
  ['import "@libs/hand/client";', 'import { thing } from "@libs/hand/lib/thing";', ""].join("\n"),
350
395
  );
396
+ expect(await rewrite('import { st } from "@libs/half/client";\n')).toBe(
397
+ ['import "@libs/half/client";', 'import { st } from "@libs/half/lib/st";', ""].join("\n"),
398
+ );
399
+ });
400
+
401
+ test("keeps each statement's line count and writes the specifier specifierOf gives a target", async () => {
402
+ const analyzer = fakeAnalyzer([
403
+ ["A", { subpath: "@scope/pkg/leaf", originalName: "A" }],
404
+ ["ns", { subpath: "@scope/pkg/namespace", originalName: "ns", namespace: true }],
405
+ ]);
406
+ const source = 'import {\n A,\n ns,\n} from "@scope/pkg";\nconsole.log(A, ns);\n';
407
+
408
+ const rewritten = await rewriteBarrelImports(source, ["@scope/pkg"], analyzer, {
409
+ keepLines: true,
410
+ specifierOf: (subpath) => `./${subpath.slice("@scope/pkg/".length)}.ts`,
411
+ });
412
+
413
+ expect(rewritten).toBe(
414
+ 'import { A } from "./leaf.ts"; import * as ns from "./namespace.ts";\n\n\n\nconsole.log(A, ns);\n',
415
+ );
416
+ expect(rewritten?.split("\n")).toHaveLength(source.split("\n").length);
351
417
  });
352
418
 
353
419
  test("does not rewrite import-looking code inside template literals", async () => {
@@ -16,6 +16,7 @@ import type {
16
16
  WorkflowNextActionCode,
17
17
  WorkflowOverallStatus,
18
18
  WorkflowPlan,
19
+ WorkflowRecommendation,
19
20
  WorkflowRunArtifact,
20
21
  WorkflowRunSource,
21
22
  WorkflowValidationCommandResult,
@@ -133,6 +134,11 @@ export const createWorkflowApplyReport = ({
133
134
  };
134
135
  };
135
136
 
137
+ export const withoutApplyHints = (recommendations: readonly WorkflowRecommendation[]) =>
138
+ recommendations.filter(
139
+ (recommendation) => recommendation.kind !== "auto-apply" && recommendation.kind !== "validation",
140
+ );
141
+
136
142
  export const resolveWorkflowCommand = (command: string, plan: WorkflowPlan) => {
137
143
  const target = typeof plan.inputs.app === "string" ? plan.inputs.app : "<app-or-lib>";
138
144
  return command
@@ -526,6 +532,17 @@ export const createWorkflowValidationRunReport = async ({
526
532
  };
527
533
 
528
534
  export const createDryRunWorkflowApplyReport = (plan: WorkflowPlan) => {
535
+ if (workflowStatus(plan.diagnostics) === "failed")
536
+ return createWorkflowApplyReport({
537
+ workflow: plan.workflow,
538
+ mode: "dry-run",
539
+ changedFiles: [],
540
+ generatedFiles: [],
541
+ diagnostics: [...plan.diagnostics],
542
+ recommendations: withoutApplyHints(plan.recommendations),
543
+ nextActions: [],
544
+ plan,
545
+ });
529
546
  const changedFiles: PrimitiveChangedFile[] = plan.predictedChanges.flatMap((change) => {
530
547
  if (change.action !== "create" && change.action !== "modify") return [];
531
548
  return [
package/workflow/index.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./keyRename";
6
6
  export * from "./moduleIndex";
7
7
  export * from "./mutationScaffold";
8
8
  export * from "./mutationSource";
9
+ export * from "./plainName";
9
10
  export * from "./plan";
10
11
  export * from "./render";
11
12
  export * from "./rolloutGate";
@@ -0,0 +1,15 @@
1
+ export const sysNamePattern = /^[a-z0-9][a-zA-Z0-9_-]*$/;
2
+ export const identifierPattern = /^_{0,2}[a-zA-Z][a-zA-Z0-9_-]*$/;
3
+
4
+ export const isWorkflowSysName = (value: unknown): value is string =>
5
+ typeof value === "string" && sysNamePattern.test(value);
6
+ export const isWorkflowModuleName = (value: unknown): value is string =>
7
+ typeof value === "string" && identifierPattern.test(value);
8
+
9
+ export const assertPlainName = (kind: "app" | "lib" | "module" | "service" | "scalar", name: string) => {
10
+ const isSys = kind === "app" || kind === "lib";
11
+ if (isSys ? isWorkflowSysName(name) : isWorkflowModuleName(name)) return;
12
+ throw new Error(
13
+ `${JSON.stringify(name)} is no ${kind} name; use letters, digits, - and _, starting with ${isSys ? "a lowercase letter or digit" : "a letter"}`,
14
+ );
15
+ };