@motionskit/cli 0.2.0 → 0.2.2
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 +1 -1
- package/dist/index.js +13 -7
- package/dist/main.js +252 -69
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @motionskit/cli
|
|
2
2
|
|
|
3
|
-
`motions init`, `check`, `test`, `push`, `pull` and `
|
|
3
|
+
`motions init`, `check`, `test`, `push`, `pull`, `open` and `retry`.
|
|
4
4
|
|
|
5
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 `http://localhost:8787`. `MOTIONS_TOKEN` is an API key (`mot_…`) sent as `authorization: Bearer`.
|
|
6
6
|
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/compile.ts
|
|
2
2
|
import { createHash as createHash2 } from "crypto";
|
|
3
3
|
import { existsSync as existsSync3, readdirSync as readdirSync3, readFileSync as readFileSync3 } from "fs";
|
|
4
|
-
import {
|
|
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
7
|
CRM_WRITES,
|
|
@@ -265,7 +265,7 @@ function ttlSeconds(ttl) {
|
|
|
265
265
|
}
|
|
266
266
|
async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
|
|
267
267
|
const tenantDir = resolve(tenantRoot);
|
|
268
|
-
const id2 = dir
|
|
268
|
+
const id2 = folderName(dir);
|
|
269
269
|
const where2 = `${tenant}/sources/${id2}`;
|
|
270
270
|
const root = resolve(dir);
|
|
271
271
|
const index = join3(root, "index.ts");
|
|
@@ -351,11 +351,15 @@ function monorepoTenants() {
|
|
|
351
351
|
if (!existsSync3(dir)) return null;
|
|
352
352
|
return readdirSync3(dir).filter((name) => !name.startsWith(".") && existsSync3(join4(dir, name, "tenant.yaml"))).sort().map((name) => join4(dir, name));
|
|
353
353
|
}
|
|
354
|
+
function folderName(dir) {
|
|
355
|
+
const leaf = win32.basename(win32.normalize(dir));
|
|
356
|
+
return leaf !== "." && leaf !== ".." ? leaf : win32.basename(resolve2(dir));
|
|
357
|
+
}
|
|
354
358
|
function tenantIdentity(dir) {
|
|
355
359
|
const meta = readYaml(join4(dir, "tenant.yaml")) ?? {};
|
|
356
360
|
const motionsDir = join4(dir, "motions");
|
|
357
361
|
const motions = existsSync3(motionsDir) ? readdirSync3(motionsDir).filter((name) => existsSync3(join4(motionsDir, name, "motion.yaml"))).sort() : [];
|
|
358
|
-
return { id: meta.id ??
|
|
362
|
+
return { id: meta.id ?? folderName(dir), motions };
|
|
359
363
|
}
|
|
360
364
|
var tenantsDir = () => {
|
|
361
365
|
const dir = resolve2(dirname2(fileURLToPath(import.meta.url)), "../../..", "tenants");
|
|
@@ -387,7 +391,8 @@ var MotionErrors = class extends Error {
|
|
|
387
391
|
async function compileMotion(tenant, dir) {
|
|
388
392
|
const tenantDir = findTenantDir(dir);
|
|
389
393
|
if (!tenantDir) throw new Error(`no tenant.yaml above ${dir}`);
|
|
390
|
-
const
|
|
394
|
+
const folder = folderName(dir);
|
|
395
|
+
const where2 = `${tenant.id}/${folder}/motion.yaml`;
|
|
391
396
|
const parsed = parseMotion(readYaml(join4(dir, "motion.yaml")));
|
|
392
397
|
if (!parsed.ok) throw new MotionErrors(where2, parsed.errors);
|
|
393
398
|
const raw = parsed.motion;
|
|
@@ -426,10 +431,11 @@ async function compileMotion(tenant, dir) {
|
|
|
426
431
|
for (const [id2, q] of Object.entries(raw.queues ?? {})) {
|
|
427
432
|
queues[id2] = { title: q.title, owner: q.owner ?? approvers[0] ?? "operator", deadline: q.deadline, default: q.default ?? "hold" };
|
|
428
433
|
}
|
|
434
|
+
const motion = raw.motion ?? folder;
|
|
429
435
|
const manifest = {
|
|
430
436
|
tenant: tenant.id,
|
|
431
|
-
motion
|
|
432
|
-
title: raw.title ??
|
|
437
|
+
motion,
|
|
438
|
+
title: raw.title ?? motion,
|
|
433
439
|
timezone: raw.timezone ?? tenant.timezone,
|
|
434
440
|
calendar: raw.calendar ?? {},
|
|
435
441
|
approvers,
|
|
@@ -456,7 +462,7 @@ function compileInput(input) {
|
|
|
456
462
|
async function compileTenant(tenantDir) {
|
|
457
463
|
const dir = resolve2(tenantDir);
|
|
458
464
|
const meta = readYaml(join4(dir, "tenant.yaml")) ?? {};
|
|
459
|
-
const id2 = meta.id ??
|
|
465
|
+
const id2 = meta.id ?? folderName(tenantDir);
|
|
460
466
|
const timezone = meta.timezone ?? "America/Sao_Paulo";
|
|
461
467
|
const connection = readYaml(join4(dir, "crm.yaml"));
|
|
462
468
|
const seedPath = join4(dir, "crm-seed.json");
|
package/dist/main.js
CHANGED
|
@@ -1,11 +1,104 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/main.ts
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { dirname as dirname5, join as join8, resolve as resolve5, sep as sep4 } from "path";
|
|
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";
|
|
7
6
|
import { parseArgs } from "util";
|
|
8
7
|
|
|
8
|
+
// src/batches.ts
|
|
9
|
+
function matchMotionsList(lists, query) {
|
|
10
|
+
const byId = lists.find((l) => l.id === query);
|
|
11
|
+
if (byId) return byId;
|
|
12
|
+
const exact = lists.filter((l) => l.name === query);
|
|
13
|
+
if (exact.length === 1) return exact[0];
|
|
14
|
+
if (exact.length > 1) {
|
|
15
|
+
throw new Error(`"${query}" matches ${exact.length} Motions lists (${exact.map((l) => l.id).join(", ")}); pass the list id`);
|
|
16
|
+
}
|
|
17
|
+
const folded = query.trim().toLowerCase();
|
|
18
|
+
const byName = lists.filter((l) => l.name.trim().toLowerCase() === folded);
|
|
19
|
+
if (byName.length === 1) return byName[0];
|
|
20
|
+
if (byName.length > 1) {
|
|
21
|
+
throw new Error(`"${query}" matches ${byName.length} Motions lists (${byName.map((l) => l.id).join(", ")}); pass the list id`);
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
async function openBatch(call, input) {
|
|
26
|
+
if (input.list !== void 0 && input.companies !== void 0) {
|
|
27
|
+
throw new Error("pass --list or --companies, not both");
|
|
28
|
+
}
|
|
29
|
+
if (input.again && input.list === void 0) {
|
|
30
|
+
throw new Error("--again requires --list <id-or-name>");
|
|
31
|
+
}
|
|
32
|
+
const base = `/api/tenants/${encodeURIComponent(input.tenant)}`;
|
|
33
|
+
const body = {};
|
|
34
|
+
if (input.label !== void 0) body.label = input.label;
|
|
35
|
+
if (input.companies !== void 0) {
|
|
36
|
+
const ids = input.companies.split(",").map((s) => s.trim()).filter(Boolean);
|
|
37
|
+
if (!ids.length) throw new Error("--companies needs at least one company id");
|
|
38
|
+
body.companies = ids;
|
|
39
|
+
} else if (input.list !== void 0) {
|
|
40
|
+
const query = input.list.trim();
|
|
41
|
+
if (!query) throw new Error("--list needs a list id or name");
|
|
42
|
+
const { lists } = await call(`${base}/lists`, void 0, input.tenant);
|
|
43
|
+
const matched = matchMotionsList(lists, query);
|
|
44
|
+
if (matched) {
|
|
45
|
+
body.fromList = { id: matched.id, ...input.again ? { again: true } : {} };
|
|
46
|
+
} else {
|
|
47
|
+
if (input.again) throw new Error(`no Motions list "${query}" in ${input.tenant}; --again only applies to Motions lists`);
|
|
48
|
+
body.list = query;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return call(`${base}/motions/${encodeURIComponent(input.motion)}/batches`, body, input.tenant);
|
|
52
|
+
}
|
|
53
|
+
function isStoppedOrFailed(cell) {
|
|
54
|
+
if (!cell || cell.fenced) return false;
|
|
55
|
+
return cell.state === "failed" || cell.state === "parked" && cell.nextAt === void 0;
|
|
56
|
+
}
|
|
57
|
+
function runnableSteps(motion) {
|
|
58
|
+
return new Set(motion.steps.filter((s) => s.kind === "company" || s.kind === "batch").map((s) => s.id));
|
|
59
|
+
}
|
|
60
|
+
function findCompany(companies, query) {
|
|
61
|
+
return companies.find((c) => c.key === query) ?? companies.find((c) => c.key.endsWith(`:${query}`));
|
|
62
|
+
}
|
|
63
|
+
function selectRetries(snap, batch, options2 = {}) {
|
|
64
|
+
const runnable = runnableSteps(snap.motion);
|
|
65
|
+
const step2 = options2.step?.trim();
|
|
66
|
+
const company = options2.company?.trim();
|
|
67
|
+
if (step2 !== void 0) {
|
|
68
|
+
if (!step2) throw new Error("--step needs a step id");
|
|
69
|
+
if (!runnable.has(step2)) throw new Error(`${step2} is not a runnable step in ${snap.motion.motion}`);
|
|
70
|
+
}
|
|
71
|
+
if (company !== void 0) {
|
|
72
|
+
if (!company) throw new Error("--company needs a company id");
|
|
73
|
+
const found = findCompany(snap.status.companies, company);
|
|
74
|
+
if (!found) throw new Error(`no company ${company} in ${batch}`);
|
|
75
|
+
if (step2) {
|
|
76
|
+
if (found.cells[step2]?.fenced) throw new Error(`${found.key} is already sealed past ${step2} in ${batch}`);
|
|
77
|
+
return [{ company: found.key, step: step2 }];
|
|
78
|
+
}
|
|
79
|
+
const steps = snap.motion.steps.filter((s) => runnable.has(s.id) && isStoppedOrFailed(found.cells[s.id])).map((s) => s.id);
|
|
80
|
+
if (!steps.length) throw new Error(`${found.key} has no failed or stopped step in ${batch}; pass --step <id>`);
|
|
81
|
+
return steps.map((s) => ({ company: found.key, step: s }));
|
|
82
|
+
}
|
|
83
|
+
const ordered = step2 ? [step2] : snap.motion.steps.filter((s) => runnable.has(s.id)).map((s) => s.id);
|
|
84
|
+
const out = [];
|
|
85
|
+
for (const c of snap.status.companies) {
|
|
86
|
+
for (const s of ordered) {
|
|
87
|
+
if (isStoppedOrFailed(c.cells[s])) out.push({ company: c.key, step: s });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
async function retryBatch(call, input) {
|
|
93
|
+
const snap = await call(`/api/batches/${encodeURIComponent(input.batch)}`, void 0, input.tenant);
|
|
94
|
+
const tenant = snap.status.tenant;
|
|
95
|
+
const targets = selectRetries(snap, input.batch, { company: input.company, step: input.step });
|
|
96
|
+
for (const target of targets) {
|
|
97
|
+
await call(`/api/batches/${encodeURIComponent(input.batch)}/retry`, target, tenant);
|
|
98
|
+
}
|
|
99
|
+
return { tenant, retried: targets };
|
|
100
|
+
}
|
|
101
|
+
|
|
9
102
|
// src/client.ts
|
|
10
103
|
import { existsSync, readFileSync } from "fs";
|
|
11
104
|
import { dirname, join } from "path";
|
|
@@ -23,9 +116,9 @@ function tokenFiles() {
|
|
|
23
116
|
}
|
|
24
117
|
return files;
|
|
25
118
|
}
|
|
26
|
-
function tokenFor(tenant, url2) {
|
|
119
|
+
function tokenFor(tenant, url2, batch) {
|
|
27
120
|
if (process.env.MOTIONS_TOKEN) return process.env.MOTIONS_TOKEN;
|
|
28
|
-
if (!tenant) return void 0;
|
|
121
|
+
if (!tenant && !batch) return void 0;
|
|
29
122
|
let hostname;
|
|
30
123
|
try {
|
|
31
124
|
hostname = new URL(url2).hostname;
|
|
@@ -36,8 +129,16 @@ function tokenFor(tenant, url2) {
|
|
|
36
129
|
for (const file2 of tokenFiles()) {
|
|
37
130
|
if (!existsSync(file2)) continue;
|
|
38
131
|
try {
|
|
39
|
-
const
|
|
40
|
-
if (
|
|
132
|
+
const tenants = JSON.parse(readFileSync(file2, "utf8")).tenants;
|
|
133
|
+
if (!tenants) continue;
|
|
134
|
+
if (tenant) {
|
|
135
|
+
if (tenants[tenant]) return tenants[tenant];
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (batch) {
|
|
139
|
+
const match = Object.keys(tenants).filter((t) => batch === t || batch.startsWith(`${t}-`)).sort((a, b) => b.length - a.length)[0];
|
|
140
|
+
if (match && tenants[match]) return tenants[match];
|
|
141
|
+
}
|
|
41
142
|
} catch {
|
|
42
143
|
}
|
|
43
144
|
}
|
|
@@ -58,9 +159,10 @@ function describeFailure(path, status, text) {
|
|
|
58
159
|
if (status === 403) return code ? `forbidden (${code}): ${message}` : `forbidden: ${message}`;
|
|
59
160
|
return `${path} \u2192 ${status}${code ? ` ${code}` : ""}: ${message}`;
|
|
60
161
|
}
|
|
61
|
-
async function api(url2, path, body) {
|
|
62
|
-
const
|
|
63
|
-
const
|
|
162
|
+
async function api(url2, path, body, tenant) {
|
|
163
|
+
const pathTenant = tenant ?? path.match(/\/api\/tenants\/([^/]+)/)?.[1];
|
|
164
|
+
const batch = pathTenant ? void 0 : path.match(/\/api\/batches\/([^/?#]+)/)?.[1];
|
|
165
|
+
const key = tokenFor(pathTenant ? decodeURIComponent(pathTenant) : void 0, url2, batch ? decodeURIComponent(batch) : void 0);
|
|
64
166
|
const headers = { accept: "application/json", ...key ? { authorization: `Bearer ${key}` } : {} };
|
|
65
167
|
if (body !== void 0) headers["content-type"] = "application/json";
|
|
66
168
|
const res = await fetch(`${url2.replace(/\/$/, "")}${path}`, {
|
|
@@ -76,7 +178,7 @@ async function api(url2, path, body) {
|
|
|
76
178
|
// src/compile.ts
|
|
77
179
|
import { createHash as createHash2 } from "crypto";
|
|
78
180
|
import { existsSync as existsSync4, readdirSync as readdirSync3, readFileSync as readFileSync4 } from "fs";
|
|
79
|
-
import {
|
|
181
|
+
import { dirname as dirname3, isAbsolute as isAbsolute2, join as join5, relative as relative2, resolve as resolve2, sep as sep2, win32 } from "path";
|
|
80
182
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
81
183
|
import {
|
|
82
184
|
CRM_WRITES,
|
|
@@ -352,7 +454,7 @@ function ttlSeconds(ttl) {
|
|
|
352
454
|
}
|
|
353
455
|
async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
|
|
354
456
|
const tenantDir = resolve(tenantRoot);
|
|
355
|
-
const id2 = dir
|
|
457
|
+
const id2 = folderName(dir);
|
|
356
458
|
const where2 = `${tenant}/sources/${id2}`;
|
|
357
459
|
const root = resolve(dir);
|
|
358
460
|
const index = join4(root, "index.ts");
|
|
@@ -479,11 +581,15 @@ function monorepoTenants() {
|
|
|
479
581
|
if (!existsSync4(dir)) return null;
|
|
480
582
|
return readdirSync3(dir).filter((name) => !name.startsWith(".") && existsSync4(join5(dir, name, "tenant.yaml"))).sort().map((name) => join5(dir, name));
|
|
481
583
|
}
|
|
584
|
+
function folderName(dir) {
|
|
585
|
+
const leaf = win32.basename(win32.normalize(dir));
|
|
586
|
+
return leaf !== "." && leaf !== ".." ? leaf : win32.basename(resolve2(dir));
|
|
587
|
+
}
|
|
482
588
|
function tenantIdentity(dir) {
|
|
483
589
|
const meta = readYaml(join5(dir, "tenant.yaml")) ?? {};
|
|
484
590
|
const motionsDir = join5(dir, "motions");
|
|
485
591
|
const motions = existsSync4(motionsDir) ? readdirSync3(motionsDir).filter((name) => existsSync4(join5(motionsDir, name, "motion.yaml"))).sort() : [];
|
|
486
|
-
return { id: meta.id ??
|
|
592
|
+
return { id: meta.id ?? folderName(dir), motions };
|
|
487
593
|
}
|
|
488
594
|
var readYaml = (path) => YAML2.parse(readFileSync4(path, "utf8"));
|
|
489
595
|
var grant = (g) => ({ read: g?.read ?? [], propose: g?.propose ?? [] });
|
|
@@ -510,7 +616,8 @@ var MotionErrors = class extends Error {
|
|
|
510
616
|
async function compileMotion(tenant, dir) {
|
|
511
617
|
const tenantDir = findTenantDir(dir);
|
|
512
618
|
if (!tenantDir) throw new Error(`no tenant.yaml above ${dir}`);
|
|
513
|
-
const
|
|
619
|
+
const folder = folderName(dir);
|
|
620
|
+
const where2 = `${tenant.id}/${folder}/motion.yaml`;
|
|
514
621
|
const parsed2 = parseMotion(readYaml(join5(dir, "motion.yaml")));
|
|
515
622
|
if (!parsed2.ok) throw new MotionErrors(where2, parsed2.errors);
|
|
516
623
|
const raw = parsed2.motion;
|
|
@@ -549,10 +656,11 @@ async function compileMotion(tenant, dir) {
|
|
|
549
656
|
for (const [id2, q] of Object.entries(raw.queues ?? {})) {
|
|
550
657
|
queues[id2] = { title: q.title, owner: q.owner ?? approvers[0] ?? "operator", deadline: q.deadline, default: q.default ?? "hold" };
|
|
551
658
|
}
|
|
659
|
+
const motion = raw.motion ?? folder;
|
|
552
660
|
const manifest = {
|
|
553
661
|
tenant: tenant.id,
|
|
554
|
-
motion
|
|
555
|
-
title: raw.title ??
|
|
662
|
+
motion,
|
|
663
|
+
title: raw.title ?? motion,
|
|
556
664
|
timezone: raw.timezone ?? tenant.timezone,
|
|
557
665
|
calendar: raw.calendar ?? {},
|
|
558
666
|
approvers,
|
|
@@ -579,7 +687,7 @@ function compileInput(input) {
|
|
|
579
687
|
async function compileTenant(tenantDir) {
|
|
580
688
|
const dir = resolve2(tenantDir);
|
|
581
689
|
const meta = readYaml(join5(dir, "tenant.yaml")) ?? {};
|
|
582
|
-
const id2 = meta.id ??
|
|
690
|
+
const id2 = meta.id ?? folderName(tenantDir);
|
|
583
691
|
const timezone = meta.timezone ?? "America/Sao_Paulo";
|
|
584
692
|
const connection = readYaml(join5(dir, "crm.yaml"));
|
|
585
693
|
const seedPath = join5(dir, "crm-seed.json");
|
|
@@ -601,7 +709,7 @@ function tenantCrmProblems(tenant, systems = CRM_WRITES) {
|
|
|
601
709
|
|
|
602
710
|
// src/init.ts
|
|
603
711
|
import { existsSync as existsSync5, mkdirSync as mkdirSync2, readdirSync as readdirSync4, readFileSync as readFileSync5, writeFileSync as writeFileSync2 } from "fs";
|
|
604
|
-
import { basename
|
|
712
|
+
import { basename, dirname as dirname4, join as join6, resolve as resolve3 } from "path";
|
|
605
713
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
606
714
|
var TENANT_ID = /^[a-z][a-z0-9_-]{0,62}$/;
|
|
607
715
|
function cliVersion() {
|
|
@@ -614,7 +722,7 @@ var file = (dir, path, contents) => {
|
|
|
614
722
|
};
|
|
615
723
|
function init(target = ".") {
|
|
616
724
|
const dir = resolve3(target);
|
|
617
|
-
const id2 =
|
|
725
|
+
const id2 = basename(dir);
|
|
618
726
|
if (!TENANT_ID.test(id2)) throw new Error(`${id2} is not a tenant id: lowercase letters, digits, - and _, starting with a letter`);
|
|
619
727
|
if (existsSync5(dir) && readdirSync4(dir).length) throw new Error(`${dir} is not empty`);
|
|
620
728
|
mkdirSync2(dir, { recursive: true });
|
|
@@ -818,13 +926,78 @@ function inside(root, rel) {
|
|
|
818
926
|
return dest;
|
|
819
927
|
}
|
|
820
928
|
|
|
929
|
+
// src/vitest.ts
|
|
930
|
+
import { spawnSync } from "child_process";
|
|
931
|
+
import { existsSync as existsSync7, mkdirSync as mkdirSync3, readFileSync as readFileSync7, writeFileSync as writeFileSync3 } from "fs";
|
|
932
|
+
import { dirname as dirname5, join as join8, resolve as resolve5, sep as sep4 } from "path";
|
|
933
|
+
function findVitestPackage(dir) {
|
|
934
|
+
for (let at = resolve5(dir); ; ) {
|
|
935
|
+
const candidate = join8(at, "node_modules", "vitest", "package.json");
|
|
936
|
+
if (existsSync7(candidate)) return candidate;
|
|
937
|
+
const parent = dirname5(at);
|
|
938
|
+
if (parent === at) return null;
|
|
939
|
+
at = parent;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
function resolveVitest(dir) {
|
|
943
|
+
const pkgPath = findVitestPackage(dir);
|
|
944
|
+
if (pkgPath) {
|
|
945
|
+
try {
|
|
946
|
+
const pkg = JSON.parse(readFileSync7(pkgPath, "utf8"));
|
|
947
|
+
const rel = typeof pkg.bin === "string" ? pkg.bin : pkg.bin?.vitest ?? "./vitest.mjs";
|
|
948
|
+
const entry = resolve5(dirname5(pkgPath), rel);
|
|
949
|
+
if (existsSync7(entry)) return entry;
|
|
950
|
+
} catch {
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
throw new Error(`${dir} has no vitest. Add it to devDependencies and install.`);
|
|
954
|
+
}
|
|
955
|
+
function vitestConfig(dir) {
|
|
956
|
+
const root = resolve5(dir);
|
|
957
|
+
const folder = join8(root, "node_modules", ".motions");
|
|
958
|
+
mkdirSync3(folder, { recursive: true });
|
|
959
|
+
const stub = join8(folder, "workers-stub.js");
|
|
960
|
+
writeFileSync3(stub, "export class WorkerEntrypoint {}\n");
|
|
961
|
+
const config = join8(folder, "vitest.config.ts");
|
|
962
|
+
writeFileSync3(
|
|
963
|
+
config,
|
|
964
|
+
`import { defineConfig } from "vitest/config";
|
|
965
|
+
|
|
966
|
+
export default defineConfig({
|
|
967
|
+
root: ${JSON.stringify(root.split(sep4).join("/"))},
|
|
968
|
+
resolve: { alias: { "cloudflare:workers": ${JSON.stringify(stub.split(sep4).join("/"))} } },
|
|
969
|
+
test: {
|
|
970
|
+
include: ["motions/*/tests/**/*.test.ts"],
|
|
971
|
+
passWithNoTests: true,
|
|
972
|
+
server: { deps: { inline: [/@motionskit\\/sdk/, /@motionskit\\/kernel/] } },
|
|
973
|
+
},
|
|
974
|
+
});
|
|
975
|
+
`
|
|
976
|
+
);
|
|
977
|
+
return config;
|
|
978
|
+
}
|
|
979
|
+
function runVitest(dir, spawn = spawnSync) {
|
|
980
|
+
const entry = resolveVitest(dir);
|
|
981
|
+
const run2 = spawn(process.execPath, [entry, "run", "--config", vitestConfig(dir)], { cwd: dir, stdio: "inherit" });
|
|
982
|
+
if (run2.error) throw run2.error;
|
|
983
|
+
return run2.status === 0;
|
|
984
|
+
}
|
|
985
|
+
function runTenantTests(dirs, spawn = spawnSync) {
|
|
986
|
+
let failed = 0;
|
|
987
|
+
for (const dir of dirs) {
|
|
988
|
+
if (!runVitest(dir, spawn)) failed++;
|
|
989
|
+
}
|
|
990
|
+
if (failed) throw new Error(`${failed} tenant${failed > 1 ? "s" : ""} failed the tests`);
|
|
991
|
+
}
|
|
992
|
+
|
|
821
993
|
// src/main.ts
|
|
822
|
-
var USAGE = `usage: motions <check|push|pull|test|open|init|publish> \u2026
|
|
994
|
+
var USAGE = `usage: motions <check|push|pull|test|open|retry|init|publish> \u2026
|
|
823
995
|
check [tenant] compile the tenant in this directory, or every tenant in this repo
|
|
824
996
|
push [--live] [-m message] publish a draft (or go live with --live) and print a link
|
|
825
997
|
pull <version> [--motion id] [--force]
|
|
826
998
|
test run motions/*/tests with the tenant's vitest
|
|
827
|
-
open <tenant> <motion> [--list name | --companies id,id] [--label text]
|
|
999
|
+
open <tenant> <motion> [--list id-or-name | --companies id,id] [--label text] [--again]
|
|
1000
|
+
retry <batch> [--company id] [--step id]
|
|
828
1001
|
init [dir] scaffold a tenant repo
|
|
829
1002
|
publish [tenant] from this repo, push --live over tenants/*`;
|
|
830
1003
|
var parsed = (() => {
|
|
@@ -841,6 +1014,9 @@ var parsed = (() => {
|
|
|
841
1014
|
force: { type: "boolean", default: false },
|
|
842
1015
|
list: { type: "string" },
|
|
843
1016
|
companies: { type: "string" },
|
|
1017
|
+
company: { type: "string" },
|
|
1018
|
+
step: { type: "string" },
|
|
1019
|
+
again: { type: "boolean", default: false },
|
|
844
1020
|
label: { type: "string" },
|
|
845
1021
|
reseed: { type: "boolean", default: false },
|
|
846
1022
|
help: { type: "boolean", short: "h", default: false }
|
|
@@ -862,14 +1038,14 @@ var [command, ...args] = positionals;
|
|
|
862
1038
|
var url = values.url.replace(/\/$/, "");
|
|
863
1039
|
function resolveTenants(positional, fallbackAll) {
|
|
864
1040
|
if (values.tenant) {
|
|
865
|
-
const found = findTenantDir(
|
|
1041
|
+
const found = findTenantDir(resolve6(values.tenant));
|
|
866
1042
|
if (!found) throw new Error(`no tenant.yaml in ${values.tenant}`);
|
|
867
1043
|
return [found];
|
|
868
1044
|
}
|
|
869
1045
|
if (positional) {
|
|
870
|
-
const direct = findTenantDir(
|
|
871
|
-
if (
|
|
872
|
-
const match = monorepoTenants()?.find((dir) => dir
|
|
1046
|
+
const direct = findTenantDir(resolve6(positional));
|
|
1047
|
+
if (existsSync8(positional) && direct) return [direct];
|
|
1048
|
+
const match = monorepoTenants()?.find((dir) => folderName(dir) === positional);
|
|
873
1049
|
if (match) return [match];
|
|
874
1050
|
throw new Error(`no tenant ${positional}`);
|
|
875
1051
|
}
|
|
@@ -911,7 +1087,7 @@ async function check(only) {
|
|
|
911
1087
|
for (const dir of resolveTenants(only, true)) {
|
|
912
1088
|
const tenant = await compileTenant(dir).catch((e) => {
|
|
913
1089
|
failed++;
|
|
914
|
-
console.error(`${dir
|
|
1090
|
+
console.error(`${folderName(dir)}: ${e.message}`);
|
|
915
1091
|
return null;
|
|
916
1092
|
});
|
|
917
1093
|
if (!tenant) continue;
|
|
@@ -945,7 +1121,7 @@ async function push() {
|
|
|
945
1121
|
async function publish(only) {
|
|
946
1122
|
const all = monorepoTenants();
|
|
947
1123
|
if (!all) throw new Error("publish runs over this repo's tenants/*; from a tenant repo use motions push --live");
|
|
948
|
-
const dirs = only ? all.filter((dir) => dir
|
|
1124
|
+
const dirs = only ? all.filter((dir) => folderName(dir) === only) : all;
|
|
949
1125
|
if (only && !dirs.length) throw new Error(`no tenant ${only}`);
|
|
950
1126
|
await pushDirs(dirs, !values.draft);
|
|
951
1127
|
}
|
|
@@ -966,8 +1142,8 @@ async function pull(version) {
|
|
|
966
1142
|
if (!motion) throw new Error(`this tenant has ${motions.length} motions; pass --motion <id>`);
|
|
967
1143
|
const sha = await resolveSha(id2, motion, version);
|
|
968
1144
|
const { files } = await api(url, `/api/tenants/${id2}/motions/${motion}/versions/${sha}/files`);
|
|
969
|
-
const motionDir =
|
|
970
|
-
const writes = Object.entries(files).map(([path, next]) => ({ rel:
|
|
1145
|
+
const motionDir = join9(dir, "motions", motion);
|
|
1146
|
+
const writes = Object.entries(files).map(([path, next]) => ({ rel: join9("motions", motion, path), path, next }));
|
|
971
1147
|
if (!values.force) {
|
|
972
1148
|
const blocked = blockedOverwrites(dir, writes);
|
|
973
1149
|
if (blocked.length) throw new Error(`refusing to overwrite uncommitted changes (pass --force):
|
|
@@ -978,11 +1154,11 @@ async function pull(version) {
|
|
|
978
1154
|
let unchanged = 0;
|
|
979
1155
|
for (const write of writes.sort((a, b) => a.rel < b.rel ? -1 : 1)) {
|
|
980
1156
|
const dest = inside(motionDir, write.path);
|
|
981
|
-
const existed =
|
|
982
|
-
const same = existed &&
|
|
1157
|
+
const existed = existsSync8(dest);
|
|
1158
|
+
const same = existed && readFileSync8(dest, "utf8") === write.next;
|
|
983
1159
|
if (!same) {
|
|
984
|
-
|
|
985
|
-
|
|
1160
|
+
mkdirSync4(dirname6(dest), { recursive: true });
|
|
1161
|
+
writeFileSync4(dest, write.next);
|
|
986
1162
|
}
|
|
987
1163
|
if (!existed) {
|
|
988
1164
|
created++;
|
|
@@ -994,55 +1170,62 @@ async function pull(version) {
|
|
|
994
1170
|
}
|
|
995
1171
|
console.log(`pulled ${id2}/${motion} @ ${sha.slice(0, 10)} (${created} created, ${updated} updated, ${unchanged} unchanged)`);
|
|
996
1172
|
}
|
|
997
|
-
function vitestConfig(dir) {
|
|
998
|
-
const folder = join8(dir, "node_modules", ".motions");
|
|
999
|
-
mkdirSync3(folder, { recursive: true });
|
|
1000
|
-
const stub = join8(folder, "workers-stub.js");
|
|
1001
|
-
writeFileSync3(stub, "export class WorkerEntrypoint {}\n");
|
|
1002
|
-
const config = join8(folder, "vitest.config.ts");
|
|
1003
|
-
writeFileSync3(
|
|
1004
|
-
config,
|
|
1005
|
-
`import { defineConfig } from "vitest/config";
|
|
1006
|
-
|
|
1007
|
-
export default defineConfig({
|
|
1008
|
-
root: ${JSON.stringify(dir)},
|
|
1009
|
-
resolve: { alias: { "cloudflare:workers": ${JSON.stringify(stub)} } },
|
|
1010
|
-
test: {
|
|
1011
|
-
include: ["motions/*/tests/**/*.test.ts"],
|
|
1012
|
-
passWithNoTests: true,
|
|
1013
|
-
server: { deps: { inline: [/@motionskit\\/sdk/, /@motionskit\\/kernel/] } },
|
|
1014
|
-
},
|
|
1015
|
-
});
|
|
1016
|
-
`
|
|
1017
|
-
);
|
|
1018
|
-
return config;
|
|
1019
|
-
}
|
|
1020
1173
|
async function test(only) {
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
const
|
|
1026
|
-
|
|
1174
|
+
runTenantTests(resolveTenants(only, true));
|
|
1175
|
+
}
|
|
1176
|
+
function optionalTenant() {
|
|
1177
|
+
if (values.tenant) {
|
|
1178
|
+
const found = findTenantDir(resolve6(values.tenant));
|
|
1179
|
+
return found ? tenantIdentity(found).id : values.tenant;
|
|
1027
1180
|
}
|
|
1028
|
-
|
|
1181
|
+
const here = findTenantDir(process.cwd());
|
|
1182
|
+
return here ? tenantIdentity(here).id : void 0;
|
|
1029
1183
|
}
|
|
1030
|
-
async function open(tenant, motion) {
|
|
1031
|
-
if (!tenant || !motion
|
|
1032
|
-
|
|
1184
|
+
async function open(tenant, motion, extra) {
|
|
1185
|
+
if (!tenant || !motion || extra) {
|
|
1186
|
+
throw new Error("usage: motions open <tenant> <motion> [--list id-or-name | --companies id,id] [--label text] [--again]");
|
|
1187
|
+
}
|
|
1188
|
+
const out = await openBatch((path, body, t) => api(url, path, body, t), {
|
|
1189
|
+
tenant,
|
|
1190
|
+
motion,
|
|
1033
1191
|
list: values.list,
|
|
1034
|
-
companies: values.companies
|
|
1035
|
-
label: values.label
|
|
1192
|
+
companies: values.companies,
|
|
1193
|
+
label: values.label,
|
|
1194
|
+
again: values.again
|
|
1036
1195
|
});
|
|
1037
|
-
|
|
1196
|
+
const suffix = out.reused ? " (existing batch for this list and version; pass --again to open another)" : out.skipped ? ` (${out.skipped} skipped)` : out.missing?.length ? ` (${out.missing.length} not in CRM)` : "";
|
|
1197
|
+
console.log(`${out.reused ? "reused" : "opened"} batch ${out.id} with ${out.companies} companies${suffix}`);
|
|
1038
1198
|
console.log(` ${url}/t/${tenant}/batches/${out.id}`);
|
|
1039
1199
|
}
|
|
1200
|
+
async function retry(batch, extra) {
|
|
1201
|
+
if (!batch || extra || values.companies !== void 0) {
|
|
1202
|
+
throw new Error("usage: motions retry <batch> [--company id] [--step id]");
|
|
1203
|
+
}
|
|
1204
|
+
const { tenant, retried } = await retryBatch((path, body, t) => api(url, path, body, t), {
|
|
1205
|
+
batch,
|
|
1206
|
+
company: values.company,
|
|
1207
|
+
step: values.step,
|
|
1208
|
+
tenant: optionalTenant()
|
|
1209
|
+
});
|
|
1210
|
+
if (!retried.length) {
|
|
1211
|
+
console.log(values.step ? `nothing to retry at ${values.step} in ${batch}` : `nothing to retry in ${batch}`);
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
if (retried.length === 1) {
|
|
1215
|
+
console.log(`retried ${retried[0].company} at ${retried[0].step}`);
|
|
1216
|
+
} else {
|
|
1217
|
+
console.log(`retried ${retried.length} cells in ${batch}:`);
|
|
1218
|
+
for (const target of retried) console.log(` ${target.company} \xB7 ${target.step}`);
|
|
1219
|
+
}
|
|
1220
|
+
console.log(` ${url}/t/${tenant}/batches/${batch}`);
|
|
1221
|
+
}
|
|
1040
1222
|
var commands = {
|
|
1041
1223
|
check,
|
|
1042
1224
|
push,
|
|
1043
1225
|
pull,
|
|
1044
1226
|
test,
|
|
1045
1227
|
open,
|
|
1228
|
+
retry,
|
|
1046
1229
|
init: (dir) => {
|
|
1047
1230
|
const created = init(dir);
|
|
1048
1231
|
console.log(`created ${created}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motionskit/cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
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.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/node": "^22.19.0",
|