@composable-os/cli 0.1.0-beta.7 → 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-HVTAQ5W2.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-2P4AZTFL.js → chunk-P6YZMS3F.js} +11 -5
- package/dist/{chunk-Y7PQ7NIL.js → chunk-RFJOPI42.js} +138 -14
- package/dist/{chunk-VL62IMBN.js → chunk-RXRD34QW.js} +10 -8
- package/dist/{chunk-BEKL3CA2.js → chunk-STBMGUTM.js} +7065 -4580
- package/dist/chunk-VTQNOQGA.js +150 -0
- package/dist/chunk-XSE37HK5.js +426 -0
- package/dist/{chunk-MINQ46RU.js → chunk-Z52IX5WL.js} +257 -844
- package/dist/chunk-ZSR5H4O7.js +552 -0
- package/dist/{cli-XHC6QBQW.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-G55G4OLV.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-AZFEPZOE.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/0520_publish_requests.sql +20 -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-5SR5ZWW7.js +0 -26
- package/dist/src-SSVOBH6L.js +0 -31
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
importRecords,
|
|
4
|
+
knownByHash,
|
|
5
|
+
outcomeLines,
|
|
6
|
+
sha256
|
|
7
|
+
} from "./chunk-7CQGMVDO.js";
|
|
8
|
+
import {
|
|
9
|
+
BUNDLE,
|
|
10
|
+
COURSE,
|
|
11
|
+
ENROLMENT,
|
|
12
|
+
PROGRESS,
|
|
13
|
+
THEME,
|
|
14
|
+
readCoursesFolder,
|
|
15
|
+
withMedia
|
|
16
|
+
} from "./chunk-XSE37HK5.js";
|
|
17
|
+
import {
|
|
18
|
+
confirmer,
|
|
19
|
+
sessionHost,
|
|
20
|
+
signInForRun
|
|
21
|
+
} from "./chunk-E4WCYTCT.js";
|
|
22
|
+
import "./chunk-2MLAPQU7.js";
|
|
23
|
+
import "./chunk-5KSXSK7Y.js";
|
|
24
|
+
|
|
25
|
+
// src/courses-import/run.ts
|
|
26
|
+
import { createHash } from "node:crypto";
|
|
27
|
+
import {
|
|
28
|
+
closeSync,
|
|
29
|
+
existsSync,
|
|
30
|
+
openSync,
|
|
31
|
+
readdirSync,
|
|
32
|
+
readFileSync,
|
|
33
|
+
readSync,
|
|
34
|
+
statSync
|
|
35
|
+
} from "node:fs";
|
|
36
|
+
import { join, resolve } from "node:path";
|
|
37
|
+
|
|
38
|
+
// src/courses-import/engine.ts
|
|
39
|
+
var placeholder = (i) => `med_import${i}`;
|
|
40
|
+
async function personFor(host, email) {
|
|
41
|
+
const found = await host.call("person.list", {
|
|
42
|
+
filter: { field: "emails", op: "eq", value: email },
|
|
43
|
+
limit: 3
|
|
44
|
+
});
|
|
45
|
+
if (!found.items.length) {
|
|
46
|
+
return {
|
|
47
|
+
problem: `No person on the host has ${email}: import people first (the people CSV import)`
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (found.items.length > 1) {
|
|
51
|
+
return {
|
|
52
|
+
problem: `${found.items.length} people on the host have ${email}: merge them first (left out, not guessed)`
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return { id: found.items[0].id };
|
|
56
|
+
}
|
|
57
|
+
async function runCoursesImport(plan, host, options = {}) {
|
|
58
|
+
const report = {
|
|
59
|
+
problems: [...plan.problems],
|
|
60
|
+
records: [],
|
|
61
|
+
unmatched: [],
|
|
62
|
+
media: { uploaded: 0, already: 0, videosUploaded: 0, videosAlready: 0 },
|
|
63
|
+
waiting: [],
|
|
64
|
+
published: [],
|
|
65
|
+
notes: [],
|
|
66
|
+
wroteNothing: true
|
|
67
|
+
};
|
|
68
|
+
if (report.problems.length) return report;
|
|
69
|
+
const say = options.progress ?? (() => {
|
|
70
|
+
});
|
|
71
|
+
const sleep = options.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
72
|
+
const emails = [
|
|
73
|
+
.../* @__PURE__ */ new Set([...plan.access.map((r) => r.email), ...(plan.progress ?? []).map((r) => r.email)])
|
|
74
|
+
];
|
|
75
|
+
const people = /* @__PURE__ */ new Map();
|
|
76
|
+
for (const email of emails) people.set(email, await personFor(host, email));
|
|
77
|
+
const programs = /* @__PURE__ */ new Map();
|
|
78
|
+
for (const name of new Set(plan.courses.flatMap((c) => c.programs.map((p) => p.name)))) {
|
|
79
|
+
const page = await host.call(
|
|
80
|
+
"record.list",
|
|
81
|
+
{
|
|
82
|
+
object: "programs.program",
|
|
83
|
+
filter: { field: "publishState", op: "eq", value: "published" },
|
|
84
|
+
limit: 200
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
const hit = page.items.find((p) => p.id === name) ?? page.items.filter((p) => p.values.title === name);
|
|
88
|
+
const one = Array.isArray(hit) ? hit.length === 1 ? hit[0] : void 0 : hit;
|
|
89
|
+
programs.set(name, one?.id ?? null);
|
|
90
|
+
if (!one) {
|
|
91
|
+
for (const c of plan.courses.filter((c2) => c2.programs.some((p) => p.name === name))) {
|
|
92
|
+
report.problems.push({
|
|
93
|
+
file: c.file,
|
|
94
|
+
message: `No one published program is "${name}" (its id, or its exact title)`
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (report.problems.length) return report;
|
|
100
|
+
const withPrograms = (values, course) => {
|
|
101
|
+
const outline = JSON.parse(JSON.stringify(values.outline));
|
|
102
|
+
for (const p of course.programs) {
|
|
103
|
+
const lesson = outline.sections[p.path[0]]?.lessons[p.path[1]];
|
|
104
|
+
if (lesson) lesson.program = programs.get(p.name);
|
|
105
|
+
}
|
|
106
|
+
return { ...values, outline };
|
|
107
|
+
};
|
|
108
|
+
const fileHashes = /* @__PURE__ */ new Map();
|
|
109
|
+
for (const [ref, file] of plan.files) fileHashes.set(ref, await sha256(file.bytes));
|
|
110
|
+
const videoHashes = /* @__PURE__ */ new Map();
|
|
111
|
+
for (const [ref, video] of plan.videos) {
|
|
112
|
+
const source = video.file ? options.videoFiles?.(video.file) : void 0;
|
|
113
|
+
if (video.file && !source) {
|
|
114
|
+
report.problems.push({ file: `videos/${video.file}`, message: "Cannot be read" });
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
videoHashes.set(ref, source ? source.sha256 : await sha256(`url:${video.url}`));
|
|
118
|
+
}
|
|
119
|
+
if (report.problems.length) return report;
|
|
120
|
+
const known = await knownByHash(host, [...fileHashes.values(), ...videoHashes.values()]);
|
|
121
|
+
let n = 0;
|
|
122
|
+
const dryIds = /* @__PURE__ */ new Map();
|
|
123
|
+
const dryMedia = (ref) => {
|
|
124
|
+
const hash = fileHashes.get(ref) ?? videoHashes.get(ref);
|
|
125
|
+
const have = hash ? known.get(hash)?.id : void 0;
|
|
126
|
+
if (have) return have;
|
|
127
|
+
if (!dryIds.has(ref)) {
|
|
128
|
+
n += 1;
|
|
129
|
+
dryIds.set(ref, placeholder(n));
|
|
130
|
+
}
|
|
131
|
+
return dryIds.get(ref);
|
|
132
|
+
};
|
|
133
|
+
const themeRecords = plan.themes.map((name) => ({
|
|
134
|
+
key: `theme:${name}`,
|
|
135
|
+
file: null,
|
|
136
|
+
values: { name }
|
|
137
|
+
}));
|
|
138
|
+
const courseRecords = (media, themeId) => plan.courses.map((c) => ({
|
|
139
|
+
key: c.key,
|
|
140
|
+
file: c.file,
|
|
141
|
+
values: {
|
|
142
|
+
...withMedia(withPrograms(c.values, c), media),
|
|
143
|
+
...c.theme && themeId(c.theme) ? { theme: themeId(c.theme) } : {}
|
|
144
|
+
}
|
|
145
|
+
}));
|
|
146
|
+
const dryThemes = await importRecords(host, THEME, themeRecords, {
|
|
147
|
+
dryRun: true,
|
|
148
|
+
checkReferences: false
|
|
149
|
+
});
|
|
150
|
+
const dryCourses = await importRecords(
|
|
151
|
+
host,
|
|
152
|
+
COURSE,
|
|
153
|
+
courseRecords(dryMedia, () => null),
|
|
154
|
+
{
|
|
155
|
+
dryRun: true,
|
|
156
|
+
checkReferences: false
|
|
157
|
+
}
|
|
158
|
+
);
|
|
159
|
+
const dryBundles = await importRecords(
|
|
160
|
+
host,
|
|
161
|
+
BUNDLE,
|
|
162
|
+
plan.bundles.map((b) => ({
|
|
163
|
+
key: b.key,
|
|
164
|
+
file: b.file,
|
|
165
|
+
values: {
|
|
166
|
+
...withMedia(b.values, dryMedia),
|
|
167
|
+
courses: { items: b.courses.map((_c, i) => `rec_importplaceholder${i}`) }
|
|
168
|
+
}
|
|
169
|
+
})),
|
|
170
|
+
{ dryRun: true, checkReferences: false }
|
|
171
|
+
);
|
|
172
|
+
for (const r of [...dryThemes, ...dryCourses, ...dryBundles]) {
|
|
173
|
+
if (r.outcome === "invalid") {
|
|
174
|
+
for (const message of r.problems) report.problems.push({ file: r.file ?? r.key, message });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const courseOf = new Map(plan.courses.map((c) => [c.key, c]));
|
|
178
|
+
const access = plan.access.flatMap((row) => {
|
|
179
|
+
const who = people.get(row.email);
|
|
180
|
+
if (!who || "problem" in who) {
|
|
181
|
+
report.unmatched.push({
|
|
182
|
+
file: `${row.file}:${row.line}`,
|
|
183
|
+
message: who?.problem ?? "No person"
|
|
184
|
+
});
|
|
185
|
+
return [];
|
|
186
|
+
}
|
|
187
|
+
return [{ row, person: who.id }];
|
|
188
|
+
});
|
|
189
|
+
const progress = (plan.progress ?? []).flatMap((row) => {
|
|
190
|
+
const who = people.get(row.email);
|
|
191
|
+
if (!who || "problem" in who) {
|
|
192
|
+
report.unmatched.push({
|
|
193
|
+
file: `${row.file}:${row.line}`,
|
|
194
|
+
message: who?.problem ?? "No person"
|
|
195
|
+
});
|
|
196
|
+
return [];
|
|
197
|
+
}
|
|
198
|
+
const lessonKey = courseOf.get(row.course)?.lessons.get(row.sourceId);
|
|
199
|
+
if (!lessonKey) {
|
|
200
|
+
report.unmatched.push({
|
|
201
|
+
file: `${row.file}:${row.line}`,
|
|
202
|
+
message: `No lesson of ${row.course} has the sourceId "${row.sourceId}" (dropped from the outline?)`
|
|
203
|
+
});
|
|
204
|
+
return [];
|
|
205
|
+
}
|
|
206
|
+
return [{ row, person: who.id, lessonKey }];
|
|
207
|
+
});
|
|
208
|
+
if (report.problems.length || options.dryRun) {
|
|
209
|
+
report.records = [...dryThemes, ...dryCourses, ...dryBundles];
|
|
210
|
+
return report;
|
|
211
|
+
}
|
|
212
|
+
if (options.publish && options.confirm) {
|
|
213
|
+
const live = [...plan.courses, ...plan.bundles].filter((x) => x.status === "published");
|
|
214
|
+
const summary = `Publish ${live.length} course(s) and bundle(s), as you`;
|
|
215
|
+
if (!await options.confirm(summary)) {
|
|
216
|
+
report.notes.push({ file: "", message: "Stopped before writing: not confirmed" });
|
|
217
|
+
return report;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const ids = /* @__PURE__ */ new Map();
|
|
221
|
+
for (const [ref, file] of plan.files) {
|
|
222
|
+
const hash = fileHashes.get(ref);
|
|
223
|
+
const have = known.get(hash);
|
|
224
|
+
if (have) {
|
|
225
|
+
report.media.already += 1;
|
|
226
|
+
ids.set(ref, have.id);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const up = await host.upload(file.bytes, file.name);
|
|
230
|
+
report.wroteNothing = false;
|
|
231
|
+
report.media[up.existing ? "already" : "uploaded"] += 1;
|
|
232
|
+
ids.set(ref, up.id);
|
|
233
|
+
}
|
|
234
|
+
for (const [ref, video] of plan.videos) {
|
|
235
|
+
const hash = videoHashes.get(ref);
|
|
236
|
+
const have = known.get(hash);
|
|
237
|
+
if (have && have.state !== "uploading" && have.state !== "failed") {
|
|
238
|
+
report.media.videosAlready += 1;
|
|
239
|
+
ids.set(ref, have.id);
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (have) {
|
|
243
|
+
await host.call("media.delete", { id: have.id });
|
|
244
|
+
report.notes.push({
|
|
245
|
+
file: video.file ? `videos/${video.file}` : video.url ?? ref,
|
|
246
|
+
message: `Sent again (it was ${have.state})`
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
report.wroteNothing = false;
|
|
250
|
+
if (video.url) {
|
|
251
|
+
const made = await host.call("media.videoFromUrl", {
|
|
252
|
+
name: video.name,
|
|
253
|
+
url: video.url,
|
|
254
|
+
sha256: hash
|
|
255
|
+
});
|
|
256
|
+
ids.set(ref, made.item.id);
|
|
257
|
+
say(`${video.url}: Stream is copying it`);
|
|
258
|
+
} else {
|
|
259
|
+
const source = options.videoFiles?.(video.file);
|
|
260
|
+
const asked = await host.call("media.videoUpload", { name: video.name, bytes: source.size, sha256: hash });
|
|
261
|
+
await sendVideo(
|
|
262
|
+
host,
|
|
263
|
+
asked.upload,
|
|
264
|
+
source,
|
|
265
|
+
(share) => say(`videos/${video.file}: ${Math.round(share * 100)}%`)
|
|
266
|
+
);
|
|
267
|
+
ids.set(ref, asked.item.id);
|
|
268
|
+
}
|
|
269
|
+
report.media.videosUploaded += 1;
|
|
270
|
+
for (const caption of video.captions) {
|
|
271
|
+
await host.uploadCaption?.(
|
|
272
|
+
ids.get(ref),
|
|
273
|
+
caption.language,
|
|
274
|
+
caption.label,
|
|
275
|
+
caption.vtt
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const ready = /* @__PURE__ */ new Set();
|
|
280
|
+
const deadline = Date.now() + (options.waitMs ?? 10 * 6e4);
|
|
281
|
+
for (; ; ) {
|
|
282
|
+
for (const [ref] of plan.videos) {
|
|
283
|
+
if (ready.has(ref)) continue;
|
|
284
|
+
const item = await host.call("media.videoRefresh", { id: ids.get(ref) });
|
|
285
|
+
if (item.state === "ready") ready.add(ref);
|
|
286
|
+
}
|
|
287
|
+
const left = [...plan.videos.keys()].filter((r) => !ready.has(r));
|
|
288
|
+
if (!left.length || Date.now() >= deadline) break;
|
|
289
|
+
say(`Waiting for Stream: ${left.length} video(s) not ready yet`);
|
|
290
|
+
await sleep(options.pollMs ?? 5e3);
|
|
291
|
+
}
|
|
292
|
+
const mediaId = (ref) => ids.get(ref);
|
|
293
|
+
const themes = await importRecords(host, THEME, themeRecords, {
|
|
294
|
+
dryRun: false,
|
|
295
|
+
checkReferences: true
|
|
296
|
+
});
|
|
297
|
+
const themeIds = new Map(themes.map((t) => [t.key, t.id]));
|
|
298
|
+
const writable = plan.courses.filter((c) => c.videos.every((v) => ready.has(v)));
|
|
299
|
+
for (const c of plan.courses.filter((c2) => !writable.includes(c2))) {
|
|
300
|
+
report.waiting.push({ file: c.file, videos: c.videos.filter((v) => !ready.has(v)) });
|
|
301
|
+
}
|
|
302
|
+
const courses = await importRecords(
|
|
303
|
+
host,
|
|
304
|
+
COURSE,
|
|
305
|
+
courseRecords(mediaId, (name) => themeIds.get(`theme:${name}`) ?? null).filter(
|
|
306
|
+
(r) => writable.some((c) => c.key === r.key)
|
|
307
|
+
),
|
|
308
|
+
{ dryRun: false, checkReferences: true }
|
|
309
|
+
);
|
|
310
|
+
const courseIds = new Map(courses.filter((r) => r.id).map((r) => [r.key, r.id]));
|
|
311
|
+
const bundles = await importRecords(
|
|
312
|
+
host,
|
|
313
|
+
BUNDLE,
|
|
314
|
+
plan.bundles.filter((b) => b.courses.every((c) => courseIds.has(c))).map((b) => ({
|
|
315
|
+
key: b.key,
|
|
316
|
+
file: b.file,
|
|
317
|
+
values: {
|
|
318
|
+
...withMedia(b.values, mediaId),
|
|
319
|
+
courses: { items: b.courses.map((c) => courseIds.get(c)) }
|
|
320
|
+
}
|
|
321
|
+
})),
|
|
322
|
+
{ dryRun: false, checkReferences: true }
|
|
323
|
+
);
|
|
324
|
+
for (const b of plan.bundles.filter((b2) => !b2.courses.every((c) => courseIds.has(c)))) {
|
|
325
|
+
report.waiting.push({ file: b.file, videos: [] });
|
|
326
|
+
}
|
|
327
|
+
const bundleIds = new Map(bundles.filter((r) => r.id).map((r) => [r.key, r.id]));
|
|
328
|
+
const enrolments = await importRecords(
|
|
329
|
+
host,
|
|
330
|
+
ENROLMENT,
|
|
331
|
+
access.filter(
|
|
332
|
+
({ row }) => row.course ? courseIds.has(row.course) : bundleIds.has(row.bundle)
|
|
333
|
+
).map(({ row, person }) => {
|
|
334
|
+
const key = `${row.email}|${row.course ? `course:${row.course}` : `bundle:${row.bundle}`}`;
|
|
335
|
+
return {
|
|
336
|
+
key,
|
|
337
|
+
file: `${row.file}:${row.line}`,
|
|
338
|
+
values: {
|
|
339
|
+
holder: person,
|
|
340
|
+
...row.course ? { course: courseIds.get(row.course) } : { bundle: bundleIds.get(row.bundle) },
|
|
341
|
+
source: "import",
|
|
342
|
+
reason: "Imported from the old platform",
|
|
343
|
+
...row.from ? { from: row.from } : {},
|
|
344
|
+
...row.until ? { until: row.until } : {},
|
|
345
|
+
completed: row.completed
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
}),
|
|
349
|
+
{ dryRun: false, checkReferences: true }
|
|
350
|
+
);
|
|
351
|
+
const own = /* @__PURE__ */ new Map();
|
|
352
|
+
for (const courseKey of new Set(progress.map((p) => p.row.course))) {
|
|
353
|
+
const id = courseIds.get(courseKey);
|
|
354
|
+
if (!id) continue;
|
|
355
|
+
const rows = /* @__PURE__ */ new Set();
|
|
356
|
+
let cursor = null;
|
|
357
|
+
do {
|
|
358
|
+
const page = await host.call("record.list", {
|
|
359
|
+
object: PROGRESS,
|
|
360
|
+
filter: {
|
|
361
|
+
and: [
|
|
362
|
+
{ field: "course", op: "eq", value: id },
|
|
363
|
+
{ field: "source", op: "neq", value: "import" }
|
|
364
|
+
]
|
|
365
|
+
},
|
|
366
|
+
limit: 200,
|
|
367
|
+
...cursor ? { cursor } : {}
|
|
368
|
+
});
|
|
369
|
+
for (const p of page.items) rows.add(`${p.values.holder}|${p.values.lessonKey}`);
|
|
370
|
+
cursor = page.nextCursor;
|
|
371
|
+
} while (cursor);
|
|
372
|
+
own.set(id, rows);
|
|
373
|
+
}
|
|
374
|
+
const kept = [];
|
|
375
|
+
const progressRecords = progress.flatMap(({ row, person, lessonKey }) => {
|
|
376
|
+
const id = courseIds.get(row.course);
|
|
377
|
+
if (!id) return [];
|
|
378
|
+
const key = `${row.email}|${row.course}|${row.sourceId}`;
|
|
379
|
+
if (own.get(id)?.has(`${person}|${lessonKey}`)) {
|
|
380
|
+
kept.push({
|
|
381
|
+
key,
|
|
382
|
+
file: `${row.file}:${row.line}`,
|
|
383
|
+
object: PROGRESS,
|
|
384
|
+
id: null,
|
|
385
|
+
outcome: "conflict",
|
|
386
|
+
fields: [],
|
|
387
|
+
problems: ["The student has their own progress on this lesson: kept"]
|
|
388
|
+
});
|
|
389
|
+
return [];
|
|
390
|
+
}
|
|
391
|
+
return [
|
|
392
|
+
{
|
|
393
|
+
key,
|
|
394
|
+
file: `${row.file}:${row.line}`,
|
|
395
|
+
values: {
|
|
396
|
+
holder: person,
|
|
397
|
+
course: id,
|
|
398
|
+
lessonKey,
|
|
399
|
+
state: row.state,
|
|
400
|
+
source: "import",
|
|
401
|
+
...row.doneAt ? { doneAt: row.doneAt, startedAt: row.doneAt } : {},
|
|
402
|
+
...row.position !== null ? { position: row.position } : {}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
];
|
|
406
|
+
});
|
|
407
|
+
const progressResults = progressRecords.length ? await importRecords(host, PROGRESS, progressRecords, { dryRun: false, checkReferences: true }) : [];
|
|
408
|
+
report.records = [...themes, ...courses, ...bundles, ...enrolments, ...progressResults, ...kept];
|
|
409
|
+
if (report.records.some((r) => r.outcome === "created" || r.outcome === "updated")) {
|
|
410
|
+
report.wroteNothing = false;
|
|
411
|
+
}
|
|
412
|
+
const withProgress = new Set(progress.map((p) => p.row.course));
|
|
413
|
+
const brought = plan.courses.filter(
|
|
414
|
+
(c) => plan.access.some((a) => a.course === c.key) && !withProgress.has(c.key)
|
|
415
|
+
);
|
|
416
|
+
if (plan.progress === null && plan.access.length) {
|
|
417
|
+
report.notes.push({
|
|
418
|
+
file: "",
|
|
419
|
+
message: "No progress.csv: completions imported, lesson progress not"
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
for (const c of brought) {
|
|
423
|
+
report.notes.push({ file: c.file, message: "No lesson progress to bring for this course" });
|
|
424
|
+
}
|
|
425
|
+
if (options.publish) {
|
|
426
|
+
for (const item of [
|
|
427
|
+
...plan.courses.map((c) => ({ object: COURSE, plan: c, id: courseIds.get(c.key) })),
|
|
428
|
+
...plan.bundles.map((b) => ({ object: BUNDLE, plan: b, id: bundleIds.get(b.key) }))
|
|
429
|
+
]) {
|
|
430
|
+
if (item.plan.status !== "published" || !item.id) continue;
|
|
431
|
+
const record = await host.call("record.get", {
|
|
432
|
+
object: item.object,
|
|
433
|
+
id: item.id
|
|
434
|
+
});
|
|
435
|
+
if (record.values.publishState === "published" && typeof record.values.publishedVersion === "number") {
|
|
436
|
+
const diff = await host.call("record.compareVersions", {
|
|
437
|
+
object: item.object,
|
|
438
|
+
id: item.id,
|
|
439
|
+
from: record.values.publishedVersion
|
|
440
|
+
});
|
|
441
|
+
if (!diff.changes.length) {
|
|
442
|
+
report.published.push({ file: item.plan.file, id: item.id, as: "unchanged" });
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
await host.call("record.publish", { object: item.object, id: item.id, note: "Imported" });
|
|
447
|
+
report.wroteNothing = false;
|
|
448
|
+
report.published.push({ file: item.plan.file, id: item.id, as: "published" });
|
|
449
|
+
}
|
|
450
|
+
} else {
|
|
451
|
+
for (const x of [...plan.courses, ...plan.bundles].filter((x2) => x2.status === "published")) {
|
|
452
|
+
report.notes.push({
|
|
453
|
+
file: x.file,
|
|
454
|
+
message: "Published in the source: imported as a draft (pass --publish to publish it)"
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
return report;
|
|
459
|
+
}
|
|
460
|
+
async function sendVideo(host, upload, source, progress) {
|
|
461
|
+
const send = host.send;
|
|
462
|
+
if (!send) throw new Error("This host cannot send to Stream");
|
|
463
|
+
if (upload.protocol === "post") {
|
|
464
|
+
const form = new FormData();
|
|
465
|
+
form.append("file", new Blob([new Uint8Array(await source.read(0, source.size))]), "video");
|
|
466
|
+
const response = await send(upload.url, { method: "POST", body: form });
|
|
467
|
+
if (!response.ok) throw new Error(`Stream refused the upload (HTTP ${response.status})`);
|
|
468
|
+
progress(1);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
let offset = 0;
|
|
472
|
+
let retries = 0;
|
|
473
|
+
while (offset < source.size) {
|
|
474
|
+
const length = Math.min(upload.chunkBytes, source.size - offset);
|
|
475
|
+
const chunk = await source.read(offset, length);
|
|
476
|
+
const response = await send(upload.url, {
|
|
477
|
+
method: "PATCH",
|
|
478
|
+
headers: {
|
|
479
|
+
"Tus-Resumable": "1.0.0",
|
|
480
|
+
"Upload-Offset": String(offset),
|
|
481
|
+
"Content-Type": "application/offset+octet-stream"
|
|
482
|
+
},
|
|
483
|
+
body: new Uint8Array(chunk)
|
|
484
|
+
}).catch(() => null);
|
|
485
|
+
if (!response?.ok) {
|
|
486
|
+
if (retries++ >= 5) throw new Error("Stream stopped taking the upload; run the import again");
|
|
487
|
+
const head = await send(upload.url, {
|
|
488
|
+
method: "HEAD",
|
|
489
|
+
headers: { "Tus-Resumable": "1.0.0" }
|
|
490
|
+
}).catch(() => null);
|
|
491
|
+
offset = Number(head?.headers.get("upload-offset") ?? offset);
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
retries = 0;
|
|
495
|
+
offset = Number(response.headers.get("upload-offset") ?? offset + length);
|
|
496
|
+
progress(offset / source.size);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
function coursesReportLines(report, dryRun) {
|
|
500
|
+
const lines = [];
|
|
501
|
+
if (report.problems.length) {
|
|
502
|
+
lines.push(`Nothing was written: ${report.problems.length} problem(s).`);
|
|
503
|
+
for (const p of report.problems) lines.push(` ${p.file}: ${p.message}`);
|
|
504
|
+
return lines;
|
|
505
|
+
}
|
|
506
|
+
lines.push(...outcomeLines(report.records, dryRun));
|
|
507
|
+
if (report.unmatched.length) {
|
|
508
|
+
lines.push(`${report.unmatched.length} row(s) matched nothing and were left out:`);
|
|
509
|
+
for (const u of report.unmatched) lines.push(` ${u.file}: ${u.message}`);
|
|
510
|
+
}
|
|
511
|
+
if (!dryRun) {
|
|
512
|
+
const m = report.media;
|
|
513
|
+
lines.push(
|
|
514
|
+
`Media: ${m.uploaded} file(s) uploaded, ${m.already} already in the library; ${m.videosUploaded} video(s) sent, ${m.videosAlready} already in Stream.`
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
for (const w of report.waiting) {
|
|
518
|
+
lines.push(
|
|
519
|
+
` ${w.file}: not written yet: ${w.videos.length ? "its videos are not ready in Stream" : "a course it lists is not written yet"}. Run the import again later.`
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
for (const p of report.published) if (p.as !== "unchanged") lines.push(` ${p.file}: ${p.as}`);
|
|
523
|
+
for (const note of report.notes)
|
|
524
|
+
lines.push(` ${note.file ? `${note.file}: ` : ""}${note.message}`);
|
|
525
|
+
return lines;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// src/courses-import/run.ts
|
|
529
|
+
var filesIn = (dir) => existsSync(dir) ? readdirSync(dir).filter((n) => statSync(join(dir, n)).isFile()) : [];
|
|
530
|
+
function readCoursesDisk(dir) {
|
|
531
|
+
const root = resolve(dir);
|
|
532
|
+
if (!existsSync(root) || !statSync(root).isDirectory()) throw new Error(`${dir} is not a folder`);
|
|
533
|
+
const json = (sub) => new Map(
|
|
534
|
+
filesIn(join(root, sub)).filter((n) => n.toLowerCase().endsWith(".json")).map((n) => [n, readFileSync(join(root, sub, n), "utf8")])
|
|
535
|
+
);
|
|
536
|
+
const bytes = (sub) => new Map(
|
|
537
|
+
filesIn(join(root, sub)).map(
|
|
538
|
+
(n) => [n, new Uint8Array(readFileSync(join(root, sub, n)))]
|
|
539
|
+
)
|
|
540
|
+
);
|
|
541
|
+
const text = (name) => existsSync(join(root, name)) ? readFileSync(join(root, name), "utf8") : null;
|
|
542
|
+
return {
|
|
543
|
+
courses: json("courses"),
|
|
544
|
+
bundles: json("bundles"),
|
|
545
|
+
videos: new Map(
|
|
546
|
+
filesIn(join(root, "videos")).map(
|
|
547
|
+
(n) => [n, statSync(join(root, "videos", n)).size]
|
|
548
|
+
)
|
|
549
|
+
),
|
|
550
|
+
captions: bytes("captions"),
|
|
551
|
+
pictures: bytes("pictures"),
|
|
552
|
+
downloads: bytes("downloads"),
|
|
553
|
+
access: text("access.csv"),
|
|
554
|
+
progress: text("progress.csv")
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
function videoOnDisk(path) {
|
|
558
|
+
const size = statSync(path).size;
|
|
559
|
+
const hash = createHash("sha256");
|
|
560
|
+
const fd = openSync(path, "r");
|
|
561
|
+
try {
|
|
562
|
+
const buffer = Buffer.alloc(8 * 1024 * 1024);
|
|
563
|
+
for (let at = 0; at < size; ) {
|
|
564
|
+
const read = readSync(fd, buffer, 0, buffer.length, at);
|
|
565
|
+
if (!read) break;
|
|
566
|
+
hash.update(buffer.subarray(0, read));
|
|
567
|
+
at += read;
|
|
568
|
+
}
|
|
569
|
+
} finally {
|
|
570
|
+
closeSync(fd);
|
|
571
|
+
}
|
|
572
|
+
return {
|
|
573
|
+
size,
|
|
574
|
+
sha256: hash.digest("hex"),
|
|
575
|
+
async read(offset, length) {
|
|
576
|
+
const handle = openSync(path, "r");
|
|
577
|
+
try {
|
|
578
|
+
const out = Buffer.alloc(length);
|
|
579
|
+
const got = readSync(handle, out, 0, length, offset);
|
|
580
|
+
return new Uint8Array(out.subarray(0, got));
|
|
581
|
+
} finally {
|
|
582
|
+
closeSync(handle);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
async function coursesImport(positional, flags, io) {
|
|
588
|
+
const dir = positional[0];
|
|
589
|
+
if (!dir) {
|
|
590
|
+
io.err(
|
|
591
|
+
"Usage: cos courses import <folder> --host <https://host> [--email <you>] [--dry-run] [--publish] [--yes] [--wait <minutes>]\n\nThe folder: courses/*.json, bundles/*.json, videos/, captions/, pictures/, downloads/, access.csv, progress.csv. See docs/importing.md."
|
|
592
|
+
);
|
|
593
|
+
return 2;
|
|
594
|
+
}
|
|
595
|
+
const host = flags.host?.replace(/\/+$/, "");
|
|
596
|
+
if (!host || !/^https?:\/\//.test(host)) {
|
|
597
|
+
io.err("--host is the host's address, e.g. https://admin.example.com");
|
|
598
|
+
return 2;
|
|
599
|
+
}
|
|
600
|
+
const plan = readCoursesFolder(readCoursesDisk(dir));
|
|
601
|
+
if (plan.problems.length) {
|
|
602
|
+
io.err(`Nothing was written: ${plan.problems.length} problem(s) in ${dir}.`);
|
|
603
|
+
for (const p of plan.problems) io.err(` ${p.file}: ${p.message}`);
|
|
604
|
+
return 1;
|
|
605
|
+
}
|
|
606
|
+
io.out(
|
|
607
|
+
`${plan.courses.length} course(s), ${plan.bundles.length} bundle(s), ${plan.videos.size} video(s), ${plan.access.length} access row(s) and ${plan.progress?.length ?? 0} progress row(s) in ${dir}.`
|
|
608
|
+
);
|
|
609
|
+
const videos = /* @__PURE__ */ new Map();
|
|
610
|
+
for (const [ref, v] of plan.videos) {
|
|
611
|
+
if (v.file) {
|
|
612
|
+
io.out(`Reading videos/${v.file}\u2026`);
|
|
613
|
+
videos.set(v.file, videoOnDisk(join(resolve(dir), "videos", v.file)));
|
|
614
|
+
}
|
|
615
|
+
void ref;
|
|
616
|
+
}
|
|
617
|
+
let session;
|
|
618
|
+
try {
|
|
619
|
+
session = await signInForRun(host, flags.email, io, "cos courses import");
|
|
620
|
+
} catch (error) {
|
|
621
|
+
io.err(`Could not sign in: ${error.message}`);
|
|
622
|
+
return 1;
|
|
623
|
+
}
|
|
624
|
+
try {
|
|
625
|
+
const report = await runCoursesImport(
|
|
626
|
+
plan,
|
|
627
|
+
sessionHost(host, () => session.headers()),
|
|
628
|
+
{
|
|
629
|
+
dryRun: Boolean(flags.dryRun),
|
|
630
|
+
publish: Boolean(flags.publish),
|
|
631
|
+
confirm: confirmer(io, flags.yes),
|
|
632
|
+
videoFiles: (name) => videos.get(name),
|
|
633
|
+
waitMs: (flags.wait ?? 10) * 6e4,
|
|
634
|
+
progress: (line) => io.out(line)
|
|
635
|
+
}
|
|
636
|
+
);
|
|
637
|
+
for (const line of coursesReportLines(report, Boolean(flags.dryRun))) {
|
|
638
|
+
(report.problems.length ? io.err : io.out)(line);
|
|
639
|
+
}
|
|
640
|
+
return report.problems.length ? 1 : report.waiting.length ? 3 : 0;
|
|
641
|
+
} finally {
|
|
642
|
+
await session.signOut();
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
export {
|
|
646
|
+
coursesImport,
|
|
647
|
+
readCoursesDisk
|
|
648
|
+
};
|