@diagonal-hq/cli 0.42.16 → 0.42.18

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.
@@ -3,7 +3,7 @@ const require = __diagonalCreateRequire(import.meta.url);
3
3
  import {
4
4
  WORKFLOW_STAGE,
5
5
  stripTypes
6
- } from "./cli-chunk-MX5DAHDR.mjs";
6
+ } from "./cli-chunk-RNGY5MCU.mjs";
7
7
  import {
8
8
  scriptSdkRuntimeFiles
9
9
  } from "./cli-chunk-6VD2BES2.mjs";
package/dist/cli.mjs CHANGED
@@ -58,7 +58,7 @@ import {
58
58
  workflowConfigObject,
59
59
  workflowMetadata,
60
60
  workspaceBuildNeeds
61
- } from "./cli-chunk-MX5DAHDR.mjs";
61
+ } from "./cli-chunk-RNGY5MCU.mjs";
62
62
  import {
63
63
  GENERATED_NOTIFICATION_TYPES_MODULE,
64
64
  GENERATED_WORKFLOWS_MODULE,
@@ -7034,19 +7034,27 @@ function readLayoutOption(ast, source, path, property2, branchPaths) {
7034
7034
  return null;
7035
7035
  }
7036
7036
  const paths = new Set(branchPaths ?? []);
7037
+ const statement = value.type === "Identifier" ? layoutImportOf(ast, value.name) : null;
7037
7038
  let target = null;
7038
7039
  if (value.type === "StringLiteral") {
7039
7040
  const named = `layouts/${value.value}.tsx`;
7040
7041
  target = paths.has(named) ? named : null;
7041
- } else if (value.type === "Identifier") {
7042
- const statement = ast.program.body.find(
7043
- (item) => item.type === "ImportDeclaration" && item.specifiers.some(
7044
- (specifier) => specifier.type === "ImportDefaultSpecifier" && specifier.local.name === value.name
7045
- )
7046
- );
7047
- target = statement === void 0 ? null : resolveLocalSpecifierIn(paths, path, statement.source.value);
7042
+ } else if (statement !== null) {
7043
+ target = resolveLocalSpecifierIn(paths, path, statement.source.value);
7048
7044
  }
7049
- return { read_only: true, display: sliceOf(source, value), path: target };
7045
+ return {
7046
+ read_only: true,
7047
+ display: sliceOf(source, value),
7048
+ path: target,
7049
+ writable: statement !== null
7050
+ };
7051
+ }
7052
+ function layoutImportOf(ast, local) {
7053
+ return ast.program.body.find(
7054
+ (statement) => statement.type === "ImportDeclaration" && statement.importKind !== "type" && importKind(statement.source.value) === "app" && statement.specifiers.some(
7055
+ (specifier) => specifier.type === "ImportDefaultSpecifier" && specifier.local.name === local
7056
+ )
7057
+ ) ?? null;
7050
7058
  }
7051
7059
  function readPageFile(path, source, resources, branchPaths = null, permissions = []) {
7052
7060
  return readPage(path, source, branchResourceNames(resources), branchPaths, declaredPermissionEntries(permissions));
@@ -7206,12 +7214,8 @@ function writePagePaused(path, source, paused, resources, { branchPaths = null,
7206
7214
  }
7207
7215
  function writeParts(path, options, parts, { previous, readPath: readPath2, names, permissionEntries, keep, branchPaths, sourceOptions, addsCodeImports }) {
7208
7216
  options = migrateOptionValues(options);
7209
- if (options.layout !== void 0 && !keep.includes("layout")) {
7210
- throw new SourceError(
7211
- "layout: The page builder cannot set a layout. Keep it from the file with keep: ['layout']",
7212
- null
7213
- );
7214
- }
7217
+ const keepsLayout = keep.includes("layout") || Object.hasOwn(sourceOptions, "layout");
7218
+ const layout = keepsLayout ? void 0 : layoutChoice(options.layout, branchPaths);
7215
7219
  if (keep.length > 0) {
7216
7220
  const read = readPage(readPath2, previous, names, null, permissionEntries);
7217
7221
  if (!read.ok) {
@@ -7233,6 +7237,11 @@ function writeParts(path, options, parts, { previous, readPath: readPath2, names
7233
7237
  const written = parts();
7234
7238
  const { helpers, component, trailing } = written;
7235
7239
  let imports = written.imports;
7240
+ if (!keepsLayout) {
7241
+ const chosen = writtenLayout(path, imports, [helpers, `(${component});`, trailing].join("\n"), layout, previousLayoutOf(previous), branchPaths);
7242
+ imports = chosen.imports;
7243
+ options = chosen.value === null ? omit(options, "layout") : { ...options, layout: chosen.value };
7244
+ }
7236
7245
  const binding = resourcesBinding(parseSource(imports, { jsx: false }), names);
7237
7246
  const local = binding?.local ?? freeName("Resources", parseSource([imports, helpers, `(${component});`].join("\n"), { jsx: true }));
7238
7247
  const references2 = withResourceReferences(options, names.namesBySlug, local);
@@ -7339,6 +7348,136 @@ function filePartsOf(source) {
7339
7348
  trailing: source.slice(exportStatement.end).trim()
7340
7349
  };
7341
7350
  }
7351
+ var LAYOUT_CHOICE_MESSAGE = "layout: The page builder sets a layout as { path: 'layouts/<name>.tsx' }, or null for Blank";
7352
+ function layoutChoice(value, branchPaths) {
7353
+ if (value === void 0 || value === null) {
7354
+ return null;
7355
+ }
7356
+ if (!isPlainObject(value) || Object.keys(value).length !== 1 || typeof value.path !== "string") {
7357
+ throw new SourceError(LAYOUT_CHOICE_MESSAGE, null);
7358
+ }
7359
+ if (!isLayoutFile(value.path) || branchPaths !== null && !branchPaths.includes(value.path)) {
7360
+ throw new SourceError(`layout: ${value.path} is not a layout file of the workspace`, null);
7361
+ }
7362
+ return { path: value.path };
7363
+ }
7364
+ function isLayoutFile(path) {
7365
+ return path.startsWith("layouts/") && SOURCE_EXTENSIONS.some((extension) => path.endsWith(extension)) && !/\.(?:test|spec|d)\.[a-z]+$/.test(path);
7366
+ }
7367
+ function importsLayoutFile(path, statement, branchPaths) {
7368
+ const paths = branchPaths === null ? { has: isLayoutFile } : new Set(branchPaths);
7369
+ const target = resolveLocalSpecifierIn(paths, path, statement.source.value);
7370
+ return target !== null && isLayoutFile(target);
7371
+ }
7372
+ function previousLayoutOf(previous) {
7373
+ if (previous === null) {
7374
+ return null;
7375
+ }
7376
+ const ast = parseSource(previous, { jsx: true });
7377
+ const exportStatement = ast.program.body.find(
7378
+ (statement) => statement.type === "ExportDefaultDeclaration"
7379
+ );
7380
+ if (exportStatement === void 0) {
7381
+ return null;
7382
+ }
7383
+ let metaNode;
7384
+ try {
7385
+ [metaNode] = expectDefineCall(exportStatement.declaration, "definePage", 2);
7386
+ } catch (error) {
7387
+ if (error instanceof SourceError) {
7388
+ return null;
7389
+ }
7390
+ throw error;
7391
+ }
7392
+ const property2 = layoutProperty(metaNode);
7393
+ if (property2 === void 0 || property2.value.type === "NullLiteral") {
7394
+ return null;
7395
+ }
7396
+ const value = property2.value;
7397
+ const local = value.type === "Identifier" && layoutImportOf(ast, value.name) !== null ? value.name : null;
7398
+ return { display: sliceOf(previous, value), local };
7399
+ }
7400
+ function writtenLayout(path, imports, code, layout, previousLayout, branchPaths) {
7401
+ let result = imports;
7402
+ const importsAst = parseSource(imports, { jsx: true });
7403
+ const previousStatement = previousLayout === null || previousLayout.local === null ? null : layoutImportOf(importsAst, previousLayout.local);
7404
+ let removed = null;
7405
+ if (previousLayout !== null && (previousStatement === null || !importsLayoutFile(path, previousStatement, branchPaths))) {
7406
+ if (layout === null) {
7407
+ return { imports, value: null };
7408
+ }
7409
+ throw new SourceError(
7410
+ `layout: The page file sets its layout with ${previousLayout.display}, which the page builder cannot change. Change it in Files`,
7411
+ null
7412
+ );
7413
+ }
7414
+ if (previousStatement !== null) {
7415
+ if (layout !== null && namesFile(path, previousStatement, layout.path, branchPaths)) {
7416
+ return { imports, value: new SourceExpression2(previousLayout.local) };
7417
+ }
7418
+ const withoutImport = withoutDefaultImport(imports, previousStatement);
7419
+ if (!usesName(parseSource([withoutImport, code].join("\n"), { jsx: true }), previousLayout.local)) {
7420
+ result = withoutImport;
7421
+ removed = previousStatement;
7422
+ }
7423
+ }
7424
+ if (layout === null) {
7425
+ return { imports: result, value: null };
7426
+ }
7427
+ const existing = parseSource(result, { jsx: true }).program.body.filter(
7428
+ (statement2) => statement2.type === "ImportDeclaration" && namesFile(path, statement2, layout.path, branchPaths)
7429
+ ).flatMap((statement2) => statement2.specifiers).find((specifier) => specifier.type === "ImportDefaultSpecifier");
7430
+ if (existing !== void 0) {
7431
+ return { imports: result, value: new SourceExpression2(existing.local.name) };
7432
+ }
7433
+ const local = freeName(layoutLocalName(layout.path), parseSource([result, code].join("\n"), { jsx: true }));
7434
+ const statement = `import ${local} from ${quotedLiteral(layoutSpecifier(path, layout.path, branchPaths))};`;
7435
+ const value = new SourceExpression2(local);
7436
+ if (removed !== null && removed.specifiers.length === 1) {
7437
+ return { imports: `${imports.slice(0, removed.start)}${statement}${imports.slice(removed.end)}`, value };
7438
+ }
7439
+ return { imports: result === "" ? statement : `${result}
7440
+ ${statement}`, value };
7441
+ }
7442
+ function namesFile(path, statement, target, branchPaths) {
7443
+ return statement.importKind !== "type" && importKind(statement.source.value) === "app" && resolveLocalSpecifierIn(new Set(branchPaths ?? [target]), path, statement.source.value) === target;
7444
+ }
7445
+ function layoutSpecifier(path, layoutPath, branchPaths) {
7446
+ const specifier = relativeSpecifier(path, layoutPath);
7447
+ if (branchPaths === null || resolveLocalSpecifierIn(new Set(branchPaths), path, specifier) === layoutPath) {
7448
+ return specifier;
7449
+ }
7450
+ const relativePath = posix3.relative(posix3.dirname(path), layoutPath);
7451
+ return relativePath.startsWith("../") ? relativePath : `./${relativePath}`;
7452
+ }
7453
+ function usesName(ast, name) {
7454
+ return allNodes(ast).some(
7455
+ (node) => (node.type === "Identifier" || node.type === "JSXIdentifier") && node.name === name
7456
+ );
7457
+ }
7458
+ function withoutDefaultImport(imports, statement) {
7459
+ const specifier = statement.specifiers.find((item) => item.type === "ImportDefaultSpecifier");
7460
+ if (statement.specifiers.length === 1) {
7461
+ const leading = /\n*$/.exec(imports.slice(0, statement.start))[0];
7462
+ const trailing = /^\n*/.exec(imports.slice(statement.end))[0];
7463
+ const before = imports.slice(0, statement.start - leading.length);
7464
+ const after = imports.slice(statement.end + trailing.length);
7465
+ const separator = before === "" || after === "" ? "" : leading.length >= trailing.length ? leading : trailing;
7466
+ return `${before}${separator}${after}`;
7467
+ }
7468
+ let rest = imports.indexOf(",", specifier.end) + 1;
7469
+ while (imports[rest] === " ") {
7470
+ rest += 1;
7471
+ }
7472
+ return `${imports.slice(0, specifier.start)}${imports.slice(rest)}`;
7473
+ }
7474
+ function layoutLocalName(layoutPath) {
7475
+ const segments = layoutPath.replace(/\.[jt]sx?$/, "").split("/");
7476
+ const base = segments.at(-1) === "index" && segments.length > 2 ? segments.at(-2) : segments.at(-1);
7477
+ const name = studlyName(base);
7478
+ const local = name.endsWith("Layout") ? name : `${name}Layout`;
7479
+ return /^[A-Za-z]/.test(local) ? local : `Layout${local}`;
7480
+ }
7342
7481
  function withKeptOptions(options, previous, keep) {
7343
7482
  const result = { ...options };
7344
7483
  const seo = isPlainObject(result.seo) ? { ...result.seo } : {};
@@ -11650,7 +11789,7 @@ function holdsSkills(root, plan) {
11650
11789
  async function checkCheckout(root, options) {
11651
11790
  const sources = await checkSources();
11652
11791
  const plan = composePackages(root);
11653
- const testRunner = async () => (await import("./cli-workflows-6RHPPBQL.mjs")).runBuildTests;
11792
+ const testRunner = async () => (await import("./cli-workflows-5IMXLJMI.mjs")).runBuildTests;
11654
11793
  const skillReader = async () => (await import("./cli-skill-file-PHFXSLAU.mjs")).skillFrontMatter;
11655
11794
  const finish = (checkoutRoot, buildPlan, answer, runTests2) => {
11656
11795
  const run = hasScriptTests(answer) ? runTests2(checkoutRoot, answer.tests) : null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diagonal-hq/cli",
3
- "version": "0.42.16",
3
+ "version": "0.42.18",
4
4
  "description": "The diagonal CLI and the configuration helpers for file-based Diagonal workspaces.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {