@motionskit/cli 0.2.4 → 0.2.6
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 +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +11 -4
- package/dist/main.js +247 -45
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @motionskit/cli
|
|
2
2
|
|
|
3
|
-
`motions init`, `check`, `test`, `push`, `pull`, `open` and
|
|
3
|
+
`motions init`, `check`, `test`, `push`, `pull`, `open`, `retry`, `cancel`, `help` and `--version`.
|
|
4
4
|
|
|
5
|
-
From a directory with `tenant.yaml`, `motions push` publishes a draft and prints a link. `motions push --live` makes it the version new batches open on. `MOTIONS_URL` defaults to `
|
|
5
|
+
From a directory with `tenant.yaml`, `motions push` publishes a draft and prints a link. `motions push --live` makes it the version new batches open on. `MOTIONS_URL` defaults to `https://motions.pruma.tech`. `MOTIONS_TOKEN` is an API key (`mot_…`) sent as `authorization: Bearer`.
|
|
6
6
|
|
|
7
|
-
Inside the motions repo, `motions publish [tenant]` is `push --live` over `tenants
|
|
7
|
+
Inside the motions repo, `motions publish [tenant]` is `push --live` over `tenants/*` (defaulting `MOTIONS_URL` to `http://localhost:8787`).
|
package/dist/index.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ type Compiled = {
|
|
|
47
47
|
declare function compileMotion(tenant: {
|
|
48
48
|
id: string;
|
|
49
49
|
timezone: string;
|
|
50
|
-
}, dir: string): Promise<Compiled>;
|
|
50
|
+
}, dir: string, catalog?: Set<string>): Promise<Compiled>;
|
|
51
51
|
declare function compileTenant(tenantDir: string): Promise<Tenant>;
|
|
52
52
|
|
|
53
53
|
type PublishWarningsInput = {
|
package/dist/index.js
CHANGED
|
@@ -4,11 +4,13 @@ import { existsSync as existsSync3, readdirSync as readdirSync3, readFileSync as
|
|
|
4
4
|
import { dirname as dirname2, isAbsolute as isAbsolute2, join as join4, relative as relative2, resolve as resolve2, sep as sep2, win32 } from "path";
|
|
5
5
|
import { fileURLToPath } from "url";
|
|
6
6
|
import {
|
|
7
|
+
BUILTIN_SOURCE_OPS,
|
|
7
8
|
CRM_WRITES,
|
|
8
9
|
crmProblems,
|
|
9
10
|
rulesVersion,
|
|
10
11
|
STEP_BUILD,
|
|
11
12
|
STEP_ENTRY_FILE,
|
|
13
|
+
seedProblems,
|
|
12
14
|
stepEntry,
|
|
13
15
|
unknownCrmSystem,
|
|
14
16
|
validateManifest,
|
|
@@ -470,7 +472,7 @@ var MotionErrors = class extends Error {
|
|
|
470
472
|
where;
|
|
471
473
|
errors;
|
|
472
474
|
};
|
|
473
|
-
async function compileMotion(tenant, dir) {
|
|
475
|
+
async function compileMotion(tenant, dir, catalog = new Set(BUILTIN_SOURCE_OPS)) {
|
|
474
476
|
const tenantDir = findTenantDir(dir);
|
|
475
477
|
if (!tenantDir) throw new Error(`no tenant.yaml above ${dir}`);
|
|
476
478
|
const folder = folderName(dir);
|
|
@@ -528,7 +530,7 @@ async function compileMotion(tenant, dir) {
|
|
|
528
530
|
steps,
|
|
529
531
|
queues
|
|
530
532
|
};
|
|
531
|
-
const errors = validateManifest(manifest);
|
|
533
|
+
const errors = validateManifest(manifest, catalog);
|
|
532
534
|
if (errors.length) throw new MotionErrors(where2, errors);
|
|
533
535
|
return { dir: root, manifest: { ...manifest, sha: versionId2(manifest, files) }, bundles, files };
|
|
534
536
|
}
|
|
@@ -550,12 +552,17 @@ async function compileTenant(tenantDir) {
|
|
|
550
552
|
const seedPath = join4(dir, "crm-seed.json");
|
|
551
553
|
const seed = existsSync3(seedPath) ? JSON.parse(readFileSync3(seedPath, "utf8")) : null;
|
|
552
554
|
const fixtures = readTenantFixtures(dir, meta.sources);
|
|
555
|
+
const sources = await compileSources(dir, id2);
|
|
556
|
+
const catalog = /* @__PURE__ */ new Set([
|
|
557
|
+
...BUILTIN_SOURCE_OPS,
|
|
558
|
+
...sources.flatMap((s) => Object.keys(s.manifest.ops).map((op) => `${s.manifest.source}.${op}`))
|
|
559
|
+
]);
|
|
553
560
|
const motionsDir = join4(dir, "motions");
|
|
554
561
|
const motions = [];
|
|
555
562
|
for (const name of readdirSync3(motionsDir).sort()) {
|
|
556
|
-
if (existsSync3(join4(motionsDir, name, "motion.yaml"))) motions.push(await compileMotion({ id: id2, timezone }, join4(motionsDir, name)));
|
|
563
|
+
if (existsSync3(join4(motionsDir, name, "motion.yaml"))) motions.push(await compileMotion({ id: id2, timezone }, join4(motionsDir, name), catalog));
|
|
557
564
|
}
|
|
558
|
-
return { id: id2, title: meta.title ?? id2, timezone, connection, seed, sources
|
|
565
|
+
return { id: id2, title: meta.title ?? id2, timezone, connection, seed, sources, fixtures, motions };
|
|
559
566
|
}
|
|
560
567
|
|
|
561
568
|
// src/publish.ts
|
package/dist/main.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
// src/main.ts
|
|
4
4
|
import { existsSync as existsSync8, mkdirSync as mkdirSync4, readFileSync as readFileSync8, writeFileSync as writeFileSync4 } from "fs";
|
|
5
|
-
import { dirname as dirname6, join as join9, resolve as resolve6 } from "path";
|
|
5
|
+
import { basename as basename2, dirname as dirname6, join as join9, resolve as resolve6 } from "path";
|
|
6
|
+
import { createInterface } from "readline/promises";
|
|
6
7
|
import { parseArgs } from "util";
|
|
7
8
|
|
|
8
9
|
// src/batches.ts
|
|
@@ -61,6 +62,7 @@ function findCompany(companies, query) {
|
|
|
61
62
|
return companies.find((c) => c.key === query) ?? companies.find((c) => c.key.endsWith(`:${query}`));
|
|
62
63
|
}
|
|
63
64
|
function selectRetries(snap, batch, options2 = {}) {
|
|
65
|
+
if (snap.status.canceled) throw new Error(`${batch} was canceled`);
|
|
64
66
|
const runnable = runnableSteps(snap.motion);
|
|
65
67
|
const step2 = options2.step?.trim();
|
|
66
68
|
const company = options2.company?.trim();
|
|
@@ -98,6 +100,32 @@ async function retryBatch(call, input) {
|
|
|
98
100
|
}
|
|
99
101
|
return { tenant, retried: targets };
|
|
100
102
|
}
|
|
103
|
+
function describeCancel(snap, batch) {
|
|
104
|
+
const { status: st } = snap;
|
|
105
|
+
const incomplete = st.lanes.active + st.lanes.waiting + st.lanes.parked;
|
|
106
|
+
const openQuestions = st.questions.filter((q) => q.state === "open").length;
|
|
107
|
+
const approvals = st.changesets.filter((c) => c.state === "approval").length;
|
|
108
|
+
const parts = [
|
|
109
|
+
`${incomplete} incomplete ${incomplete === 1 ? "company" : "companies"}`,
|
|
110
|
+
...openQuestions ? [`${openQuestions} open ${openQuestions === 1 ? "question" : "questions"}`] : [],
|
|
111
|
+
...approvals ? [`${approvals} pending ${approvals === 1 ? "approval" : "approvals"}`] : []
|
|
112
|
+
];
|
|
113
|
+
return `Cancel ${batch} ("${st.label}", ${parts.join(", ")})?`;
|
|
114
|
+
}
|
|
115
|
+
async function cancelBatch(call, input) {
|
|
116
|
+
const snap = await call(`/api/batches/${encodeURIComponent(input.batch)}`, void 0, input.tenant);
|
|
117
|
+
const { status: st } = snap;
|
|
118
|
+
if (st.canceled) throw new Error(`batch ${input.batch} is already canceled`);
|
|
119
|
+
if (st.done) throw new Error(`batch ${input.batch} is already done`);
|
|
120
|
+
const writing = st.changesets.find((c) => c.state === "writing");
|
|
121
|
+
if (writing) throw new Error(`cannot cancel ${input.batch} while changeset ${writing.sha.slice(0, 8)} is writing to the CRM`);
|
|
122
|
+
if (input.confirm && !await input.confirm(describeCancel(snap, input.batch))) {
|
|
123
|
+
return { tenant: st.tenant, canceled: false };
|
|
124
|
+
}
|
|
125
|
+
const trimmed = input.reason?.trim();
|
|
126
|
+
await call(`/api/batches/${encodeURIComponent(input.batch)}/cancel`, trimmed ? { reason: trimmed } : {}, st.tenant);
|
|
127
|
+
return { tenant: st.tenant, canceled: true };
|
|
128
|
+
}
|
|
101
129
|
|
|
102
130
|
// src/client.ts
|
|
103
131
|
import { existsSync, readFileSync } from "fs";
|
|
@@ -144,20 +172,36 @@ function tokenFor(tenant, url2, batch) {
|
|
|
144
172
|
}
|
|
145
173
|
return void 0;
|
|
146
174
|
}
|
|
175
|
+
function formatDetails(details) {
|
|
176
|
+
if (details === void 0 || details === null) return "";
|
|
177
|
+
if (typeof details === "string") return details.trim() ? `
|
|
178
|
+
${details.trim()}` : "";
|
|
179
|
+
if (Array.isArray(details)) return details.length ? `
|
|
180
|
+
${details.map(String).join("\n ")}` : "";
|
|
181
|
+
if (typeof details === "object") {
|
|
182
|
+
const errors = details.errors;
|
|
183
|
+
if (Array.isArray(errors) && errors.length) return `
|
|
184
|
+
${errors.map(String).join("\n ")}`;
|
|
185
|
+
}
|
|
186
|
+
return "";
|
|
187
|
+
}
|
|
147
188
|
function describeFailure(path, status, text) {
|
|
148
189
|
let code;
|
|
149
190
|
let message = text.trim();
|
|
191
|
+
let details;
|
|
150
192
|
try {
|
|
151
193
|
const body = JSON.parse(text);
|
|
152
194
|
if (typeof body.error?.message === "string") {
|
|
153
195
|
code = body.error.code;
|
|
154
196
|
message = body.error.message;
|
|
197
|
+
details = body.error.details;
|
|
155
198
|
}
|
|
156
199
|
} catch {
|
|
157
200
|
}
|
|
158
|
-
|
|
159
|
-
if (status ===
|
|
160
|
-
return `${
|
|
201
|
+
const extra = formatDetails(details);
|
|
202
|
+
if (status === 401) return `${code ? `unauthorized (${code}): ${message}` : `unauthorized: ${message}`}${extra}`;
|
|
203
|
+
if (status === 403) return `${code ? `forbidden (${code}): ${message}` : `forbidden: ${message}`}${extra}`;
|
|
204
|
+
return `${path} \u2192 ${status}${code ? ` ${code}` : ""}: ${message}${extra}`;
|
|
161
205
|
}
|
|
162
206
|
async function api(url2, path, body, tenant) {
|
|
163
207
|
const pathTenant = tenant ?? path.match(/\/api\/tenants\/([^/]+)/)?.[1];
|
|
@@ -165,11 +209,19 @@ async function api(url2, path, body, tenant) {
|
|
|
165
209
|
const key = tokenFor(pathTenant ? decodeURIComponent(pathTenant) : void 0, url2, batch ? decodeURIComponent(batch) : void 0);
|
|
166
210
|
const headers = { accept: "application/json", ...key ? { authorization: `Bearer ${key}` } : {} };
|
|
167
211
|
if (body !== void 0) headers["content-type"] = "application/json";
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
212
|
+
const base = url2.replace(/\/$/, "");
|
|
213
|
+
let res;
|
|
214
|
+
try {
|
|
215
|
+
res = await fetch(`${base}${path}`, {
|
|
216
|
+
method: body === void 0 ? "GET" : "POST",
|
|
217
|
+
headers,
|
|
218
|
+
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
219
|
+
});
|
|
220
|
+
} catch (e) {
|
|
221
|
+
const cause = e?.cause;
|
|
222
|
+
const reason = cause?.message ?? cause?.code ?? (e instanceof Error ? e.message : String(e));
|
|
223
|
+
throw new Error(`could not reach ${base} (${path}): ${reason}. Set MOTIONS_URL or pass --url if the server is elsewhere.`);
|
|
224
|
+
}
|
|
173
225
|
const text = await res.text();
|
|
174
226
|
if (!res.ok) throw new Error(describeFailure(path, res.status, text));
|
|
175
227
|
return text ? JSON.parse(text) : {};
|
|
@@ -181,11 +233,13 @@ import { existsSync as existsSync4, readdirSync as readdirSync3, readFileSync as
|
|
|
181
233
|
import { dirname as dirname3, isAbsolute as isAbsolute2, join as join5, relative as relative2, resolve as resolve2, sep as sep2, win32 } from "path";
|
|
182
234
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
183
235
|
import {
|
|
236
|
+
BUILTIN_SOURCE_OPS,
|
|
184
237
|
CRM_WRITES,
|
|
185
238
|
crmProblems,
|
|
186
239
|
rulesVersion,
|
|
187
240
|
STEP_BUILD,
|
|
188
241
|
STEP_ENTRY_FILE,
|
|
242
|
+
seedProblems,
|
|
189
243
|
stepEntry,
|
|
190
244
|
unknownCrmSystem,
|
|
191
245
|
validateManifest,
|
|
@@ -699,7 +753,7 @@ var MotionErrors = class extends Error {
|
|
|
699
753
|
where;
|
|
700
754
|
errors;
|
|
701
755
|
};
|
|
702
|
-
async function compileMotion(tenant, dir) {
|
|
756
|
+
async function compileMotion(tenant, dir, catalog = new Set(BUILTIN_SOURCE_OPS)) {
|
|
703
757
|
const tenantDir = findTenantDir(dir);
|
|
704
758
|
if (!tenantDir) throw new Error(`no tenant.yaml above ${dir}`);
|
|
705
759
|
const folder = folderName(dir);
|
|
@@ -757,7 +811,7 @@ async function compileMotion(tenant, dir) {
|
|
|
757
811
|
steps,
|
|
758
812
|
queues
|
|
759
813
|
};
|
|
760
|
-
const errors = validateManifest(manifest);
|
|
814
|
+
const errors = validateManifest(manifest, catalog);
|
|
761
815
|
if (errors.length) throw new MotionErrors(where2, errors);
|
|
762
816
|
return { dir: root, manifest: { ...manifest, sha: versionId2(manifest, files) }, bundles, files };
|
|
763
817
|
}
|
|
@@ -779,18 +833,45 @@ async function compileTenant(tenantDir) {
|
|
|
779
833
|
const seedPath = join5(dir, "crm-seed.json");
|
|
780
834
|
const seed = existsSync4(seedPath) ? JSON.parse(readFileSync4(seedPath, "utf8")) : null;
|
|
781
835
|
const fixtures = readTenantFixtures(dir, meta.sources);
|
|
836
|
+
const sources = await compileSources(dir, id2);
|
|
837
|
+
const catalog = /* @__PURE__ */ new Set([
|
|
838
|
+
...BUILTIN_SOURCE_OPS,
|
|
839
|
+
...sources.flatMap((s) => Object.keys(s.manifest.ops).map((op) => `${s.manifest.source}.${op}`))
|
|
840
|
+
]);
|
|
782
841
|
const motionsDir = join5(dir, "motions");
|
|
783
842
|
const motions = [];
|
|
784
843
|
for (const name of readdirSync3(motionsDir).sort()) {
|
|
785
|
-
if (existsSync4(join5(motionsDir, name, "motion.yaml"))) motions.push(await compileMotion({ id: id2, timezone }, join5(motionsDir, name)));
|
|
844
|
+
if (existsSync4(join5(motionsDir, name, "motion.yaml"))) motions.push(await compileMotion({ id: id2, timezone }, join5(motionsDir, name), catalog));
|
|
845
|
+
}
|
|
846
|
+
return { id: id2, title: meta.title ?? id2, timezone, connection, seed, sources, fixtures, motions };
|
|
847
|
+
}
|
|
848
|
+
function escapeRegExp(s) {
|
|
849
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
850
|
+
}
|
|
851
|
+
function unusedQueueWarnings(manifest, files) {
|
|
852
|
+
const keys = Object.keys(manifest.queues);
|
|
853
|
+
if (!keys.length) return [];
|
|
854
|
+
const code = Object.entries(files).filter(([path]) => path !== "motion.yaml" && path !== "rules.yaml").map(([, content]) => content).join("\n");
|
|
855
|
+
const warnings = [];
|
|
856
|
+
for (const key of keys) {
|
|
857
|
+
const escaped = escapeRegExp(key);
|
|
858
|
+
const mentioned = new RegExp(`(["'\`])${escaped}\\1|\\.answers\\.${escaped}\\b`).test(code);
|
|
859
|
+
if (!mentioned) {
|
|
860
|
+
warnings.push(`warning: ${manifest.motion}: queue "${key}" is declared in motion.yaml but not referenced by any step (does it match a ctx.ask type?)`);
|
|
861
|
+
}
|
|
786
862
|
}
|
|
787
|
-
return
|
|
863
|
+
return warnings;
|
|
788
864
|
}
|
|
789
865
|
function tenantCrmProblems(tenant, systems = CRM_WRITES) {
|
|
866
|
+
const problems = [];
|
|
790
867
|
const system = tenant.connection?.system;
|
|
791
868
|
const unknown = unknownCrmSystem(system, systems);
|
|
792
|
-
if (unknown)
|
|
793
|
-
|
|
869
|
+
if (unknown) problems.push(`crm.yaml: ${unknown}`);
|
|
870
|
+
else problems.push(...tenant.motions.flatMap(({ manifest }) => crmProblems(system, manifest.steps, systems).map((p) => `${manifest.motion}: ${p}`)));
|
|
871
|
+
if (tenant.seed !== void 0 && tenant.seed !== null) {
|
|
872
|
+
problems.push(...seedProblems(tenant.seed).map((p) => `crm-seed.json: ${p}`));
|
|
873
|
+
}
|
|
874
|
+
return problems;
|
|
794
875
|
}
|
|
795
876
|
|
|
796
877
|
// src/init.ts
|
|
@@ -798,19 +879,39 @@ import { existsSync as existsSync5, mkdirSync as mkdirSync2, readdirSync as read
|
|
|
798
879
|
import { basename, dirname as dirname4, join as join6, resolve as resolve3 } from "path";
|
|
799
880
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
800
881
|
var TENANT_ID = /^[a-z][a-z0-9_-]{0,62}$/;
|
|
882
|
+
var HANDLE = /^[a-z][a-z0-9_-]{0,31}$/;
|
|
883
|
+
function readCliPackage() {
|
|
884
|
+
return JSON.parse(readFileSync5(fileURLToPath3(new URL("../package.json", import.meta.url)), "utf8"));
|
|
885
|
+
}
|
|
801
886
|
function cliVersion() {
|
|
802
|
-
return
|
|
887
|
+
return readCliPackage().version;
|
|
888
|
+
}
|
|
889
|
+
function sdkVersion() {
|
|
890
|
+
const pkg = readCliPackage();
|
|
891
|
+
const kernel = pkg.dependencies?.["@motionskit/kernel"];
|
|
892
|
+
if (kernel && /^\d+\.\d+\.\d+/.test(kernel)) return `^${kernel}`;
|
|
893
|
+
return `^${pkg.version}`;
|
|
894
|
+
}
|
|
895
|
+
function handleFrom(raw) {
|
|
896
|
+
if (!raw) return null;
|
|
897
|
+
const base = raw.trim().split("@")[0].toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^[^a-z]+/, "").replace(/-+$/, "").slice(0, 28);
|
|
898
|
+
return HANDLE.test(base) ? base : null;
|
|
899
|
+
}
|
|
900
|
+
function defaultApprover() {
|
|
901
|
+
return handleFrom(process.env.MOTIONS_APPROVER) ?? "member";
|
|
803
902
|
}
|
|
804
903
|
var file = (dir, path, contents) => {
|
|
805
904
|
const dest = join6(dir, path);
|
|
806
905
|
mkdirSync2(dirname4(dest), { recursive: true });
|
|
807
906
|
writeFileSync2(dest, contents);
|
|
808
907
|
};
|
|
809
|
-
function init(target = ".") {
|
|
908
|
+
function init(target = ".", options2 = {}) {
|
|
810
909
|
const dir = resolve3(target);
|
|
811
910
|
const id2 = basename(dir);
|
|
812
911
|
if (!TENANT_ID.test(id2)) throw new Error(`${id2} is not a tenant id: lowercase letters, digits, - and _, starting with a letter`);
|
|
813
912
|
if (existsSync5(dir) && readdirSync4(dir).length) throw new Error(`${dir} is not empty`);
|
|
913
|
+
const approver = options2.approver ?? defaultApprover();
|
|
914
|
+
if (!HANDLE.test(approver)) throw new Error(`${approver} is not a member handle: lowercase letters, digits, - and _, starting with a letter`);
|
|
814
915
|
mkdirSync2(dir, { recursive: true });
|
|
815
916
|
const version = cliVersion();
|
|
816
917
|
file(dir, "tenant.yaml", `id: ${id2}
|
|
@@ -818,10 +919,37 @@ title: ${id2}
|
|
|
818
919
|
timezone: America/Sao_Paulo
|
|
819
920
|
`);
|
|
820
921
|
file(dir, "crm.yaml", "system: memory\nmode: read_write\ncompanies:\n core: { name: name, domain: domain }\n");
|
|
922
|
+
file(
|
|
923
|
+
dir,
|
|
924
|
+
"crm-seed.json",
|
|
925
|
+
`${JSON.stringify(
|
|
926
|
+
{
|
|
927
|
+
companies: [
|
|
928
|
+
{ id: "1001", properties: { name: "Aurora Industrial", domain: "aurora.example" } },
|
|
929
|
+
{ id: "1002", properties: { name: "Serra Verde Alimentos", domain: "" } }
|
|
930
|
+
],
|
|
931
|
+
lists: { weekly: ["1001", "1002"] }
|
|
932
|
+
},
|
|
933
|
+
null,
|
|
934
|
+
2
|
|
935
|
+
)}
|
|
936
|
+
`
|
|
937
|
+
);
|
|
821
938
|
file(
|
|
822
939
|
dir,
|
|
823
940
|
"motions/example/motion.yaml",
|
|
824
|
-
|
|
941
|
+
`title: Example
|
|
942
|
+
approvers: [${approver}]
|
|
943
|
+
reasons: [no_domain]
|
|
944
|
+
|
|
945
|
+
input:
|
|
946
|
+
list: weekly
|
|
947
|
+
|
|
948
|
+
steps:
|
|
949
|
+
- id: greet
|
|
950
|
+
title: Record the domain
|
|
951
|
+
run: steps/greet.ts
|
|
952
|
+
`
|
|
825
953
|
);
|
|
826
954
|
file(
|
|
827
955
|
dir,
|
|
@@ -839,30 +967,20 @@ export default defineStep({
|
|
|
839
967
|
file(
|
|
840
968
|
dir,
|
|
841
969
|
"motions/example/tests/greet.test.ts",
|
|
842
|
-
`import {
|
|
970
|
+
`import { runStep } from "@motionskit/sdk/testing";
|
|
843
971
|
import { expect, test } from "vitest";
|
|
844
972
|
import greet from "../steps/greet.ts";
|
|
845
973
|
|
|
846
|
-
const
|
|
847
|
-
kind: "company" as const,
|
|
848
|
-
batch: { id: "b", tenant: ${JSON.stringify(id2)}, motion: "example", version: "v", label: "t", openedAt: 0 },
|
|
849
|
-
step: "greet",
|
|
850
|
-
rules: {},
|
|
851
|
-
clock: Date.parse("2026-11-02T12:00:00Z"),
|
|
852
|
-
calendar: {},
|
|
853
|
-
companies: [{ record: { key: "co", core: { name: "Acme", domain, country: null, tax_id: null, crm: { system: "memory", object: "company" as const, id: "1", owner_id: null } }, fields: {} }, facts: {}, answers: {}, attempt: "a" }],
|
|
854
|
-
});
|
|
855
|
-
|
|
856
|
-
const caps = { sources: { call: async () => ({ ok: true as const, data: null }) }, crm: { read: async () => ({ ok: true as const, data: null }), prepare: async () => ({ ok: true as const, data: { before: null } }) } };
|
|
974
|
+
const now = "2026-11-02T12:00:00Z";
|
|
857
975
|
|
|
858
976
|
test("drops a company that has no domain", async () => {
|
|
859
|
-
const out = await
|
|
860
|
-
expect(out
|
|
977
|
+
const out = await runStep(greet, { company: { core: { name: "Serra Verde", domain: null } }, fixtures: [], now });
|
|
978
|
+
expect(out).toMatchObject({ status: "ok", outcome: { lane: "drop", reason: "no_domain" } });
|
|
861
979
|
});
|
|
862
980
|
|
|
863
981
|
test("records the domain", async () => {
|
|
864
|
-
const out = await
|
|
865
|
-
expect(out
|
|
982
|
+
const out = await runStep(greet, { company: { core: { name: "Aurora Industrial", domain: "aurora.example" } }, fixtures: [], now });
|
|
983
|
+
expect(out).toMatchObject({ status: "ok", facts: { domain: "aurora.example" } });
|
|
866
984
|
});
|
|
867
985
|
`
|
|
868
986
|
);
|
|
@@ -875,7 +993,7 @@ test("records the domain", async () => {
|
|
|
875
993
|
private: true,
|
|
876
994
|
type: "module",
|
|
877
995
|
scripts: { check: "motions check", test: "motions test", typecheck: "tsc -p .", push: "motions push" },
|
|
878
|
-
devDependencies: { "@motionskit/cli": version, "@motionskit/sdk":
|
|
996
|
+
devDependencies: { "@motionskit/cli": version, "@motionskit/sdk": sdkVersion(), typescript: "^5.9.3", vitest: "^5.1.0" }
|
|
879
997
|
},
|
|
880
998
|
null,
|
|
881
999
|
2
|
|
@@ -918,7 +1036,9 @@ npm run typecheck
|
|
|
918
1036
|
npx motions test
|
|
919
1037
|
\`\`\`
|
|
920
1038
|
|
|
921
|
-
\`motions push\` publishes a draft and prints a link. \`motions push --live\` makes it the version new batches open on. Set \`
|
|
1039
|
+
\`motions push\` publishes a draft and prints a link. \`motions push --live\` makes it the version new batches open on. Set \`MOTIONS_TOKEN\` (an API key, \`mot_\u2026\`), and \`MOTIONS_URL\` only if you run against another server (defaults to \`https://motions.pruma.tech\`).
|
|
1040
|
+
|
|
1041
|
+
Make sure the handle in \`motions/example/motion.yaml\` (\`approvers: [${approver}]\`) matches a member in Settings -> Members before promoting live. \`crm-seed.json\` holds sample companies for the in-memory CRM; company ids are digits only.
|
|
922
1042
|
|
|
923
1043
|
GitHub: pull requests run \`check\`, \`typecheck\` and \`test\`. Pushes to \`main\` run the Motions action, which publishes live. Add the \`MOTIONS_TOKEN\` secret and the \`MOTIONS_URL\` variable to the repo.
|
|
924
1044
|
`
|
|
@@ -1092,21 +1212,26 @@ function runTenantTests(dirs, spawn = spawnSync) {
|
|
|
1092
1212
|
}
|
|
1093
1213
|
|
|
1094
1214
|
// src/main.ts
|
|
1095
|
-
var
|
|
1215
|
+
var DEFAULT_URL = "https://motions.pruma.tech";
|
|
1216
|
+
var LOCAL_URL = "http://localhost:8787";
|
|
1217
|
+
var USAGE = `usage: motions <check|push|pull|test|open|retry|cancel|init|publish|help> \u2026
|
|
1096
1218
|
check [tenant] compile the tenant in this directory, or every tenant in this repo
|
|
1097
1219
|
push [--live] [-m message] publish a draft (or go live with --live) and print a link
|
|
1098
1220
|
pull <version> [--motion id] [--force]
|
|
1099
1221
|
test run motions/*/tests with the tenant's vitest
|
|
1100
1222
|
open <tenant> <motion> [--list id-or-name | --companies id,id] [--label text] [--again]
|
|
1101
1223
|
retry <batch> [--company id] [--step id]
|
|
1102
|
-
|
|
1103
|
-
|
|
1224
|
+
cancel <batch> [--reason text] [--yes]
|
|
1225
|
+
init [dir] [--approver handle] scaffold a tenant repo
|
|
1226
|
+
publish [tenant] from this repo, push --live over tenants/*
|
|
1227
|
+
help print this usage text
|
|
1228
|
+
--version, -v print the CLI version`;
|
|
1104
1229
|
var parsed = (() => {
|
|
1105
1230
|
try {
|
|
1106
1231
|
return parseArgs({
|
|
1107
1232
|
allowPositionals: true,
|
|
1108
1233
|
options: {
|
|
1109
|
-
url: { type: "string"
|
|
1234
|
+
url: { type: "string" },
|
|
1110
1235
|
tenant: { type: "string" },
|
|
1111
1236
|
live: { type: "boolean", default: false },
|
|
1112
1237
|
draft: { type: "boolean", default: false },
|
|
@@ -1117,9 +1242,13 @@ var parsed = (() => {
|
|
|
1117
1242
|
companies: { type: "string" },
|
|
1118
1243
|
company: { type: "string" },
|
|
1119
1244
|
step: { type: "string" },
|
|
1245
|
+
reason: { type: "string" },
|
|
1246
|
+
yes: { type: "boolean", short: "y", default: false },
|
|
1120
1247
|
again: { type: "boolean", default: false },
|
|
1121
1248
|
label: { type: "string" },
|
|
1122
1249
|
reseed: { type: "boolean", default: false },
|
|
1250
|
+
approver: { type: "string" },
|
|
1251
|
+
version: { type: "boolean", short: "v", default: false },
|
|
1123
1252
|
help: { type: "boolean", short: "h", default: false }
|
|
1124
1253
|
}
|
|
1125
1254
|
});
|
|
@@ -1131,12 +1260,16 @@ ${USAGE}`);
|
|
|
1131
1260
|
}
|
|
1132
1261
|
})();
|
|
1133
1262
|
var { positionals, values } = parsed;
|
|
1134
|
-
if (values.
|
|
1263
|
+
if (values.version) {
|
|
1264
|
+
console.log(cliVersion());
|
|
1265
|
+
process.exit(0);
|
|
1266
|
+
}
|
|
1267
|
+
if (values.help || positionals[0] === "help") {
|
|
1135
1268
|
console.log(USAGE);
|
|
1136
1269
|
process.exit(0);
|
|
1137
1270
|
}
|
|
1138
1271
|
var [command, ...args] = positionals;
|
|
1139
|
-
var url = values.url.replace(/\/$/, "");
|
|
1272
|
+
var url = (values.url ?? process.env.MOTIONS_URL ?? (command === "publish" ? LOCAL_URL : DEFAULT_URL)).replace(/\/$/, "");
|
|
1140
1273
|
function resolveTenants(positional, fallbackAll) {
|
|
1141
1274
|
if (values.tenant) {
|
|
1142
1275
|
const found = findTenantDir(resolve6(values.tenant));
|
|
@@ -1162,8 +1295,33 @@ function versionLink(tenant, motion, sha) {
|
|
|
1162
1295
|
return `${url}/t/${tenant}/motions/${motion}/versions/${sha}`;
|
|
1163
1296
|
}
|
|
1164
1297
|
async function pushDirs(dirs, live) {
|
|
1298
|
+
const compiled = [];
|
|
1165
1299
|
for (const dir of dirs) {
|
|
1166
1300
|
const tenant = await compileTenant(dir);
|
|
1301
|
+
const refused = tenantCrmProblems(tenant);
|
|
1302
|
+
if (refused.length) throw new Error(`${tenant.id}:
|
|
1303
|
+
${refused.join("\n ")}`);
|
|
1304
|
+
compiled.push(tenant);
|
|
1305
|
+
}
|
|
1306
|
+
for (const tenant of compiled) {
|
|
1307
|
+
await api(url, `/api/tenants/${tenant.id}`, { ...upsertBody(tenant, values.reseed), dryRun: true });
|
|
1308
|
+
const extraSources = tenant.sources.flatMap((s) => Object.keys(s.manifest.ops).map((op) => `${s.manifest.source}.${op}`));
|
|
1309
|
+
const crmSystem = tenant.connection?.system;
|
|
1310
|
+
for (const { dir: motionDir, manifest, bundles, files } of tenant.motions) {
|
|
1311
|
+
await api(url, `/api/tenants/${tenant.id}/motions/${manifest.motion}/versions`, {
|
|
1312
|
+
manifest,
|
|
1313
|
+
bundles,
|
|
1314
|
+
files,
|
|
1315
|
+
origin: origin(motionDir),
|
|
1316
|
+
live,
|
|
1317
|
+
...values.message ? { message: values.message } : {},
|
|
1318
|
+
dryRun: true,
|
|
1319
|
+
...extraSources.length ? { extraSources } : {},
|
|
1320
|
+
...crmSystem ? { crmSystem } : {}
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
for (const tenant of compiled) {
|
|
1167
1325
|
const upserted = await api(url, `/api/tenants/${tenant.id}`, upsertBody(tenant, values.reseed));
|
|
1168
1326
|
if (upserted.seeded) console.log(`seeded ${tenant.id}'s CRM with ${upserted.seeded} records`);
|
|
1169
1327
|
if (upserted.fixtures !== void 0) console.log(`${tenant.id} is in fixture mode: built-in sources answer from ${upserted.fixtures} fixtures`);
|
|
@@ -1179,6 +1337,7 @@ async function pushDirs(dirs, live) {
|
|
|
1179
1337
|
});
|
|
1180
1338
|
console.log(`${tenant.id}/${manifest.motion} @ ${manifest.sha.slice(0, 10)} (${out.created ? "new" : "unchanged"}, ${out.live ? "live" : "draft"})`);
|
|
1181
1339
|
console.log(` ${versionLink(tenant.id, manifest.motion, manifest.sha)}`);
|
|
1340
|
+
for (const line of unusedQueueWarnings(manifest, files)) console.log(` ${line}`);
|
|
1182
1341
|
for (const line of formatPublishWarnings(tenant.id, manifest.motion, out)) console.log(line);
|
|
1183
1342
|
}
|
|
1184
1343
|
}
|
|
@@ -1193,7 +1352,7 @@ async function check(only) {
|
|
|
1193
1352
|
});
|
|
1194
1353
|
if (!tenant) continue;
|
|
1195
1354
|
console.log(`${tenant.id} (${tenant.timezone})`);
|
|
1196
|
-
for (const { manifest, bundles } of tenant.motions) {
|
|
1355
|
+
for (const { manifest, bundles, files } of tenant.motions) {
|
|
1197
1356
|
const kb = Object.values(bundles).reduce((n, code) => n + code.length, 0) / 1024;
|
|
1198
1357
|
console.log(
|
|
1199
1358
|
` ${manifest.motion} @ ${manifest.sha.slice(0, 10)} ${manifest.steps.length} steps, ${Object.keys(bundles).length} bundles (${kb.toFixed(0)} KB), ${manifest.rulesVersion}`
|
|
@@ -1202,6 +1361,7 @@ async function check(only) {
|
|
|
1202
1361
|
const detail = "bundle" in s ? ` ${s.bundle.slice(0, 8)} sources=[${s.sources.join(", ")}]` : "";
|
|
1203
1362
|
console.log(` ${s.kind.padEnd(10)} ${s.id}${detail}`);
|
|
1204
1363
|
}
|
|
1364
|
+
for (const line of unusedQueueWarnings(manifest, files)) console.log(` ${line}`);
|
|
1205
1365
|
}
|
|
1206
1366
|
const fixtures = fixturesLine(tenant.fixtures);
|
|
1207
1367
|
if (fixtures) console.log(fixtures);
|
|
@@ -1320,6 +1480,35 @@ async function retry(batch, extra) {
|
|
|
1320
1480
|
}
|
|
1321
1481
|
console.log(` ${url}/t/${tenant}/batches/${batch}`);
|
|
1322
1482
|
}
|
|
1483
|
+
async function cancel(batch, extra) {
|
|
1484
|
+
if (!batch || extra) {
|
|
1485
|
+
throw new Error("usage: motions cancel <batch> [--reason text] [--yes]");
|
|
1486
|
+
}
|
|
1487
|
+
const confirm = values.yes ? void 0 : async (summary) => {
|
|
1488
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
1489
|
+
throw new Error(`refusing to cancel ${batch} without confirmation (pass --yes)`);
|
|
1490
|
+
}
|
|
1491
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
1492
|
+
try {
|
|
1493
|
+
const answer = await rl.question(`${summary} [y/N] `);
|
|
1494
|
+
return /^y(es)?$/i.test(answer.trim());
|
|
1495
|
+
} finally {
|
|
1496
|
+
rl.close();
|
|
1497
|
+
}
|
|
1498
|
+
};
|
|
1499
|
+
const out = await cancelBatch((path, body, t) => api(url, path, body, t), {
|
|
1500
|
+
batch,
|
|
1501
|
+
reason: values.reason,
|
|
1502
|
+
tenant: optionalTenant(),
|
|
1503
|
+
confirm
|
|
1504
|
+
});
|
|
1505
|
+
if (!out.canceled) {
|
|
1506
|
+
console.log("aborted");
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
console.log(`canceled batch ${batch}`);
|
|
1510
|
+
console.log(` ${url}/t/${out.tenant}/batches/${batch}`);
|
|
1511
|
+
}
|
|
1323
1512
|
var commands = {
|
|
1324
1513
|
check,
|
|
1325
1514
|
push,
|
|
@@ -1327,9 +1516,22 @@ var commands = {
|
|
|
1327
1516
|
test,
|
|
1328
1517
|
open,
|
|
1329
1518
|
retry,
|
|
1330
|
-
|
|
1331
|
-
|
|
1519
|
+
cancel,
|
|
1520
|
+
init: async (dir) => {
|
|
1521
|
+
let approver = values.approver ?? handleFrom(process.env.MOTIONS_APPROVER) ?? void 0;
|
|
1522
|
+
if (!approver && process.env.MOTIONS_TOKEN) {
|
|
1523
|
+
try {
|
|
1524
|
+
const tenantId = basename2(resolve6(dir ?? "."));
|
|
1525
|
+
const me = await api(url, "/api/me", void 0, tenantId);
|
|
1526
|
+
const match = me.memberships?.find((m) => m.tenant === tenantId) ?? me.memberships?.[0];
|
|
1527
|
+
approver = handleFrom(match?.handle) ?? void 0;
|
|
1528
|
+
} catch {
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
const resolved = approver ?? defaultApprover();
|
|
1532
|
+
const created = init(dir, { approver: resolved });
|
|
1332
1533
|
console.log(`created ${created}`);
|
|
1534
|
+
console.log(` approver set to "${resolved}" in motions/example/motion.yaml (match your handle in Settings -> Members)`);
|
|
1333
1535
|
},
|
|
1334
1536
|
publish
|
|
1335
1537
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motionskit/cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"esbuild": "^0.28.2",
|
|
30
30
|
"yaml": "^2.9.1",
|
|
31
31
|
"zod": "^4.6.7",
|
|
32
|
-
"@motionskit/kernel": "0.2.
|
|
32
|
+
"@motionskit/kernel": "0.2.5"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/node": "^22.19.0",
|