@nanobpm/urban 0.17.1 → 0.19.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/dist/cli.js +131 -5
- package/dist/runtime/adapters/node.js +61 -1
- package/dist/runtime/connector-worker-sdk.d.ts +39 -0
- package/dist/runtime/connector-worker-sdk.js +40 -0
- package/dist/runtime/core/host.d.ts +30 -0
- package/dist/runtime/core/host.js +26 -1
- package/dist/runtime/core/modules/connectors.d.ts +53 -0
- package/dist/runtime/core/modules/connectors.js +204 -0
- package/dist/runtime/core/modules/datasource.js +20 -5
- package/dist/runtime/core/modules/workers.js +5 -0
- package/dist/runtime/core/runtime.js +7 -0
- package/dist/runtime/core/validate.js +34 -8
- package/dist/runtime/engine/nanosdk.d.ts +7 -0
- package/dist/runtime/engine/nanosdk.js +27 -0
- package/dist/runtime/index.d.ts +3 -0
- package/dist/runtime/index.js +5 -0
- package/dist/toolkit/addConnector.d.ts +29 -0
- package/dist/toolkit/addConnector.js +170 -0
- package/dist/toolkit/fsio.js +31 -2
- package/dist/toolkit/gen.d.ts +28 -0
- package/dist/toolkit/gen.js +55 -12
- package/dist/toolkit/index.d.ts +5 -1
- package/dist/toolkit/index.js +4 -1
- package/dist/toolkit/models.d.ts +63 -0
- package/dist/toolkit/models.js +142 -0
- package/package.json +2 -2
- package/src/cli.ts +128 -5
- package/src/runtime/adapters/node.ts +64 -1
- package/src/runtime/connector-worker-sdk.test.ts +56 -0
- package/src/runtime/connector-worker-sdk.ts +78 -0
- package/src/runtime/core/host.ts +40 -0
- package/src/runtime/core/modules/connectors.test.ts +206 -0
- package/src/runtime/core/modules/connectors.ts +269 -0
- package/src/runtime/core/modules/datasource.migrations.test.ts +114 -0
- package/src/runtime/core/modules/datasource.ts +24 -5
- package/src/runtime/core/modules/workers.test.ts +13 -0
- package/src/runtime/core/modules/workers.ts +4 -0
- package/src/runtime/core/runtime.ts +6 -0
- package/src/runtime/core/validate.test.ts +24 -0
- package/src/runtime/core/validate.ts +32 -8
- package/src/runtime/engine/nanosdk.test.ts +76 -0
- package/src/runtime/engine/nanosdk.ts +30 -0
- package/src/runtime/index.ts +12 -0
- package/src/toolkit/addConnector.test.ts +161 -0
- package/src/toolkit/addConnector.ts +233 -0
- package/src/toolkit/fsio.test.ts +23 -0
- package/src/toolkit/fsio.ts +22 -2
- package/src/toolkit/gen.test.ts +76 -1
- package/src/toolkit/gen.ts +81 -13
- package/src/toolkit/index.ts +16 -1
- package/src/toolkit/models.test.ts +159 -0
- package/src/toolkit/models.ts +193 -0
package/src/toolkit/gen.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { deriveDomain } from "./derivers/domain.ts";
|
|
|
10
10
|
import { deriveWorkerBindings, type ModelSource } from "./derivers/worker-io.ts";
|
|
11
11
|
import { deriveMeta } from "./derivers/meta.ts";
|
|
12
12
|
import { deriveMessageBindings } from "./derivers/messages.ts";
|
|
13
|
+
import { deriveModels, type DerivedModels, type ModelError, MODEL_PROVENANCE } from "./models.ts";
|
|
13
14
|
|
|
14
15
|
/** Minimal filesystem port. Node/Deno impls live in `fsio.ts`. */
|
|
15
16
|
export interface GenIO {
|
|
@@ -18,18 +19,40 @@ export interface GenIO {
|
|
|
18
19
|
/** File names (not paths) in a directory; empty if it does not exist. */
|
|
19
20
|
listDir(path: string): Promise<string[]>;
|
|
20
21
|
exists(path: string): Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Import an app module and return its exports. Optional: only present when the caller runs on
|
|
24
|
+
* a runtime that can load the app's TypeScript (Node with type-stripping, or Deno). Code-first
|
|
25
|
+
* model derivation (`workflows/*.ts` → BPMN) needs it; when absent, gen still derives everything
|
|
26
|
+
* else (migrations, domain, worker I/O from authored/derived models).
|
|
27
|
+
*/
|
|
28
|
+
importModule?(path: string): Promise<Record<string, unknown>>;
|
|
29
|
+
/** Delete a file. Optional: enables the provenance-scoped stale-model sweep (skipped when absent). */
|
|
30
|
+
remove?(path: string): Promise<void>;
|
|
21
31
|
}
|
|
22
32
|
|
|
23
33
|
export interface GenOptions {
|
|
24
34
|
root: string;
|
|
25
35
|
io: GenIO;
|
|
26
36
|
manifestFile?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Whether to emit (write) derived `.bpmn` models. Default true. When false, code-first models
|
|
39
|
+
* are still derived *in-memory* to feed the type-contract derivers (worker I/O, meta, messages)
|
|
40
|
+
* — they are just not written to disk or swept. This is the `urban gen --no-models` path, so
|
|
41
|
+
* the console's type-contract regen never mutates `processes/*.bpmn`.
|
|
42
|
+
*/
|
|
43
|
+
emitModels?: boolean;
|
|
44
|
+
/** Derive ONLY the models (skip migrations/domain/worker-I/O). The `urban derive` path. */
|
|
45
|
+
modelsOnly?: boolean;
|
|
27
46
|
}
|
|
28
47
|
|
|
29
48
|
export interface GenResult {
|
|
30
49
|
artifacts: DerivedArtifact[];
|
|
31
50
|
/** Paths that differ from disk (only populated by `check`). */
|
|
32
51
|
drift: string[];
|
|
52
|
+
/** True if any code-first workflow failed to import/derive. */
|
|
53
|
+
incomplete: boolean;
|
|
54
|
+
/** Per-file model-derivation errors (empty on success). */
|
|
55
|
+
modelErrors: ModelError[];
|
|
33
56
|
}
|
|
34
57
|
|
|
35
58
|
function join(root: string, rel: string): string {
|
|
@@ -44,7 +67,7 @@ function dirOf(p: string): string {
|
|
|
44
67
|
}
|
|
45
68
|
|
|
46
69
|
/** Resolve a `dir/*.ext` (or literal) manifest pattern to file paths relative to root. */
|
|
47
|
-
async function expandPattern(root: string, io: GenIO, pattern: string): Promise<string[]> {
|
|
70
|
+
export async function expandPattern(root: string, io: GenIO, pattern: string): Promise<string[]> {
|
|
48
71
|
const star = pattern.indexOf("*");
|
|
49
72
|
if (star === -1) {
|
|
50
73
|
return (await io.exists(join(root, pattern))) ? [pattern] : [];
|
|
@@ -78,36 +101,55 @@ export async function readModels(
|
|
|
78
101
|
|
|
79
102
|
/** Collect all artifacts a run would produce, without touching disk beyond reads. */
|
|
80
103
|
export async function collectArtifacts(opts: GenOptions): Promise<DerivedArtifact[]> {
|
|
104
|
+
return (await collectAll(opts)).artifacts;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Internal: collect artifacts AND the model-derivation details (needed by `runGen` for the
|
|
108
|
+
* incomplete signal + the provenance-scoped stale sweep). */
|
|
109
|
+
async function collectAll(opts: GenOptions): Promise<{ artifacts: DerivedArtifact[]; derived: DerivedModels }> {
|
|
81
110
|
const { root, io } = opts;
|
|
111
|
+
const emitModels = opts.emitModels ?? true;
|
|
82
112
|
const manifestPath = join(root, opts.manifestFile ?? "nano.app.json");
|
|
83
113
|
const manifest = JSON.parse(await io.readText(manifestPath)) as ToolkitManifest & {
|
|
84
|
-
models?: { processes?: string[] };
|
|
114
|
+
models?: { processes?: string[]; workflows?: string[] };
|
|
85
115
|
};
|
|
86
116
|
|
|
87
117
|
const artifacts: DerivedArtifact[] = [];
|
|
88
118
|
|
|
89
119
|
// 1. types → migrations + domain row types (DomainTables spine + DomainTypes registry)
|
|
90
|
-
if (manifest.types && Object.keys(manifest.types).length > 0) {
|
|
120
|
+
if (!opts.modelsOnly && manifest.types && Object.keys(manifest.types).length > 0) {
|
|
91
121
|
artifacts.push(...deriveMigrations(manifest));
|
|
92
122
|
artifacts.push(...deriveDomain(manifest));
|
|
93
123
|
}
|
|
94
124
|
|
|
95
|
-
// 2. models →
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
125
|
+
// 2. code-first models → derive BPMN from `workflows/*.ts` (executes the app's TS). The derived
|
|
126
|
+
// `.bpmn` are emitted only when `emitModels` (default), but always fed to the type-contract
|
|
127
|
+
// derivers below so worker I/O works for a code-first app even on a `--no-models` run.
|
|
128
|
+
const derived = await deriveModels(root, io, manifest);
|
|
129
|
+
if (emitModels) artifacts.push(...derived.artifacts);
|
|
130
|
+
|
|
131
|
+
// 3. models (authored on-disk + derived) → worker I/O index + meta accessor + message map.
|
|
132
|
+
// Drop any on-disk model whose path a derived model already covers, to avoid double-counting.
|
|
133
|
+
if (!opts.modelsOnly) {
|
|
134
|
+
const derivedPaths = new Set(derived.artifacts.map((a) => a.path));
|
|
135
|
+
const diskModels = (await readModels(root, io, manifest)).filter((m) => !derivedPaths.has(m.path));
|
|
136
|
+
const models = [...diskModels, ...derived.models];
|
|
137
|
+
if (models.length > 0) {
|
|
138
|
+
const declaredTypeIds = Object.keys(manifest.types ?? {});
|
|
139
|
+
artifacts.push(...deriveWorkerBindings(models, declaredTypeIds));
|
|
140
|
+
artifacts.push(...deriveMeta(models));
|
|
141
|
+
artifacts.push(...deriveMessageBindings(models, declaredTypeIds));
|
|
142
|
+
}
|
|
102
143
|
}
|
|
103
144
|
|
|
104
|
-
return sortArtifacts(artifacts);
|
|
145
|
+
return { artifacts: sortArtifacts(artifacts), derived };
|
|
105
146
|
}
|
|
106
147
|
|
|
107
148
|
/** Run the derivers and write artifacts (or, with `check`, report drift without writing). */
|
|
108
149
|
export async function runGen(opts: GenOptions & { check?: boolean }): Promise<GenResult> {
|
|
109
|
-
const artifacts = await
|
|
150
|
+
const { artifacts, derived } = await collectAll(opts);
|
|
110
151
|
const { root, io } = opts;
|
|
152
|
+
const emitModels = opts.emitModels ?? true;
|
|
111
153
|
const drift: string[] = [];
|
|
112
154
|
|
|
113
155
|
for (const a of artifacts) {
|
|
@@ -119,7 +161,33 @@ export async function runGen(opts: GenOptions & { check?: boolean }): Promise<Ge
|
|
|
119
161
|
await io.writeText(abs, a.content);
|
|
120
162
|
}
|
|
121
163
|
}
|
|
122
|
-
|
|
164
|
+
|
|
165
|
+
// Provenance-scoped stale sweep: delete derived `.bpmn` for flows that no longer exist. Only
|
|
166
|
+
// in write mode, only when we emitted models, only on a complete derivation (never delete when
|
|
167
|
+
// a workflow failed to load), and only files bearing our marker (authored models are untouched).
|
|
168
|
+
if (!opts.check && emitModels && derived.attempted && !derived.incomplete && io.remove) {
|
|
169
|
+
const keep = new Set(derived.artifacts.map((a) => a.path));
|
|
170
|
+
for (const name of await io.listDir(join(root, derived.outDir))) {
|
|
171
|
+
if (!name.endsWith(".bpmn")) continue;
|
|
172
|
+
const rel = `${derived.outDir}/${name}`;
|
|
173
|
+
if (keep.has(rel)) continue;
|
|
174
|
+
const content = await io.readText(join(root, rel));
|
|
175
|
+
if (content.includes(MODEL_PROVENANCE)) await io.remove(join(root, rel));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return { artifacts, drift, incomplete: derived.incomplete, modelErrors: derived.errors };
|
|
123
180
|
}
|
|
124
181
|
|
|
125
182
|
export { dirOf, join as joinPath };
|
|
183
|
+
|
|
184
|
+
/** Derive code-first models in-memory (no writes) — backs `urban derive --stdout`, the
|
|
185
|
+
* non-mutating preview the console's read-only model viewer uses. */
|
|
186
|
+
export async function previewModels(opts: GenOptions): Promise<DerivedModels> {
|
|
187
|
+
const { root, io } = opts;
|
|
188
|
+
const manifestPath = join(root, opts.manifestFile ?? "nano.app.json");
|
|
189
|
+
const manifest = JSON.parse(await io.readText(manifestPath)) as {
|
|
190
|
+
models?: { processes?: string[]; workflows?: string[] };
|
|
191
|
+
};
|
|
192
|
+
return deriveModels(root, io, manifest);
|
|
193
|
+
}
|
package/src/toolkit/index.ts
CHANGED
|
@@ -83,10 +83,22 @@ import { messagesDeriver } from "./derivers/messages.ts";
|
|
|
83
83
|
export const DERIVERS = [migrationsDeriver, domainDeriver, workerIoDeriver, metaDeriver, messagesDeriver] as const;
|
|
84
84
|
|
|
85
85
|
// Gen orchestrator + IO
|
|
86
|
-
export { collectArtifacts, runGen, joinPath, readModels } from "./gen.ts";
|
|
86
|
+
export { collectArtifacts, runGen, joinPath, readModels, expandPattern, previewModels } from "./gen.ts";
|
|
87
87
|
export type { GenIO, GenOptions, GenResult } from "./gen.ts";
|
|
88
88
|
export { createNodeGenIO } from "./fsio.ts";
|
|
89
89
|
|
|
90
|
+
// Code-first model derivation (workflows/*.ts → BPMN)
|
|
91
|
+
export {
|
|
92
|
+
deriveModels,
|
|
93
|
+
isWorkflow,
|
|
94
|
+
bpmnFilename,
|
|
95
|
+
processesOutDir,
|
|
96
|
+
DEFAULT_WORKFLOW_PATTERNS,
|
|
97
|
+
PROCESSES_DIR,
|
|
98
|
+
MODEL_PROVENANCE,
|
|
99
|
+
} from "./models.ts";
|
|
100
|
+
export type { DerivedModels, DerivedModelInfo, ModelError, ModelsManifest } from "./models.ts";
|
|
101
|
+
|
|
90
102
|
// Worker-stub scaffolder (ADR 0056): write-once handler stubs from the model.
|
|
91
103
|
export {
|
|
92
104
|
planWorkerScaffold,
|
|
@@ -103,3 +115,6 @@ export type {
|
|
|
103
115
|
} from "./scaffold/workers.ts";
|
|
104
116
|
export { scaffoldWorkers } from "./scaffold.ts";
|
|
105
117
|
export type { ScaffoldOptions, ScaffoldRun, StubOutcome, StubStatus } from "./scaffold.ts";
|
|
118
|
+
|
|
119
|
+
export { addConnector } from "./addConnector.ts";
|
|
120
|
+
export type { AddConnectorOptions, AddConnectorResult } from "./addConnector.ts";
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { defineFlow, envelope } from "@nanobpm/workflow";
|
|
4
|
+
import { deriveModels, isWorkflow, bpmnFilename, processesOutDir, MODEL_PROVENANCE } from "./models.ts";
|
|
5
|
+
import type { GenIO } from "./gen.ts";
|
|
6
|
+
|
|
7
|
+
/** In-memory GenIO whose `importModule` returns pre-registered module exports by path. */
|
|
8
|
+
function memIO(
|
|
9
|
+
files: Record<string, string>,
|
|
10
|
+
modules: Record<string, Record<string, unknown>> = {},
|
|
11
|
+
): GenIO {
|
|
12
|
+
return {
|
|
13
|
+
async readText(p) {
|
|
14
|
+
if (!(p in files)) throw new Error(`ENOENT ${p}`);
|
|
15
|
+
return files[p];
|
|
16
|
+
},
|
|
17
|
+
async writeText(p, c) {
|
|
18
|
+
files[p] = c;
|
|
19
|
+
},
|
|
20
|
+
async listDir(p) {
|
|
21
|
+
const prefix = p.replace(/\/+$/, "") + "/";
|
|
22
|
+
const names = new Set<string>();
|
|
23
|
+
for (const f of Object.keys(files)) {
|
|
24
|
+
if (f.startsWith(prefix)) {
|
|
25
|
+
const rest = f.slice(prefix.length);
|
|
26
|
+
if (!rest.includes("/")) names.add(rest);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return [...names];
|
|
30
|
+
},
|
|
31
|
+
async exists(p) {
|
|
32
|
+
return p in files || p in modules;
|
|
33
|
+
},
|
|
34
|
+
importModule(p) {
|
|
35
|
+
const key = Object.keys(modules).find((k) => p.endsWith(k));
|
|
36
|
+
if (!key) return Promise.reject(new Error(`ENOENT ${p}`));
|
|
37
|
+
return Promise.resolve(modules[key]);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const GreetIn = envelope("GreetIn", { who: "string" });
|
|
43
|
+
const GreetOut = envelope("GreetOut", { message: "string" });
|
|
44
|
+
|
|
45
|
+
/** A real one-step declarative flow (exercises the actual toBpmn + layout pipeline). */
|
|
46
|
+
const greet = defineFlow("greet", { hello: { in: GreetIn, out: GreetOut } }, (w) => {
|
|
47
|
+
w.run("hello", async () => ({ message: "hi" }));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const CODE_FIRST_MANIFEST = { models: { workflows: ["workflows/*.ts"] } };
|
|
51
|
+
|
|
52
|
+
test("isWorkflow recognizes defineFlow exports, rejects everything else", () => {
|
|
53
|
+
assert.equal(isWorkflow(greet), true);
|
|
54
|
+
assert.equal(isWorkflow({ id: "x", kind: "imperative" }), true);
|
|
55
|
+
assert.equal(isWorkflow({ id: "x", kind: "nope" }), false);
|
|
56
|
+
assert.equal(isWorkflow({ kind: "declarative" }), false);
|
|
57
|
+
assert.equal(isWorkflow(null), false);
|
|
58
|
+
assert.equal(isWorkflow("greet"), false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("bpmnFilename sanitizes ids into safe .bpmn filenames", () => {
|
|
62
|
+
assert.equal(bpmnFilename("greet"), "greet.bpmn");
|
|
63
|
+
assert.equal(bpmnFilename("orders/place"), "orders-place.bpmn");
|
|
64
|
+
assert.equal(bpmnFilename("a b:c"), "a-b-c.bpmn");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("processesOutDir defaults to processes/, or the dir of a models.processes glob", () => {
|
|
68
|
+
assert.equal(processesOutDir({}), "processes");
|
|
69
|
+
assert.equal(processesOutDir({ models: { processes: ["resources/processes/*.bpmn"] } }), "resources/processes");
|
|
70
|
+
assert.equal(processesOutDir({ models: { processes: ["src/models/*.bpmn"] } }), "src/models");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("deriveModels turns workflows/*.ts into a provenance-stamped processes/<id>.bpmn", async () => {
|
|
74
|
+
const io = memIO(
|
|
75
|
+
{ "/app/workflows/greet.ts": "// source" },
|
|
76
|
+
{ "workflows/greet.ts": { greet } },
|
|
77
|
+
);
|
|
78
|
+
const d = await deriveModels("/app", io, CODE_FIRST_MANIFEST);
|
|
79
|
+
|
|
80
|
+
assert.equal(d.attempted, true);
|
|
81
|
+
assert.equal(d.incomplete, false);
|
|
82
|
+
assert.deepEqual(d.ids, ["greet"]);
|
|
83
|
+
assert.equal(d.artifacts.length, 1);
|
|
84
|
+
assert.equal(d.artifacts[0].path, "processes/greet.bpmn");
|
|
85
|
+
assert.match(d.artifacts[0].content, /<!--.*@nanobpm\/urban/);
|
|
86
|
+
assert.ok(d.artifacts[0].content.includes(MODEL_PROVENANCE));
|
|
87
|
+
assert.match(d.artifacts[0].content, /greet/);
|
|
88
|
+
// the in-memory ModelSource is fed to the type-contract derivers
|
|
89
|
+
assert.deepEqual(d.models.map((m) => m.path), ["processes/greet.bpmn"]);
|
|
90
|
+
assert.equal(d.list[0].id, "greet");
|
|
91
|
+
assert.equal(d.list[0].kind, "declarative");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("deriveModels is a no-op without a module loader (pure/IDE callers)", async () => {
|
|
95
|
+
const io = memIO({ "/app/workflows/greet.ts": "// source" });
|
|
96
|
+
delete (io as { importModule?: unknown }).importModule;
|
|
97
|
+
const d = await deriveModels("/app", io, CODE_FIRST_MANIFEST);
|
|
98
|
+
assert.equal(d.attempted, false);
|
|
99
|
+
assert.equal(d.artifacts.length, 0);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("deriveModels writes into the manifest's models.processes dir when declared", async () => {
|
|
103
|
+
const io = memIO(
|
|
104
|
+
{ "/app/workflows/greet.ts": "// source" },
|
|
105
|
+
{ "workflows/greet.ts": { greet } },
|
|
106
|
+
);
|
|
107
|
+
const d = await deriveModels("/app", io, {
|
|
108
|
+
models: { workflows: ["workflows/*.ts"], processes: ["resources/processes/*.bpmn"] },
|
|
109
|
+
});
|
|
110
|
+
assert.equal(d.outDir, "resources/processes");
|
|
111
|
+
assert.equal(d.artifacts[0].path, "resources/processes/greet.bpmn");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("deriveModels records a failing workflow as incomplete without throwing", async () => {
|
|
115
|
+
const io = memIO(
|
|
116
|
+
{ "/app/workflows/bad.ts": "// source" },
|
|
117
|
+
{}, // no module registered → importModule rejects
|
|
118
|
+
);
|
|
119
|
+
const d = await deriveModels("/app", io, CODE_FIRST_MANIFEST);
|
|
120
|
+
assert.equal(d.attempted, true);
|
|
121
|
+
assert.equal(d.incomplete, true);
|
|
122
|
+
assert.equal(d.errors.length, 1);
|
|
123
|
+
assert.equal(d.errors[0].path, "workflows/bad.ts");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("deriveModels flags a duplicate flow id across two files", async () => {
|
|
127
|
+
const io = memIO(
|
|
128
|
+
{ "/app/workflows/a.ts": "// a", "/app/workflows/b.ts": "// b" },
|
|
129
|
+
{ "workflows/a.ts": { greet }, "workflows/b.ts": { greet } },
|
|
130
|
+
);
|
|
131
|
+
const d = await deriveModels("/app", io, CODE_FIRST_MANIFEST);
|
|
132
|
+
assert.equal(d.incomplete, true);
|
|
133
|
+
assert.ok(d.errors.some((e) => /duplicate flow id "greet"/.test(e.message)));
|
|
134
|
+
assert.equal(d.ids.length, 1); // first wins
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("deriveModels flags two distinct flows sharing an id in the same module", async () => {
|
|
138
|
+
// two SEPARATE defineFlow objects, same id, same file — a real collision, not a re-export.
|
|
139
|
+
const greetA = defineFlow("greet", { hello: { in: GreetIn, out: GreetOut } }, (w) => {
|
|
140
|
+
w.run("hello", async () => ({ message: "a" }));
|
|
141
|
+
});
|
|
142
|
+
const greetB = defineFlow("greet", { hello: { in: GreetIn, out: GreetOut } }, (w) => {
|
|
143
|
+
w.run("hello", async () => ({ message: "b" }));
|
|
144
|
+
});
|
|
145
|
+
const io = memIO({ "/app/workflows/dup.ts": "// dup" }, { "workflows/dup.ts": { greetA, greetB } });
|
|
146
|
+
const d = await deriveModels("/app", io, CODE_FIRST_MANIFEST);
|
|
147
|
+
assert.equal(d.incomplete, true);
|
|
148
|
+
assert.ok(d.errors.some((e) => /duplicate flow id "greet" \(also defined in the same module\)/.test(e.message)));
|
|
149
|
+
assert.equal(d.ids.length, 1); // only the first is derived
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("deriveModels treats a genuine same-module re-export as one model, no error", async () => {
|
|
153
|
+
// the SAME flow object exported under two names in one file — harmless.
|
|
154
|
+
const io = memIO({ "/app/workflows/re.ts": "// re" }, { "workflows/re.ts": { greet, alsoGreet: greet } });
|
|
155
|
+
const d = await deriveModels("/app", io, CODE_FIRST_MANIFEST);
|
|
156
|
+
assert.equal(d.incomplete, false);
|
|
157
|
+
assert.deepEqual(d.errors, []);
|
|
158
|
+
assert.equal(d.ids.length, 1);
|
|
159
|
+
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// Model derivation for code-first apps — the toolkit's one *executing* edge.
|
|
2
|
+
//
|
|
3
|
+
// Unlike the pure derivers (migrations, domain, worker I/O), which transform strings, this
|
|
4
|
+
// imports the app's `workflows/*.ts`, turns each `defineFlow` into executable BPMN via
|
|
5
|
+
// `@nanobpm/workflow` (`toBpmn` + `bpmn-auto-layout` DI), and writes `processes/<id>.bpmn`.
|
|
6
|
+
// Executing the app's TypeScript needs a runtime module loader, so this is gated on the
|
|
7
|
+
// optional `GenIO.importModule` (present on Node with type-stripping, and on Deno). When the
|
|
8
|
+
// loader is absent — or the app authors its models directly (model-first) — derivation is a
|
|
9
|
+
// no-op and the rest of `urban gen` proceeds on the authored/derived models unchanged.
|
|
10
|
+
|
|
11
|
+
import { toDeployableBpmn } from "@nanobpm/workflow";
|
|
12
|
+
import type { Workflow } from "@nanobpm/workflow";
|
|
13
|
+
import type { DerivedArtifact } from "./artifact.ts";
|
|
14
|
+
import type { GenIO } from "./gen.ts";
|
|
15
|
+
import { expandPattern, joinPath } from "./gen.ts";
|
|
16
|
+
import type { ModelSource } from "./derivers/worker-io.ts";
|
|
17
|
+
|
|
18
|
+
/** Default source glob for code-first flows (matches the code-first scaffold layout). */
|
|
19
|
+
export const DEFAULT_WORKFLOW_PATTERNS = ["workflows/*.ts"];
|
|
20
|
+
|
|
21
|
+
/** Default output dir for derived models (matches the model-first scaffold, so a derived
|
|
22
|
+
* model is ejectable to model-first and picked up by `models.processes: ["processes/*.bpmn"]`). */
|
|
23
|
+
export const PROCESSES_DIR = "processes";
|
|
24
|
+
|
|
25
|
+
/** Marker stamped into every derived `.bpmn` (an XML comment) so re-gen overwrites and the
|
|
26
|
+
* stale sweep deletes ONLY derived output — never an authored, human-owned model. */
|
|
27
|
+
export const MODEL_PROVENANCE =
|
|
28
|
+
"Generated by @nanobpm/urban from workflows/*.ts — do not edit; edit the flow and re-run `urban gen`.";
|
|
29
|
+
|
|
30
|
+
/** Minimal shape a code-first manifest declares for model derivation. */
|
|
31
|
+
export interface ModelsManifest {
|
|
32
|
+
models?: { processes?: string[]; workflows?: string[] };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A `defineFlow` export: has a string `id` and an `imperative`/`declarative` `kind`. */
|
|
36
|
+
export function isWorkflow(v: unknown): v is Workflow {
|
|
37
|
+
if (!v || typeof v !== "object") return false;
|
|
38
|
+
const o = v as { id?: unknown; kind?: unknown };
|
|
39
|
+
return typeof o.id === "string" && (o.kind === "imperative" || o.kind === "declarative");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Sanitize a flow id into a safe `.bpmn` filename (mirrors the host generator). */
|
|
43
|
+
export function bpmnFilename(id: string): string {
|
|
44
|
+
return `${id.replace(/[^A-Za-z0-9._-]/g, "-")}.bpmn`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The directory derived models are written to: the dir of the first `models.processes`
|
|
48
|
+
* glob if the manifest declares one, else the `processes/` default. */
|
|
49
|
+
export function processesOutDir(manifest: ModelsManifest): string {
|
|
50
|
+
for (const pat of manifest.models?.processes ?? []) {
|
|
51
|
+
const star = pat.indexOf("*");
|
|
52
|
+
const slash = star === -1 ? pat.lastIndexOf("/") : pat.lastIndexOf("/", star);
|
|
53
|
+
if (slash > 0) return pat.slice(0, slash);
|
|
54
|
+
}
|
|
55
|
+
return PROCESSES_DIR;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Insert the provenance marker as an XML comment right after the `<?xml … ?>` prolog. */
|
|
59
|
+
function withProvenance(xml: string): string {
|
|
60
|
+
const comment = `<!-- ${MODEL_PROVENANCE} -->`;
|
|
61
|
+
const m = xml.match(/^\uFEFF?\s*<\?xml[^>]*\?>/);
|
|
62
|
+
if (m) return `${m[0]}\n${comment}\n${xml.slice(m[0].length).replace(/^\r?\n/, "")}`;
|
|
63
|
+
return `${comment}\n${xml}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ModelError {
|
|
67
|
+
path: string;
|
|
68
|
+
message: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface DerivedModels {
|
|
72
|
+
/** `processes/<id>.bpmn` artifacts (path + provenance-stamped content). */
|
|
73
|
+
artifacts: DerivedArtifact[];
|
|
74
|
+
/** The same models, in-memory, for feeding the type-contract derivers (worker I/O, meta, messages). */
|
|
75
|
+
models: ModelSource[];
|
|
76
|
+
/** Derived flow ids (used for the provenance-scoped stale sweep). */
|
|
77
|
+
ids: string[];
|
|
78
|
+
/** Per-flow `{ id, kind, xml }` for the non-writing preview (`urban derive --stdout`). */
|
|
79
|
+
list: DerivedModelInfo[];
|
|
80
|
+
/** The output directory the artifacts were written into. */
|
|
81
|
+
outDir: string;
|
|
82
|
+
/** True once we attempted derivation (loader present AND at least one workflow file matched). */
|
|
83
|
+
attempted: boolean;
|
|
84
|
+
/** True if any workflow module failed to import/derive — suppresses the destructive sweep. */
|
|
85
|
+
incomplete: boolean;
|
|
86
|
+
/** Per-file derivation errors (empty on full success). */
|
|
87
|
+
errors: ModelError[];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface DerivedModelInfo {
|
|
91
|
+
id: string;
|
|
92
|
+
kind: Workflow["kind"];
|
|
93
|
+
xml: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function emptyDerived(outDir: string): DerivedModels {
|
|
97
|
+
return {
|
|
98
|
+
artifacts: [],
|
|
99
|
+
models: [],
|
|
100
|
+
ids: [],
|
|
101
|
+
list: [],
|
|
102
|
+
outDir,
|
|
103
|
+
attempted: false,
|
|
104
|
+
incomplete: false,
|
|
105
|
+
errors: [],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function messageOf(e: unknown): string {
|
|
110
|
+
return e instanceof Error ? e.message : String(e);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Append an actionable hint when a `.ts` import fails because the runtime can't strip types. */
|
|
114
|
+
function importHint(rel: string, e: unknown): string {
|
|
115
|
+
const base = messageOf(e);
|
|
116
|
+
const code = (e as { code?: string })?.code ?? "";
|
|
117
|
+
const looksLikeNoTsLoader =
|
|
118
|
+
rel.endsWith(".ts") &&
|
|
119
|
+
(code === "ERR_UNKNOWN_FILE_EXTENSION" || /Unknown file extension "\.ts"/.test(base));
|
|
120
|
+
if (!looksLikeNoTsLoader) return base;
|
|
121
|
+
return (
|
|
122
|
+
`${base}\n ↳ importing TypeScript workflows needs a runtime that strips types: ` +
|
|
123
|
+
"Node ≥22.18 (default) or `node --experimental-strip-types`, or run under Deno."
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Derive executable BPMN models from an app's code-first `workflows/*.ts`.
|
|
129
|
+
*
|
|
130
|
+
* Returns everything in-memory (artifacts + `ModelSource`s + ids). The caller decides whether
|
|
131
|
+
* to write the `.bpmn` to disk (`urban gen` / `urban derive`) or keep them in memory to feed
|
|
132
|
+
* the type-contract derivers only (`urban gen --no-models`) or preview them (`urban derive
|
|
133
|
+
* --stdout`). Never throws on a bad workflow: a failing module is recorded in `errors`,
|
|
134
|
+
* `incomplete` is set, and the sweep is suppressed so a partial failure can't delete good models.
|
|
135
|
+
*/
|
|
136
|
+
export async function deriveModels(
|
|
137
|
+
root: string,
|
|
138
|
+
io: GenIO,
|
|
139
|
+
manifest: ModelsManifest,
|
|
140
|
+
): Promise<DerivedModels> {
|
|
141
|
+
const outDir = processesOutDir(manifest);
|
|
142
|
+
const importModule = io.importModule;
|
|
143
|
+
if (!importModule) return emptyDerived(outDir);
|
|
144
|
+
|
|
145
|
+
const patterns = manifest.models?.workflows ?? DEFAULT_WORKFLOW_PATTERNS;
|
|
146
|
+
const files: string[] = [];
|
|
147
|
+
for (const pat of patterns) {
|
|
148
|
+
for (const rel of await expandPattern(root, io, pat)) files.push(rel);
|
|
149
|
+
}
|
|
150
|
+
if (files.length === 0) return emptyDerived(outDir);
|
|
151
|
+
|
|
152
|
+
const out: DerivedModels = { ...emptyDerived(outDir), attempted: true };
|
|
153
|
+
// id → { source file, workflow ref } — distinguishes a genuine re-export (same
|
|
154
|
+
// object under two export names) from a true collision (two distinct flows, one id).
|
|
155
|
+
const byId = new Map<string, { rel: string; ref: unknown }>();
|
|
156
|
+
|
|
157
|
+
for (const rel of files.sort()) {
|
|
158
|
+
let mod: Record<string, unknown>;
|
|
159
|
+
try {
|
|
160
|
+
mod = await importModule(joinPath(root, rel));
|
|
161
|
+
} catch (e) {
|
|
162
|
+
out.incomplete = true;
|
|
163
|
+
out.errors.push({ path: rel, message: importHint(rel, e) });
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
for (const value of Object.values(mod)) {
|
|
167
|
+
if (!isWorkflow(value)) continue;
|
|
168
|
+
const id = value.id;
|
|
169
|
+
const prior = byId.get(id);
|
|
170
|
+
if (prior) {
|
|
171
|
+
if (prior.ref === value && prior.rel === rel) continue; // genuine intra-module re-export
|
|
172
|
+
out.incomplete = true;
|
|
173
|
+
const where = prior.rel === rel ? "also defined in the same module" : `also defined in ${prior.rel}`;
|
|
174
|
+
out.errors.push({ path: rel, message: `duplicate flow id "${id}" (${where})` });
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
byId.set(id, { rel, ref: value });
|
|
178
|
+
try {
|
|
179
|
+
const semantic = await toDeployableBpmn(value);
|
|
180
|
+
const xml = withProvenance(semantic);
|
|
181
|
+
const path = `${outDir}/${bpmnFilename(id)}`;
|
|
182
|
+
out.artifacts.push({ path, content: xml });
|
|
183
|
+
out.models.push({ path, xml });
|
|
184
|
+
out.list.push({ id, kind: value.kind, xml });
|
|
185
|
+
out.ids.push(id);
|
|
186
|
+
} catch (e) {
|
|
187
|
+
out.incomplete = true;
|
|
188
|
+
out.errors.push({ path: rel, message: `deriving "${id}": ${messageOf(e)}` });
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return out;
|
|
193
|
+
}
|