@basementuniverse/jsonpad-cli 1.5.0 → 1.6.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.
- package/CHANGELOG.md +31 -0
- package/README.md +49 -1
- package/REFERENCE.md +64 -4
- package/build/commands/flows.js +100 -0
- package/build/commands/schema/export.js +39 -0
- package/build/commands/schema/sync.js +12 -4
- package/build/errors.js +2 -0
- package/build/flows.js +153 -0
- package/build/program.js +4 -1
- package/build/sdk.js +1 -1
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,37 @@ and this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
Dates are npm publish dates.
|
|
9
9
|
|
|
10
|
+
## [Unreleased]
|
|
11
|
+
|
|
12
|
+
Needs `@basementuniverse/jsonpad-sdk` with `runFlow()` (the release after
|
|
13
|
+
2.2.0), and the JSONPad API release with flows. `package.json` points at the
|
|
14
|
+
SDK's local checkout (`file:../jsonpad-sdk-js`) until that's published.
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- **`jsonpad flows`**, which checks and tests flows with the copy of the flows
|
|
19
|
+
engine built into the CLI (`@basementuniverse/jsonpad-flows` 1.0.0), so it
|
|
20
|
+
needs no network and costs no requests:
|
|
21
|
+
- `flows check` compiles a flow file and shows the checker's warnings, with
|
|
22
|
+
`--strict` to fail on them.
|
|
23
|
+
- `flows test` runs a flow's tests (`<name>.tests.json` by default) against
|
|
24
|
+
in-memory items, with `--filter`, `--trace` and `--json`.
|
|
25
|
+
- `flows run` calls an endpoint flow, with `--input`, `--method`, `--public`
|
|
26
|
+
and `--json`.
|
|
27
|
+
- **Flows in schema documents.** `flowFile` and `flowTestsFile` are read
|
|
28
|
+
relative to the document and sent as `document` and `tests`. `sync-schema`
|
|
29
|
+
compiles and tests flows before sending anything (`--skip-flow-tests` turns
|
|
30
|
+
that off), and shows flow changes.
|
|
31
|
+
- `export-schema --split-flows <directory>` writes each flow and its tests to
|
|
32
|
+
their own files and references them from the document.
|
|
33
|
+
- Exit code `10`, for a flow that fails. Exit code `9` now also covers flow
|
|
34
|
+
tests that fail.
|
|
35
|
+
|
|
36
|
+
### Changed
|
|
37
|
+
|
|
38
|
+
- `@basementuniverse/jsonpad-rules` is now `^1.2.0`, the version the flows
|
|
39
|
+
engine needs, so there's one copy of it.
|
|
40
|
+
|
|
10
41
|
## [1.5.0] - 2026-09-23
|
|
11
42
|
|
|
12
43
|
Needs `@basementuniverse/jsonpad-sdk` 2.2.0, and the JSONPad API release with
|
package/README.md
CHANGED
|
@@ -402,6 +402,53 @@ press Tab.
|
|
|
402
402
|
JSONPAD_TOKEN: ${{ secrets.JSONPAD_TOKEN }}
|
|
403
403
|
```
|
|
404
404
|
|
|
405
|
+
### Flows
|
|
406
|
+
|
|
407
|
+
[Flows](https://jsonpad.io/docs/flows) are small programs drawn in the
|
|
408
|
+
dashboard: endpoints your app calls, and steps that run when items change. The
|
|
409
|
+
CLI has its own copy of the flows engine, so checking and testing a flow needs
|
|
410
|
+
no network and doesn't count against your request allowance. Tests run against
|
|
411
|
+
an in-memory copy of the items they declare.
|
|
412
|
+
|
|
413
|
+
```bash
|
|
414
|
+
# Check that a flow file compiles, and show the checker's warnings
|
|
415
|
+
jsonpad flows check flows/deal-card.flow.json
|
|
416
|
+
|
|
417
|
+
# Run its tests (flows/deal-card.tests.json unless another file is given)
|
|
418
|
+
jsonpad flows test flows/deal-card.flow.json --trace
|
|
419
|
+
|
|
420
|
+
# Call an endpoint flow (the token needs the run permission on it)
|
|
421
|
+
jsonpad flows run deal-card --input '{"gameId":"g1"}'
|
|
422
|
+
jsonpad flows run leaderboard --method GET --input '{"top":10}'
|
|
423
|
+
|
|
424
|
+
# Call a public flow by its id, without a token
|
|
425
|
+
jsonpad flows run 5b9d2c1e-7f0a-4c55-9d1e-3a6b8f2e4c10 --public
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
A flow that fails exits with `10`, and says why and at which node. Flows are
|
|
429
|
+
created and updated with schema sync: declare them in the schema document, next
|
|
430
|
+
to the lists they use.
|
|
431
|
+
|
|
432
|
+
```json
|
|
433
|
+
{
|
|
434
|
+
"scope": "cards",
|
|
435
|
+
"lists": { "games": {}, "decks": {}, "hands": {} },
|
|
436
|
+
"flows": {
|
|
437
|
+
"deal-card": {
|
|
438
|
+
"flowFile": "flows/deal-card.flow.json",
|
|
439
|
+
"flowTestsFile": "flows/deal-card.tests.json"
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
`sync-schema` compiles and tests every flow before sending anything
|
|
446
|
+
(`--skip-flow-tests` turns that off). Where the nodes sit in the dashboard is
|
|
447
|
+
kept when a flow is updated, so moving them around there doesn't make the file
|
|
448
|
+
out of date. `export-schema --split-flows flows` writes each flow and its tests
|
|
449
|
+
to their own files. Syncing flows needs a token that is allowed everything,
|
|
450
|
+
because a flow runs with your full privileges.
|
|
451
|
+
|
|
405
452
|
## Exit codes
|
|
406
453
|
|
|
407
454
|
| Code | Meaning |
|
|
@@ -415,7 +462,8 @@ press Tab.
|
|
|
415
462
|
| `6` | Not found |
|
|
416
463
|
| `7` | The token isn't allowed to do this, or isn't valid |
|
|
417
464
|
| `8` | Rate limited (after retrying), or a plan limit or the quota was reached |
|
|
418
|
-
| `9` | Write rule tests failed, or a rules check refused a write
|
|
465
|
+
| `9` | Write rule or flow tests failed, or a rules check refused a write |
|
|
466
|
+
| `10` | A flow failed (`flows run`) |
|
|
419
467
|
|
|
420
468
|
A dry run exits the same way the real sync would.
|
|
421
469
|
|
package/REFERENCE.md
CHANGED
|
@@ -6,11 +6,11 @@ Run `jsonpad <command> --help` for the same information in a terminal.
|
|
|
6
6
|
|
|
7
7
|
## Commands
|
|
8
8
|
|
|
9
|
-
- [`jsonpad sync-schema`](#jsonpad-sync-schema): Create and update lists and
|
|
9
|
+
- [`jsonpad sync-schema`](#jsonpad-sync-schema): Create and update lists, indexes and flows to match a schema document
|
|
10
10
|
- [`jsonpad export-schema`](#jsonpad-export-schema): Write a schema document for existing lists
|
|
11
11
|
- [`jsonpad move-lists`](#jsonpad-move-lists): Move lists (by id or path name) to a scope, or release them from their scope
|
|
12
12
|
- [`jsonpad rebuild-index`](#jsonpad-rebuild-index): Rebuild an index whose last build failed, once the problem has been fixed
|
|
13
|
-
- [`jsonpad schema sync`](#jsonpad-schema-sync): Create and update lists and
|
|
13
|
+
- [`jsonpad schema sync`](#jsonpad-schema-sync): Create and update lists, indexes and flows to match a schema document
|
|
14
14
|
- [`jsonpad schema export`](#jsonpad-schema-export): Write a schema document for existing lists
|
|
15
15
|
- [`jsonpad schema move`](#jsonpad-schema-move): Move lists (by id or path name) to a scope, or release them from their scope
|
|
16
16
|
- [`jsonpad lists list`](#jsonpad-lists-list): List lists (the default when no command is given)
|
|
@@ -75,6 +75,9 @@ Run `jsonpad <command> --help` for the same information in a terminal.
|
|
|
75
75
|
- [`jsonpad rules check`](#jsonpad-rules-check): Check that a rule file compiles, and show the checker's warnings
|
|
76
76
|
- [`jsonpad rules test`](#jsonpad-rules-test): Run a rule set against its tests
|
|
77
77
|
- [`jsonpad rules eval`](#jsonpad-rules-eval): Check one write against a rule set, and show what each rule did
|
|
78
|
+
- [`jsonpad flows check`](#jsonpad-flows-check): Check that a flow file compiles, and show the checker's warnings
|
|
79
|
+
- [`jsonpad flows test`](#jsonpad-flows-test): Run a flow's tests, against an in-memory copy of your data
|
|
80
|
+
- [`jsonpad flows run`](#jsonpad-flows-run): Call an endpoint flow, e.g. jsonpad flows run create-order --input '{"productId":"p1"}'
|
|
78
81
|
- [`jsonpad whoami`](#jsonpad-whoami): Show the token you're using, its plan's limits and usage
|
|
79
82
|
- [`jsonpad config set-profile`](#jsonpad-config-set-profile): Add or update a profile
|
|
80
83
|
- [`jsonpad config use`](#jsonpad-config-use): Choose the default profile
|
|
@@ -86,7 +89,7 @@ Run `jsonpad <command> --help` for the same information in a terminal.
|
|
|
86
89
|
|
|
87
90
|
### `jsonpad sync-schema`
|
|
88
91
|
|
|
89
|
-
Create and update lists and
|
|
92
|
+
Create and update lists, indexes and flows to match a schema document
|
|
90
93
|
|
|
91
94
|
```
|
|
92
95
|
jsonpad sync-schema [options] [file]
|
|
@@ -106,6 +109,7 @@ jsonpad sync-schema [options] [file]
|
|
|
106
109
|
| `--timeout <seconds>` | How long --wait waits for each index (default 600) |
|
|
107
110
|
| `--show-unchanged` | Also list resources that don't change |
|
|
108
111
|
| `--skip-rule-tests` | Don't compile and test write rules here first (the API always checks them) |
|
|
112
|
+
| `--skip-flow-tests` | Don't compile and test flows here first (the API always checks them) |
|
|
109
113
|
| `--json` | Print the API's response as JSON |
|
|
110
114
|
|
|
111
115
|
### `jsonpad export-schema`
|
|
@@ -123,6 +127,7 @@ jsonpad export-schema [options]
|
|
|
123
127
|
| `--lists <path names>` | Only these comma-separated lists |
|
|
124
128
|
| `--out <file>` | Write the document to a file, not stdout |
|
|
125
129
|
| `--split-rules <directory>` | Write each list's write rules and rule tests to their own files in this directory, and reference them from the document (needs --out) |
|
|
130
|
+
| `--split-flows <directory>` | Write each flow and its tests to their own files in this directory (name.flow.json and name.tests.json), and reference them from the document (needs --out) |
|
|
126
131
|
|
|
127
132
|
### `jsonpad move-lists`
|
|
128
133
|
|
|
@@ -1515,6 +1520,60 @@ jsonpad rules eval [options] [rules]
|
|
|
1515
1520
|
| `--trace` | Show every expression that was evaluated |
|
|
1516
1521
|
| `--json` | Print the result as JSON |
|
|
1517
1522
|
|
|
1523
|
+
### `jsonpad flows check`
|
|
1524
|
+
|
|
1525
|
+
Check that a flow file compiles, and show the checker's warnings
|
|
1526
|
+
|
|
1527
|
+
```
|
|
1528
|
+
jsonpad flows check [options] <file>
|
|
1529
|
+
```
|
|
1530
|
+
|
|
1531
|
+
| Argument | Description |
|
|
1532
|
+
| --- | --- |
|
|
1533
|
+
| `file` | The flow file (a JSON flow document) |
|
|
1534
|
+
|
|
1535
|
+
| Option | Description |
|
|
1536
|
+
| --- | --- |
|
|
1537
|
+
| `--strict` | Fail if there are any warnings |
|
|
1538
|
+
|
|
1539
|
+
### `jsonpad flows test`
|
|
1540
|
+
|
|
1541
|
+
Run a flow's tests, against an in-memory copy of your data
|
|
1542
|
+
|
|
1543
|
+
```
|
|
1544
|
+
jsonpad flows test [options] <file> [tests]
|
|
1545
|
+
```
|
|
1546
|
+
|
|
1547
|
+
| Argument | Description |
|
|
1548
|
+
| --- | --- |
|
|
1549
|
+
| `file` | The flow file |
|
|
1550
|
+
| `tests` (optional) | The test file (default: the flow file with .tests.json, e.g. deal-card.flow.json and deal-card.tests.json) |
|
|
1551
|
+
|
|
1552
|
+
| Option | Description |
|
|
1553
|
+
| --- | --- |
|
|
1554
|
+
| `--filter <text>` | Only tests whose name contains this text |
|
|
1555
|
+
| `--trace` | Show every node of a failing test |
|
|
1556
|
+
| `--json` | Print the results as JSON |
|
|
1557
|
+
|
|
1558
|
+
### `jsonpad flows run`
|
|
1559
|
+
|
|
1560
|
+
Call an endpoint flow, e.g. jsonpad flows run create-order --input '{"productId":"p1"}'
|
|
1561
|
+
|
|
1562
|
+
```
|
|
1563
|
+
jsonpad flows run [options] <path>
|
|
1564
|
+
```
|
|
1565
|
+
|
|
1566
|
+
| Argument | Description |
|
|
1567
|
+
| --- | --- |
|
|
1568
|
+
| `path` | The flow's endpoint path, or its id with --public |
|
|
1569
|
+
|
|
1570
|
+
| Option | Description |
|
|
1571
|
+
| --- | --- |
|
|
1572
|
+
| `--input <json>` | The input (the request body, or the query parameters for GET), as JSON (@file, - for stdin) |
|
|
1573
|
+
| `--method <method>` | GET, POST, PUT, PATCH, DELETE (default POST) |
|
|
1574
|
+
| `--public` | Call a public flow by its id, without a token |
|
|
1575
|
+
| `--json` | Print the status, body and run id as JSON |
|
|
1576
|
+
|
|
1518
1577
|
### `jsonpad whoami`
|
|
1519
1578
|
|
|
1520
1579
|
Show the token you're using: what it can do, its plan's limits, and the account's usage this month
|
|
@@ -1674,7 +1733,8 @@ JSONPAD_TOKEN, then the default profile.
|
|
|
1674
1733
|
7 The token isn't allowed to do this, or isn't valid
|
|
1675
1734
|
8 Rate limited (after retrying), or a plan limit or the monthly quota was
|
|
1676
1735
|
reached
|
|
1677
|
-
9 Write rule tests failed, or a rules check refused a write
|
|
1736
|
+
9 Write rule or flow tests failed, or a rules check refused a write
|
|
1737
|
+
10 A flow failed (flows run)
|
|
1678
1738
|
|
|
1679
1739
|
The schema commands and rebuild-index (and indexes rebuild) exit with 1 for
|
|
1680
1740
|
every API error, as they did in @basementuniverse/jsonpad-sdk.
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { CliError, describeApiError, EXIT_FLOW_FAILED } from '../errors.js';
|
|
2
|
+
import { compileFlow, defaultFlowTestsFile, engineVersion, runFlowTests, } from '../flows.js';
|
|
3
|
+
import { readJsonInput } from '../input.js';
|
|
4
|
+
import { readJsonFile } from '../rules.js';
|
|
5
|
+
import { FlowError } from '../sdk.js';
|
|
6
|
+
const METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
|
|
7
|
+
export function defineFlows(command, context) {
|
|
8
|
+
command
|
|
9
|
+
.description('Check, test and run flows')
|
|
10
|
+
.addHelpText('after', `\nFlows are checked and tested by the copy of the flows engine built into this\nCLI (version ${engineVersion}), so check and test need no network and don't count\nagainst your request allowance. The API always has the final say.\n\nTo create and update flows, declare them in your schema document (see\nsync-schema) with flowFile and flowTestsFile.\n`);
|
|
11
|
+
command
|
|
12
|
+
.command('check')
|
|
13
|
+
.description("Check that a flow file compiles, and show the checker's warnings")
|
|
14
|
+
.argument('<file>', 'The flow file (a JSON flow document)')
|
|
15
|
+
.option('--strict', 'Fail if there are any warnings')
|
|
16
|
+
.action((file, options) => checkFlow(context, file, options));
|
|
17
|
+
command
|
|
18
|
+
.command('test')
|
|
19
|
+
.description("Run a flow's tests, against an in-memory copy of your data")
|
|
20
|
+
.argument('<file>', 'The flow file')
|
|
21
|
+
.argument('[tests]', 'The test file (default: the flow file with .tests.json, e.g. deal-card.flow.json and deal-card.tests.json)')
|
|
22
|
+
.option('--filter <text>', 'Only tests whose name contains this text')
|
|
23
|
+
.option('--trace', 'Show every node of a failing test')
|
|
24
|
+
.option('--json', 'Print the results as JSON')
|
|
25
|
+
.action((file, tests, options) => testFlow(context, file, tests, options));
|
|
26
|
+
command
|
|
27
|
+
.command('run')
|
|
28
|
+
.description('Call an endpoint flow, e.g. jsonpad flows run create-order --input \'{"productId":"p1"}\'')
|
|
29
|
+
.argument('<path>', "The flow's endpoint path, or its id with --public")
|
|
30
|
+
.option('--input <json>', 'The input (the request body, or the query parameters for GET), as JSON (@file, - for stdin)')
|
|
31
|
+
.option('--method <method>', `${METHODS.join(', ')} (default POST)`)
|
|
32
|
+
.option('--public', 'Call a public flow by its id, without a token')
|
|
33
|
+
.option('--json', 'Print the status, body and run id as JSON')
|
|
34
|
+
.action((flowPath, options) => runFlow(context, flowPath, options));
|
|
35
|
+
return command;
|
|
36
|
+
}
|
|
37
|
+
async function checkFlow(context, file, options) {
|
|
38
|
+
compileFlow(context, readJsonFile(context, file, 'the flow file'), file, {
|
|
39
|
+
strict: options.strict,
|
|
40
|
+
});
|
|
41
|
+
context.log(`${context.colours.green('✓')} ${file} compiles`);
|
|
42
|
+
}
|
|
43
|
+
async function testFlow(context, file, testsFile, options) {
|
|
44
|
+
const flow = compileFlow(context, readJsonFile(context, file, 'the flow file'), file, { quiet: options.json });
|
|
45
|
+
const tests = testsFile ?? defaultFlowTestsFile(file);
|
|
46
|
+
await runFlowTests(context, flow, readJsonFile(context, tests, 'the test file'), tests, options);
|
|
47
|
+
}
|
|
48
|
+
async function runFlow(context, flowPath, options) {
|
|
49
|
+
const method = (options.method ?? 'POST').toUpperCase();
|
|
50
|
+
if (!METHODS.includes(method)) {
|
|
51
|
+
throw new CliError(`--method must be one of ${METHODS.join(', ')}`);
|
|
52
|
+
}
|
|
53
|
+
const input = options.input
|
|
54
|
+
? await readJsonInput(context, '--input', options.input)
|
|
55
|
+
: undefined;
|
|
56
|
+
if (input !== undefined &&
|
|
57
|
+
(typeof input !== 'object' || input === null || Array.isArray(input))) {
|
|
58
|
+
throw new CliError('--input must be a JSON object');
|
|
59
|
+
}
|
|
60
|
+
const jsonpad = context.createClient();
|
|
61
|
+
let response;
|
|
62
|
+
try {
|
|
63
|
+
response = options.public
|
|
64
|
+
? await jsonpad.runPublicFlow(flowPath, input, {
|
|
65
|
+
method: method,
|
|
66
|
+
})
|
|
67
|
+
: await jsonpad.runFlow(flowPath, input, {
|
|
68
|
+
method: method,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (error instanceof FlowError) {
|
|
73
|
+
if (options.json) {
|
|
74
|
+
context.log(JSON.stringify({
|
|
75
|
+
status: error.status,
|
|
76
|
+
error: {
|
|
77
|
+
code: error.flowCode,
|
|
78
|
+
message: error.flowMessage,
|
|
79
|
+
node: error.node,
|
|
80
|
+
},
|
|
81
|
+
runId: error.runId,
|
|
82
|
+
}, null, 2));
|
|
83
|
+
}
|
|
84
|
+
throw new CliError(`The flow failed (${error.status}): ${error.flowMessage}${error.node ? ` at node "${error.node}"` : ''}${error.flowCode ? ` [${error.flowCode}]` : ''}${error.runId ? `\nRun ${error.runId}` : ''}`, EXIT_FLOW_FAILED);
|
|
85
|
+
}
|
|
86
|
+
throw new CliError(describeApiError(error));
|
|
87
|
+
}
|
|
88
|
+
if (options.json) {
|
|
89
|
+
context.log(JSON.stringify({
|
|
90
|
+
status: response.status,
|
|
91
|
+
body: response.body,
|
|
92
|
+
runId: response.runId,
|
|
93
|
+
}, null, 2));
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
context.error(context.colours.dim(`${response.status}${response.runId ? ` (run ${response.runId})` : ''}`));
|
|
97
|
+
if (response.body !== null) {
|
|
98
|
+
context.log(JSON.stringify(response.body, null, 2));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -13,6 +13,7 @@ export function defineExportSchema(command, context) {
|
|
|
13
13
|
.option('--lists <path names>', 'Only these comma-separated lists')
|
|
14
14
|
.option('--out <file>', 'Write the document to a file, not stdout')
|
|
15
15
|
.option('--split-rules <directory>', "Write each list's write rules and rule tests to their own files in this directory, and reference them from the document (needs --out)")
|
|
16
|
+
.option('--split-flows <directory>', 'Write each flow and its tests to their own files in this directory (name.flow.json and name.tests.json), and reference them from the document (needs --out)')
|
|
16
17
|
.action((options) => exportSchema(context, options));
|
|
17
18
|
}
|
|
18
19
|
export async function exportSchema(context, options) {
|
|
@@ -39,6 +40,9 @@ export async function exportSchema(context, options) {
|
|
|
39
40
|
if (options.splitRules) {
|
|
40
41
|
splitRules(context, result.document, options);
|
|
41
42
|
}
|
|
43
|
+
if (options.splitFlows) {
|
|
44
|
+
splitFlows(context, result.document, options);
|
|
45
|
+
}
|
|
42
46
|
const json = `${JSON.stringify(result.document, null, 2)}\n`;
|
|
43
47
|
if (options.out) {
|
|
44
48
|
fs.writeFileSync(path.resolve(context.cwd, options.out), json);
|
|
@@ -88,3 +92,38 @@ function splitRules(context, document, options) {
|
|
|
88
92
|
}
|
|
89
93
|
context.error(`Wrote ${written} rule ${written === 1 ? 'file' : 'files'} to ${options.splitRules}`);
|
|
90
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Move each flow's document and tests out of the schema document and into
|
|
97
|
+
* their own files, which the schema document then points at with flowFile
|
|
98
|
+
* and flowTestsFile
|
|
99
|
+
*/
|
|
100
|
+
function splitFlows(context, document, options) {
|
|
101
|
+
if (!options.out) {
|
|
102
|
+
throw new CliError('--split-flows needs --out');
|
|
103
|
+
}
|
|
104
|
+
const directory = path.resolve(context.cwd, options.splitFlows);
|
|
105
|
+
const documentDirectory = path.dirname(path.resolve(context.cwd, options.out));
|
|
106
|
+
const reference = (file) => path.relative(documentDirectory, file).split(path.sep).join('/');
|
|
107
|
+
let written = 0;
|
|
108
|
+
fs.mkdirSync(directory, { recursive: true });
|
|
109
|
+
for (const [name, flow] of Object.entries(document.flows ?? {})) {
|
|
110
|
+
if (flow.document) {
|
|
111
|
+
const file = path.join(directory, `${name}.flow.json`);
|
|
112
|
+
fs.writeFileSync(file, `${JSON.stringify(flow.document, null, 2)}\n`);
|
|
113
|
+
delete flow.document;
|
|
114
|
+
flow.flowFile = reference(file);
|
|
115
|
+
written++;
|
|
116
|
+
}
|
|
117
|
+
if (flow.tests) {
|
|
118
|
+
const file = path.join(directory, `${name}.tests.json`);
|
|
119
|
+
fs.writeFileSync(file, `${JSON.stringify(flow.tests, null, 2)}\n`);
|
|
120
|
+
delete flow.tests;
|
|
121
|
+
flow.flowTestsFile = reference(file);
|
|
122
|
+
written++;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
delete flow.tests;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
context.error(`Wrote ${written} flow ${written === 1 ? 'file' : 'files'} to ${options.splitFlows}`);
|
|
129
|
+
}
|
|
@@ -2,12 +2,13 @@ import fs from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { CliError, describeApiError, EXIT_DESTRUCTIVE_NOT_ALLOWED, EXIT_ERROR, EXIT_REBUILD_NOT_ALLOWED, } from '../../errors.js';
|
|
4
4
|
import { parseTimeout, waitForIndexes } from '../../indexes.js';
|
|
5
|
+
import { checkDocumentFlows, resolveFlowFiles } from '../../flows.js';
|
|
5
6
|
import { checkDocumentRules, printRuleDiff, resolveRuleFiles, } from '../../rules.js';
|
|
6
7
|
import { formatValue, plural } from '../../output.js';
|
|
7
8
|
export const DEFAULT_SCHEMA_FILE = 'jsonpad-schema.json';
|
|
8
9
|
export function defineSyncSchema(command, context) {
|
|
9
10
|
return command
|
|
10
|
-
.description('Create and update lists and
|
|
11
|
+
.description('Create and update lists, indexes and flows to match a schema document')
|
|
11
12
|
.argument('[file]', 'The schema document', DEFAULT_SCHEMA_FILE)
|
|
12
13
|
.option('--dry-run', 'Show what would change, without changing it')
|
|
13
14
|
.option('--allow-rebuild', "Allow changes that rebuild an index in a list with items (the index can't be used until the rebuild finishes)")
|
|
@@ -17,6 +18,7 @@ export function defineSyncSchema(command, context) {
|
|
|
17
18
|
.option('--timeout <seconds>', 'How long --wait waits for each index (default 600)')
|
|
18
19
|
.option('--show-unchanged', "Also list resources that don't change")
|
|
19
20
|
.option('--skip-rule-tests', "Don't compile and test write rules here first (the API always checks them)")
|
|
21
|
+
.option('--skip-flow-tests', "Don't compile and test flows here first (the API always checks them)")
|
|
20
22
|
.option('--json', "Print the API's response as JSON")
|
|
21
23
|
.action((file, options) => syncSchema(context, file, options));
|
|
22
24
|
}
|
|
@@ -30,9 +32,11 @@ function printChange(context, change) {
|
|
|
30
32
|
'no-change': dim('='),
|
|
31
33
|
error: red('!'),
|
|
32
34
|
};
|
|
33
|
-
const name = change.resourceType === '
|
|
34
|
-
? `
|
|
35
|
-
:
|
|
35
|
+
const name = change.resourceType === 'flow'
|
|
36
|
+
? `flow ${bold(change.flow)}`
|
|
37
|
+
: change.resourceType === 'list'
|
|
38
|
+
? `list ${bold(change.list)}`
|
|
39
|
+
: `index ${bold(`${change.list}/${change.index}`)}`;
|
|
36
40
|
const action = change.action === 'no-change' ? 'no change' : change.action;
|
|
37
41
|
const build = change.build
|
|
38
42
|
? dim(` (${change.build.reason === 'created' ? 'build' : 'rebuild'}: ${change.build.items} ${change.build.items === 1 ? 'item' : 'items'}${change.build.requiresConfirmation ? ', needs --allow-rebuild' : ''})`)
|
|
@@ -103,11 +107,15 @@ export async function syncSchema(context, file, options) {
|
|
|
103
107
|
: `Can't read ${file}: ${error.message}`);
|
|
104
108
|
}
|
|
105
109
|
resolveRuleFiles(context, document, file);
|
|
110
|
+
resolveFlowFiles(context, document, file);
|
|
106
111
|
// Rules and their tests are checked here first, so a mistake costs no
|
|
107
112
|
// requests. The API checks them again, and has the final say
|
|
108
113
|
if (!options.skipRuleTests) {
|
|
109
114
|
checkDocumentRules(context, document);
|
|
110
115
|
}
|
|
116
|
+
if (!options.skipFlowTests) {
|
|
117
|
+
await checkDocumentFlows(context, document);
|
|
118
|
+
}
|
|
111
119
|
const jsonpad = context.createClient();
|
|
112
120
|
let result;
|
|
113
121
|
try {
|
package/build/errors.js
CHANGED
|
@@ -9,6 +9,8 @@ export const EXIT_NOT_FOUND = 6;
|
|
|
9
9
|
export const EXIT_PERMISSION_DENIED = 7;
|
|
10
10
|
export const EXIT_LIMIT_REACHED = 8;
|
|
11
11
|
export const EXIT_RULE_TESTS_FAILED = 9;
|
|
12
|
+
export const EXIT_TESTS_FAILED = EXIT_RULE_TESTS_FAILED;
|
|
13
|
+
export const EXIT_FLOW_FAILED = 10;
|
|
12
14
|
export const EXIT_INTERRUPTED = 130;
|
|
13
15
|
/**
|
|
14
16
|
* API errors (403s) that mean a plan limit was reached, rather than that the
|
package/build/flows.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { compile, runTests, version as engineVersion, } from '@basementuniverse/jsonpad-flows';
|
|
3
|
+
import { CliError, EXIT_TESTS_FAILED } from './errors.js';
|
|
4
|
+
import { readJsonFile } from './rules.js';
|
|
5
|
+
export { engineVersion };
|
|
6
|
+
/**
|
|
7
|
+
* Where `flows test` looks for a flow's tests when it isn't told
|
|
8
|
+
*/
|
|
9
|
+
export function defaultFlowTestsFile(flowFile) {
|
|
10
|
+
return flowFile.replace(/(\.flow)?(\.json)?$/, '.tests.json');
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Print a flow's diagnostics as file: path, with the line and column of the
|
|
14
|
+
* problem inside an expression when there is one
|
|
15
|
+
*/
|
|
16
|
+
export function printFlowDiagnostics(context, diagnostics, file) {
|
|
17
|
+
const { red, yellow, dim } = context.colours;
|
|
18
|
+
let errors = 0;
|
|
19
|
+
let warnings = 0;
|
|
20
|
+
for (const diagnostic of diagnostics) {
|
|
21
|
+
const isError = diagnostic.severity === 'error';
|
|
22
|
+
if (isError) {
|
|
23
|
+
errors++;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
warnings++;
|
|
27
|
+
}
|
|
28
|
+
const where = `${file}:${diagnostic.path || '/'}${diagnostic.span
|
|
29
|
+
? `:${diagnostic.span.line}:${diagnostic.span.column}`
|
|
30
|
+
: ''}`;
|
|
31
|
+
context.log(`${dim(where)} ${isError ? red('error') : yellow('warning')} ${diagnostic.message} ${dim(`[${diagnostic.code}]`)}`);
|
|
32
|
+
}
|
|
33
|
+
return { errors, warnings };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Compile a flow document, printing its diagnostics. Throws when it doesn't
|
|
37
|
+
* compile
|
|
38
|
+
*/
|
|
39
|
+
export function compileFlow(context, document, file, options = {}) {
|
|
40
|
+
const { flow, diagnostics } = compile(document);
|
|
41
|
+
const { errors, warnings } = options.quiet
|
|
42
|
+
? {
|
|
43
|
+
errors: diagnostics.filter(d => d.severity === 'error').length,
|
|
44
|
+
warnings: 0,
|
|
45
|
+
}
|
|
46
|
+
: printFlowDiagnostics(context, diagnostics, file);
|
|
47
|
+
if (!flow) {
|
|
48
|
+
throw new CliError(`${file} has ${errors} ${errors === 1 ? 'error' : 'errors'}`);
|
|
49
|
+
}
|
|
50
|
+
if (options.strict && warnings > 0) {
|
|
51
|
+
throw new CliError(`${file} has ${warnings} ${warnings === 1 ? 'warning' : 'warnings'} (--strict)`);
|
|
52
|
+
}
|
|
53
|
+
return flow;
|
|
54
|
+
}
|
|
55
|
+
function printFlowTestResult(context, result, options) {
|
|
56
|
+
const { green, red, dim } = context.colours;
|
|
57
|
+
if (result.passed) {
|
|
58
|
+
context.log(`${green('✓')} ${result.name}`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
context.log(`${red('✗')} ${result.name}`);
|
|
62
|
+
for (const failure of result.failures) {
|
|
63
|
+
context.log(` ${failure}`);
|
|
64
|
+
}
|
|
65
|
+
if (options.trace && result.run?.trace) {
|
|
66
|
+
for (const entry of result.run.trace) {
|
|
67
|
+
const status = entry.status === 'ran'
|
|
68
|
+
? green('ran')
|
|
69
|
+
: entry.status === 'failed'
|
|
70
|
+
? red('failed')
|
|
71
|
+
: dim('skipped');
|
|
72
|
+
context.log(` ${dim(entry.id)} ${entry.type}: ${status}${entry.port ? dim(` → ${entry.port}`) : ''}${entry.error ? ` ${red(entry.error)}` : ''}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Run a flow's tests, printing each one, and throw if any of them fail
|
|
78
|
+
*/
|
|
79
|
+
export async function runFlowTests(context, flow, document, file, options = {}) {
|
|
80
|
+
const run = await runTests(flow, document, {
|
|
81
|
+
filter: options.filter,
|
|
82
|
+
trace: options.trace ? 'values' : 'off',
|
|
83
|
+
});
|
|
84
|
+
if (run.errors.length > 0) {
|
|
85
|
+
throw new CliError([
|
|
86
|
+
`${file} isn't a valid flow test document:`,
|
|
87
|
+
...run.errors.map(error => error.path
|
|
88
|
+
? ` ${error.path}: ${error.message}`
|
|
89
|
+
: ` ${error.message}`),
|
|
90
|
+
].join('\n'));
|
|
91
|
+
}
|
|
92
|
+
if (options.json) {
|
|
93
|
+
context.log(JSON.stringify({
|
|
94
|
+
passed: run.passed,
|
|
95
|
+
failed: run.failed,
|
|
96
|
+
results: run.results.map(result => ({
|
|
97
|
+
name: result.name,
|
|
98
|
+
passed: result.passed,
|
|
99
|
+
failures: result.failures,
|
|
100
|
+
})),
|
|
101
|
+
}, null, 2));
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
for (const result of run.results) {
|
|
105
|
+
printFlowTestResult(context, result, options);
|
|
106
|
+
}
|
|
107
|
+
const { green, red } = context.colours;
|
|
108
|
+
context.log(`\n${run.passed} passed, ${run.failed > 0 ? red(`${run.failed} failed`) : green('0 failed')}`);
|
|
109
|
+
}
|
|
110
|
+
if (run.failed > 0) {
|
|
111
|
+
throw new CliError(`${run.failed} of ${run.results.length} ${run.results.length === 1 ? 'test' : 'tests'} failed`, EXIT_TESTS_FAILED);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Resolve the CLI-only flowFile and flowTestsFile fields in a schema sync
|
|
116
|
+
* document, relative to the document, into the document and tests the API
|
|
117
|
+
* takes
|
|
118
|
+
*/
|
|
119
|
+
export function resolveFlowFiles(context, document, documentFile) {
|
|
120
|
+
const directory = path.dirname(path.resolve(context.cwd, documentFile));
|
|
121
|
+
for (const [name, flow] of Object.entries(document.flows ?? {})) {
|
|
122
|
+
for (const [field, target] of [
|
|
123
|
+
['flowFile', 'document'],
|
|
124
|
+
['flowTestsFile', 'tests'],
|
|
125
|
+
]) {
|
|
126
|
+
const file = flow[field];
|
|
127
|
+
if (file === undefined) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (flow[target] !== undefined) {
|
|
131
|
+
throw new CliError(`Flow "${name}" has both ${field} and ${target}: use one or the other`);
|
|
132
|
+
}
|
|
133
|
+
const resolved = path.relative(context.cwd, path.resolve(directory, file));
|
|
134
|
+
flow[target] = readJsonFile(context, resolved, `${field} for flow "${name}"`);
|
|
135
|
+
delete flow[field];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Compile and test the flows in a schema sync document, before it's sent
|
|
141
|
+
*/
|
|
142
|
+
export async function checkDocumentFlows(context, document) {
|
|
143
|
+
for (const [name, flow] of Object.entries(document.flows ?? {})) {
|
|
144
|
+
if (!flow.document || typeof flow.document !== 'object') {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
const where = `flow "${name}"`;
|
|
148
|
+
const compiled = compileFlow(context, { name, ...flow.document }, where);
|
|
149
|
+
if (flow.tests) {
|
|
150
|
+
await runFlowTests(context, compiled, flow.tests, `the tests for ${where}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
package/build/program.js
CHANGED
|
@@ -9,6 +9,7 @@ import { defineItems } from './commands/items.js';
|
|
|
9
9
|
import { defineListen } from './commands/listen.js';
|
|
10
10
|
import { defineLists } from './commands/lists.js';
|
|
11
11
|
import { defineRebuildIndex } from './commands/rebuild-index.js';
|
|
12
|
+
import { defineFlows } from './commands/flows.js';
|
|
12
13
|
import { defineRules } from './commands/rules.js';
|
|
13
14
|
import { defineExportSchema } from './commands/schema/export.js';
|
|
14
15
|
import { defineMoveLists } from './commands/schema/move.js';
|
|
@@ -46,7 +47,8 @@ export const EXIT_CODES_HELP = `Exit codes:
|
|
|
46
47
|
7 The token isn't allowed to do this, or isn't valid
|
|
47
48
|
8 Rate limited (after retrying), or a plan limit or the monthly quota was
|
|
48
49
|
reached
|
|
49
|
-
9 Write rule tests failed, or a rules check refused a write
|
|
50
|
+
9 Write rule or flow tests failed, or a rules check refused a write
|
|
51
|
+
10 A flow failed (flows run)
|
|
50
52
|
|
|
51
53
|
The schema commands and rebuild-index (and indexes rebuild) exit with 1 for
|
|
52
54
|
every API error, as they did in @basementuniverse/jsonpad-sdk.`;
|
|
@@ -103,6 +105,7 @@ export function createProgram(context) {
|
|
|
103
105
|
defineItems(program.command('items'), context);
|
|
104
106
|
defineIdentities(program.command('identities'), context);
|
|
105
107
|
defineRules(program.command('rules'), context);
|
|
108
|
+
defineFlows(program.command('flows'), context);
|
|
106
109
|
defineWhoami(program.command('whoami'), context);
|
|
107
110
|
defineConfig(program.command('config'), context);
|
|
108
111
|
defineListen(program.command('listen'), context);
|
package/build/sdk.js
CHANGED
|
@@ -2,4 +2,4 @@ import sdk from '@basementuniverse/jsonpad-sdk';
|
|
|
2
2
|
// The SDK is a UMD build, which Node loads as CommonJS, so an ES module only
|
|
3
3
|
// sees its exports object as the default import
|
|
4
4
|
export const JSONPad = sdk.default;
|
|
5
|
-
export const { IndexBuildError, JSONPadError, WriteRuleError } = sdk;
|
|
5
|
+
export const { FlowError, IndexBuildError, JSONPadError, WriteRuleError } = sdk;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basementuniverse/jsonpad-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "Command line tool for JSONPad",
|
|
5
5
|
"author": "Gordon Larrigan <gordonlarrigan@gmail.com> (https://gordonlarrigan.com)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -38,9 +38,10 @@
|
|
|
38
38
|
"reference": "node scripts/generate-reference.ts"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
+
"@basementuniverse/jsonpad-flows": "1.0.0",
|
|
41
42
|
"@basementuniverse/jsonpad-realtime-sdk": "^1.6.0",
|
|
42
|
-
"@basementuniverse/jsonpad-rules": "^1.
|
|
43
|
-
"@basementuniverse/jsonpad-sdk": "^2.
|
|
43
|
+
"@basementuniverse/jsonpad-rules": "^1.2.0",
|
|
44
|
+
"@basementuniverse/jsonpad-sdk": "^2.3.0",
|
|
44
45
|
"commander": "^15.0.0"
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|