@diagonal-hq/cli 0.20.4 → 0.21.1
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 +4 -3
- package/dist/cli.mjs +47 -8
- package/dist/index.d.ts +14 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -76,6 +76,7 @@ The build reads each file with a parser. It runs only the workflow and migration
|
|
|
76
76
|
- Every file that names a page imports `Pages` from `@app/generated/pages`: `diagonal.config.ts`, resource files, pages, layouts, components, and menus. There is one `Pages` object. `Pages` is not a global, and `@diagonal-hq/pages` does not export it. A file that uses `Pages` without the import fails the check with `'Pages' is not defined`. Every file names a page by its uid, `Pages.<Name>.uid`, as it names a workflow by `Workflows.<Name>.slug`. Pages, layouts, and components pass the uid to `utils` from `@diagonal-hq/pages`: `utils.page.url(Pages.Members.uid)`, `utils.page.url(Pages.Members_$Id.uid, { id })`, `utils.page.navigate(Pages.Members.uid)`, and `utils.page.navigate(Pages.Members_$Id.uid, { id }, { openInNewTab: true })`. A menu item is `{ kind: 'page', label, page: Pages.Members.uid }`. A config or resource file names a page with `Pages.<Name>.uid` too. See [Page reference settings](#page-reference-settings). The check fails at an unknown name, such as a stale name after a file move: `Unknown page "Pages.X". Each name in Pages is a file in pages/`. The type check also fails at a call without the route parameters of the page, at route parameters for a page without any, and at a menu item for a page with route parameters. The error names the page and its route, such as `Pages.Members_$Id has the route /members/{id}. Pass its route parameters as params.`
|
|
77
77
|
- 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 imports the Page SDK members that it uses from `@diagonal-hq/pages`, such as `import { resource, useRecords } from '@diagonal-hq/pages'`. A page names a resource by slug, for example `resource(Resources.Members.slug)`.
|
|
78
78
|
- Each `workflows/<name>.ts` is a script workflow. Its default export is `defineWorkflow({ uid, name, trigger, runAs?, input?, validate? }, handler)` from `@diagonal/workflow`, the same shape as `definePage(options, component)`. The config is the first argument and must be an object literal. The build reads every option except `input` and `validate` as static data and never runs the handler to find it. `input` is a `v.object({...})` schema, or a top-level `const` of one, which the script compiler reads. `validate` is a function that runs after the input check and before the handler, with the same input type. It returns errors keyed by field, and any error ends the run with a 422. The editor checks the config against `WorkflowConfig` from `@diagonal-hq/cli`. The build validates it: the uid (see [Definition uids and slugs](#definition-uids-and-slugs)), the trigger fields, the resources (see [Resource references](#resource-references)), the workflow references of `workflowEvent` triggers (see [Workflow references](#workflow-references)), the filter tree of a `recordEvent` trigger's `filters` (see [Policies](#policies)), and the run-as rules. A `recordEvent`, `recordSchedule`, `scheduled`, or `workflowEvent` workflow needs `runAs`.
|
|
79
|
+
- A `recordSchedule` trigger runs each offset from the `dateField` of each record. `timing` defaults to `'day'`, which runs at 8 a.m. in the workspace timezone on the day of the field plus the offset, and ignores the time of the field. Day timing takes whole-day offsets only, so the build refuses an offset such as `'-PT2H'` or `'+PT0M'`. `timing: 'exact'` runs at the time of the field plus the offset, for example 2 hours before a class with `offsets: ['-PT2H']`. Exact timing needs a datetime field.
|
|
79
80
|
- A workflow with `export const config = { title, ... }` and `export default defineWorkflow(handler)` or `defineWorkflow(input, handler)` still builds. The build warns at the config export and at `title`. Move the config into the `defineWorkflow` call and rename `title` to `name`. A workflow cannot have both forms of config, or both `name` and `title`. A `validateInput` export still runs before the handler, with a warning; move it into the config as `validate`. A workflow cannot have both.
|
|
80
81
|
- Each `skills/<name>.md` is a workspace AI skill: YAML front matter with `uid`, `name`, `description`, and optionally `enabled` and `manualOnly`, then a Markdown body with its instructions. The build validates the front matter, the uid (see [Definition uids and slugs](#definition-uids-and-slugs)), and that no two skill files declare one `name`. The build writes `generated/skills.ts`, which exports `Skills`: the `uid`, `name`, and `description` of each skill, keyed by the studly case of its file name, so `skills/front-desk.md` is `Skills.FrontDesk`. It never holds the instructions. Import it from `@app/generated/skills`. The build replaces each `Skills.<Name>.<key>` reference in a page or workflow with its value, as it does for `Resources`. A file name that gives no identifier, and a second file with one name, get no entry and a warning.
|
|
81
82
|
- The build removes the config options other than `input` and `validate`, and the `@diagonal-hq/cli` imports, from the workflow, with spaces in their place, and compiles the rest with the workflow script compiler against the workspace resources. The compiled script calls `defineWorkflow({ input, validate }, handler)`, so a change to a static option does not change the compiled code. A compiler error has the line and column of the workflow file.
|
|
@@ -658,8 +659,8 @@ export default defineNotificationSchedule({
|
|
|
658
659
|
|
|
659
660
|
This sends the Standard welcome 7 days after each active member's `joined_on`.
|
|
660
661
|
|
|
661
|
-
- `offsets` holds one to 30 distinct ISO 8601 durations in days, hours, or minutes: `'-P30D'` is 30 days before, `'P0D'` is the day, `'-PT2H'` is 2 hours before. A duration without a sign counts after the date, so `'P7D'` and `'+P7D'` are the same offset.
|
|
662
|
-
- `timing` defaults to `'day'`, which sends at 8 a.m. in the workspace timezone. `'exact'` sends at the
|
|
662
|
+
- `offsets` holds one to 30 distinct ISO 8601 durations in days, hours, or minutes: `'-P30D'` is 30 days before, `'P0D'` is the day, `'-PT2H'` is 2 hours before in exact timing. A duration without a sign counts after the date, so `'P7D'` and `'+P7D'` are the same offset.
|
|
663
|
+
- `timing` defaults to `'day'`, which sends at 8 a.m. in the workspace timezone on the day, so `'-PT2H'` sends at 6 a.m. `'exact'` sends at the time of a datetime field or the start of an event schedule field, plus the offset.
|
|
663
664
|
- `filters` is optional. It is a filter tree, `{ operator, conditions }`, with the shape of a policy `filter`. See [Policies](#policies).
|
|
664
665
|
- `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.
|
|
665
666
|
- `variables` maps each variable of the type to a field path on the source record. A record variable maps to a record's `id`.
|
|
@@ -722,7 +723,7 @@ The editor checks the shape of each definition. `diagonal check` and the build a
|
|
|
722
723
|
- A template has at most 50 references. A reference key starts with a lowercase letter, has only a-z, 0-9, and `_`, and has at most 64 characters. A label has at most 255 characters. The generator is one that a file can deploy.
|
|
723
724
|
- A layout's slug and its folder name are not `default`, and its `body` is not empty. A layout field key starts with a letter or `_` and has only letters, digits, and `_`. Each field has a `label` of at most 255 characters.
|
|
724
725
|
- A schedule has a `name` of 1 to 255 characters. Its `type` names a type of the workspace files that has a `recipient`, and its `template` names a template of that type that has an email. `source.resource` is a resource of the workspace. A schedule's `filters` is a filter tree with a known operator in each condition.
|
|
725
|
-
- A schedule's `dateField` is a date, datetime, or event schedule field of the source resource. Exact timing needs
|
|
726
|
+
- A schedule's `dateField` is a date, datetime, or event schedule field of the source resource. Exact timing needs a datetime or event schedule field. `offsets` holds 1 to 30 distinct durations.
|
|
726
727
|
- A schedule's `recipient` path reaches the type's recipient resource through relations that each hold one record. Without `recipient`, the source resource is the recipient resource.
|
|
727
728
|
- Each key of a schedule's `variables` is a variable of the type, and each path reaches a field of the source record or of a record that its single-record relations reach. A record variable maps to the id of a record of its resource, for example `'first_event.id'`. Each required variable is mapped.
|
|
728
729
|
|
package/dist/cli.mjs
CHANGED
|
@@ -301842,6 +301842,9 @@ function notificationTypeEntries(notifications) {
|
|
|
301842
301842
|
}
|
|
301843
301843
|
return { entries };
|
|
301844
301844
|
}
|
|
301845
|
+
function recordHoldsValue(field2) {
|
|
301846
|
+
return field2.required === true && field2.type !== "relation" && field2.type !== "password";
|
|
301847
|
+
}
|
|
301845
301848
|
function workflowResourceEntries(resources, openResources = []) {
|
|
301846
301849
|
const resourcesBySlug = new Map([
|
|
301847
301850
|
...openResources.map((resource) => [resource.slug, null]),
|
|
@@ -301863,7 +301866,7 @@ function workflowResourceEntries(resources, openResources = []) {
|
|
|
301863
301866
|
const inputType = field2.type === "relation" ? `${type} | Array<{ id: string }>` : type;
|
|
301864
301867
|
const key = propertyKey(field2.key);
|
|
301865
301868
|
input.push(`${key}?: ${inputType} | null`);
|
|
301866
|
-
record.push(`${key}: ${type} | null`);
|
|
301869
|
+
record.push(recordHoldsValue(field2) ? `${key}: ${type}` : `${key}: ${type} | null`);
|
|
301867
301870
|
}
|
|
301868
301871
|
const capabilities = Object.keys(resource.capabilities ?? {}).toSorted(byteOrder).map((capability) => `'${capability}'`);
|
|
301869
301872
|
entries.push(
|
|
@@ -310296,6 +310299,7 @@ function filterGroupProblem(group, path, node) {
|
|
|
310296
310299
|
// scripts/lib/runtime-build/record-schedule.mjs
|
|
310297
310300
|
init_define_globalThis_DIAGONAL_SDK_RUNTIME();
|
|
310298
310301
|
var OFFSET_PATTERN = /^([+-]?)P(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?)?$/i;
|
|
310302
|
+
var TIMINGS = ["day", "exact"];
|
|
310299
310303
|
var FIELD_TYPE_LABELS = {
|
|
310300
310304
|
date: "date",
|
|
310301
310305
|
datetime: "datetime",
|
|
@@ -310333,9 +310337,26 @@ function dateFieldProblem(resource, dateField, fieldTypes) {
|
|
|
310333
310337
|
if (field2 !== void 0 && fieldTypes.includes(field2.type)) {
|
|
310334
310338
|
return null;
|
|
310335
310339
|
}
|
|
310340
|
+
return `"${dateField}" is not a ${fieldTypeList(fieldTypes)} field of resource "${resource.slug}"`;
|
|
310341
|
+
}
|
|
310342
|
+
function readTiming(timing, node, fail) {
|
|
310343
|
+
const value = timing ?? "day";
|
|
310344
|
+
if (!TIMINGS.includes(value)) {
|
|
310345
|
+
fail(node, `"timing" must be one of ${TIMINGS.join(", ")}`);
|
|
310346
|
+
return null;
|
|
310347
|
+
}
|
|
310348
|
+
return value;
|
|
310349
|
+
}
|
|
310350
|
+
function exactTimingProblem(field2, fieldTypes) {
|
|
310351
|
+
return fieldTypes.includes(field2.type) ? null : `Exact timing needs a ${fieldTypeList(fieldTypes)} field`;
|
|
310352
|
+
}
|
|
310353
|
+
function dayTimingOffsetsProblem(offsets) {
|
|
310354
|
+
const offset = offsets.find((item) => /T/i.test(item));
|
|
310355
|
+
return offset === void 0 ? null : `Offset ${JSON.stringify(offset)} has hours or minutes, but day timing runs at 8 a.m. on the day of the date field. Set timing: 'exact' to count from the time of the field, or use whole days, for example '-P1D'`;
|
|
310356
|
+
}
|
|
310357
|
+
function fieldTypeList(fieldTypes) {
|
|
310336
310358
|
const labels = fieldTypes.map((type) => FIELD_TYPE_LABELS[type]);
|
|
310337
|
-
|
|
310338
|
-
return `"${dateField}" is not a ${list} field of resource "${resource.slug}"`;
|
|
310359
|
+
return labels.length < 3 ? labels.join(" or ") : `${labels.slice(0, -1).join(", ")}, or ${labels.at(-1)}`;
|
|
310339
310360
|
}
|
|
310340
310361
|
|
|
310341
310362
|
// scripts/lib/runtime-build/workflow-references.mjs
|
|
@@ -310764,6 +310785,7 @@ var RECORD_EVENTS = ["created", "updated", "deleted", "restored", "saved"];
|
|
|
310764
310785
|
var LIFECYCLE_HOOKS = ["creating", "updating", "saving", "deleting", "restoring"];
|
|
310765
310786
|
var WORKFLOW_EVENTS = ["execution.success", "execution.error", "execution.exception"];
|
|
310766
310787
|
var SCHEDULE_FIELD_TYPES = ["date", "datetime"];
|
|
310788
|
+
var EXACT_TIMING_FIELD_TYPES = ["datetime"];
|
|
310767
310789
|
var TRIGGERS = {
|
|
310768
310790
|
request: {
|
|
310769
310791
|
type: "request",
|
|
@@ -310785,7 +310807,7 @@ var TRIGGERS = {
|
|
|
310785
310807
|
},
|
|
310786
310808
|
recordSchedule: {
|
|
310787
310809
|
type: "schedule.record",
|
|
310788
|
-
keys: ["type", "resource", "dateField", "offsets", "filters"],
|
|
310810
|
+
keys: ["type", "resource", "dateField", "offsets", "timing", "filters"],
|
|
310789
310811
|
background: true,
|
|
310790
310812
|
sync: false
|
|
310791
310813
|
},
|
|
@@ -311324,6 +311346,7 @@ function triggerSettings(trigger, at, context, fail) {
|
|
|
311324
311346
|
case "recordSchedule": {
|
|
311325
311347
|
const resource = resolveResource(trigger.resource, at("resource"), context, fail);
|
|
311326
311348
|
const offsets = readOffsets(trigger.offsets, at("offsets"), fail);
|
|
311349
|
+
const timing = readTiming(trigger.timing, at("timing"), fail);
|
|
311327
311350
|
const dateField = trigger.dateField;
|
|
311328
311351
|
if (typeof dateField !== "string" || dateField === "") {
|
|
311329
311352
|
fail(at("dateField"), '"dateField" must be the key of a date or datetime field');
|
|
@@ -311335,8 +311358,23 @@ function triggerSettings(trigger, at, context, fail) {
|
|
|
311335
311358
|
fail(at("dateField"), problem2);
|
|
311336
311359
|
return null;
|
|
311337
311360
|
}
|
|
311361
|
+
const field2 = resource.fields.find((item) => item.key === dateField);
|
|
311362
|
+
const timingProblem = timing === "exact" ? exactTimingProblem(field2, EXACT_TIMING_FIELD_TYPES) : null;
|
|
311363
|
+
if (timingProblem !== null) {
|
|
311364
|
+
fail(at("timing"), timingProblem);
|
|
311365
|
+
return null;
|
|
311366
|
+
}
|
|
311367
|
+
}
|
|
311368
|
+
const offsetsProblem = timing === "day" && offsets !== null ? dayTimingOffsetsProblem(offsets) : null;
|
|
311369
|
+
if (offsetsProblem !== null) {
|
|
311370
|
+
fail(at("offsets"), offsetsProblem);
|
|
311371
|
+
return null;
|
|
311372
|
+
}
|
|
311373
|
+
if (resource === null || offsets === null || timing === null) {
|
|
311374
|
+
return null;
|
|
311338
311375
|
}
|
|
311339
|
-
|
|
311376
|
+
const schedule = timing === "exact" ? { offsets, timing } : { offsets };
|
|
311377
|
+
return { schedule, date_field: dateField, resource: resource.slug, ...filters };
|
|
311340
311378
|
}
|
|
311341
311379
|
case "scheduled": {
|
|
311342
311380
|
const expression2 = trigger.expression;
|
|
@@ -315228,8 +315266,8 @@ var RECIPIENT_MAX_LENGTH = 255;
|
|
|
315228
315266
|
var VARIABLE_PATH_MAX_LENGTH = 500;
|
|
315229
315267
|
var VARIABLE_PATH_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_.-]*$/;
|
|
315230
315268
|
var RELATION_PATH_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*$/;
|
|
315231
|
-
var TIMINGS = ["day", "exact"];
|
|
315232
315269
|
var DATE_FIELD_TYPES = ["date", "datetime", "event_schedule"];
|
|
315270
|
+
var EXACT_TIMING_FIELD_TYPES2 = ["datetime", "event_schedule"];
|
|
315233
315271
|
var SINGULAR_RELATIONS = ["belongs_to", "has_one"];
|
|
315234
315272
|
function readReference(value, key, node, report, label) {
|
|
315235
315273
|
const item = value[key];
|
|
@@ -315492,10 +315530,11 @@ function resolveSchedules(schedules, definitions, context, diagnostics) {
|
|
|
315492
315530
|
if (source !== null && source !== void 0) {
|
|
315493
315531
|
const problem2 = dateFieldProblem(source, schedule.dateField, DATE_FIELD_TYPES);
|
|
315494
315532
|
const field2 = source.fields.find((item) => item.key === schedule.dateField);
|
|
315533
|
+
const timingProblem = problem2 === null && schedule.timing === "exact" ? exactTimingProblem(field2, EXACT_TIMING_FIELD_TYPES2) : null;
|
|
315495
315534
|
if (problem2 !== null) {
|
|
315496
315535
|
report(at("dateField"), problem2);
|
|
315497
|
-
} else if (
|
|
315498
|
-
report(at("timing"),
|
|
315536
|
+
} else if (timingProblem !== null) {
|
|
315537
|
+
report(at("timing"), timingProblem);
|
|
315499
315538
|
}
|
|
315500
315539
|
for (const [key, path] of Object.entries(schedule.variables)) {
|
|
315501
315540
|
const variable = variables.get(key);
|
package/dist/index.d.ts
CHANGED
|
@@ -947,8 +947,18 @@ export type RecordScheduleTrigger = {
|
|
|
947
947
|
resource: ResourceReference;
|
|
948
948
|
/** The key of a date or datetime field of the resource. */
|
|
949
949
|
dateField: string;
|
|
950
|
-
/**
|
|
950
|
+
/**
|
|
951
|
+
* Signed ISO 8601 durations, for example `'-P1D'`. Day timing takes whole
|
|
952
|
+
* days. Exact timing also takes hours and minutes, for example `'-PT2H'`.
|
|
953
|
+
*/
|
|
951
954
|
offsets: readonly string[];
|
|
955
|
+
/**
|
|
956
|
+
* When each offset runs. `'day'` (the default) runs at 8 a.m. in the
|
|
957
|
+
* workspace timezone on the day of the field plus the offset, and ignores
|
|
958
|
+
* the time of the field. `'exact'` runs at the time of the field plus the
|
|
959
|
+
* offset, and needs a datetime field.
|
|
960
|
+
*/
|
|
961
|
+
timing?: 'day' | 'exact';
|
|
952
962
|
filters?: FilterGroup;
|
|
953
963
|
};
|
|
954
964
|
|
|
@@ -1157,8 +1167,8 @@ export declare function defineEmailLayout<const T extends EmailLayoutDefinition>
|
|
|
1157
1167
|
|
|
1158
1168
|
/**
|
|
1159
1169
|
* When a schedule sends. `'day'` sends at 8 a.m. in the workspace timezone on
|
|
1160
|
-
* the day. `'exact'` sends at the
|
|
1161
|
-
* event schedule field.
|
|
1170
|
+
* the day. `'exact'` sends at the time of a datetime field or the start of an
|
|
1171
|
+
* event schedule field, plus the offset.
|
|
1162
1172
|
*/
|
|
1163
1173
|
export type NotificationScheduleTiming = 'day' | 'exact';
|
|
1164
1174
|
|
|
@@ -1187,7 +1197,7 @@ export type NotificationScheduleDefinition = UidOption & {
|
|
|
1187
1197
|
/**
|
|
1188
1198
|
* One to 30 distinct ISO 8601 durations from the date, for example
|
|
1189
1199
|
* `'-P30D'` (30 days before), `'P0D'` (the day), or `'-PT2H'` (2 hours
|
|
1190
|
-
* before). A duration without a sign counts after the date.
|
|
1200
|
+
* before in exact timing). A duration without a sign counts after the date.
|
|
1191
1201
|
*/
|
|
1192
1202
|
offsets: readonly string[];
|
|
1193
1203
|
/** Only the source records that match. */
|