@composable-os/cli 0.0.0-stage → 0.1.0-beta.10

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.
Files changed (176) hide show
  1. package/LICENSE +109 -0
  2. package/README.md +24 -2
  3. package/dist/accounts-X4CKM3HL.js +46 -0
  4. package/dist/blocks-YWDHEBOK.js +233 -0
  5. package/dist/chunk-2ABSVMGB.js +13 -0
  6. package/dist/chunk-5KSXSK7Y.js +47 -0
  7. package/dist/chunk-6GDEDOV7.js +157 -0
  8. package/dist/chunk-6UPUF4ML.js +29998 -0
  9. package/dist/chunk-6UWRT4VM.js +220 -0
  10. package/dist/chunk-6VXHPKGG.js +1302 -0
  11. package/dist/chunk-7CQGMVDO.js +63 -0
  12. package/dist/chunk-A4NTSYWV.js +11092 -0
  13. package/dist/chunk-CP6BXI6F.js +25825 -0
  14. package/dist/chunk-DU6OIFOU.js +2358 -0
  15. package/dist/chunk-E4WCYTCT.js +347 -0
  16. package/dist/chunk-GYSGVT6R.js +284 -0
  17. package/dist/chunk-GYTEW66E.js +50 -0
  18. package/dist/chunk-HDIOVFAZ.js +544 -0
  19. package/dist/chunk-IFG4FRW5.js +504 -0
  20. package/dist/chunk-JJHHNV46.js +227 -0
  21. package/dist/chunk-K6T42YCH.js +704 -0
  22. package/dist/chunk-KGHA67HG.js +765 -0
  23. package/dist/chunk-LJFLENFC.js +88 -0
  24. package/dist/chunk-LLULPZAJ.js +365 -0
  25. package/dist/chunk-M7A6FR2Z.js +2586 -0
  26. package/dist/chunk-O2UIZHND.js +249 -0
  27. package/dist/chunk-Q4N55M2Z.js +552 -0
  28. package/dist/chunk-QGGJ2G6Z.js +349 -0
  29. package/dist/chunk-R4W5XAUD.js +837 -0
  30. package/dist/chunk-TQFGGSA4.js +748 -0
  31. package/dist/chunk-VY64DMQO.js +15302 -0
  32. package/dist/chunk-WC5LHBBA.js +426 -0
  33. package/dist/chunk-WO6D3XP3.js +9351 -0
  34. package/dist/chunk-Y7JZFSTQ.js +1425 -0
  35. package/dist/chunk-YJJHF65I.js +759 -0
  36. package/dist/chunk-Z3RMMB6S.js +668 -0
  37. package/dist/cli-2DFENR62.js +134 -0
  38. package/dist/cli-YIOVSKOD.js +24531 -0
  39. package/dist/cli.js +25 -0
  40. package/dist/client-repo-PRU3VCQQ.js +55 -0
  41. package/dist/create.js +25 -0
  42. package/dist/fields-2NC7LSYE.js +50 -0
  43. package/dist/grants-QXMKYMGB.js +32 -0
  44. package/dist/invoke-I3NNNXVL.js +39 -0
  45. package/dist/order-PUWD65BC.js +19 -0
  46. package/dist/person-WIQQ7ITU.js +68 -0
  47. package/dist/plan-on-sale-NB774CQL.js +26 -0
  48. package/dist/platform/migrations/0001_init.sql +59 -0
  49. package/dist/platform/migrations/0002_users_settings.sql +34 -0
  50. package/dist/platform/migrations/0003_people_organisations.sql +225 -0
  51. package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
  52. package/dist/platform/migrations/0005_views_audit.sql +79 -0
  53. package/dist/platform/migrations/0006_search.sql +223 -0
  54. package/dist/platform/migrations/0007_crm_views.sql +141 -0
  55. package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
  56. package/dist/platform/migrations/0010_approval_queue.sql +90 -0
  57. package/dist/platform/migrations/0011_connected_apps.sql +36 -0
  58. package/dist/platform/migrations/0020_extensions.sql +373 -0
  59. package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
  60. package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
  61. package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
  62. package/dist/platform/migrations/0061_audit_retention.sql +95 -0
  63. package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
  64. package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
  65. package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
  66. package/dist/platform/migrations/0065_storage_stats.sql +13 -0
  67. package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
  68. package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
  69. package/dist/platform/migrations/0100_stage_history.sql +54 -0
  70. package/dist/platform/migrations/0110_integrations.sql +103 -0
  71. package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
  72. package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
  73. package/dist/platform/migrations/0130_automations.sql +113 -0
  74. package/dist/platform/migrations/0131_notifications.sql +29 -0
  75. package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
  76. package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
  77. package/dist/platform/migrations/0160_releases.sql +41 -0
  78. package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
  79. package/dist/platform/migrations/0170_automation_connections.sql +32 -0
  80. package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
  81. package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
  82. package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
  83. package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
  84. package/dist/platform/migrations/0210_daily_digest.sql +33 -0
  85. package/dist/platform/migrations/0220_entitlements.sql +13 -0
  86. package/dist/platform/migrations/0221_support_grants.sql +33 -0
  87. package/dist/platform/migrations/0250_judge.sql +11 -0
  88. package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
  89. package/dist/platform/migrations/0252_judge_usage.sql +14 -0
  90. package/dist/platform/migrations/0255_summary_checks.sql +9 -0
  91. package/dist/platform/migrations/0256_outbound_check.sql +9 -0
  92. package/dist/platform/migrations/0260_ai_usage.sql +20 -0
  93. package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
  94. package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
  95. package/dist/platform/migrations/0320_duplicates.sql +41 -0
  96. package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
  97. package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
  98. package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
  99. package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
  100. package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
  101. package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
  102. package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
  103. package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
  104. package/dist/platform/migrations/0362_voice_usage.sql +17 -0
  105. package/dist/platform/migrations/0365_routines.sql +38 -0
  106. package/dist/platform/migrations/0370_approval_execution.sql +20 -0
  107. package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
  108. package/dist/platform/migrations/0380_rec_views.sql +94 -0
  109. package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
  110. package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
  111. package/dist/platform/migrations/0402_erase_requests.sql +34 -0
  112. package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
  113. package/dist/platform/migrations/0411_device_sessions.sql +33 -0
  114. package/dist/platform/migrations/0420_transition_history.sql +100 -0
  115. package/dist/platform/migrations/0421_orders.sql +134 -0
  116. package/dist/platform/migrations/0422_order_payments.sql +109 -0
  117. package/dist/platform/migrations/0440_capacity.sql +100 -0
  118. package/dist/platform/migrations/0441_capacity_views.sql +40 -0
  119. package/dist/platform/migrations/0450_order_grants.sql +21 -0
  120. package/dist/platform/migrations/0451_pack_roles.sql +18 -0
  121. package/dist/platform/migrations/0460_content.sql +59 -0
  122. package/dist/platform/migrations/0461_media.sql +52 -0
  123. package/dist/platform/migrations/0470_publish_versions.sql +36 -0
  124. package/dist/platform/migrations/0471_permission_grants.sql +18 -0
  125. package/dist/platform/migrations/0490_staff_invites.sql +40 -0
  126. package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
  127. package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
  128. package/dist/platform/migrations/0500_published_media_field.sql +25 -0
  129. package/dist/platform/migrations/0501_import_keys.sql +23 -0
  130. package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
  131. package/dist/platform/migrations/0516_order_tax.sql +63 -0
  132. package/dist/platform/migrations/0520_publish_requests.sql +20 -0
  133. package/dist/platform/migrations/0530_subscriptions.sql +173 -0
  134. package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
  135. package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
  136. package/dist/platform/migrations/0533_allowances.sql +262 -0
  137. package/dist/platform/migrations/0560_recurring.sql +75 -0
  138. package/dist/platform/migrations/0580_subscription_grants.sql +21 -0
  139. package/dist/platform/migrations/0581_once_per_person.sql +34 -0
  140. package/dist/platform/migrations/0600_tool_actions_ran_at_queue.sql +22 -0
  141. package/dist/platform/sdk/allowance.ts +522 -0
  142. package/dist/platform/sdk/automation-check.ts +534 -0
  143. package/dist/platform/sdk/automation.ts +743 -0
  144. package/dist/platform/sdk/behaviour.ts +215 -0
  145. package/dist/platform/sdk/capacity.ts +201 -0
  146. package/dist/platform/sdk/contract.ts +884 -0
  147. package/dist/platform/sdk/document.ts +935 -0
  148. package/dist/platform/sdk/field-types.ts +111 -0
  149. package/dist/platform/sdk/formula/ast.ts +72 -0
  150. package/dist/platform/sdk/formula/compile.ts +454 -0
  151. package/dist/platform/sdk/formula/index.ts +4 -0
  152. package/dist/platform/sdk/formula/parser.ts +417 -0
  153. package/dist/platform/sdk/index.ts +36 -0
  154. package/dist/platform/sdk/manifest.ts +1709 -0
  155. package/dist/platform/sdk/media.ts +108 -0
  156. package/dist/platform/sdk/permissions.ts +60 -0
  157. package/dist/platform/sdk/recurring.ts +321 -0
  158. package/dist/platform/sdk/renamed.ts +63 -0
  159. package/dist/platform/sdk/rich-text-fixture.ts +68 -0
  160. package/dist/platform/sdk/rich-text.ts +221 -0
  161. package/dist/platform/sdk/runtime.ts +410 -0
  162. package/dist/platform/sdk/sellable.ts +888 -0
  163. package/dist/platform/sdk/slug.ts +25 -0
  164. package/dist/platform/sdk/states.ts +484 -0
  165. package/dist/platform/sdk/testing.ts +847 -0
  166. package/dist/platform/sdk/unique.ts +81 -0
  167. package/dist/products-2H5DR232.js +11 -0
  168. package/dist/run-PZUCSDDL.js +508 -0
  169. package/dist/run-RT2HVWMK.js +648 -0
  170. package/dist/run-Y7T4TVOL.js +309 -0
  171. package/dist/src-GYS6NBQB.js +44 -0
  172. package/dist/src-XTOD7EXW.js +348 -0
  173. package/dist/token-MOJUA2U6.js +70 -0
  174. package/dist/token-util-TENRN4V7.js +6 -0
  175. package/dist/visibility-DLGHU4YN.js +33 -0
  176. package/package.json +33 -4
@@ -0,0 +1,426 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ MEDIA_CAPTION_MAX_BYTES,
4
+ mediaProblem,
5
+ sniffFileType
6
+ } from "./chunk-CP6BXI6F.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
+ };