@diagonal-hq/cli 0.6.0 → 0.6.2

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  This package is the authoring SDK for file-based workspaces. A workspace repository installs it as a dev dependency. It gives these things:
4
4
 
5
- - The configuration helpers: `defineConfig`, `defineResource`, `definePage`, `defineNavigation`, `defineAssets`, `asset.version`, `defineNotificationType`, `defineNotificationTemplate`, `defineEmailLayout`, and the `field` builders. Each `define*` function returns its first argument. Each `field.<type>(options)` call returns `{ $field, ...options }`, where `$field` is the stored field type. The package root exports them as ESM JavaScript with declarations, so a Vite build or a Node script can import a configuration file.
5
+ - The configuration helpers: `defineConfig`, `defineResource`, `definePage`, `defineNavigation`, `defineAssets`, `asset.version`, `defineNotificationType`, `defineNotificationTemplate`, `defineEmailLayout`, `defineNotificationSchedule`, and the `field` builders. Each `define*` function returns its first argument. Each `field.<type>(options)` call returns `{ $field, ...options }`, where `$field` is the stored field type. The package root exports them as ESM JavaScript with declarations, so a Vite build or a Node script can import a configuration file.
6
6
  - Types for the helpers, `WorkflowConfig`, and the `Sdk` and `React` globals.
7
7
  - Editor types for the workflow script SDK: `@diagonal/workflow`, `@diagonal/workflow/testing`, and `@diagonal/workflow/workspace`.
8
8
  - The `diagonal check` command. It runs the same build and checks that a platform deploy runs, including the workflow tests.
@@ -51,7 +51,7 @@ The build reads each file with a parser. It runs only the workflow tests.
51
51
  - Each `resources/*.ts` file goes through the resource checks in [Resources and fields](#resources-and-fields).
52
52
  - The build removes imports from `@diagonal-hq/cli`. Pages, layouts, components, and workflow scripts can import workspace files through `@app/`. A resource file can import only `@app/generated/resources`. Workflow scripts can also import `@diagonal/workflow` and `@diagonal/workflow/workspace`. Other external imports in these source files are rejected.
53
53
  - The second argument of `definePage`, and the default export of a layout or component file, must be one function.
54
- - Each `definePage` needs a `key` that is unique in the workspace: a camelCase name of at most 64 letters and digits, such as `members`. The page's id comes from the key, so a file move keeps it. A missing key fails with a suggested key.
54
+ - Each page has a key that is unique in the workspace: a camelCase name of at most 64 letters and digits, such as `members`. The page's id comes from the key. Without a `key` option, the key comes from the file path below `pages/`: `pages/members/[id].tsx` is `membersId`, `pages/404.tsx` is `page404`, and `pages/index.tsx` is `home`. Set `key` when the page must keep its identity and its `Pages.<key>` links after a file move. Two pages with the same key fail at the second file.
55
55
  - Pages, layouts, and components link to a page with `Pages.<key>`: `Pages.members.url()`, `Pages.member.url({ id })`, and `Pages.members.navigate()`. `Pages` needs no import, and `import { Pages } from '@diagonal/pages'` also works. The check fails at an unknown key and at a call without the route parameters of the page.
56
56
  - Pages, layouts, and components go through the page lint gate: syntax, scope, the component return shape, and a type check against the page Sdk contract and the workspace resources. A page names a resource by slug, for example `Sdk.resource(Resources.Members)`.
57
57
  - Each `workflows/<name>.ts` is a script workflow. It has one `export const config = { title, trigger, runAs? } satisfies WorkflowConfig` and one `export default defineWorkflow(...)`. The config must be a static literal. The build validates it: the trigger fields, the resources (see [Resource references](#resource-references)), the workflow paths of `workflowEvent` triggers, and the run-as rules. A `recordEvent`, `recordSchedule`, `scheduled`, or `workflowEvent` workflow needs `runAs`.
@@ -93,7 +93,7 @@ The file gives the resource its `Resources` entry, so source names it like any o
93
93
 
94
94
  ## Notifications
95
95
 
96
- A notification has a type, one or more templates, and optionally an email layout. Each definition is a static literal. Its Liquid is inline text or a `.liquid` file next to it, written as `{ file: './standard.email.liquid' }`.
96
+ A notification has a type, one or more templates, and optionally an email layout. A schedule sends a template on dates of a resource, for example 7 days after each member's `joined_on`. Each definition is a static literal. Its Liquid is inline text or a `.liquid` file next to it, written as `{ file: './standard.email.liquid' }`.
97
97
 
98
98
  ```text
99
99
  notifications/
@@ -102,6 +102,7 @@ notifications/
102
102
  standard.template.ts
103
103
  standard.email.liquid
104
104
  standard.sms.liquid
105
+ week-one.schedule.ts
105
106
  fixtures/
106
107
  default.json
107
108
  email-layouts/
@@ -171,6 +172,37 @@ export default defineEmailLayout({
171
172
  });
172
173
  ```
173
174
 
175
+ ### Schedules
176
+
177
+ A schedule sends one template of its type on dates of a resource. It is a `<key>.schedule.ts` file in the type's folder. The file holds the rule. The workspace keeps the generated notifications, their edits and holds, and the pause state.
178
+
179
+ ```ts
180
+ import { Resources } from '@app/generated/resources';
181
+ import { defineNotificationSchedule } from '@diagonal-hq/cli';
182
+
183
+ export default defineNotificationSchedule({
184
+ key: 'week-one',
185
+ name: 'Week one check-in',
186
+ type: 'member-welcome',
187
+ template: 'standard',
188
+ source: { resource: Resources.Members },
189
+ dateField: 'joined_on',
190
+ offsets: ['P7D'],
191
+ filters: { operator: 'and', conditions: [{ field: 'status', operator: '=', value: 'active' }] },
192
+ variables: { joined_on: 'joined_on', first_event: 'first_event.id' }
193
+ });
194
+ ```
195
+
196
+ This sends the Standard welcome 7 days after each active member's `joined_on`.
197
+
198
+ - `offsets` holds one to 30 distinct signed ISO 8601 durations: `'-P30D'` is 30 days before, `'P0D'` is the day, `'-PT2H'` is 2 hours before.
199
+ - `timing` defaults to `'day'`, which sends at 8 a.m. in the workspace timezone. `'exact'` sends at the event start plus the offset and needs an event schedule field.
200
+ - `filters` is optional. It is the same filter object a workflow `recordEvent` trigger takes.
201
+ - `recipient` is the path of single-record relations from the source record to the type's recipient, for example `'member'` on a `memberships` source. Leave it out when the source resource is the recipient, as here.
202
+ - `variables` maps each variable of the type to a field path on the source record. A record variable maps to a record's `id`.
203
+
204
+ A deployed schedule is a System Notification. It sends with no staff identity, and `filters` decides who gets it. A file cannot name a person to send as. Staff actions on one generated notification, such as **Send now** or an edit, still act as the person who clicks.
205
+
174
206
  ### Rules
175
207
 
176
208
  The editor checks the shape of each definition. These rules need other files or server data, so the editor does not check them yet:
@@ -181,6 +213,10 @@ The editor checks the shape of each definition. These rules need other files or
181
213
  - A layout cannot use the key `default`.
182
214
  - Each reference key starts with a lowercase letter. A template has at most 50 references, and each one names a generator that the server has.
183
215
  - The `recipient` resource can receive notifications.
216
+ - A schedule's `type` is the type of its folder, and its `template` names a template of that type that has an email.
217
+ - A schedule's `dateField` is a date, datetime, or event schedule field of the source resource. Exact timing needs an event schedule field.
218
+ - A schedule's type has a `recipient`, and the schedule's `recipient` path reaches that resource through single-record relations.
219
+ - Each key of a schedule's `variables` is a variable of the type, each path reaches a field of the source record or a related record, a record variable maps to the id of a record of its resource, and every required variable is mapped.
184
220
 
185
221
  ### Fixtures
186
222
 
@@ -188,6 +224,7 @@ Fixtures are sample data for previews. They are not tests, and they are never se
188
224
 
189
225
  - A layout fixture holds a value for each layout field, for example `{ "heading": "Welcome aboard", "body": "<p>Hi Avery.</p>" }`.
190
226
  - A notification fixture holds a template key, a sample recipient, and sample variables, for example `{ "template": "standard", "recipient": { "name": "Avery Shaw", "email": "avery@example.com", "phone": null }, "variables": { "joined_on": "2026-11-02", "first_event": { "name": "Open house" } } }`. A record variable holds the record's fields.
227
+ - A schedule has no fixtures. The type's fixture already holds the sample recipient and variables.
191
228
 
192
229
  ## Resources and fields
193
230
 
package/dist/cli.mjs CHANGED
@@ -298444,6 +298444,7 @@ __export(cli_exports, {
298444
298444
  defineConfig: () => defineConfig,
298445
298445
  defineEmailLayout: () => defineEmailLayout,
298446
298446
  defineNavigation: () => defineNavigation,
298447
+ defineNotificationSchedule: () => defineNotificationSchedule,
298447
298448
  defineNotificationTemplate: () => defineNotificationTemplate,
298448
298449
  defineNotificationType: () => defineNotificationType,
298449
298450
  definePage: () => definePage,
@@ -298500,6 +298501,7 @@ var defineNavigation = (navigation) => navigation;
298500
298501
  var defineNotificationType = (type) => type;
298501
298502
  var defineNotificationTemplate = (template) => template;
298502
298503
  var defineEmailLayout = (layout) => layout;
298504
+ var defineNotificationSchedule = (schedule) => schedule;
298503
298505
 
298504
298506
  // scripts/lib/runtime-build/app-modules.mjs
298505
298507
  var APP_IMPORT = "__appImport";
@@ -298719,7 +298721,7 @@ function pageKeyFrom(text, taken = []) {
298719
298721
  }
298720
298722
  return key;
298721
298723
  }
298722
- function suggestedPageKey(path) {
298724
+ function derivedPageKey(path) {
298723
298725
  const route = path.replace(/^pages\//, "").replace(/\.tsx$/, "").replace(/(^|\/)index$/, "");
298724
298726
  return route === "" ? "home" : pageKeyFrom(route);
298725
298727
  }
@@ -299151,7 +299153,7 @@ function buildPage(path, source, ast, root = null, resources = null) {
299151
299153
  throw new SourceError(`Unknown page option "${key2}"`, nodes[key2]);
299152
299154
  }
299153
299155
  }
299154
- const key = pageKey(path, meta, nodes, metaNode);
299156
+ const key = pageKey(path, meta, nodes);
299155
299157
  if (typeof meta.route !== "string" || !meta.route.startsWith("/")) {
299156
299158
  throw new SourceError('"route" must be a string that starts with "/"', nodes.route ?? metaNode);
299157
299159
  }
@@ -299187,7 +299189,7 @@ function buildPage(path, source, ast, root = null, resources = null) {
299187
299189
  params: pageParams(meta, nodes, meta.route),
299188
299190
  ...functionUnit(source, ast, functionNode, helpers, appImports(root, path, source, ast)),
299189
299191
  layoutNode: nodes.layout ?? null,
299190
- keyNode: nodes.key,
299192
+ keyNode: nodes.key ?? metaNode,
299191
299193
  resourceNodes: {
299192
299194
  meta: metaNode,
299193
299195
  authorizer: nodes.authorizer ?? null,
@@ -299195,12 +299197,9 @@ function buildPage(path, source, ast, root = null, resources = null) {
299195
299197
  }
299196
299198
  };
299197
299199
  }
299198
- function pageKey(path, meta, nodes, metaNode) {
299200
+ function pageKey(path, meta, nodes) {
299199
299201
  if (meta.key === void 0) {
299200
- throw new SourceError(
299201
- `definePage needs a "key". Add key: '${suggestedPageKey(path)}'`,
299202
- metaNode
299203
- );
299202
+ return derivedPageKey(path);
299204
299203
  }
299205
299204
  if (!isPageKey(meta.key)) {
299206
299205
  throw new SourceError(
@@ -299210,14 +299209,20 @@ function pageKey(path, meta, nodes, metaNode) {
299210
299209
  }
299211
299210
  return meta.key;
299212
299211
  }
299213
- function pageKeyOf(ast) {
299212
+ function pageKeyOf(path, ast) {
299214
299213
  try {
299215
299214
  const { declaration } = defaultExportOf(ast, { helpers: "anywhere", allowAppImports: true });
299216
299215
  const [metaNode] = expectDefineCall(declaration, "definePage", 2);
299217
- const keyProperty = metaNode.type === "ObjectExpression" ? metaNode.properties.find(
299216
+ if (metaNode.type !== "ObjectExpression") {
299217
+ return null;
299218
+ }
299219
+ const keyProperty = metaNode.properties.find(
299218
299220
  (property) => property.type === "ObjectProperty" && !property.computed && (property.key.name ?? property.key.value) === "key"
299219
- ) : void 0;
299220
- const value = keyProperty?.value.type === "StringLiteral" ? keyProperty.value.value : null;
299221
+ );
299222
+ if (keyProperty === void 0) {
299223
+ return derivedPageKey(path);
299224
+ }
299225
+ const value = keyProperty.value.type === "StringLiteral" ? keyProperty.value.value : null;
299221
299226
  return isPageKey(value) ? value : null;
299222
299227
  } catch (error) {
299223
299228
  if (error instanceof SourceError) {
@@ -301357,9 +301362,9 @@ function lintUnits(units, resources, components, pages, diagnostics) {
301357
301362
  diagnostics.push(...lintUnit(unit, catalogs));
301358
301363
  }
301359
301364
  }
301360
- function pageKeyOfSource(source) {
301365
+ function pageKeyOfSource(path, source) {
301361
301366
  try {
301362
- const key = pageKeyOf(parseSource(source, { jsx: true }));
301367
+ const key = pageKeyOf(path, parseSource(source, { jsx: true }));
301363
301368
  return key === null ? [] : [key];
301364
301369
  } catch (error) {
301365
301370
  if (error instanceof SyntaxError) {
@@ -301466,7 +301471,7 @@ function buildWorkspace(root, { assetCatalog = null, writeGeneratedResources = f
301466
301471
  } catch (error) {
301467
301472
  diagnostics.push(diagnosticFromError(path, error));
301468
301473
  if (kind === "page") {
301469
- failedPageKeys.push(...pageKeyOfSource(source));
301474
+ failedPageKeys.push(...pageKeyOfSource(path, source));
301470
301475
  } else if (kind === "layout") {
301471
301476
  failedLayouts.add(stem(path));
301472
301477
  } else if (kind === "component") {
package/dist/index.d.ts CHANGED
@@ -160,10 +160,12 @@ export type PageDefinition = {
160
160
  /**
161
161
  * The page's identity, unique in the workspace: a camelCase name of at most
162
162
  * 64 letters and digits that starts with a lowercase letter, such as
163
- * `members`. Code links to the page as `Pages.<key>`. A file move keeps the
164
- * key, and a key change is an identity change.
163
+ * `members`. Code links to the page as `Pages.<key>`. Without it, the key
164
+ * comes from the file path: `pages/members/[id].tsx` is `membersId`, and
165
+ * `pages/index.tsx` is `home`. Set it to keep the page's identity when the
166
+ * file moves. A key change is an identity change.
165
167
  */
166
- key: string;
168
+ key?: string;
167
169
  /** The URL pattern. Route parameters use `{name}`, for example `/members/{id}`. */
168
170
  route: string;
169
171
  /** The name in the admin. Defaults to the title. */
@@ -394,3 +396,60 @@ export type EmailLayoutDefinition = {
394
396
  };
395
397
 
396
398
  export declare function defineEmailLayout<const T extends EmailLayoutDefinition>(layout: T): T;
399
+
400
+ /**
401
+ * When a schedule sends. `'day'` sends at 8 a.m. in the workspace timezone on
402
+ * the day. `'exact'` sends at the event start plus the offset, and needs an
403
+ * event schedule field.
404
+ */
405
+ export type NotificationScheduleTiming = 'day' | 'exact';
406
+
407
+ /**
408
+ * A `notifications/<type key>/<key>.schedule.ts` file. A schedule sends one
409
+ * template of its type on dates of a resource, for example 7 days after each
410
+ * member's `joined_on`. The deployed schedule is a System Notification: it
411
+ * sends with no staff identity, and `filters` decides who gets it. The file
412
+ * holds the rule. The workspace keeps the generated notifications, their edits
413
+ * and holds, and the pause state.
414
+ */
415
+ export type NotificationScheduleDefinition = {
416
+ /**
417
+ * Lowercase letters, digits, and hyphens, for example `'week-one'`. Unique
418
+ * within the type. Moving the file keeps it.
419
+ */
420
+ key: string;
421
+ name: string;
422
+ /** The type whose folder holds the file. */
423
+ type: NotificationTypeReference;
424
+ /** A template of that type. The template must have an email. */
425
+ template: NotificationTemplateReference;
426
+ /** The resource whose records get the notification, as `Resources.<Name>`. */
427
+ source: { resource: ResourceReference };
428
+ /** The key of a date, datetime, or event schedule field of the source resource. */
429
+ dateField: string;
430
+ /** Defaults to `'day'`. */
431
+ timing?: NotificationScheduleTiming;
432
+ /**
433
+ * One to 30 distinct signed ISO 8601 durations from the date, for example
434
+ * `'-P30D'` (30 days before), `'P0D'` (the day), or `'-PT2H'` (2 hours before).
435
+ */
436
+ offsets: readonly string[];
437
+ /** Only the source records that match. The same filter object as a `recordEvent` trigger. */
438
+ filters?: StaticValue;
439
+ /**
440
+ * The path of single-record relations from the source record to the type's
441
+ * recipient, for example `'member'` on a `memberships` source. Leave it out
442
+ * when the source resource is the recipient resource.
443
+ */
444
+ recipient?: string;
445
+ /**
446
+ * A field path on the source record for each variable of the type, for
447
+ * example `'joined_on'` or `'member.name'`. A record variable maps to the id
448
+ * of a record of its resource, for example `'first_event.id'`.
449
+ */
450
+ variables?: { readonly [key: string]: string };
451
+ };
452
+
453
+ export declare function defineNotificationSchedule<const T extends NotificationScheduleDefinition>(
454
+ schedule: T
455
+ ): T;
package/dist/index.js CHANGED
@@ -29,4 +29,6 @@ export const defineNotificationTemplate = (template) => template;
29
29
 
30
30
  export const defineEmailLayout = (layout) => layout;
31
31
 
32
+ export const defineNotificationSchedule = (schedule) => schedule;
33
+
32
34
  export { field } from './fields.generated.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diagonal-hq/cli",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
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": {