@shware/workflow-ui 2.1.16 → 2.2.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/README.md +17 -0
- package/dist/cli.mjs +84 -1
- package/dist/cli.mjs.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/cli.ts +32 -0
- package/src/plan.ts +116 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shware/workflow-ui",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/ShwareHQ/shware-sdk.git"
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"@floating-ui/react": "^0.27.20",
|
|
41
41
|
"@fontsource-variable/inter": "^5.3.0",
|
|
42
42
|
"@react-email/render": "^2.1.1",
|
|
43
|
-
"@shware/workflow": "
|
|
43
|
+
"@shware/workflow": "10.0.0",
|
|
44
44
|
"@tailwindcss/vite": "^4.3.3",
|
|
45
45
|
"@tanstack/react-query": "^5.104.5",
|
|
46
46
|
"@tanstack/react-router": "^1.170.46",
|
package/src/cli.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { generateSchema } from './generate';
|
|
3
|
+
import { formatPlan, planDeploy } from './plan';
|
|
3
4
|
import { startStudio } from './server';
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -9,6 +10,7 @@ import { startStudio } from './server';
|
|
|
9
10
|
* npx workflow-ui --port 5000
|
|
10
11
|
* npx workflow-ui --config apps/marketing/workflow.config.ts
|
|
11
12
|
* npx workflow-ui generate --out ../../packages/db/src/schema/workflow.ts [--check]
|
|
13
|
+
* npx workflow-ui plan --database-url $DATABASE_URL
|
|
12
14
|
*/
|
|
13
15
|
|
|
14
16
|
interface Args {
|
|
@@ -16,6 +18,7 @@ interface Args {
|
|
|
16
18
|
config?: string;
|
|
17
19
|
stats?: string;
|
|
18
20
|
out?: string;
|
|
21
|
+
databaseUrl?: string;
|
|
19
22
|
check: boolean;
|
|
20
23
|
open: boolean;
|
|
21
24
|
help: boolean;
|
|
@@ -29,6 +32,7 @@ function parseArgs(argv: string[]): Args {
|
|
|
29
32
|
else if (arg === '--open') args.open = true;
|
|
30
33
|
else if (arg === '--check') args.check = true;
|
|
31
34
|
else if (arg === '--out' || arg === '-o') args.out = argv[++i];
|
|
35
|
+
else if (arg === '--database-url' || arg === '-d') args.databaseUrl = argv[++i];
|
|
32
36
|
else if (arg === '--port' || arg === '-p') args.port = Number(argv[++i]);
|
|
33
37
|
else if (arg === '--config' || arg === '-c') args.config = argv[++i];
|
|
34
38
|
else if (arg === '--stats' || arg === '-s') args.stats = argv[++i];
|
|
@@ -42,6 +46,7 @@ const HELP = `
|
|
|
42
46
|
Usage
|
|
43
47
|
$ workflow-ui [dev] [options]
|
|
44
48
|
$ workflow-ui generate --out <file> [--check] [--config <path>]
|
|
49
|
+
$ workflow-ui plan --database-url <url> [--config <path>]
|
|
45
50
|
|
|
46
51
|
Options
|
|
47
52
|
-p, --port <port> Port to listen on (default 4321)
|
|
@@ -50,6 +55,8 @@ const HELP = `
|
|
|
50
55
|
--open Open the browser on start
|
|
51
56
|
-o, --out <file> generate: where the drizzle schema file goes
|
|
52
57
|
--check generate: fail when the file is out of date, write nothing
|
|
58
|
+
-d, --database-url plan: the engine's database (default $DATABASE_URL); exits 1 when the
|
|
59
|
+
bundle would break journeys still running there
|
|
53
60
|
-h, --help Show this message
|
|
54
61
|
`;
|
|
55
62
|
|
|
@@ -83,6 +90,31 @@ async function main(): Promise<void> {
|
|
|
83
90
|
return;
|
|
84
91
|
}
|
|
85
92
|
|
|
93
|
+
if (command === 'plan') {
|
|
94
|
+
const args = parseArgs(rest);
|
|
95
|
+
const databaseUrl = args.databaseUrl ?? process.env.DATABASE_URL;
|
|
96
|
+
if (args.help || databaseUrl === undefined) {
|
|
97
|
+
console.log(HELP);
|
|
98
|
+
if (!args.help) {
|
|
99
|
+
console.error('\n plan needs --database-url <url> (or DATABASE_URL in the environment)\n');
|
|
100
|
+
process.exitCode = 1;
|
|
101
|
+
}
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const report = await planDeploy({
|
|
106
|
+
databaseUrl,
|
|
107
|
+
...(args.config !== undefined ? { config: args.config } : {}),
|
|
108
|
+
});
|
|
109
|
+
console.log(`\n ${formatPlan(report).join('\n ')}\n`);
|
|
110
|
+
if (report.problems.length > 0) process.exitCode = 1;
|
|
111
|
+
} catch (error) {
|
|
112
|
+
console.error(`\n${error instanceof Error ? error.message : String(error)}\n`);
|
|
113
|
+
process.exitCode = 1;
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
86
118
|
// `dev` is accepted so the command reads well; the studio is the default mode
|
|
87
119
|
const argv = process.argv.slice(2).filter((arg) => arg !== 'dev');
|
|
88
120
|
const args = parseArgs(argv);
|
package/src/plan.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { relative } from 'node:path';
|
|
2
|
+
import {
|
|
3
|
+
type InFlightProblem,
|
|
4
|
+
type JourneyConfig,
|
|
5
|
+
type PinnedWorkflow,
|
|
6
|
+
checkInFlight,
|
|
7
|
+
compileConfig,
|
|
8
|
+
describeInFlight,
|
|
9
|
+
isConfig,
|
|
10
|
+
pinnedWorkflows,
|
|
11
|
+
} from '@shware/workflow';
|
|
12
|
+
import { postgresJsStore } from '@shware/workflow/postgres-js';
|
|
13
|
+
import { runnerImport } from 'vite';
|
|
14
|
+
import { findConfig } from './server';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `workflow-ui plan` — the deploy-time check, before the deploy: compiles the project and holds
|
|
18
|
+
* the bundle, with the templates this code would serve, against the workflow versions journeys
|
|
19
|
+
* are still running on in the database (`checkInFlight` in `@shware/workflow`). On Cloudflare a
|
|
20
|
+
* new version's code serves the moment it is published, so the host's own refusal comes too late
|
|
21
|
+
* to protect a running journey from a template change; CI runs this ahead of `wrangler deploy`
|
|
22
|
+
* and stops on a non-zero exit.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export interface PlanOptions {
|
|
26
|
+
cwd?: string;
|
|
27
|
+
config?: string;
|
|
28
|
+
/** The engine's database, as `postgres` connects to it. */
|
|
29
|
+
databaseUrl: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface PlanReport {
|
|
33
|
+
/**
|
|
34
|
+
* Every workflow version with running journeys; `carried` when this bundle still contains that
|
|
35
|
+
* exact version (otherwise those journeys finish on theirs, and new ones enter the new version).
|
|
36
|
+
*/
|
|
37
|
+
pinned: (PinnedWorkflow & { carried: boolean })[];
|
|
38
|
+
/** What this bundle would take from them; empty when the deploy is safe. */
|
|
39
|
+
problems: InFlightProblem[];
|
|
40
|
+
/** What the database lacks against the SDK's tables: migrations to run before the deploy. */
|
|
41
|
+
schema: string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function planDeploy(options: PlanOptions): Promise<PlanReport> {
|
|
45
|
+
const cwd = options.cwd ?? process.cwd();
|
|
46
|
+
const configPath = findConfig(cwd, options.config);
|
|
47
|
+
const { module } = await runnerImport<{ default?: unknown }>(configPath);
|
|
48
|
+
if (!isConfig(module.default)) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`workflow-ui: ${relative(cwd, configPath)} must \`export default defineConfig({...})\``
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const config: JourneyConfig = module.default;
|
|
54
|
+
const bundle = compileConfig(config);
|
|
55
|
+
const schemaName = config.database?.schema;
|
|
56
|
+
const { store, close } = postgresJsStore(
|
|
57
|
+
options.databaseUrl,
|
|
58
|
+
schemaName === undefined ? {} : { schema: schemaName }
|
|
59
|
+
);
|
|
60
|
+
try {
|
|
61
|
+
const schema = await store.checkSchema();
|
|
62
|
+
// Without the tables there are no journeys to keep; with columns missing, the pins still read
|
|
63
|
+
const pinned = schema.some((problem) => problem.startsWith('table '))
|
|
64
|
+
? []
|
|
65
|
+
: await pinnedWorkflows(store);
|
|
66
|
+
const carried = new Set(bundle.workflows.map((w) => w.contentHash));
|
|
67
|
+
return {
|
|
68
|
+
pinned: pinned.map((pin) => ({
|
|
69
|
+
workflow: pin.workflow,
|
|
70
|
+
contentHash: pin.contentHash,
|
|
71
|
+
running: pin.running,
|
|
72
|
+
ir: pin.ir,
|
|
73
|
+
carried: carried.has(pin.contentHash),
|
|
74
|
+
})),
|
|
75
|
+
problems: checkInFlight(pinned, bundle, config),
|
|
76
|
+
schema,
|
|
77
|
+
};
|
|
78
|
+
} finally {
|
|
79
|
+
await close();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const plural = (n: number, word: string) => `${n} ${word}${n === 1 ? '' : 's'}`;
|
|
84
|
+
|
|
85
|
+
/** The report as the lines the CLI prints. */
|
|
86
|
+
export function formatPlan(report: PlanReport): string[] {
|
|
87
|
+
const lines: string[] = [];
|
|
88
|
+
const journeys = report.pinned.reduce((sum, pin) => sum + pin.running, 0);
|
|
89
|
+
if (report.pinned.length === 0) {
|
|
90
|
+
lines.push('no journey is running: nothing is pinned');
|
|
91
|
+
} else {
|
|
92
|
+
lines.push(
|
|
93
|
+
`${plural(report.pinned.length, 'pinned version')}, ${plural(journeys, 'running journey')}:`
|
|
94
|
+
);
|
|
95
|
+
for (const pin of report.pinned) {
|
|
96
|
+
const note = pin.carried
|
|
97
|
+
? ''
|
|
98
|
+
: ' (finish on their version; this bundle changes the workflow)';
|
|
99
|
+
lines.push(` ${pin.workflow}@${pin.contentHash.slice(0, 8)} ${pin.running} running${note}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (report.schema.length > 0) {
|
|
103
|
+
lines.push('the database lacks (run the migrations before deploying):');
|
|
104
|
+
for (const problem of report.schema) lines.push(` ${problem}`);
|
|
105
|
+
}
|
|
106
|
+
if (report.problems.length === 0) {
|
|
107
|
+
lines.push('this bundle keeps every running journey: safe to deploy');
|
|
108
|
+
} else {
|
|
109
|
+
lines.push('this bundle would break running journeys:');
|
|
110
|
+
for (const line of describeInFlight(report.problems)) lines.push(` ${line}`);
|
|
111
|
+
lines.push(
|
|
112
|
+
'fix the bundle (keep the segment or action, give the template a new id, make the prop optional), or deploy with POST /bundle/deploy?force=1 to fail them'
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
return lines;
|
|
116
|
+
}
|