@motionskit/cli 0.2.6 → 0.2.8
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/index.js +88 -18
- package/dist/main.js +226 -36
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -5,8 +5,12 @@ import { dirname as dirname2, isAbsolute as isAbsolute2, join as join4, relative
|
|
|
5
5
|
import { fileURLToPath } from "url";
|
|
6
6
|
import {
|
|
7
7
|
BUILTIN_SOURCE_OPS,
|
|
8
|
+
bundleUploadProblem as bundleUploadProblem2,
|
|
8
9
|
CRM_WRITES,
|
|
9
10
|
crmProblems,
|
|
11
|
+
fileUploadProblems as fileUploadProblems2,
|
|
12
|
+
manifestUploadProblem as manifestUploadProblem2,
|
|
13
|
+
optionReasonProblems,
|
|
10
14
|
rulesVersion,
|
|
11
15
|
STEP_BUILD,
|
|
12
16
|
STEP_ENTRY_FILE,
|
|
@@ -78,8 +82,27 @@ function readTenantFixtures(dir, sources) {
|
|
|
78
82
|
}
|
|
79
83
|
|
|
80
84
|
// src/manifest.ts
|
|
81
|
-
import { parseDuration } from "@motionskit/kernel";
|
|
85
|
+
import { isValidTimeZone, parseDuration } from "@motionskit/kernel";
|
|
82
86
|
import { z } from "zod";
|
|
87
|
+
var TENANT_ID = /^[a-z][a-z0-9_-]{0,62}$/;
|
|
88
|
+
var tenantId = z.string().regex(TENANT_ID, "tenant ids are lowercase letters, digits, - and _");
|
|
89
|
+
var RawTenant = z.object({
|
|
90
|
+
id: tenantId.optional(),
|
|
91
|
+
title: z.string().optional(),
|
|
92
|
+
timezone: z.string().optional(),
|
|
93
|
+
sources: z.object({ fixtures: z.boolean().optional() }).nullable().optional()
|
|
94
|
+
});
|
|
95
|
+
function parseTenant(raw) {
|
|
96
|
+
const doc = raw ?? {};
|
|
97
|
+
const result = RawTenant.safeParse(doc);
|
|
98
|
+
const errors = result.success ? [] : result.error.issues.map((i) => i.path.length ? `${i.path.join(".")}: ${i.message}` : i.message);
|
|
99
|
+
const tz = doc && typeof doc === "object" && !Array.isArray(doc) ? doc.timezone : void 0;
|
|
100
|
+
if (typeof tz === "string" && !isValidTimeZone(tz)) {
|
|
101
|
+
errors.push(`timezone "${tz}" is not a valid IANA time zone`);
|
|
102
|
+
}
|
|
103
|
+
if (result.success && !errors.length) return { ok: true, tenant: result.data };
|
|
104
|
+
return { ok: false, errors };
|
|
105
|
+
}
|
|
83
106
|
var ID = /^[a-z][a-z0-9_]*$/;
|
|
84
107
|
var id = z.string().regex(ID, "ids are lowercase snake_case");
|
|
85
108
|
var json = z.lazy(() => z.union([z.null(), z.boolean(), z.number(), z.string(), z.array(json), z.record(z.string(), json)]));
|
|
@@ -219,7 +242,14 @@ function parseMotion(raw) {
|
|
|
219
242
|
import { createHash } from "crypto";
|
|
220
243
|
import { existsSync as existsSync2, mkdirSync, readdirSync as readdirSync2, readFileSync as readFileSync2, writeFileSync } from "fs";
|
|
221
244
|
import { dirname, isAbsolute, join as join3, relative, resolve, sep } from "path";
|
|
222
|
-
import {
|
|
245
|
+
import {
|
|
246
|
+
bundleUploadProblem,
|
|
247
|
+
fileUploadProblems,
|
|
248
|
+
manifestUploadProblem,
|
|
249
|
+
parseDuration as parseDuration2,
|
|
250
|
+
validateSourceManifest,
|
|
251
|
+
versionId
|
|
252
|
+
} from "@motionskit/kernel";
|
|
223
253
|
import { build } from "esbuild";
|
|
224
254
|
import YAML from "yaml";
|
|
225
255
|
import { z as z2 } from "zod";
|
|
@@ -334,7 +364,15 @@ async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
|
|
|
334
364
|
const index = join3(root, "index.ts");
|
|
335
365
|
if (!existsSync2(index)) throw new SourceErrors(where2, ["index.ts does not exist"]);
|
|
336
366
|
const yamlPath = join3(root, "source.yaml");
|
|
337
|
-
|
|
367
|
+
let rawYaml = null;
|
|
368
|
+
if (existsSync2(yamlPath)) {
|
|
369
|
+
try {
|
|
370
|
+
rawYaml = YAML.parse(readFileSync2(yamlPath, "utf8"));
|
|
371
|
+
} catch (e) {
|
|
372
|
+
throw new SourceErrors(`${where2}/source.yaml`, [`invalid YAML: ${e.message}`]);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const yaml = SourceYaml.safeParse(rawYaml);
|
|
338
376
|
if (!yaml.success)
|
|
339
377
|
throw new SourceErrors(
|
|
340
378
|
`${where2}/source.yaml`,
|
|
@@ -400,8 +438,6 @@ export default serveSource(def);
|
|
|
400
438
|
ops,
|
|
401
439
|
bundle: sha256(bundle2)
|
|
402
440
|
};
|
|
403
|
-
errors.push(...validateSourceManifest(manifest));
|
|
404
|
-
if (errors.length) throw new SourceErrors(where2, errors);
|
|
405
441
|
const files = {};
|
|
406
442
|
for (const input of Object.keys(out.metafile.inputs).map((p) => resolve(tenantDir, p))) {
|
|
407
443
|
const path = relative(root, input);
|
|
@@ -409,6 +445,12 @@ export default serveSource(def);
|
|
|
409
445
|
files[path.split(sep).join("/")] = readFileSync2(input, "utf8");
|
|
410
446
|
}
|
|
411
447
|
if (existsSync2(yamlPath)) files["source.yaml"] = readFileSync2(yamlPath, "utf8");
|
|
448
|
+
errors.push(...validateSourceManifest(manifest), ...fileUploadProblems(files));
|
|
449
|
+
const bundleProblem = bundleUploadProblem(manifest.bundle, bundle2);
|
|
450
|
+
if (bundleProblem) errors.push(bundleProblem);
|
|
451
|
+
const manifestProblem = manifestUploadProblem(manifest);
|
|
452
|
+
if (manifestProblem) errors.push(manifestProblem);
|
|
453
|
+
if (errors.length) throw new SourceErrors(where2, errors);
|
|
412
454
|
return { dir: root, manifest: { ...manifest, sha: versionId(manifest, files) }, bundle: bundle2, files };
|
|
413
455
|
}
|
|
414
456
|
async function compileSources(tenantDir, tenant) {
|
|
@@ -440,17 +482,32 @@ function folderName(dir) {
|
|
|
440
482
|
return leaf !== "." && leaf !== ".." ? leaf : win32.basename(resolve2(dir));
|
|
441
483
|
}
|
|
442
484
|
function tenantIdentity(dir) {
|
|
443
|
-
const meta = readYaml(join4(dir, "tenant.yaml"))
|
|
485
|
+
const meta = readYaml(join4(dir, "tenant.yaml"), "tenant.yaml");
|
|
486
|
+
const parsed = parseTenant(meta);
|
|
487
|
+
const id2 = parsed.ok && parsed.tenant.id ? parsed.tenant.id : folderName(dir);
|
|
444
488
|
const motionsDir = join4(dir, "motions");
|
|
445
489
|
const motions = existsSync3(motionsDir) ? readdirSync3(motionsDir).filter((name) => existsSync3(join4(motionsDir, name, "motion.yaml"))).sort() : [];
|
|
446
|
-
return { id:
|
|
490
|
+
return { id: id2, motions };
|
|
447
491
|
}
|
|
448
492
|
var tenantsDir = () => {
|
|
449
493
|
const dir = resolve2(dirname2(fileURLToPath(import.meta.url)), "../../..", "tenants");
|
|
450
494
|
if (!existsSync3(dir)) throw new Error("no tenants directory; run this from a tenant, or from the motions repo");
|
|
451
495
|
return dir;
|
|
452
496
|
};
|
|
453
|
-
var readYaml = (path) =>
|
|
497
|
+
var readYaml = (path, label) => {
|
|
498
|
+
try {
|
|
499
|
+
return YAML2.parse(readFileSync3(path, "utf8"));
|
|
500
|
+
} catch (e) {
|
|
501
|
+
throw new Error(`${label ?? folderName(path)}: invalid YAML: ${e.message}`);
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
var readJson = (path, label) => {
|
|
505
|
+
try {
|
|
506
|
+
return JSON.parse(readFileSync3(path, "utf8"));
|
|
507
|
+
} catch (e) {
|
|
508
|
+
throw new Error(`${label}: invalid JSON: ${e.message}`);
|
|
509
|
+
}
|
|
510
|
+
};
|
|
454
511
|
var grant = (g) => ({ read: g?.read ?? [], propose: g?.propose ?? [] });
|
|
455
512
|
async function bundle(stepFile, tenantDir) {
|
|
456
513
|
const result = await build2({
|
|
@@ -477,11 +534,11 @@ async function compileMotion(tenant, dir, catalog = new Set(BUILTIN_SOURCE_OPS))
|
|
|
477
534
|
if (!tenantDir) throw new Error(`no tenant.yaml above ${dir}`);
|
|
478
535
|
const folder = folderName(dir);
|
|
479
536
|
const where2 = `${tenant.id}/${folder}/motion.yaml`;
|
|
480
|
-
const parsed = parseMotion(readYaml(join4(dir, "motion.yaml")));
|
|
537
|
+
const parsed = parseMotion(readYaml(join4(dir, "motion.yaml"), `motions/${folder}/motion.yaml`));
|
|
481
538
|
if (!parsed.ok) throw new MotionErrors(where2, parsed.errors);
|
|
482
539
|
const raw = parsed.motion;
|
|
483
540
|
const rulesPath = join4(dir, "rules.yaml");
|
|
484
|
-
const rules = existsSync3(rulesPath) ? readYaml(rulesPath) ?? {} : {};
|
|
541
|
+
const rules = existsSync3(rulesPath) ? readYaml(rulesPath, `motions/${folder}/rules.yaml`) ?? {} : {};
|
|
485
542
|
const bundles = {};
|
|
486
543
|
const files = { "motion.yaml": readFileSync3(join4(dir, "motion.yaml"), "utf8") };
|
|
487
544
|
if (existsSync3(rulesPath)) files["rules.yaml"] = readFileSync3(rulesPath, "utf8");
|
|
@@ -530,7 +587,17 @@ async function compileMotion(tenant, dir, catalog = new Set(BUILTIN_SOURCE_OPS))
|
|
|
530
587
|
steps,
|
|
531
588
|
queues
|
|
532
589
|
};
|
|
533
|
-
const errors =
|
|
590
|
+
const errors = [
|
|
591
|
+
...validateManifest(manifest, catalog),
|
|
592
|
+
...optionReasonProblems(manifest.reasons, files),
|
|
593
|
+
...fileUploadProblems2(files),
|
|
594
|
+
...Object.entries(bundles).flatMap(([sha, code]) => {
|
|
595
|
+
const p = bundleUploadProblem2(sha, code);
|
|
596
|
+
return p ? [p] : [];
|
|
597
|
+
})
|
|
598
|
+
];
|
|
599
|
+
const manifestProblem = manifestUploadProblem2(manifest);
|
|
600
|
+
if (manifestProblem) errors.push(manifestProblem);
|
|
534
601
|
if (errors.length) throw new MotionErrors(where2, errors);
|
|
535
602
|
return { dir: root, manifest: { ...manifest, sha: versionId2(manifest, files) }, bundles, files };
|
|
536
603
|
}
|
|
@@ -545,18 +612,21 @@ function compileInput(input) {
|
|
|
545
612
|
}
|
|
546
613
|
async function compileTenant(tenantDir) {
|
|
547
614
|
const dir = resolve2(tenantDir);
|
|
548
|
-
const
|
|
615
|
+
const rawMeta = readYaml(join4(dir, "tenant.yaml"), "tenant.yaml");
|
|
616
|
+
const parsedMeta = parseTenant(rawMeta);
|
|
617
|
+
if (!parsedMeta.ok) {
|
|
618
|
+
if (parsedMeta.errors.length === 1) throw new Error(`tenant.yaml: ${parsedMeta.errors[0]}`);
|
|
619
|
+
throw new MotionErrors("tenant.yaml", parsedMeta.errors);
|
|
620
|
+
}
|
|
621
|
+
const meta = parsedMeta.tenant;
|
|
549
622
|
const id2 = meta.id ?? folderName(tenantDir);
|
|
550
623
|
const timezone = meta.timezone ?? "America/Sao_Paulo";
|
|
551
|
-
const connection = readYaml(join4(dir, "crm.yaml"));
|
|
624
|
+
const connection = readYaml(join4(dir, "crm.yaml"), "crm.yaml");
|
|
552
625
|
const seedPath = join4(dir, "crm-seed.json");
|
|
553
|
-
const seed = existsSync3(seedPath) ?
|
|
626
|
+
const seed = existsSync3(seedPath) ? readJson(seedPath, "crm-seed.json") : null;
|
|
554
627
|
const fixtures = readTenantFixtures(dir, meta.sources);
|
|
555
628
|
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
|
-
]);
|
|
629
|
+
const catalog = /* @__PURE__ */ new Set([...BUILTIN_SOURCE_OPS, ...sources.flatMap((s) => Object.keys(s.manifest.ops).map((op) => `${s.manifest.source}.${op}`))]);
|
|
560
630
|
const motionsDir = join4(dir, "motions");
|
|
561
631
|
const motions = [];
|
|
562
632
|
for (const name of readdirSync3(motionsDir).sort()) {
|
package/dist/main.js
CHANGED
|
@@ -27,9 +27,6 @@ async function openBatch(call, input) {
|
|
|
27
27
|
if (input.list !== void 0 && input.companies !== void 0) {
|
|
28
28
|
throw new Error("pass --list or --companies, not both");
|
|
29
29
|
}
|
|
30
|
-
if (input.again && input.list === void 0) {
|
|
31
|
-
throw new Error("--again requires --list <id-or-name>");
|
|
32
|
-
}
|
|
33
30
|
const base = `/api/tenants/${encodeURIComponent(input.tenant)}`;
|
|
34
31
|
const body = {};
|
|
35
32
|
if (input.label !== void 0) body.label = input.label;
|
|
@@ -45,7 +42,6 @@ async function openBatch(call, input) {
|
|
|
45
42
|
if (matched) {
|
|
46
43
|
body.fromList = { id: matched.id, ...input.again ? { again: true } : {} };
|
|
47
44
|
} else {
|
|
48
|
-
if (input.again) throw new Error(`no Motions list "${query}" in ${input.tenant}; --again only applies to Motions lists`);
|
|
49
45
|
body.list = query;
|
|
50
46
|
}
|
|
51
47
|
}
|
|
@@ -179,9 +175,25 @@ function formatDetails(details) {
|
|
|
179
175
|
if (Array.isArray(details)) return details.length ? `
|
|
180
176
|
${details.map(String).join("\n ")}` : "";
|
|
181
177
|
if (typeof details === "object") {
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
178
|
+
const obj = details;
|
|
179
|
+
const lines = [];
|
|
180
|
+
if (Array.isArray(obj.issues)) {
|
|
181
|
+
for (const item of obj.issues) {
|
|
182
|
+
if (item && typeof item === "object") {
|
|
183
|
+
const issue = item;
|
|
184
|
+
const path = Array.isArray(issue.path) ? issue.path.map(String).filter(Boolean).join(".") : "";
|
|
185
|
+
const msg = typeof issue.message === "string" ? issue.message : String(issue.message ?? "");
|
|
186
|
+
if (path || msg) lines.push(path ? `${path}: ${msg}` : msg);
|
|
187
|
+
} else if (item !== void 0 && item !== null) {
|
|
188
|
+
lines.push(String(item));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (Array.isArray(obj.errors)) {
|
|
193
|
+
for (const err of obj.errors) lines.push(String(err));
|
|
194
|
+
}
|
|
195
|
+
if (lines.length) return `
|
|
196
|
+
${lines.join("\n ")}`;
|
|
185
197
|
}
|
|
186
198
|
return "";
|
|
187
199
|
}
|
|
@@ -234,8 +246,12 @@ import { dirname as dirname3, isAbsolute as isAbsolute2, join as join5, relative
|
|
|
234
246
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
235
247
|
import {
|
|
236
248
|
BUILTIN_SOURCE_OPS,
|
|
249
|
+
bundleUploadProblem as bundleUploadProblem2,
|
|
237
250
|
CRM_WRITES,
|
|
238
251
|
crmProblems,
|
|
252
|
+
fileUploadProblems as fileUploadProblems2,
|
|
253
|
+
manifestUploadProblem as manifestUploadProblem2,
|
|
254
|
+
optionReasonProblems,
|
|
239
255
|
rulesVersion,
|
|
240
256
|
STEP_BUILD,
|
|
241
257
|
STEP_ENTRY_FILE,
|
|
@@ -319,8 +335,27 @@ function upsertBody(tenant, reseed) {
|
|
|
319
335
|
}
|
|
320
336
|
|
|
321
337
|
// src/manifest.ts
|
|
322
|
-
import { parseDuration } from "@motionskit/kernel";
|
|
338
|
+
import { isValidTimeZone, parseDuration } from "@motionskit/kernel";
|
|
323
339
|
import { z } from "zod";
|
|
340
|
+
var TENANT_ID = /^[a-z][a-z0-9_-]{0,62}$/;
|
|
341
|
+
var tenantId = z.string().regex(TENANT_ID, "tenant ids are lowercase letters, digits, - and _");
|
|
342
|
+
var RawTenant = z.object({
|
|
343
|
+
id: tenantId.optional(),
|
|
344
|
+
title: z.string().optional(),
|
|
345
|
+
timezone: z.string().optional(),
|
|
346
|
+
sources: z.object({ fixtures: z.boolean().optional() }).nullable().optional()
|
|
347
|
+
});
|
|
348
|
+
function parseTenant(raw) {
|
|
349
|
+
const doc = raw ?? {};
|
|
350
|
+
const result = RawTenant.safeParse(doc);
|
|
351
|
+
const errors = result.success ? [] : result.error.issues.map((i) => i.path.length ? `${i.path.join(".")}: ${i.message}` : i.message);
|
|
352
|
+
const tz = doc && typeof doc === "object" && !Array.isArray(doc) ? doc.timezone : void 0;
|
|
353
|
+
if (typeof tz === "string" && !isValidTimeZone(tz)) {
|
|
354
|
+
errors.push(`timezone "${tz}" is not a valid IANA time zone`);
|
|
355
|
+
}
|
|
356
|
+
if (result.success && !errors.length) return { ok: true, tenant: result.data };
|
|
357
|
+
return { ok: false, errors };
|
|
358
|
+
}
|
|
324
359
|
var ID = /^[a-z][a-z0-9_]*$/;
|
|
325
360
|
var id = z.string().regex(ID, "ids are lowercase snake_case");
|
|
326
361
|
var json = z.lazy(() => z.union([z.null(), z.boolean(), z.number(), z.string(), z.array(json), z.record(z.string(), json)]));
|
|
@@ -460,7 +495,14 @@ function parseMotion(raw) {
|
|
|
460
495
|
import { createHash } from "crypto";
|
|
461
496
|
import { existsSync as existsSync3, mkdirSync, readdirSync as readdirSync2, readFileSync as readFileSync3, writeFileSync } from "fs";
|
|
462
497
|
import { dirname as dirname2, isAbsolute, join as join4, relative, resolve, sep } from "path";
|
|
463
|
-
import {
|
|
498
|
+
import {
|
|
499
|
+
bundleUploadProblem,
|
|
500
|
+
fileUploadProblems,
|
|
501
|
+
manifestUploadProblem,
|
|
502
|
+
parseDuration as parseDuration2,
|
|
503
|
+
validateSourceManifest,
|
|
504
|
+
versionId
|
|
505
|
+
} from "@motionskit/kernel";
|
|
464
506
|
import { build } from "esbuild";
|
|
465
507
|
import YAML from "yaml";
|
|
466
508
|
import { z as z2 } from "zod";
|
|
@@ -575,7 +617,15 @@ async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
|
|
|
575
617
|
const index = join4(root, "index.ts");
|
|
576
618
|
if (!existsSync3(index)) throw new SourceErrors(where2, ["index.ts does not exist"]);
|
|
577
619
|
const yamlPath = join4(root, "source.yaml");
|
|
578
|
-
|
|
620
|
+
let rawYaml = null;
|
|
621
|
+
if (existsSync3(yamlPath)) {
|
|
622
|
+
try {
|
|
623
|
+
rawYaml = YAML.parse(readFileSync3(yamlPath, "utf8"));
|
|
624
|
+
} catch (e) {
|
|
625
|
+
throw new SourceErrors(`${where2}/source.yaml`, [`invalid YAML: ${e.message}`]);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
const yaml = SourceYaml.safeParse(rawYaml);
|
|
579
629
|
if (!yaml.success)
|
|
580
630
|
throw new SourceErrors(
|
|
581
631
|
`${where2}/source.yaml`,
|
|
@@ -641,8 +691,6 @@ export default serveSource(def);
|
|
|
641
691
|
ops,
|
|
642
692
|
bundle: sha256(bundle2)
|
|
643
693
|
};
|
|
644
|
-
errors.push(...validateSourceManifest(manifest));
|
|
645
|
-
if (errors.length) throw new SourceErrors(where2, errors);
|
|
646
694
|
const files = {};
|
|
647
695
|
for (const input of Object.keys(out.metafile.inputs).map((p) => resolve(tenantDir, p))) {
|
|
648
696
|
const path = relative(root, input);
|
|
@@ -650,6 +698,12 @@ export default serveSource(def);
|
|
|
650
698
|
files[path.split(sep).join("/")] = readFileSync3(input, "utf8");
|
|
651
699
|
}
|
|
652
700
|
if (existsSync3(yamlPath)) files["source.yaml"] = readFileSync3(yamlPath, "utf8");
|
|
701
|
+
errors.push(...validateSourceManifest(manifest), ...fileUploadProblems(files));
|
|
702
|
+
const bundleProblem = bundleUploadProblem(manifest.bundle, bundle2);
|
|
703
|
+
if (bundleProblem) errors.push(bundleProblem);
|
|
704
|
+
const manifestProblem = manifestUploadProblem(manifest);
|
|
705
|
+
if (manifestProblem) errors.push(manifestProblem);
|
|
706
|
+
if (errors.length) throw new SourceErrors(where2, errors);
|
|
653
707
|
return { dir: root, manifest: { ...manifest, sha: versionId(manifest, files) }, bundle: bundle2, files };
|
|
654
708
|
}
|
|
655
709
|
async function compileSources(tenantDir, tenant) {
|
|
@@ -726,12 +780,27 @@ function folderName(dir) {
|
|
|
726
780
|
return leaf !== "." && leaf !== ".." ? leaf : win32.basename(resolve2(dir));
|
|
727
781
|
}
|
|
728
782
|
function tenantIdentity(dir) {
|
|
729
|
-
const meta = readYaml(join5(dir, "tenant.yaml"))
|
|
783
|
+
const meta = readYaml(join5(dir, "tenant.yaml"), "tenant.yaml");
|
|
784
|
+
const parsed2 = parseTenant(meta);
|
|
785
|
+
const id2 = parsed2.ok && parsed2.tenant.id ? parsed2.tenant.id : folderName(dir);
|
|
730
786
|
const motionsDir = join5(dir, "motions");
|
|
731
787
|
const motions = existsSync4(motionsDir) ? readdirSync3(motionsDir).filter((name) => existsSync4(join5(motionsDir, name, "motion.yaml"))).sort() : [];
|
|
732
|
-
return { id:
|
|
788
|
+
return { id: id2, motions };
|
|
733
789
|
}
|
|
734
|
-
var readYaml = (path) =>
|
|
790
|
+
var readYaml = (path, label) => {
|
|
791
|
+
try {
|
|
792
|
+
return YAML2.parse(readFileSync4(path, "utf8"));
|
|
793
|
+
} catch (e) {
|
|
794
|
+
throw new Error(`${label ?? folderName(path)}: invalid YAML: ${e.message}`);
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
var readJson = (path, label) => {
|
|
798
|
+
try {
|
|
799
|
+
return JSON.parse(readFileSync4(path, "utf8"));
|
|
800
|
+
} catch (e) {
|
|
801
|
+
throw new Error(`${label}: invalid JSON: ${e.message}`);
|
|
802
|
+
}
|
|
803
|
+
};
|
|
735
804
|
var grant = (g) => ({ read: g?.read ?? [], propose: g?.propose ?? [] });
|
|
736
805
|
async function bundle(stepFile, tenantDir) {
|
|
737
806
|
const result = await build2({
|
|
@@ -758,11 +827,11 @@ async function compileMotion(tenant, dir, catalog = new Set(BUILTIN_SOURCE_OPS))
|
|
|
758
827
|
if (!tenantDir) throw new Error(`no tenant.yaml above ${dir}`);
|
|
759
828
|
const folder = folderName(dir);
|
|
760
829
|
const where2 = `${tenant.id}/${folder}/motion.yaml`;
|
|
761
|
-
const parsed2 = parseMotion(readYaml(join5(dir, "motion.yaml")));
|
|
830
|
+
const parsed2 = parseMotion(readYaml(join5(dir, "motion.yaml"), `motions/${folder}/motion.yaml`));
|
|
762
831
|
if (!parsed2.ok) throw new MotionErrors(where2, parsed2.errors);
|
|
763
832
|
const raw = parsed2.motion;
|
|
764
833
|
const rulesPath = join5(dir, "rules.yaml");
|
|
765
|
-
const rules = existsSync4(rulesPath) ? readYaml(rulesPath) ?? {} : {};
|
|
834
|
+
const rules = existsSync4(rulesPath) ? readYaml(rulesPath, `motions/${folder}/rules.yaml`) ?? {} : {};
|
|
766
835
|
const bundles = {};
|
|
767
836
|
const files = { "motion.yaml": readFileSync4(join5(dir, "motion.yaml"), "utf8") };
|
|
768
837
|
if (existsSync4(rulesPath)) files["rules.yaml"] = readFileSync4(rulesPath, "utf8");
|
|
@@ -811,7 +880,17 @@ async function compileMotion(tenant, dir, catalog = new Set(BUILTIN_SOURCE_OPS))
|
|
|
811
880
|
steps,
|
|
812
881
|
queues
|
|
813
882
|
};
|
|
814
|
-
const errors =
|
|
883
|
+
const errors = [
|
|
884
|
+
...validateManifest(manifest, catalog),
|
|
885
|
+
...optionReasonProblems(manifest.reasons, files),
|
|
886
|
+
...fileUploadProblems2(files),
|
|
887
|
+
...Object.entries(bundles).flatMap(([sha, code]) => {
|
|
888
|
+
const p = bundleUploadProblem2(sha, code);
|
|
889
|
+
return p ? [p] : [];
|
|
890
|
+
})
|
|
891
|
+
];
|
|
892
|
+
const manifestProblem = manifestUploadProblem2(manifest);
|
|
893
|
+
if (manifestProblem) errors.push(manifestProblem);
|
|
815
894
|
if (errors.length) throw new MotionErrors(where2, errors);
|
|
816
895
|
return { dir: root, manifest: { ...manifest, sha: versionId2(manifest, files) }, bundles, files };
|
|
817
896
|
}
|
|
@@ -826,18 +905,21 @@ function compileInput(input) {
|
|
|
826
905
|
}
|
|
827
906
|
async function compileTenant(tenantDir) {
|
|
828
907
|
const dir = resolve2(tenantDir);
|
|
829
|
-
const
|
|
908
|
+
const rawMeta = readYaml(join5(dir, "tenant.yaml"), "tenant.yaml");
|
|
909
|
+
const parsedMeta = parseTenant(rawMeta);
|
|
910
|
+
if (!parsedMeta.ok) {
|
|
911
|
+
if (parsedMeta.errors.length === 1) throw new Error(`tenant.yaml: ${parsedMeta.errors[0]}`);
|
|
912
|
+
throw new MotionErrors("tenant.yaml", parsedMeta.errors);
|
|
913
|
+
}
|
|
914
|
+
const meta = parsedMeta.tenant;
|
|
830
915
|
const id2 = meta.id ?? folderName(tenantDir);
|
|
831
916
|
const timezone = meta.timezone ?? "America/Sao_Paulo";
|
|
832
|
-
const connection = readYaml(join5(dir, "crm.yaml"));
|
|
917
|
+
const connection = readYaml(join5(dir, "crm.yaml"), "crm.yaml");
|
|
833
918
|
const seedPath = join5(dir, "crm-seed.json");
|
|
834
|
-
const seed = existsSync4(seedPath) ?
|
|
919
|
+
const seed = existsSync4(seedPath) ? readJson(seedPath, "crm-seed.json") : null;
|
|
835
920
|
const fixtures = readTenantFixtures(dir, meta.sources);
|
|
836
921
|
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
|
-
]);
|
|
922
|
+
const catalog = /* @__PURE__ */ new Set([...BUILTIN_SOURCE_OPS, ...sources.flatMap((s) => Object.keys(s.manifest.ops).map((op) => `${s.manifest.source}.${op}`))]);
|
|
841
923
|
const motionsDir = join5(dir, "motions");
|
|
842
924
|
const motions = [];
|
|
843
925
|
for (const name of readdirSync3(motionsDir).sort()) {
|
|
@@ -878,7 +960,7 @@ function tenantCrmProblems(tenant, systems = CRM_WRITES) {
|
|
|
878
960
|
import { existsSync as existsSync5, mkdirSync as mkdirSync2, readdirSync as readdirSync4, readFileSync as readFileSync5, writeFileSync as writeFileSync2 } from "fs";
|
|
879
961
|
import { basename, dirname as dirname4, join as join6, resolve as resolve3 } from "path";
|
|
880
962
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
881
|
-
var
|
|
963
|
+
var TENANT_ID2 = /^[a-z][a-z0-9_-]{0,62}$/;
|
|
882
964
|
var HANDLE = /^[a-z][a-z0-9_-]{0,31}$/;
|
|
883
965
|
function readCliPackage() {
|
|
884
966
|
return JSON.parse(readFileSync5(fileURLToPath3(new URL("../package.json", import.meta.url)), "utf8"));
|
|
@@ -908,7 +990,7 @@ var file = (dir, path, contents) => {
|
|
|
908
990
|
function init(target = ".", options2 = {}) {
|
|
909
991
|
const dir = resolve3(target);
|
|
910
992
|
const id2 = basename(dir);
|
|
911
|
-
if (!
|
|
993
|
+
if (!TENANT_ID2.test(id2)) throw new Error(`${id2} is not a tenant id: lowercase letters, digits, - and _, starting with a letter`);
|
|
912
994
|
if (existsSync5(dir) && readdirSync4(dir).length) throw new Error(`${dir} is not empty`);
|
|
913
995
|
const approver = options2.approver ?? defaultApprover();
|
|
914
996
|
if (!HANDLE.test(approver)) throw new Error(`${approver} is not a member handle: lowercase letters, digits, - and _, starting with a letter`);
|
|
@@ -1215,17 +1297,113 @@ function runTenantTests(dirs, spawn = spawnSync) {
|
|
|
1215
1297
|
var DEFAULT_URL = "https://motions.pruma.tech";
|
|
1216
1298
|
var LOCAL_URL = "http://localhost:8787";
|
|
1217
1299
|
var USAGE = `usage: motions <check|push|pull|test|open|retry|cancel|init|publish|help> \u2026
|
|
1218
|
-
check [tenant] compile the tenant in this directory, or every tenant in this repo
|
|
1219
|
-
push [--live] [-m message]
|
|
1300
|
+
check [tenant] compile and validate the tenant in this directory, or every tenant in this repo
|
|
1301
|
+
push [--live] [--draft] [-m message] [--reseed]
|
|
1302
|
+
publish a draft (or go live with --live) and print a link
|
|
1220
1303
|
pull <version> [--motion id] [--force]
|
|
1221
|
-
|
|
1304
|
+
pull a published motion version's files into this tenant
|
|
1305
|
+
test [tenant] run motions/*/tests with the tenant's vitest
|
|
1222
1306
|
open <tenant> <motion> [--list id-or-name | --companies id,id] [--label text] [--again]
|
|
1307
|
+
open a batch on the motion's live version
|
|
1223
1308
|
retry <batch> [--company id] [--step id]
|
|
1309
|
+
retry failed or stopped cells in a batch
|
|
1224
1310
|
cancel <batch> [--reason text] [--yes]
|
|
1311
|
+
permanently cancel a running batch
|
|
1225
1312
|
init [dir] [--approver handle] scaffold a tenant repo
|
|
1226
|
-
publish [tenant]
|
|
1227
|
-
|
|
1313
|
+
publish [tenant] [--draft] [-m message] [--reseed]
|
|
1314
|
+
from this repo, push --live over tenants/*
|
|
1315
|
+
help [command] print global usage or help for one command
|
|
1228
1316
|
--version, -v print the CLI version`;
|
|
1317
|
+
var COMMAND_HELP = {
|
|
1318
|
+
check: `usage: motions check [tenant] [--tenant <dir>]
|
|
1319
|
+
|
|
1320
|
+
Compile and validate the tenant in this directory (or every tenant in this repo) without contacting the server.
|
|
1321
|
+
Checks tenant.yaml, crm.yaml, crm-seed.json, fixtures, custom sources, motion manifests, step bundles, upload limits, and declared option reasons.
|
|
1322
|
+
|
|
1323
|
+
options:
|
|
1324
|
+
--tenant <dir> path to a tenant directory containing tenant.yaml
|
|
1325
|
+
-h, --help print this help`,
|
|
1326
|
+
push: `usage: motions push [--live | --draft] [-m <message>] [--reseed] [--tenant <dir>] [--url <url>]
|
|
1327
|
+
|
|
1328
|
+
Compile, dry-run on the server, and publish the tenant, its custom sources, and its motions.
|
|
1329
|
+
Publishes a draft by default; pass --live to promote to live (or --draft to force a draft).
|
|
1330
|
+
|
|
1331
|
+
options:
|
|
1332
|
+
--live promote pushed sources and motions to live
|
|
1333
|
+
--draft publish as a draft (default; overrides --live)
|
|
1334
|
+
-m, --message commit/version note recorded on pushed versions
|
|
1335
|
+
--reseed replace the in-memory CRM seed on the server
|
|
1336
|
+
--tenant <dir> path to a tenant directory containing tenant.yaml
|
|
1337
|
+
--url <url> Motions server URL (defaults to MOTIONS_URL or ${DEFAULT_URL})
|
|
1338
|
+
-h, --help print this help`,
|
|
1339
|
+
pull: `usage: motions pull <version> [--motion <id>] [--force] [--tenant <dir>] [--url <url>]
|
|
1340
|
+
|
|
1341
|
+
Write a published motion version's source files into motions/<motion>/.
|
|
1342
|
+
|
|
1343
|
+
options:
|
|
1344
|
+
--motion <id> motion id when the tenant has more than one motion
|
|
1345
|
+
--force overwrite files with uncommitted local changes
|
|
1346
|
+
--tenant <dir> path to a tenant directory containing tenant.yaml
|
|
1347
|
+
--url <url> Motions server URL (defaults to MOTIONS_URL or ${DEFAULT_URL})
|
|
1348
|
+
-h, --help print this help`,
|
|
1349
|
+
test: `usage: motions test [tenant] [--tenant <dir>]
|
|
1350
|
+
|
|
1351
|
+
Run motions/*/tests using the tenant's local Vitest installation.
|
|
1352
|
+
|
|
1353
|
+
options:
|
|
1354
|
+
--tenant <dir> path to a tenant directory containing tenant.yaml
|
|
1355
|
+
-h, --help print this help`,
|
|
1356
|
+
open: `usage: motions open <tenant> <motion> [--list <id-or-name> | --companies <id,id>] [--label <text>] [--again] [--url <url>]
|
|
1357
|
+
|
|
1358
|
+
Open a batch on the motion's live version and print its link.
|
|
1359
|
+
With neither --list nor --companies, opens on the motion's input.list.
|
|
1360
|
+
|
|
1361
|
+
options:
|
|
1362
|
+
--list <id-or-name> uploaded Motions list id/name (falls back to a CRM list)
|
|
1363
|
+
--companies <id,id> comma-separated CRM company ids (cannot be combined with --list)
|
|
1364
|
+
--label <text> custom label for the batch
|
|
1365
|
+
--again open a fresh batch instead of reusing an existing one (always opens fresh for --companies and CRM lists)
|
|
1366
|
+
--url <url> Motions server URL (defaults to MOTIONS_URL or ${DEFAULT_URL})
|
|
1367
|
+
-h, --help print this help`,
|
|
1368
|
+
retry: `usage: motions retry <batch> [--company <id>] [--step <id>] [--tenant <dir>] [--url <url>]
|
|
1369
|
+
|
|
1370
|
+
Retry failed or stopped cells in <batch>, or a specific --company and --step.
|
|
1371
|
+
|
|
1372
|
+
options:
|
|
1373
|
+
--company <id> company key (e.g. hubspot:101) or bare CRM id (101)
|
|
1374
|
+
--step <id> runnable step id to retry
|
|
1375
|
+
--tenant <dir> tenant directory used for local token resolution
|
|
1376
|
+
--url <url> Motions server URL (defaults to MOTIONS_URL or ${DEFAULT_URL})
|
|
1377
|
+
-h, --help print this help`,
|
|
1378
|
+
cancel: `usage: motions cancel <batch> [--reason <text>] [--yes] [--tenant <dir>] [--url <url>]
|
|
1379
|
+
|
|
1380
|
+
Permanently cancel a running batch.
|
|
1381
|
+
|
|
1382
|
+
options:
|
|
1383
|
+
--reason <text> optional note recorded on batch.canceled
|
|
1384
|
+
-y, --yes skip the interactive confirmation prompt
|
|
1385
|
+
--tenant <dir> tenant directory used for local token resolution
|
|
1386
|
+
--url <url> Motions server URL (defaults to MOTIONS_URL or ${DEFAULT_URL})
|
|
1387
|
+
-h, --help print this help`,
|
|
1388
|
+
init: `usage: motions init [dir] [--approver <handle>] [--url <url>]
|
|
1389
|
+
|
|
1390
|
+
Scaffold a new tenant repository in [dir] (defaults to the current directory).
|
|
1391
|
+
|
|
1392
|
+
options:
|
|
1393
|
+
--approver <handle> initial approver handle for motions/example/motion.yaml (defaults to MOTIONS_APPROVER, /api/me handle when MOTIONS_TOKEN is set, or "member")
|
|
1394
|
+
--url <url> Motions server URL used with MOTIONS_TOKEN to resolve your member handle
|
|
1395
|
+
-h, --help print this help`,
|
|
1396
|
+
publish: `usage: motions publish [tenant] [--draft] [-m <message>] [--reseed] [--url <url>]
|
|
1397
|
+
|
|
1398
|
+
From the motions monorepo, push --live over tenants/* (or one named tenant).
|
|
1399
|
+
|
|
1400
|
+
options:
|
|
1401
|
+
--draft publish as drafts instead of promoting to live
|
|
1402
|
+
-m, --message commit/version note recorded on pushed versions
|
|
1403
|
+
--reseed replace the in-memory CRM seed on the server
|
|
1404
|
+
--url <url> Motions server URL (defaults to MOTIONS_URL or ${LOCAL_URL})
|
|
1405
|
+
-h, --help print this help`
|
|
1406
|
+
};
|
|
1229
1407
|
var parsed = (() => {
|
|
1230
1408
|
try {
|
|
1231
1409
|
return parseArgs({
|
|
@@ -1265,7 +1443,19 @@ if (values.version) {
|
|
|
1265
1443
|
process.exit(0);
|
|
1266
1444
|
}
|
|
1267
1445
|
if (values.help || positionals[0] === "help") {
|
|
1268
|
-
|
|
1446
|
+
const topic = positionals[0] === "help" ? positionals[1] : positionals[0];
|
|
1447
|
+
if (!topic || topic === "help") {
|
|
1448
|
+
console.log(USAGE);
|
|
1449
|
+
process.exit(0);
|
|
1450
|
+
}
|
|
1451
|
+
const detail = COMMAND_HELP[topic];
|
|
1452
|
+
if (!detail) {
|
|
1453
|
+
console.error(`unknown command "${topic}"
|
|
1454
|
+
|
|
1455
|
+
${USAGE}`);
|
|
1456
|
+
process.exit(1);
|
|
1457
|
+
}
|
|
1458
|
+
console.log(detail);
|
|
1269
1459
|
process.exit(0);
|
|
1270
1460
|
}
|
|
1271
1461
|
var [command, ...args] = positionals;
|
|
@@ -1521,9 +1711,9 @@ var commands = {
|
|
|
1521
1711
|
let approver = values.approver ?? handleFrom(process.env.MOTIONS_APPROVER) ?? void 0;
|
|
1522
1712
|
if (!approver && process.env.MOTIONS_TOKEN) {
|
|
1523
1713
|
try {
|
|
1524
|
-
const
|
|
1525
|
-
const me = await api(url, "/api/me", void 0,
|
|
1526
|
-
const match = me.memberships?.find((m) => m.tenant ===
|
|
1714
|
+
const tenantId2 = basename2(resolve6(dir ?? "."));
|
|
1715
|
+
const me = await api(url, "/api/me", void 0, tenantId2);
|
|
1716
|
+
const match = me.memberships?.find((m) => m.tenant === tenantId2) ?? me.memberships?.[0];
|
|
1527
1717
|
approver = handleFrom(match?.handle) ?? void 0;
|
|
1528
1718
|
} catch {
|
|
1529
1719
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motionskit/cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
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.7"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/node": "^22.19.0",
|