@motionskit/cli 0.2.7 → 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 +42 -11
- package/dist/main.js +47 -16
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
CRM_WRITES,
|
|
10
10
|
crmProblems,
|
|
11
11
|
fileUploadProblems as fileUploadProblems2,
|
|
12
|
-
isValidTimeZone,
|
|
13
12
|
manifestUploadProblem as manifestUploadProblem2,
|
|
14
13
|
optionReasonProblems,
|
|
15
14
|
rulesVersion,
|
|
@@ -83,8 +82,27 @@ function readTenantFixtures(dir, sources) {
|
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
// src/manifest.ts
|
|
86
|
-
import { parseDuration } from "@motionskit/kernel";
|
|
85
|
+
import { isValidTimeZone, parseDuration } from "@motionskit/kernel";
|
|
87
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
|
+
}
|
|
88
106
|
var ID = /^[a-z][a-z0-9_]*$/;
|
|
89
107
|
var id = z.string().regex(ID, "ids are lowercase snake_case");
|
|
90
108
|
var json = z.lazy(() => z.union([z.null(), z.boolean(), z.number(), z.string(), z.array(json), z.record(z.string(), json)]));
|
|
@@ -346,7 +364,15 @@ async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
|
|
|
346
364
|
const index = join3(root, "index.ts");
|
|
347
365
|
if (!existsSync2(index)) throw new SourceErrors(where2, ["index.ts does not exist"]);
|
|
348
366
|
const yamlPath = join3(root, "source.yaml");
|
|
349
|
-
|
|
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);
|
|
350
376
|
if (!yaml.success)
|
|
351
377
|
throw new SourceErrors(
|
|
352
378
|
`${where2}/source.yaml`,
|
|
@@ -456,10 +482,12 @@ function folderName(dir) {
|
|
|
456
482
|
return leaf !== "." && leaf !== ".." ? leaf : win32.basename(resolve2(dir));
|
|
457
483
|
}
|
|
458
484
|
function tenantIdentity(dir) {
|
|
459
|
-
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);
|
|
460
488
|
const motionsDir = join4(dir, "motions");
|
|
461
489
|
const motions = existsSync3(motionsDir) ? readdirSync3(motionsDir).filter((name) => existsSync3(join4(motionsDir, name, "motion.yaml"))).sort() : [];
|
|
462
|
-
return { id:
|
|
490
|
+
return { id: id2, motions };
|
|
463
491
|
}
|
|
464
492
|
var tenantsDir = () => {
|
|
465
493
|
const dir = resolve2(dirname2(fileURLToPath(import.meta.url)), "../../..", "tenants");
|
|
@@ -506,11 +534,11 @@ async function compileMotion(tenant, dir, catalog = new Set(BUILTIN_SOURCE_OPS))
|
|
|
506
534
|
if (!tenantDir) throw new Error(`no tenant.yaml above ${dir}`);
|
|
507
535
|
const folder = folderName(dir);
|
|
508
536
|
const where2 = `${tenant.id}/${folder}/motion.yaml`;
|
|
509
|
-
const parsed = parseMotion(readYaml(join4(dir, "motion.yaml")));
|
|
537
|
+
const parsed = parseMotion(readYaml(join4(dir, "motion.yaml"), `motions/${folder}/motion.yaml`));
|
|
510
538
|
if (!parsed.ok) throw new MotionErrors(where2, parsed.errors);
|
|
511
539
|
const raw = parsed.motion;
|
|
512
540
|
const rulesPath = join4(dir, "rules.yaml");
|
|
513
|
-
const rules = existsSync3(rulesPath) ? readYaml(rulesPath) ?? {} : {};
|
|
541
|
+
const rules = existsSync3(rulesPath) ? readYaml(rulesPath, `motions/${folder}/rules.yaml`) ?? {} : {};
|
|
514
542
|
const bundles = {};
|
|
515
543
|
const files = { "motion.yaml": readFileSync3(join4(dir, "motion.yaml"), "utf8") };
|
|
516
544
|
if (existsSync3(rulesPath)) files["rules.yaml"] = readFileSync3(rulesPath, "utf8");
|
|
@@ -584,12 +612,15 @@ function compileInput(input) {
|
|
|
584
612
|
}
|
|
585
613
|
async function compileTenant(tenantDir) {
|
|
586
614
|
const dir = resolve2(tenantDir);
|
|
587
|
-
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;
|
|
588
622
|
const id2 = meta.id ?? folderName(tenantDir);
|
|
589
623
|
const timezone = meta.timezone ?? "America/Sao_Paulo";
|
|
590
|
-
if (typeof timezone !== "string" || !isValidTimeZone(timezone)) {
|
|
591
|
-
throw new Error(`tenant.yaml: timezone "${String(timezone)}" is not a valid IANA time zone`);
|
|
592
|
-
}
|
|
593
624
|
const connection = readYaml(join4(dir, "crm.yaml"), "crm.yaml");
|
|
594
625
|
const seedPath = join4(dir, "crm-seed.json");
|
|
595
626
|
const seed = existsSync3(seedPath) ? readJson(seedPath, "crm-seed.json") : null;
|
package/dist/main.js
CHANGED
|
@@ -250,7 +250,6 @@ import {
|
|
|
250
250
|
CRM_WRITES,
|
|
251
251
|
crmProblems,
|
|
252
252
|
fileUploadProblems as fileUploadProblems2,
|
|
253
|
-
isValidTimeZone,
|
|
254
253
|
manifestUploadProblem as manifestUploadProblem2,
|
|
255
254
|
optionReasonProblems,
|
|
256
255
|
rulesVersion,
|
|
@@ -336,8 +335,27 @@ function upsertBody(tenant, reseed) {
|
|
|
336
335
|
}
|
|
337
336
|
|
|
338
337
|
// src/manifest.ts
|
|
339
|
-
import { parseDuration } from "@motionskit/kernel";
|
|
338
|
+
import { isValidTimeZone, parseDuration } from "@motionskit/kernel";
|
|
340
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
|
+
}
|
|
341
359
|
var ID = /^[a-z][a-z0-9_]*$/;
|
|
342
360
|
var id = z.string().regex(ID, "ids are lowercase snake_case");
|
|
343
361
|
var json = z.lazy(() => z.union([z.null(), z.boolean(), z.number(), z.string(), z.array(json), z.record(z.string(), json)]));
|
|
@@ -599,7 +617,15 @@ async function compileSource(tenant, dir, tenantRoot = resolve(dir, "../..")) {
|
|
|
599
617
|
const index = join4(root, "index.ts");
|
|
600
618
|
if (!existsSync3(index)) throw new SourceErrors(where2, ["index.ts does not exist"]);
|
|
601
619
|
const yamlPath = join4(root, "source.yaml");
|
|
602
|
-
|
|
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);
|
|
603
629
|
if (!yaml.success)
|
|
604
630
|
throw new SourceErrors(
|
|
605
631
|
`${where2}/source.yaml`,
|
|
@@ -754,10 +780,12 @@ function folderName(dir) {
|
|
|
754
780
|
return leaf !== "." && leaf !== ".." ? leaf : win32.basename(resolve2(dir));
|
|
755
781
|
}
|
|
756
782
|
function tenantIdentity(dir) {
|
|
757
|
-
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);
|
|
758
786
|
const motionsDir = join5(dir, "motions");
|
|
759
787
|
const motions = existsSync4(motionsDir) ? readdirSync3(motionsDir).filter((name) => existsSync4(join5(motionsDir, name, "motion.yaml"))).sort() : [];
|
|
760
|
-
return { id:
|
|
788
|
+
return { id: id2, motions };
|
|
761
789
|
}
|
|
762
790
|
var readYaml = (path, label) => {
|
|
763
791
|
try {
|
|
@@ -799,11 +827,11 @@ async function compileMotion(tenant, dir, catalog = new Set(BUILTIN_SOURCE_OPS))
|
|
|
799
827
|
if (!tenantDir) throw new Error(`no tenant.yaml above ${dir}`);
|
|
800
828
|
const folder = folderName(dir);
|
|
801
829
|
const where2 = `${tenant.id}/${folder}/motion.yaml`;
|
|
802
|
-
const parsed2 = parseMotion(readYaml(join5(dir, "motion.yaml")));
|
|
830
|
+
const parsed2 = parseMotion(readYaml(join5(dir, "motion.yaml"), `motions/${folder}/motion.yaml`));
|
|
803
831
|
if (!parsed2.ok) throw new MotionErrors(where2, parsed2.errors);
|
|
804
832
|
const raw = parsed2.motion;
|
|
805
833
|
const rulesPath = join5(dir, "rules.yaml");
|
|
806
|
-
const rules = existsSync4(rulesPath) ? readYaml(rulesPath) ?? {} : {};
|
|
834
|
+
const rules = existsSync4(rulesPath) ? readYaml(rulesPath, `motions/${folder}/rules.yaml`) ?? {} : {};
|
|
807
835
|
const bundles = {};
|
|
808
836
|
const files = { "motion.yaml": readFileSync4(join5(dir, "motion.yaml"), "utf8") };
|
|
809
837
|
if (existsSync4(rulesPath)) files["rules.yaml"] = readFileSync4(rulesPath, "utf8");
|
|
@@ -877,12 +905,15 @@ function compileInput(input) {
|
|
|
877
905
|
}
|
|
878
906
|
async function compileTenant(tenantDir) {
|
|
879
907
|
const dir = resolve2(tenantDir);
|
|
880
|
-
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;
|
|
881
915
|
const id2 = meta.id ?? folderName(tenantDir);
|
|
882
916
|
const timezone = meta.timezone ?? "America/Sao_Paulo";
|
|
883
|
-
if (typeof timezone !== "string" || !isValidTimeZone(timezone)) {
|
|
884
|
-
throw new Error(`tenant.yaml: timezone "${String(timezone)}" is not a valid IANA time zone`);
|
|
885
|
-
}
|
|
886
917
|
const connection = readYaml(join5(dir, "crm.yaml"), "crm.yaml");
|
|
887
918
|
const seedPath = join5(dir, "crm-seed.json");
|
|
888
919
|
const seed = existsSync4(seedPath) ? readJson(seedPath, "crm-seed.json") : null;
|
|
@@ -929,7 +960,7 @@ function tenantCrmProblems(tenant, systems = CRM_WRITES) {
|
|
|
929
960
|
import { existsSync as existsSync5, mkdirSync as mkdirSync2, readdirSync as readdirSync4, readFileSync as readFileSync5, writeFileSync as writeFileSync2 } from "fs";
|
|
930
961
|
import { basename, dirname as dirname4, join as join6, resolve as resolve3 } from "path";
|
|
931
962
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
932
|
-
var
|
|
963
|
+
var TENANT_ID2 = /^[a-z][a-z0-9_-]{0,62}$/;
|
|
933
964
|
var HANDLE = /^[a-z][a-z0-9_-]{0,31}$/;
|
|
934
965
|
function readCliPackage() {
|
|
935
966
|
return JSON.parse(readFileSync5(fileURLToPath3(new URL("../package.json", import.meta.url)), "utf8"));
|
|
@@ -959,7 +990,7 @@ var file = (dir, path, contents) => {
|
|
|
959
990
|
function init(target = ".", options2 = {}) {
|
|
960
991
|
const dir = resolve3(target);
|
|
961
992
|
const id2 = basename(dir);
|
|
962
|
-
if (!
|
|
993
|
+
if (!TENANT_ID2.test(id2)) throw new Error(`${id2} is not a tenant id: lowercase letters, digits, - and _, starting with a letter`);
|
|
963
994
|
if (existsSync5(dir) && readdirSync4(dir).length) throw new Error(`${dir} is not empty`);
|
|
964
995
|
const approver = options2.approver ?? defaultApprover();
|
|
965
996
|
if (!HANDLE.test(approver)) throw new Error(`${approver} is not a member handle: lowercase letters, digits, - and _, starting with a letter`);
|
|
@@ -1680,9 +1711,9 @@ var commands = {
|
|
|
1680
1711
|
let approver = values.approver ?? handleFrom(process.env.MOTIONS_APPROVER) ?? void 0;
|
|
1681
1712
|
if (!approver && process.env.MOTIONS_TOKEN) {
|
|
1682
1713
|
try {
|
|
1683
|
-
const
|
|
1684
|
-
const me = await api(url, "/api/me", void 0,
|
|
1685
|
-
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];
|
|
1686
1717
|
approver = handleFrom(match?.handle) ?? void 0;
|
|
1687
1718
|
} catch {
|
|
1688
1719
|
}
|