@composable-os/cli 0.1.0-beta.8 → 0.1.0-beta.9
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/accounts-D6N77JGP.js +44 -0
- package/dist/{blocks-DEELKGDC.js → blocks-4CA4PP3P.js} +4 -4
- package/dist/{chunk-X6I2LWB3.js → chunk-2MLAPQU7.js} +1471 -359
- package/dist/{chunk-2S7375SC.js → chunk-3MIEYXRY.js} +3 -301
- package/dist/chunk-56A6RJIU.js +1040 -0
- package/dist/chunk-7CQGMVDO.js +63 -0
- package/dist/{chunk-STJZJIRA.js → chunk-7O4F3NU4.js} +1039 -1380
- package/dist/chunk-7U3OZLTK.js +1370 -0
- package/dist/{chunk-ZRWF6QZS.js → chunk-DU6OIFOU.js} +1 -0
- package/dist/chunk-E4WCYTCT.js +347 -0
- package/dist/{chunk-E5JLTLRM.js → chunk-GYTEW66E.js} +1 -1
- package/dist/chunk-LPD2VR5M.js +284 -0
- package/dist/chunk-NDKVIG23.js +2641 -0
- package/dist/chunk-NSSUOI3Z.js +333 -0
- package/dist/chunk-OV3OFYPI.js +746 -0
- package/dist/{chunk-KTXGEXTY.js → chunk-P6YZMS3F.js} +11 -5
- package/dist/{chunk-G3ORKXVP.js → chunk-RFJOPI42.js} +138 -14
- package/dist/{chunk-KOKYDJ7Z.js → chunk-RXRD34QW.js} +10 -8
- package/dist/{chunk-XDDBLYKW.js → chunk-STBMGUTM.js} +4613 -2307
- package/dist/chunk-VTQNOQGA.js +150 -0
- package/dist/chunk-XSE37HK5.js +426 -0
- package/dist/{chunk-LTQDLCP7.js → chunk-Z52IX5WL.js} +257 -845
- package/dist/chunk-ZSR5H4O7.js +552 -0
- package/dist/{cli-DTMGNDXN.js → cli-7A5XHYML.js} +16 -9
- package/dist/{cli-BRSACHSG.js → cli-T6MJM7TU.js} +20 -8
- package/dist/cli.js +4 -4
- package/dist/{client-repo-ZVZ6KW5S.js → client-repo-PC4ISVEZ.js} +4 -4
- package/dist/create.js +4 -4
- package/dist/{fields-64BQJA62.js → fields-L24XAIOA.js} +13 -11
- package/dist/invoke-TLYT6CFI.js +33 -0
- package/dist/order-IKQBUFCV.js +19 -0
- package/dist/{person-WMWRF2HV.js → person-DXEOOH2A.js} +9 -5
- package/dist/plan-on-sale-LRCK5MJG.js +24 -0
- package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
- package/dist/platform/migrations/0516_order_tax.sql +63 -0
- package/dist/platform/migrations/0530_subscriptions.sql +173 -0
- package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
- package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
- package/dist/platform/migrations/0533_allowances.sql +262 -0
- package/dist/platform/migrations/0560_recurring.sql +75 -0
- package/dist/platform/sdk/allowance.ts +508 -0
- package/dist/platform/sdk/capacity.ts +89 -0
- package/dist/platform/sdk/contract.ts +97 -1
- package/dist/platform/sdk/document.ts +161 -14
- package/dist/platform/sdk/field-types.ts +5 -0
- package/dist/platform/sdk/index.ts +3 -0
- package/dist/platform/sdk/manifest.ts +206 -5
- package/dist/platform/sdk/media.ts +62 -0
- package/dist/platform/sdk/recurring.ts +314 -0
- package/dist/platform/sdk/rich-text.ts +52 -4
- package/dist/platform/sdk/sellable.ts +120 -0
- package/dist/platform/sdk/states.ts +162 -1
- package/dist/platform/sdk/unique.ts +81 -0
- package/dist/run-BMSZKTN3.js +648 -0
- package/dist/run-GMQBAV6H.js +309 -0
- package/dist/{run-4JNFLYXV.js → run-TNHMG5RI.js} +51 -390
- package/dist/src-7DXXGZG2.js +38 -0
- package/dist/{src-J27WXNLY.js → src-BDG7PERB.js} +73 -1
- package/dist/visibility-XSBZTO66.js +33 -0
- package/package.json +2 -2
- package/dist/chunk-MAXLUQKB.js +0 -1287
- package/dist/invoke-NHHOLLF6.js +0 -26
- package/dist/src-G6OF5SKW.js +0 -31
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
ACTORS,
|
|
4
|
+
PERMISSIONS,
|
|
5
|
+
RISK_CLASSES
|
|
6
|
+
} from "./chunk-7O4F3NU4.js";
|
|
7
|
+
|
|
8
|
+
// ../core/src/audiences.ts
|
|
9
|
+
var AUDIENCES = ["staff", "customer", "public"];
|
|
10
|
+
var DEFAULT_AUDIENCES = Object.freeze(["staff"]);
|
|
11
|
+
var OUTSIDE_RISK_CLASSES = Object.freeze(["read", "write"]);
|
|
12
|
+
function audiencesOf(op) {
|
|
13
|
+
return op.audiences ?? DEFAULT_AUDIENCES;
|
|
14
|
+
}
|
|
15
|
+
function isOpenOutside(op) {
|
|
16
|
+
return audiencesOf(op).some((audience) => audience !== "staff");
|
|
17
|
+
}
|
|
18
|
+
function audienceOf(principal) {
|
|
19
|
+
return principal.kind;
|
|
20
|
+
}
|
|
21
|
+
function allowsAudience(op, principal) {
|
|
22
|
+
return audiencesOf(op).includes(audienceOf(principal));
|
|
23
|
+
}
|
|
24
|
+
function audienceProblems(op) {
|
|
25
|
+
const problems = [];
|
|
26
|
+
const declared = op.audiences;
|
|
27
|
+
if (declared !== void 0) {
|
|
28
|
+
if (!Array.isArray(declared) || declared.length === 0) {
|
|
29
|
+
problems.push("audiences must list at least one of staff, customer, public");
|
|
30
|
+
return problems;
|
|
31
|
+
}
|
|
32
|
+
if (declared.some((a) => !AUDIENCES.includes(a))) {
|
|
33
|
+
problems.push("audiences must be a subset of staff, customer, public");
|
|
34
|
+
return problems;
|
|
35
|
+
}
|
|
36
|
+
if (new Set(declared).size !== declared.length) problems.push("audiences lists one twice");
|
|
37
|
+
}
|
|
38
|
+
if (isOpenOutside(op) && !OUTSIDE_RISK_CLASSES.includes(op.risk)) {
|
|
39
|
+
const who = audiencesOf(op).filter((a) => a !== "staff");
|
|
40
|
+
problems.push(
|
|
41
|
+
`an operation open to ${who.join(" and ")} callers must have risk class read or write, not ${op.risk} (ADR 0402)`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return problems;
|
|
45
|
+
}
|
|
46
|
+
function packOf(name) {
|
|
47
|
+
const [first, id] = name.split(".");
|
|
48
|
+
return first === "x" && id ? id : void 0;
|
|
49
|
+
}
|
|
50
|
+
function forPacks(file, installed) {
|
|
51
|
+
return {
|
|
52
|
+
...file,
|
|
53
|
+
operations: file.operations.filter((op) => {
|
|
54
|
+
const pack = op.pack ?? packOf(op.name);
|
|
55
|
+
return pack === void 0 || installed.includes(pack);
|
|
56
|
+
}).map(({ pack: _pack, ...op }) => op)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ../core/src/operation.ts
|
|
61
|
+
var AFTER_COMMIT = /* @__PURE__ */ Symbol("afterCommit");
|
|
62
|
+
function afterCommit(read) {
|
|
63
|
+
return { [AFTER_COMMIT]: read };
|
|
64
|
+
}
|
|
65
|
+
function isAfterCommit(value) {
|
|
66
|
+
return typeof value === "object" && value !== null && AFTER_COMMIT in value;
|
|
67
|
+
}
|
|
68
|
+
function runAfterCommit(value) {
|
|
69
|
+
return value[AFTER_COMMIT]();
|
|
70
|
+
}
|
|
71
|
+
function recordHostChange(ctx, change) {
|
|
72
|
+
(ctx.recordHostChange ?? ctx.recordChange)(change);
|
|
73
|
+
}
|
|
74
|
+
var HOST_TOOLS = /* @__PURE__ */ new WeakSet();
|
|
75
|
+
function markHostTool(op) {
|
|
76
|
+
HOST_TOOLS.add(op);
|
|
77
|
+
}
|
|
78
|
+
function isHostTool(op) {
|
|
79
|
+
return HOST_TOOLS.has(op);
|
|
80
|
+
}
|
|
81
|
+
var OPERATION_NAME_RE = /^[a-z][a-zA-Z0-9]*(\.[a-z][a-zA-Z0-9]*)+$/;
|
|
82
|
+
function defineOperation(definition) {
|
|
83
|
+
const problems = operationProblems(definition);
|
|
84
|
+
if (problems.length) {
|
|
85
|
+
throw new Error(`Operation ${definition.name}: ${problems.join("; ")}`);
|
|
86
|
+
}
|
|
87
|
+
return Object.freeze({ ...definition });
|
|
88
|
+
}
|
|
89
|
+
function operationProblems(op) {
|
|
90
|
+
const problems = [];
|
|
91
|
+
if (!OPERATION_NAME_RE.test(op.name ?? "")) problems.push("name must be <object>.<verb>");
|
|
92
|
+
if (typeof op.description !== "string" || op.description.trim().length < 20) {
|
|
93
|
+
problems.push("description must explain what it does, when to use it and side effects");
|
|
94
|
+
}
|
|
95
|
+
if (!op.input || typeof op.input.safeParse !== "function") problems.push("input schema missing");
|
|
96
|
+
if (!op.output || typeof op.output.safeParse !== "function") {
|
|
97
|
+
problems.push("output schema missing");
|
|
98
|
+
}
|
|
99
|
+
if (!PERMISSIONS.includes(op.permission)) problems.push("permission missing or unknown");
|
|
100
|
+
if (op.packPermission !== void 0) {
|
|
101
|
+
const extension = /^x\.([a-z][a-z0-9]{1,29})\./.exec(op.name ?? "")?.[1];
|
|
102
|
+
if (!extension) problems.push("packPermission is for extension tools only");
|
|
103
|
+
else if (!op.packPermission.startsWith(`${extension}:`)) {
|
|
104
|
+
problems.push(`packPermission is named ${extension}:<word>`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (!RISK_CLASSES.includes(op.risk)) problems.push("risk class missing or unknown");
|
|
108
|
+
if (op.actors && (op.actors.length === 0 || op.actors.some((a) => !ACTORS.includes(a)))) {
|
|
109
|
+
problems.push("actors must list known actor types");
|
|
110
|
+
}
|
|
111
|
+
if (typeof op.handler !== "function") problems.push("handler missing");
|
|
112
|
+
problems.push(...audienceProblems(op));
|
|
113
|
+
if (op.hostInternal !== void 0) {
|
|
114
|
+
if (op.hostInternal !== true) problems.push("hostInternal is true or left out");
|
|
115
|
+
if (op.name?.startsWith("x.")) problems.push("an extension tool can never be hostInternal");
|
|
116
|
+
if (!isOpenOutside(op)) {
|
|
117
|
+
problems.push("hostInternal is only for an operation open to customer or public callers");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (op.toolOnly !== void 0) {
|
|
121
|
+
if (op.toolOnly !== true) problems.push("toolOnly is true or left out");
|
|
122
|
+
if (op.name?.startsWith("x.")) problems.push("an extension tool cannot be toolOnly");
|
|
123
|
+
if (!audiencesOf(op).includes("customer")) {
|
|
124
|
+
problems.push("toolOnly is only for an operation open to customer callers");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (isOpenOutside(op) && op.alwaysReview) {
|
|
128
|
+
problems.push("an operation open to customer or public callers is never queued for review");
|
|
129
|
+
}
|
|
130
|
+
if ((op.risk === "bulk-write" || op.risk === "destructive") && op.name.endsWith("Many")) {
|
|
131
|
+
if (typeof op.bulkSize !== "function") problems.push("batch operations need bulkSize");
|
|
132
|
+
}
|
|
133
|
+
return problems;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export {
|
|
137
|
+
AUDIENCES,
|
|
138
|
+
OUTSIDE_RISK_CLASSES,
|
|
139
|
+
audiencesOf,
|
|
140
|
+
allowsAudience,
|
|
141
|
+
forPacks,
|
|
142
|
+
afterCommit,
|
|
143
|
+
isAfterCommit,
|
|
144
|
+
runAfterCommit,
|
|
145
|
+
recordHostChange,
|
|
146
|
+
markHostTool,
|
|
147
|
+
isHostTool,
|
|
148
|
+
defineOperation,
|
|
149
|
+
operationProblems
|
|
150
|
+
};
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
MEDIA_CAPTION_MAX_BYTES,
|
|
4
|
+
mediaProblem,
|
|
5
|
+
sniffFileType
|
|
6
|
+
} from "./chunk-2MLAPQU7.js";
|
|
7
|
+
|
|
8
|
+
// src/courses-import/format.ts
|
|
9
|
+
var COURSE = "courses.course";
|
|
10
|
+
var BUNDLE = "courses.bundle";
|
|
11
|
+
var THEME = "courses.theme";
|
|
12
|
+
var ENROLMENT = "courses.enrolment";
|
|
13
|
+
var PROGRESS = "courses.progress";
|
|
14
|
+
var STATUSES = ["draft", "published"];
|
|
15
|
+
var KEY_RE = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,99}$/;
|
|
16
|
+
var LESSON_KEY_RE = /^[a-z0-9-]{1,40}$/;
|
|
17
|
+
var EMAIL_RE = /^[^\s@,]+@[^\s@,]+\.[^\s@,]+$/;
|
|
18
|
+
var DATETIME_RE = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?(Z|[+-]\d{2}:\d{2}))?$/;
|
|
19
|
+
var MEDIA = "$media";
|
|
20
|
+
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
21
|
+
function parseCsv(text) {
|
|
22
|
+
const rows = [];
|
|
23
|
+
let cells = [];
|
|
24
|
+
let cell = "";
|
|
25
|
+
let quoted = false;
|
|
26
|
+
let line = 1;
|
|
27
|
+
let start = 1;
|
|
28
|
+
const end = () => {
|
|
29
|
+
cells.push(cell);
|
|
30
|
+
if (cells.some((c) => c.trim() !== "")) rows.push({ line: start, cells });
|
|
31
|
+
cells = [];
|
|
32
|
+
cell = "";
|
|
33
|
+
};
|
|
34
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
35
|
+
const c = text[i];
|
|
36
|
+
if (quoted) {
|
|
37
|
+
if (c === '"' && text[i + 1] === '"') {
|
|
38
|
+
cell += '"';
|
|
39
|
+
i += 1;
|
|
40
|
+
} else if (c === '"') quoted = false;
|
|
41
|
+
else {
|
|
42
|
+
if (c === "\n") line += 1;
|
|
43
|
+
cell += c;
|
|
44
|
+
}
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (c === '"') quoted = true;
|
|
48
|
+
else if (c === ",") {
|
|
49
|
+
cells.push(cell);
|
|
50
|
+
cell = "";
|
|
51
|
+
} else if (c === "\n" || c === "\r") {
|
|
52
|
+
if (c === "\r" && text[i + 1] === "\n") i += 1;
|
|
53
|
+
end();
|
|
54
|
+
line += 1;
|
|
55
|
+
start = line;
|
|
56
|
+
} else cell += c;
|
|
57
|
+
}
|
|
58
|
+
if (cell !== "" || cells.length) end();
|
|
59
|
+
return rows;
|
|
60
|
+
}
|
|
61
|
+
function srtToVtt(srt) {
|
|
62
|
+
const body = srt.replace(/^/, "").replace(/\r\n?/g, "\n").replace(/(\d{2}:\d{2}:\d{2}),(\d{3})/g, "$1.$2");
|
|
63
|
+
return `WEBVTT
|
|
64
|
+
|
|
65
|
+
${body.trim()}
|
|
66
|
+
`;
|
|
67
|
+
}
|
|
68
|
+
var slugify = (text) => text.toLowerCase().normalize("NFKD").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 120);
|
|
69
|
+
function readCoursesFolder(folder) {
|
|
70
|
+
const problems = [];
|
|
71
|
+
const videos = /* @__PURE__ */ new Map();
|
|
72
|
+
const files = /* @__PURE__ */ new Map();
|
|
73
|
+
const themes = /* @__PURE__ */ new Set();
|
|
74
|
+
const courses = [];
|
|
75
|
+
const bundles = [];
|
|
76
|
+
const sourceIds = /* @__PURE__ */ new Map();
|
|
77
|
+
const captionsOf = (raw, where, fault) => {
|
|
78
|
+
const out = [];
|
|
79
|
+
if (raw === void 0) return out;
|
|
80
|
+
if (!Array.isArray(raw)) {
|
|
81
|
+
fault(`${where}.captions is a list of { language, file, label? }`);
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
for (const c of raw) {
|
|
85
|
+
const language = isObject(c) && typeof c.language === "string" ? c.language : "";
|
|
86
|
+
const name = isObject(c) && typeof c.file === "string" ? c.file : "";
|
|
87
|
+
const bytes = folder.captions.get(name);
|
|
88
|
+
if (!/^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,2}$/.test(language) || !bytes) {
|
|
89
|
+
fault(
|
|
90
|
+
`${where}.captions: ${name ? `captions/${name}` : "a caption"} with a language (en, pt-BR) and a file in captions/`
|
|
91
|
+
);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const text = new TextDecoder().decode(bytes);
|
|
95
|
+
const vtt = new TextEncoder().encode(
|
|
96
|
+
name.toLowerCase().endsWith(".srt") ? srtToVtt(text) : text
|
|
97
|
+
);
|
|
98
|
+
if (vtt.byteLength > MEDIA_CAPTION_MAX_BYTES || !new TextDecoder().decode(vtt).replace(/^/, "").startsWith("WEBVTT")) {
|
|
99
|
+
fault(`captions/${name}: a WebVTT or SubRip file of at most 10 MB`);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const label = isObject(c) && typeof c.label === "string" ? c.label : language;
|
|
103
|
+
out.push({ language, label, vtt });
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
};
|
|
107
|
+
const video = (raw, where, fault) => {
|
|
108
|
+
if (!isObject(raw)) {
|
|
109
|
+
fault(`${where} is { file } (in videos/) or { url } (https), with captions`);
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
const captions = captionsOf(raw.captions, where, fault);
|
|
113
|
+
if (typeof raw.file === "string") {
|
|
114
|
+
if (!folder.videos.has(raw.file)) {
|
|
115
|
+
fault(`${where}: videos/${raw.file} is not there`);
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
const ref = `file:${raw.file}`;
|
|
119
|
+
if (!videos.has(ref)) videos.set(ref, { ref, name: raw.file, file: raw.file, captions });
|
|
120
|
+
return { [MEDIA]: ref };
|
|
121
|
+
}
|
|
122
|
+
if (typeof raw.url === "string" && raw.url.startsWith("https://")) {
|
|
123
|
+
const ref = `url:${raw.url}`;
|
|
124
|
+
const name = raw.url.split("/").pop()?.split("?")[0] || "video";
|
|
125
|
+
if (!videos.has(ref)) videos.set(ref, { ref, name, url: raw.url, captions });
|
|
126
|
+
return { [MEDIA]: ref };
|
|
127
|
+
}
|
|
128
|
+
fault(`${where} is { file } (in videos/) or { url } (https)`);
|
|
129
|
+
return null;
|
|
130
|
+
};
|
|
131
|
+
const picture = (name, where, fault) => {
|
|
132
|
+
const bytes = typeof name === "string" ? folder.pictures.get(name) : void 0;
|
|
133
|
+
if (!bytes || typeof name !== "string") {
|
|
134
|
+
fault(`${where}: a picture is a file name in pictures/`);
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
const problem = mediaProblem(bytes);
|
|
138
|
+
if (problem) {
|
|
139
|
+
fault(`${where}: pictures/${name}: ${problem}`);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
files.set(`picture:${name}`, { name, bytes });
|
|
143
|
+
return { [MEDIA]: `picture:${name}` };
|
|
144
|
+
};
|
|
145
|
+
const download = (name, where, fault) => {
|
|
146
|
+
const bytes = typeof name === "string" ? folder.downloads.get(name) : void 0;
|
|
147
|
+
if (!bytes || typeof name !== "string" || !sniffFileType(bytes)) {
|
|
148
|
+
fault(`${where}: a download is a PDF in downloads/`);
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
files.set(`download:${name}`, { name, bytes });
|
|
152
|
+
return { [MEDIA]: `download:${name}` };
|
|
153
|
+
};
|
|
154
|
+
const richText = (raw, where, fault) => JSON.parse(JSON.stringify(raw), (_k, v) => {
|
|
155
|
+
if (isObject(v) && v.type === "image" && typeof v.file === "string" && !("media" in v)) {
|
|
156
|
+
const { file: f, ...rest } = v;
|
|
157
|
+
return { ...rest, media: picture(f, `${where} (picture ${f})`, fault) ?? "med_missing" };
|
|
158
|
+
}
|
|
159
|
+
return v;
|
|
160
|
+
});
|
|
161
|
+
const keys = /* @__PURE__ */ new Map();
|
|
162
|
+
for (const [file, text] of [...folder.courses].sort(([a], [b]) => a.localeCompare(b))) {
|
|
163
|
+
const path = `courses/${file}`;
|
|
164
|
+
const fault = (message) => problems.push({ file: path, message });
|
|
165
|
+
let raw;
|
|
166
|
+
try {
|
|
167
|
+
raw = JSON.parse(text);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
fault(`Not JSON: ${error.message}`);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (!isObject(raw)) {
|
|
173
|
+
fault("A course is a JSON object");
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const key = typeof raw.key === "string" ? raw.key : file.replace(/\.json$/i, "");
|
|
177
|
+
if (!KEY_RE.test(key)) fault(`The key "${key}" is not 1 to 100 letters, digits or ._:-`);
|
|
178
|
+
if (keys.has(key)) fault(`The key "${key}" is ${keys.get(key)}'s too`);
|
|
179
|
+
keys.set(key, path);
|
|
180
|
+
const values = {};
|
|
181
|
+
for (const name of ["title", "slug", "summary"]) {
|
|
182
|
+
if (raw[name] !== void 0) values[name] = raw[name];
|
|
183
|
+
}
|
|
184
|
+
if (typeof raw.title !== "string" || !raw.title.trim()) fault("title is required");
|
|
185
|
+
if (raw.level !== void 0) values.level = raw.level;
|
|
186
|
+
if (raw.price !== void 0) values.price = raw.price;
|
|
187
|
+
else fault("price is required (in the major unit: 99 is 99.00)");
|
|
188
|
+
if (raw.description !== void 0)
|
|
189
|
+
values.description = richText(raw.description, "description", fault);
|
|
190
|
+
if (raw.cover !== void 0) {
|
|
191
|
+
const c = isObject(raw.cover) ? raw.cover : {};
|
|
192
|
+
const ref = picture(c.file, "cover", fault);
|
|
193
|
+
if (ref) values.cover = { media: ref, alt: typeof c.alt === "string" ? c.alt : "" };
|
|
194
|
+
}
|
|
195
|
+
const courseVideos = [];
|
|
196
|
+
if (raw.trailer !== void 0) {
|
|
197
|
+
const ref = video(raw.trailer, "trailer", fault);
|
|
198
|
+
if (ref) {
|
|
199
|
+
values.trailer = { media: ref };
|
|
200
|
+
courseVideos.push(ref[MEDIA]);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const theme = typeof raw.theme === "string" && raw.theme.trim() ? raw.theme.trim() : null;
|
|
204
|
+
if (theme) themes.add(theme);
|
|
205
|
+
const lessons = /* @__PURE__ */ new Map();
|
|
206
|
+
const programs = [];
|
|
207
|
+
const outline = isObject(raw.outline) && Array.isArray(raw.outline.sections) ? raw.outline : null;
|
|
208
|
+
if (!outline) fault("outline is { sections: [{ key, title, lessons: [...] }] }");
|
|
209
|
+
const lessonKeys = /* @__PURE__ */ new Set();
|
|
210
|
+
const sections = (outline?.sections ?? []).map((section, s) => {
|
|
211
|
+
const sec = isObject(section) ? section : {};
|
|
212
|
+
const list = Array.isArray(sec.lessons) ? sec.lessons : [];
|
|
213
|
+
return {
|
|
214
|
+
...sec,
|
|
215
|
+
lessons: list.map((given, l) => {
|
|
216
|
+
const where = `outline.sections.${s}.lessons.${l}`;
|
|
217
|
+
if (!isObject(given)) {
|
|
218
|
+
fault(`${where} is a lesson object`);
|
|
219
|
+
return given;
|
|
220
|
+
}
|
|
221
|
+
const lesson = { ...given };
|
|
222
|
+
const lessonKey = typeof lesson.key === "string" ? lesson.key : "";
|
|
223
|
+
if (!LESSON_KEY_RE.test(lessonKey))
|
|
224
|
+
fault(`${where}.key is lowercase letters, digits and -`);
|
|
225
|
+
else if (lessonKeys.has(lessonKey)) fault(`${where}.key "${lessonKey}" is used twice`);
|
|
226
|
+
lessonKeys.add(lessonKey);
|
|
227
|
+
if (typeof lesson.sourceId === "string") {
|
|
228
|
+
const before = sourceIds.get(`${key}|${lesson.sourceId}`);
|
|
229
|
+
if (before) fault(`${where}.sourceId "${lesson.sourceId}" is ${before}'s too`);
|
|
230
|
+
sourceIds.set(`${key}|${lesson.sourceId}`, where);
|
|
231
|
+
lessons.set(lesson.sourceId, lessonKey);
|
|
232
|
+
}
|
|
233
|
+
if (lesson.video !== void 0) {
|
|
234
|
+
const ref = video(lesson.video, `${where}.video`, fault);
|
|
235
|
+
if (ref) {
|
|
236
|
+
lesson.video = { media: ref };
|
|
237
|
+
courseVideos.push(ref[MEDIA]);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
for (const text2 of ["notes", "body"]) {
|
|
241
|
+
if (lesson[text2] !== void 0)
|
|
242
|
+
lesson[text2] = richText(lesson[text2], `${where}.${text2}`, fault);
|
|
243
|
+
}
|
|
244
|
+
if (lesson.kind === "download") {
|
|
245
|
+
const ref = download(lesson.file, `${where}.file`, fault);
|
|
246
|
+
if (ref) lesson.file = ref;
|
|
247
|
+
}
|
|
248
|
+
if (lesson.kind === "session") {
|
|
249
|
+
if (typeof lesson.program !== "string" || !lesson.program.trim()) {
|
|
250
|
+
fault(`${where}.program is a published program's id or its title`);
|
|
251
|
+
} else programs.push({ name: lesson.program, path: [s, l] });
|
|
252
|
+
}
|
|
253
|
+
return lesson;
|
|
254
|
+
})
|
|
255
|
+
};
|
|
256
|
+
});
|
|
257
|
+
values.outline = { sections };
|
|
258
|
+
const status = raw.status ?? "draft";
|
|
259
|
+
if (!STATUSES.includes(status)) fault(`status is ${STATUSES.join(" or ")}`);
|
|
260
|
+
if (values.slug === void 0 && typeof raw.title === "string")
|
|
261
|
+
values.slug = slugify(raw.title);
|
|
262
|
+
courses.push({
|
|
263
|
+
file: path,
|
|
264
|
+
key,
|
|
265
|
+
status,
|
|
266
|
+
theme,
|
|
267
|
+
values,
|
|
268
|
+
lessons,
|
|
269
|
+
videos: courseVideos,
|
|
270
|
+
programs
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
for (const [file, text] of [...folder.bundles].sort(([a], [b]) => a.localeCompare(b))) {
|
|
274
|
+
const path = `bundles/${file}`;
|
|
275
|
+
const fault = (message) => problems.push({ file: path, message });
|
|
276
|
+
let raw;
|
|
277
|
+
try {
|
|
278
|
+
raw = JSON.parse(text);
|
|
279
|
+
} catch (error) {
|
|
280
|
+
fault(`Not JSON: ${error.message}`);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (!isObject(raw)) {
|
|
284
|
+
fault("A bundle is a JSON object");
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
const key = typeof raw.key === "string" ? raw.key : file.replace(/\.json$/i, "");
|
|
288
|
+
const values = {};
|
|
289
|
+
for (const name of ["title", "slug", "summary", "price"]) {
|
|
290
|
+
if (raw[name] !== void 0) values[name] = raw[name];
|
|
291
|
+
}
|
|
292
|
+
if (values.slug === void 0 && typeof raw.title === "string")
|
|
293
|
+
values.slug = slugify(raw.title);
|
|
294
|
+
if (raw.description !== void 0)
|
|
295
|
+
values.description = richText(raw.description, "description", fault);
|
|
296
|
+
if (raw.cover !== void 0) {
|
|
297
|
+
const c = isObject(raw.cover) ? raw.cover : {};
|
|
298
|
+
const ref = picture(c.file, "cover", fault);
|
|
299
|
+
if (ref) values.cover = { media: ref, alt: typeof c.alt === "string" ? c.alt : "" };
|
|
300
|
+
}
|
|
301
|
+
const list = Array.isArray(raw.courses) ? raw.courses.filter((c) => typeof c === "string") : [];
|
|
302
|
+
for (const c of list) if (!keys.has(c)) fault(`courses: "${c}" is no course in courses/`);
|
|
303
|
+
const status = raw.status ?? "draft";
|
|
304
|
+
if (!STATUSES.includes(status)) fault(`status is ${STATUSES.join(" or ")}`);
|
|
305
|
+
bundles.push({ file: path, key, status, values, courses: list });
|
|
306
|
+
}
|
|
307
|
+
const csv = (text, file, columns) => {
|
|
308
|
+
const rows = parseCsv(text);
|
|
309
|
+
const header = rows.shift();
|
|
310
|
+
const names = (header?.cells ?? []).map((c) => c.trim().toLowerCase());
|
|
311
|
+
for (const required of columns.filter((c) => c.endsWith("*")).map((c) => c.slice(0, -1))) {
|
|
312
|
+
if (!names.includes(required))
|
|
313
|
+
problems.push({ file, message: `The header has no "${required}" column` });
|
|
314
|
+
}
|
|
315
|
+
return rows.map((row) => {
|
|
316
|
+
const cell = (name) => {
|
|
317
|
+
const at = names.indexOf(name);
|
|
318
|
+
return at >= 0 ? (row.cells[at] ?? "").trim() : "";
|
|
319
|
+
};
|
|
320
|
+
return { line: row.line, cell };
|
|
321
|
+
});
|
|
322
|
+
};
|
|
323
|
+
const access = [];
|
|
324
|
+
const bundleKeys = new Set(bundles.map((b) => b.key));
|
|
325
|
+
if (folder.access !== null) {
|
|
326
|
+
for (const { line, cell } of csv(folder.access, "access.csv", [
|
|
327
|
+
"email*",
|
|
328
|
+
"course",
|
|
329
|
+
"bundle",
|
|
330
|
+
"from",
|
|
331
|
+
"until",
|
|
332
|
+
"completed"
|
|
333
|
+
])) {
|
|
334
|
+
const fault = (message) => problems.push({ file: `access.csv:${line}`, message });
|
|
335
|
+
const email = cell("email").toLowerCase();
|
|
336
|
+
const course = cell("course") || null;
|
|
337
|
+
const bundle = cell("bundle") || null;
|
|
338
|
+
if (!EMAIL_RE.test(email)) fault("email is an address");
|
|
339
|
+
if (Boolean(course) === Boolean(bundle)) fault("name a course or a bundle, not both");
|
|
340
|
+
if (course && !keys.has(course)) fault(`"${course}" is no course in courses/`);
|
|
341
|
+
if (bundle && !bundleKeys.has(bundle)) fault(`"${bundle}" is no bundle in bundles/`);
|
|
342
|
+
const date = (name) => {
|
|
343
|
+
const v = cell(name);
|
|
344
|
+
if (!v) return null;
|
|
345
|
+
if (!DATETIME_RE.test(v) || Number.isNaN(Date.parse(v))) {
|
|
346
|
+
fault(`${name} is a date (2024-05-01) or a date and time with its zone`);
|
|
347
|
+
return null;
|
|
348
|
+
}
|
|
349
|
+
return new Date(v).toISOString();
|
|
350
|
+
};
|
|
351
|
+
const completed = /^(true|yes|1)$/i.test(cell("completed"));
|
|
352
|
+
access.push({
|
|
353
|
+
file: "access.csv",
|
|
354
|
+
line,
|
|
355
|
+
email,
|
|
356
|
+
course,
|
|
357
|
+
bundle,
|
|
358
|
+
from: date("from"),
|
|
359
|
+
until: date("until"),
|
|
360
|
+
completed
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
let progress = null;
|
|
365
|
+
if (folder.progress !== null) {
|
|
366
|
+
progress = [];
|
|
367
|
+
for (const { line, cell } of csv(folder.progress, "progress.csv", [
|
|
368
|
+
"email*",
|
|
369
|
+
"course*",
|
|
370
|
+
"lesson*",
|
|
371
|
+
"state",
|
|
372
|
+
"done_at",
|
|
373
|
+
"position"
|
|
374
|
+
])) {
|
|
375
|
+
const fault = (message) => problems.push({ file: `progress.csv:${line}`, message });
|
|
376
|
+
const email = cell("email").toLowerCase();
|
|
377
|
+
const course = cell("course");
|
|
378
|
+
if (!EMAIL_RE.test(email)) fault("email is an address");
|
|
379
|
+
if (!keys.has(course)) fault(`"${course}" is no course in courses/`);
|
|
380
|
+
const state = (cell("state") || "done").toLowerCase();
|
|
381
|
+
if (state !== "started" && state !== "done") fault("state is started or done");
|
|
382
|
+
const doneAt = cell("done_at");
|
|
383
|
+
if (doneAt && (!DATETIME_RE.test(doneAt) || Number.isNaN(Date.parse(doneAt))))
|
|
384
|
+
fault("done_at is a date or a date and time");
|
|
385
|
+
const position = cell("position");
|
|
386
|
+
if (position && !/^\d+(\.\d+)?$/.test(position)) fault("position is seconds");
|
|
387
|
+
progress.push({
|
|
388
|
+
file: "progress.csv",
|
|
389
|
+
line,
|
|
390
|
+
email,
|
|
391
|
+
course,
|
|
392
|
+
sourceId: cell("lesson"),
|
|
393
|
+
state: state === "started" ? "started" : "done",
|
|
394
|
+
doneAt: doneAt ? new Date(doneAt).toISOString() : null,
|
|
395
|
+
position: position ? Number(position) : null
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
courses,
|
|
401
|
+
bundles,
|
|
402
|
+
themes: [...themes].sort(),
|
|
403
|
+
videos,
|
|
404
|
+
files,
|
|
405
|
+
access,
|
|
406
|
+
progress,
|
|
407
|
+
problems
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
function withMedia(value, id) {
|
|
411
|
+
if (Array.isArray(value)) return value.map((v) => withMedia(v, id));
|
|
412
|
+
if (!isObject(value)) return value;
|
|
413
|
+
if (typeof value[MEDIA] === "string" && Object.keys(value).length === 1) return id(value[MEDIA]);
|
|
414
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, withMedia(v, id)]));
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export {
|
|
418
|
+
COURSE,
|
|
419
|
+
BUNDLE,
|
|
420
|
+
THEME,
|
|
421
|
+
ENROLMENT,
|
|
422
|
+
PROGRESS,
|
|
423
|
+
parseCsv,
|
|
424
|
+
readCoursesFolder,
|
|
425
|
+
withMedia
|
|
426
|
+
};
|