@diagonal-hq/cli 0.38.3 → 0.39.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.
@@ -4,6 +4,68 @@ import {
4
4
  stringFacts
5
5
  } from "./cli-chunk-WPC3DBHX.mjs";
6
6
 
7
+ // scripts/lib/npm-package.mjs
8
+ function tarballName(name, version) {
9
+ return `${name.replace(/^@/, "").replace("/", "-")}-${version}.tgz`;
10
+ }
11
+ var VERSION_PATTERN = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/;
12
+ function isVersion(version) {
13
+ return VERSION_PATTERN.test(version);
14
+ }
15
+ function parseVersion(version) {
16
+ const match = VERSION_PATTERN.exec(version);
17
+ if (match === null) {
18
+ throw new Error(`${version} is not a version of the form major.minor.patch[-prerelease]`);
19
+ }
20
+ return {
21
+ major: Number(match[1]),
22
+ minor: Number(match[2]),
23
+ patch: Number(match[3]),
24
+ prerelease: match[4] ?? null
25
+ };
26
+ }
27
+ function comparePrereleases(left, right) {
28
+ const a = left.split(".");
29
+ const b = right.split(".");
30
+ for (let index = 0; index < Math.min(a.length, b.length); index++) {
31
+ const aNumeric = /^\d+$/.test(a[index]);
32
+ const bNumeric = /^\d+$/.test(b[index]);
33
+ if (aNumeric && bNumeric) {
34
+ const order = Math.sign(Number(a[index]) - Number(b[index]));
35
+ if (order !== 0) {
36
+ return order;
37
+ }
38
+ continue;
39
+ }
40
+ if (aNumeric !== bNumeric) {
41
+ return aNumeric ? -1 : 1;
42
+ }
43
+ if (a[index] !== b[index]) {
44
+ return a[index] < b[index] ? -1 : 1;
45
+ }
46
+ }
47
+ return Math.sign(a.length - b.length);
48
+ }
49
+ function compareVersions(left, right) {
50
+ const a = parseVersion(left);
51
+ const b = parseVersion(right);
52
+ for (const part of ["major", "minor", "patch"]) {
53
+ if (a[part] !== b[part]) {
54
+ return a[part] < b[part] ? -1 : 1;
55
+ }
56
+ }
57
+ if (a.prerelease === b.prerelease) {
58
+ return 0;
59
+ }
60
+ if (a.prerelease === null) {
61
+ return 1;
62
+ }
63
+ if (b.prerelease === null) {
64
+ return -1;
65
+ }
66
+ return comparePrereleases(a.prerelease, b.prerelease);
67
+ }
68
+
7
69
  // scripts/lib/workflow-script/compile.mjs
8
70
  import { spawn, spawnSync } from "node:child_process";
9
71
  import { createRequire as createRequire2 } from "node:module";
@@ -205,6 +267,9 @@ function runCompilerAsync(input, args) {
205
267
  }
206
268
 
207
269
  export {
270
+ tarballName,
271
+ isVersion,
272
+ compareVersions,
208
273
  stripTypes,
209
274
  runWorkspaceBuild,
210
275
  runWorkspaceBuildAsync,
@@ -2,7 +2,7 @@ import { createRequire as __diagonalCreateRequire } from 'node:module';
2
2
  const require = __diagonalCreateRequire(import.meta.url);
3
3
  import {
4
4
  stripTypes
5
- } from "./cli-chunk-IJTP2OMH.mjs";
5
+ } from "./cli-chunk-U2ET6LQJ.mjs";
6
6
  import {
7
7
  AUTHORING_HELPERS_MODULE,
8
8
  GENERATED_NOTIFICATION_TYPES_PATH,
package/dist/cli.mjs CHANGED
@@ -1,10 +1,13 @@
1
1
  import { createRequire as __diagonalCreateRequire } from 'node:module';
2
2
  const require = __diagonalCreateRequire(import.meta.url);
3
3
  import {
4
+ compareVersions,
5
+ isVersion,
4
6
  runWorkspaceBuild,
5
7
  runWorkspaceBuildAsync,
8
+ tarballName,
6
9
  workspaceBuildNeeds
7
- } from "./cli-chunk-IJTP2OMH.mjs";
10
+ } from "./cli-chunk-U2ET6LQJ.mjs";
8
11
  import "./cli-chunk-WPC3DBHX.mjs";
9
12
  import {
10
13
  ASSETS_LIST_USAGE,
@@ -30,8 +33,8 @@ import {
30
33
  import "./cli-chunk-LRS5IEI3.mjs";
31
34
 
32
35
  // scripts/lib/runtime-build/check-cli.mjs
33
- import { existsSync as existsSync3, readdirSync as readdirSync3, statSync as statSync3 } from "node:fs";
34
- import { join as join5, resolve } from "node:path";
36
+ import { existsSync as existsSync4, readdirSync as readdirSync4, statSync as statSync4 } from "node:fs";
37
+ import { join as join6, resolve as resolve2 } from "node:path";
35
38
  import { fileURLToPath } from "node:url";
36
39
 
37
40
  // scripts/lib/runtime-build/installed-packages.mjs
@@ -352,16 +355,158 @@ export default defineWorkflow(
352
355
  return 0;
353
356
  }
354
357
 
358
+ // scripts/lib/runtime-build/update-cli.mjs
359
+ import { execFileSync, spawnSync } from "node:child_process";
360
+ import { existsSync as existsSync2, readdirSync as readdirSync3, readFileSync as readFileSync2, rmSync as rmSync2, statSync as statSync3, writeFileSync as writeFileSync3 } from "node:fs";
361
+ import { basename, dirname as dirname2, isAbsolute, join as join4, relative as relative3, resolve } from "node:path";
362
+ var UPDATE_USAGE = "Usage: diagonal update";
363
+ var SCOPE = "@diagonal-hq/";
364
+ var COMPILER_SCOPE = "@diagonal-hq/workflow-script-compiler-";
365
+ var SECTIONS = ["dependencies", "devDependencies"];
366
+ var FILE = "file:";
367
+ function newestTarball(name, directory) {
368
+ const prefix = tarballName(name, "").slice(0, -".tgz".length);
369
+ let newest = null;
370
+ for (const file of readdirSync3(directory)) {
371
+ if (!file.startsWith(prefix) || !file.endsWith(".tgz")) {
372
+ continue;
373
+ }
374
+ const version = file.slice(prefix.length, -".tgz".length);
375
+ if (isVersion(version) && (newest === null || compareVersions(version, newest) > 0)) {
376
+ newest = version;
377
+ }
378
+ }
379
+ return newest === null ? null : join4(directory, tarballName(name, newest));
380
+ }
381
+ function fileSpecifier(cwd, path, absolute) {
382
+ return `${FILE}${absolute ? path : relative3(cwd, path)}`;
383
+ }
384
+ function replaceCompilerOverrides(manifest, compilerOverrides) {
385
+ const overrides = { ...manifest.overrides };
386
+ for (const name of Object.keys(overrides)) {
387
+ if (name.startsWith(COMPILER_SCOPE)) {
388
+ delete overrides[name];
389
+ }
390
+ }
391
+ Object.assign(overrides, compilerOverrides);
392
+ if (Object.keys(overrides).length > 0) {
393
+ manifest.overrides = overrides;
394
+ } else {
395
+ delete manifest.overrides;
396
+ }
397
+ }
398
+ function packedCliCompilerOverrides(cliTarball, cwd, absolute) {
399
+ const cli = JSON.parse(
400
+ execFileSync("tar", ["-xOzf", cliTarball, "package/package.json"], { encoding: "utf8" })
401
+ );
402
+ const overrides = {};
403
+ for (const [name, version] of Object.entries(cli.optionalDependencies ?? {})) {
404
+ const tarball = join4(dirname2(cliTarball), tarballName(name, version));
405
+ if (name.startsWith(COMPILER_SCOPE) && existsSync2(tarball)) {
406
+ overrides[name] = fileSpecifier(cwd, tarball, absolute);
407
+ }
408
+ }
409
+ return overrides;
410
+ }
411
+ function forgetInstalledPackages(cwd) {
412
+ const lockPath = join4(cwd, "package-lock.json");
413
+ if (existsSync2(lockPath)) {
414
+ const lock = JSON.parse(readFileSync2(lockPath, "utf8"));
415
+ for (const path of Object.keys(lock.packages ?? {})) {
416
+ if (path.startsWith(`node_modules/${SCOPE}`)) {
417
+ delete lock.packages[path];
418
+ }
419
+ }
420
+ writeFileSync3(lockPath, `${JSON.stringify(lock, null, 2)}
421
+ `);
422
+ }
423
+ rmSync2(join4(cwd, "node_modules", SCOPE), { recursive: true, force: true });
424
+ }
425
+ function updatePackages(args, { cwd, env, stdout, stderr }) {
426
+ if (args.length > 0) {
427
+ stderr.write(`${UPDATE_USAGE}
428
+ `);
429
+ return 2;
430
+ }
431
+ const manifestPath = join4(cwd, "package.json");
432
+ if (!statSync3(manifestPath, { throwIfNoEntry: false })?.isFile()) {
433
+ stderr.write("Run this command in a workspace directory with package.json.\n");
434
+ return 2;
435
+ }
436
+ const manifest = JSON.parse(readFileSync2(manifestPath, "utf8"));
437
+ const latest = [];
438
+ let packed = false;
439
+ for (const section of SECTIONS) {
440
+ for (const [name, specifier] of Object.entries(manifest[section] ?? {})) {
441
+ if (!name.startsWith(SCOPE) || name.startsWith(COMPILER_SCOPE)) {
442
+ continue;
443
+ }
444
+ if (!specifier.startsWith(FILE)) {
445
+ latest.push(`${name}@latest`);
446
+ continue;
447
+ }
448
+ packed = true;
449
+ const path = specifier.slice(FILE.length);
450
+ if (!path.endsWith(".tgz")) {
451
+ stdout.write(`${name}: reinstalling ${path}
452
+ `);
453
+ continue;
454
+ }
455
+ const current = resolve(cwd, path);
456
+ const directory = dirname2(current);
457
+ const newest = statSync3(directory, { throwIfNoEntry: false })?.isDirectory() ? newestTarball(name, directory) : null;
458
+ if (newest === null) {
459
+ stderr.write(`No ${tarballName(name, "<version>")} exists in ${directory}.
460
+ `);
461
+ return 1;
462
+ }
463
+ manifest[section][name] = fileSpecifier(cwd, newest, isAbsolute(path));
464
+ stdout.write(`${name}: ${basename(current)} -> ${basename(newest)}
465
+ `);
466
+ if (name === `${SCOPE}cli`) {
467
+ replaceCompilerOverrides(
468
+ manifest,
469
+ packedCliCompilerOverrides(newest, cwd, isAbsolute(path))
470
+ );
471
+ }
472
+ }
473
+ }
474
+ if (!packed && latest.length === 0) {
475
+ stderr.write("package.json has no @diagonal-hq packages.\n");
476
+ return 1;
477
+ }
478
+ if (packed) {
479
+ writeFileSync3(manifestPath, `${JSON.stringify(manifest, null, 2)}
480
+ `);
481
+ forgetInstalledPackages(cwd);
482
+ }
483
+ for (const spec of latest) {
484
+ stdout.write(`${spec.slice(0, -"@latest".length)}: installing the latest version from npm
485
+ `);
486
+ }
487
+ const install = spawnSync("npm", ["install", "--no-audit", "--no-fund", ...latest], {
488
+ cwd,
489
+ env,
490
+ stdio: "inherit"
491
+ });
492
+ if (install.status !== 0) {
493
+ stderr.write(`npm install failed${install.error ? `: ${install.error.message}` : ""}.
494
+ `);
495
+ return 1;
496
+ }
497
+ return 0;
498
+ }
499
+
355
500
  // scripts/lib/runtime-build/sdk-packages.mjs
356
- import { existsSync as existsSync2, readFileSync as readFileSync2 } from "node:fs";
357
- import { join as join4 } from "node:path";
501
+ import { existsSync as existsSync3, readFileSync as readFileSync3 } from "node:fs";
502
+ import { join as join5 } from "node:path";
358
503
  function readPackageFiles(root, diagnostics) {
359
504
  const read = (path) => {
360
- if (!existsSync2(join4(root, path))) {
505
+ if (!existsSync3(join5(root, path))) {
361
506
  return null;
362
507
  }
363
508
  try {
364
- return JSON.parse(readFileSync2(join4(root, path), "utf8"));
509
+ return JSON.parse(readFileSync3(join5(root, path), "utf8"));
365
510
  } catch (error) {
366
511
  if (!(error instanceof SyntaxError)) {
367
512
  throw error;
@@ -433,6 +578,7 @@ var CHECK_FLAGS = /* @__PURE__ */ new Set(["--json", "--fix"]);
433
578
  var USAGE = [
434
579
  CHECK_USAGE,
435
580
  MIGRATION_USAGE,
581
+ UPDATE_USAGE,
436
582
  WORKFLOWS_USAGE,
437
583
  RUN_USAGE,
438
584
  LOGS_USAGE,
@@ -464,28 +610,30 @@ function parseCheckArguments(args) {
464
610
  };
465
611
  }
466
612
  async function checkSources() {
467
- if (existsSync3(DATA_FILE) && existsSync3(SDK_FILE)) {
613
+ if (existsSync4(DATA_FILE) && existsSync4(SDK_FILE)) {
468
614
  return { dataFile: DATA_FILE, sdkFile: SDK_FILE };
469
615
  }
470
- const { checkData, scriptSdk } = await import("./cli-check-data-GHZW7MSI.mjs");
616
+ const { checkData, scriptSdk } = await import("./cli-check-data-IITXQPIR.mjs");
471
617
  return { data: checkData(), sdk: scriptSdk() };
472
618
  }
473
619
  function isScriptTestPath(path) {
474
620
  return /^(workflows|migrations)\/[^/]+\.test\.ts$/.test(withoutDevInfix(path));
475
621
  }
476
622
  function holdsScriptTests(root, plan) {
477
- const listed = (directory) => existsSync3(join5(root, directory)) ? readdirSync3(join5(root, directory)).map((name) => `${directory}/${name}`) : [];
478
- return [...listed("workflows"), ...listed("migrations"), ...plan.files.keys()].some(isScriptTestPath);
623
+ const listed = (directory) => existsSync4(join6(root, directory)) ? readdirSync4(join6(root, directory)).map((name) => `${directory}/${name}`) : [];
624
+ return [...listed("workflows"), ...listed("migrations"), ...plan.files.keys()].some(
625
+ isScriptTestPath
626
+ );
479
627
  }
480
628
  function holdsSkills(root, plan) {
481
629
  const isSkill = (path) => /^skills\/[^/]+\.md$/.test(withoutDevInfix(path));
482
- const listed = existsSync3(join5(root, "skills")) ? readdirSync3(join5(root, "skills")).map((name) => `skills/${name}`) : [];
630
+ const listed = existsSync4(join6(root, "skills")) ? readdirSync4(join6(root, "skills")).map((name) => `skills/${name}`) : [];
483
631
  return [...listed, ...plan.files.keys()].some(isSkill);
484
632
  }
485
633
  async function checkCheckout(root, options) {
486
634
  const sources = await checkSources();
487
635
  const plan = composePackages(root);
488
- const testRunner = async () => (await import("./cli-workflows-E2QBBI3W.mjs")).runBuildTests;
636
+ const testRunner = async () => (await import("./cli-workflows-CK5ZVNYR.mjs")).runBuildTests;
489
637
  const skillReader = async () => (await import("./cli-skill-file-TEJUXCJB.mjs")).skillFrontMatter;
490
638
  const finish = (checkoutRoot, buildPlan, answer, runTests2) => {
491
639
  const run = hasScriptTests(answer) ? runTests2(checkoutRoot, answer.tests) : null;
@@ -496,19 +644,32 @@ async function checkCheckout(root, options) {
496
644
  const needs = workspaceBuildNeedsOf(root, plan, options, sources);
497
645
  const skillFrontMatter2 = needs.skills.length > 0 ? await skillReader() : null;
498
646
  const runner = holdsScriptTests(root, plan) ? testRunner() : null;
499
- const answer = await checkWorkspaceBuildAsync(root, plan, options, { ...sources, skillFrontMatter: skillFrontMatter2 }, needs);
647
+ const answer = await checkWorkspaceBuildAsync(
648
+ root,
649
+ plan,
650
+ options,
651
+ { ...sources, skillFrontMatter: skillFrontMatter2 },
652
+ needs
653
+ );
500
654
  const runTests2 = runner === null ? null : await runner;
501
655
  if (hasScriptTests(answer) && runTests2 === null) {
502
- throw new Error("The workspace check found workflow or migration tests that the check did not expect.");
656
+ throw new Error(
657
+ "The workspace check found workflow or migration tests that the check did not expect."
658
+ );
503
659
  }
504
660
  return finish(root, plan, answer, runTests2);
505
661
  }
506
662
  const skillFrontMatter = holdsSkills(root, plan) ? await skillReader() : null;
507
663
  const runTests = holdsScriptTests(root, plan) ? await testRunner() : null;
508
664
  return buildComposedCheckout(root, plan, options, (checkoutRoot, buildPlan, buildOptions) => {
509
- const answer = checkWorkspaceBuild(checkoutRoot, buildPlan, buildOptions, { ...sources, skillFrontMatter });
665
+ const answer = checkWorkspaceBuild(checkoutRoot, buildPlan, buildOptions, {
666
+ ...sources,
667
+ skillFrontMatter
668
+ });
510
669
  if (hasScriptTests(answer) && runTests === null) {
511
- throw new Error("The workspace check found workflow or migration tests that the check did not expect.");
670
+ throw new Error(
671
+ "The workspace check found workflow or migration tests that the check did not expect."
672
+ );
512
673
  }
513
674
  return finish(checkoutRoot, buildPlan, answer, runTests);
514
675
  });
@@ -521,8 +682,8 @@ ${CHECK_USAGE}
521
682
  `);
522
683
  return 2;
523
684
  }
524
- const root = resolve(cwd, parsed.dir);
525
- if (!statSync3(root, { throwIfNoEntry: false })?.isDirectory()) {
685
+ const root = resolve2(cwd, parsed.dir);
686
+ if (!statSync4(root, { throwIfNoEntry: false })?.isDirectory()) {
526
687
  stderr.write(`Not a directory: ${root}
527
688
  `);
528
689
  return 2;
@@ -553,10 +714,14 @@ async function main(argv, io) {
553
714
  return makeMigration(args, io);
554
715
  case "check":
555
716
  return await check(args, io);
717
+ case "update":
718
+ return updatePackages(args, io);
556
719
  case "assets": {
557
720
  const [subcommand, ...assetArgs] = args;
558
- if (subcommand === "upload") return (await import("./cli-assets-cli-VQBXYEIZ.mjs")).uploadAsset(assetArgs, io);
559
- if (subcommand === "list") return (await import("./cli-assets-cli-VQBXYEIZ.mjs")).listAssets(assetArgs, io);
721
+ if (subcommand === "upload")
722
+ return (await import("./cli-assets-cli-VQBXYEIZ.mjs")).uploadAsset(assetArgs, io);
723
+ if (subcommand === "list")
724
+ return (await import("./cli-assets-cli-VQBXYEIZ.mjs")).listAssets(assetArgs, io);
560
725
  io.stderr.write(`${ASSETS_UPLOAD_USAGE}
561
726
  ${ASSETS_LIST_USAGE}
562
727
  `);
package/dist/index.d.ts CHANGED
@@ -1216,8 +1216,13 @@ export type NotificationTemplateEmail = {
1216
1216
  subject: string;
1217
1217
  /** Defaults to `'default'`, the built-in layout with one field, `content`. */
1218
1218
  layout?: EmailLayoutReference;
1219
- /** A value for each field of the layout. */
1220
- fields: { readonly [field: string]: LiquidSource };
1219
+ /**
1220
+ * A value for each field of the layout: Liquid, or for a field with a
1221
+ * `type`, `{ variable: '<path>' }`, the value at a path of the send. The
1222
+ * path starts with a variable of the type, `record` (the recipient), or
1223
+ * `workspace`, such as `{ variable: 'record.company.name' }`.
1224
+ */
1225
+ fields: { readonly [field: string]: LiquidSource | { variable: string } };
1221
1226
  };
1222
1227
 
1223
1228
  export type NotificationTemplateSms = { body: LiquidSource };
@@ -1247,11 +1252,17 @@ export declare function defineNotificationTemplate<const T extends NotificationT
1247
1252
  template: T
1248
1253
  ): T;
1249
1254
 
1250
- /** A slot in the layout body. A template fills it with Liquid. */
1255
+ /**
1256
+ * A slot in the layout body. A template fills it with Liquid. A field with a
1257
+ * `type` holds a value of that type: a template fills it with Liquid text or
1258
+ * binds it to a variable with `{ variable: '<path>' }`.
1259
+ */
1251
1260
  export type EmailLayoutFieldDefinition = {
1252
1261
  label: string;
1253
1262
  description?: string | null;
1254
1263
  required?: boolean;
1264
+ /** Leave it out for a Liquid field. */
1265
+ type?: 'text' | 'url' | 'email' | 'phone';
1255
1266
  };
1256
1267
 
1257
1268
  /**
@@ -148,8 +148,7 @@ type ResourceSortInput =
148
148
  | ResourceSortDescriptor
149
149
  | ResourceSortFieldDescriptor
150
150
  | ResourceSortDescriptorList
151
- | ResourceSortMap
152
- | Record<string, number | string>;
151
+ | ResourceSortMap;
153
152
 
154
153
  type ResourceListParams<TRelations extends readonly string[] = readonly string[]> = {
155
154
  page?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diagonal-hq/cli",
3
- "version": "0.38.3",
3
+ "version": "0.39.0",
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": {
@@ -51,8 +51,8 @@
51
51
  "bun": "1.4.4"
52
52
  },
53
53
  "optionalDependencies": {
54
- "@diagonal-hq/workflow-script-compiler-darwin-arm64": "0.1.0-ha30751b35a4d",
55
- "@diagonal-hq/workflow-script-compiler-linux-x64": "0.1.0-ha30751b35a4d",
56
- "@diagonal-hq/workflow-script-compiler-linux-arm64": "0.1.0-ha30751b35a4d"
54
+ "@diagonal-hq/workflow-script-compiler-darwin-arm64": "0.1.0-ha99248df50a2",
55
+ "@diagonal-hq/workflow-script-compiler-linux-x64": "0.1.0-ha99248df50a2",
56
+ "@diagonal-hq/workflow-script-compiler-linux-arm64": "0.1.0-ha99248df50a2"
57
57
  }
58
58
  }