@composable-os/cli 0.1.0-beta.11 → 0.1.0-beta.13

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 (166) hide show
  1. package/dist/{accounts-UHYLCLEY.js → accounts-GOCNY6WI.js} +17 -13
  2. package/dist/app-RPRZMLU5.js +43 -0
  3. package/dist/app-upload-YJVEKZC6.js +198 -0
  4. package/dist/{blocks-5IFU25J6.js → blocks-DE4RRWPJ.js} +207 -25
  5. package/dist/{chunk-XIDLW3EP.js → chunk-2B6YUYZV.js} +23499 -11766
  6. package/dist/{run-7P4RQALH.js → chunk-2CCHB37H.js} +9 -146
  7. package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
  8. package/dist/{chunk-DHS2CV2M.js → chunk-5D7BDF4Y.js} +15 -13
  9. package/dist/{chunk-ZAWES2UD.js → chunk-6CM3WZG5.js} +13 -2483
  10. package/dist/{chunk-7D4GKZOX.js → chunk-CIBJGB5H.js} +422 -75
  11. package/dist/chunk-EPDTPGZJ.js +330 -0
  12. package/dist/{chunk-LAAL4YGR.js → chunk-HAYF4G5V.js} +66 -23
  13. package/dist/chunk-HCG6T53D.js +61904 -0
  14. package/dist/{chunk-K34UKWEI.js → chunk-JX4IFWNT.js} +64 -32
  15. package/dist/{chunk-6XTU2MTP.js → chunk-KFHKNXGH.js} +632 -418
  16. package/dist/chunk-KMFMD52Z.js +13276 -0
  17. package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
  18. package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
  19. package/dist/{chunk-E64SG24J.js → chunk-Q6Y45DY6.js} +25 -2
  20. package/dist/{chunk-MBIKVRAC.js → chunk-QJZGYJVV.js} +823 -44
  21. package/dist/{chunk-PDD2ETVF.js → chunk-QQ3YSL5F.js} +172 -16
  22. package/dist/chunk-RGKJKC5T.js +876 -0
  23. package/dist/{chunk-YFTLIEWU.js → chunk-RSEWEFAV.js} +12 -2
  24. package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
  25. package/dist/{chunk-LO6WGU5Y.js → chunk-U72NEGCT.js} +8089 -3464
  26. package/dist/{chunk-P6UWZ6QQ.js → chunk-Z4IKF23A.js} +11 -7
  27. package/dist/{chunk-2IVEVPMY.js → chunk-Z7OVQ6BC.js} +105 -61
  28. package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
  29. package/dist/{cli-5UNY5G5L.js → cli-OKQHHSOE.js} +17 -25
  30. package/dist/{cli-UYZMVQMR.js → cli-VYSD2S4Z.js} +227 -81
  31. package/dist/cli.js +5 -5
  32. package/dist/{client-repo-SE4EKG6W.js → client-repo-NNUCHQ2Y.js} +7 -5
  33. package/dist/create.js +5 -5
  34. package/dist/{fields-FTWWMD7N.js → fields-GTHE6TTS.js} +27 -3
  35. package/dist/gateway-DPPEQZT4.js +25 -0
  36. package/dist/grants-6JY4XZO7.js +27 -0
  37. package/dist/invoke-MRONRZ5Q.js +33 -0
  38. package/dist/moves-UAUCMWL5.js +36 -0
  39. package/dist/{order-U4KZLLSJ.js → order-RKLGT74L.js} +2 -2
  40. package/dist/{person-UHLVBVLR.js → person-YOZTFV4Q.js} +11 -10
  41. package/dist/plan-on-sale-H4F26YIN.js +26 -0
  42. package/dist/platform/migrations/0730_practitioners.sql +79 -0
  43. package/dist/platform/migrations/0750_routine_mail.sql +98 -0
  44. package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
  45. package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
  46. package/dist/platform/migrations/0811_staff_time.sql +109 -0
  47. package/dist/platform/migrations/0812_staff_offers.sql +112 -0
  48. package/dist/platform/migrations/0813_pay_periods.sql +241 -0
  49. package/dist/platform/migrations/0814_staff_feed.sql +47 -0
  50. package/dist/platform/migrations/0815_checklists.sql +237 -0
  51. package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
  52. package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
  53. package/dist/platform/migrations/0850_agreements.sql +271 -0
  54. package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
  55. package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
  56. package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
  57. package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
  58. package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
  59. package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
  60. package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
  61. package/dist/platform/migrations/0910_routine_messages.sql +99 -0
  62. package/dist/platform/migrations/0911_texts.sql +60 -0
  63. package/dist/platform/migrations/0913_contact_consent.sql +93 -0
  64. package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
  65. package/dist/platform/migrations/0930_stock_movements.sql +95 -0
  66. package/dist/platform/migrations/0931_stock_low.sql +25 -0
  67. package/dist/platform/migrations/0932_order_delivery.sql +78 -0
  68. package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
  69. package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
  70. package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
  71. package/dist/platform/migrations/0953_discount_uses.sql +183 -0
  72. package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
  73. package/dist/platform/migrations/0970_published_text.sql +59 -0
  74. package/dist/platform/migrations/0971_publish_acks.sql +68 -0
  75. package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
  76. package/dist/platform/migrations/1010_intake_links.sql +148 -0
  77. package/dist/platform/migrations/1030_invoices.sql +747 -0
  78. package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
  79. package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
  80. package/dist/platform/migrations/1090_staff_hours.sql +43 -0
  81. package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
  82. package/dist/platform/migrations/1092_client_guard.sql +26 -0
  83. package/dist/platform/migrations/1095_thing_time.sql +70 -0
  84. package/dist/platform/migrations/1130_devices.sql +162 -0
  85. package/dist/platform/migrations/1135_person_codes.sql +88 -0
  86. package/dist/platform/migrations/1150_finance_periods.sql +316 -0
  87. package/dist/platform/migrations/1151_payouts.sql +202 -0
  88. package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
  89. package/dist/platform/migrations/1170_journeys.sql +318 -0
  90. package/dist/platform/migrations/1171_person_birthday.sql +52 -0
  91. package/dist/platform/migrations/1175_person_signals.sql +29 -0
  92. package/dist/platform/migrations/1190_restricted_values.sql +25 -0
  93. package/dist/platform/migrations/1230_note_levels.sql +113 -0
  94. package/dist/platform/migrations/1231_person_carers.sql +33 -0
  95. package/dist/platform/migrations/1232_note_kinds.sql +122 -0
  96. package/dist/platform/migrations/1270_time_entries.sql +145 -0
  97. package/dist/platform/migrations/1271_leave.sql +249 -0
  98. package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
  99. package/dist/platform/migrations/1330_accounting.sql +260 -0
  100. package/dist/platform/migrations/1350_customer_feed.sql +24 -0
  101. package/dist/platform/migrations/1390_locations.sql +77 -0
  102. package/dist/platform/migrations/1410_outside_copy.sql +213 -0
  103. package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
  104. package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
  105. package/dist/platform/sdk/acknowledge.ts +100 -0
  106. package/dist/platform/sdk/agreements.ts +185 -0
  107. package/dist/platform/sdk/allowance.ts +98 -8
  108. package/dist/platform/sdk/automation-check.ts +120 -9
  109. package/dist/platform/sdk/automation.ts +78 -10
  110. package/dist/platform/sdk/behaviour.ts +26 -6
  111. package/dist/platform/sdk/calendar-entry.ts +258 -0
  112. package/dist/platform/sdk/capacity.ts +1 -2
  113. package/dist/platform/sdk/checklists.ts +118 -0
  114. package/dist/platform/sdk/contract.ts +489 -27
  115. package/dist/platform/sdk/discount.ts +408 -0
  116. package/dist/platform/sdk/document.ts +4 -2
  117. package/dist/platform/sdk/field-types.ts +5 -0
  118. package/dist/platform/sdk/formula/compile.ts +10 -0
  119. package/dist/platform/sdk/index.ts +17 -0
  120. package/dist/platform/sdk/intake.ts +380 -0
  121. package/dist/platform/sdk/journey-signals.ts +215 -0
  122. package/dist/platform/sdk/manifest.ts +1236 -65
  123. package/dist/platform/sdk/mine.ts +161 -0
  124. package/dist/platform/sdk/occupies.ts +192 -0
  125. package/dist/platform/sdk/questions.ts +681 -0
  126. package/dist/platform/sdk/recurring.ts +90 -9
  127. package/dist/platform/sdk/restricted-fields.ts +129 -0
  128. package/dist/platform/sdk/rich-text.ts +112 -0
  129. package/dist/platform/sdk/routine-mail.ts +1272 -0
  130. package/dist/platform/sdk/runtime.ts +25 -12
  131. package/dist/platform/sdk/screens.ts +91 -0
  132. package/dist/platform/sdk/sellable.ts +344 -0
  133. package/dist/platform/sdk/slug.ts +7 -2
  134. package/dist/platform/sdk/staffed.ts +474 -0
  135. package/dist/platform/sdk/states.ts +68 -26
  136. package/dist/platform/sdk/stock.ts +332 -0
  137. package/dist/platform/sdk/team.ts +83 -0
  138. package/dist/platform/sdk/testing.ts +70 -6
  139. package/dist/platform/sdk/worked-on.ts +134 -0
  140. package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
  141. package/dist/run-7ID64RA6.js +65 -0
  142. package/dist/run-FH7OGGTV.js +131 -0
  143. package/dist/run-OYZIAP6M.js +3363 -0
  144. package/dist/{run-ZCGFMEZF.js → run-QOFRRSZV.js} +41 -60
  145. package/dist/{run-PO3W5BFA.js → run-TJAEWR5U.js} +21 -51
  146. package/dist/{src-I3IJ4NKC.js → src-52VERUPK.js} +306 -4
  147. package/dist/src-PHRELWN5.js +40 -0
  148. package/dist/{visibility-TVXZ67FD.js → visibility-SZCINPP4.js} +21 -4
  149. package/package.json +2 -2
  150. package/dist/chunk-2TWV3SVB.js +0 -349
  151. package/dist/chunk-2XC5JLBO.js +0 -1931
  152. package/dist/chunk-324WJXNP.js +0 -320
  153. package/dist/chunk-6ELZRNUU.js +0 -116
  154. package/dist/chunk-6WGFA2IV.js +0 -643
  155. package/dist/chunk-7CQGMVDO.js +0 -63
  156. package/dist/chunk-7OGCMQXG.js +0 -284
  157. package/dist/chunk-FJIZGS5Q.js +0 -698
  158. package/dist/chunk-UB4I2QKR.js +0 -31575
  159. package/dist/chunk-WHKJGF36.js +0 -2418
  160. package/dist/chunk-YNUZRTPH.js +0 -182
  161. package/dist/chunk-ZZFWMVJO.js +0 -704
  162. package/dist/grants-LOW6TSWD.js +0 -32
  163. package/dist/invoke-6KVT6WJI.js +0 -41
  164. package/dist/moves-E2HKYCTK.js +0 -41
  165. package/dist/plan-on-sale-TJPFCNL7.js +0 -26
  166. package/dist/src-CRO35DYY.js +0 -46
@@ -0,0 +1,134 @@
1
+ // The `workedOn` declaration (stage 58 step 1, ADR 0850): a record time can be recorded against (a
2
+ // job). Its manifest schema and contract checks live here; the host's side (the time entries,
3
+ // refused by the database when one person's overlap) is packages/core/src/staffing/time.ts.
4
+ import { z } from "zod";
5
+ import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
6
+
7
+ /**
8
+ * Time can be recorded against this record (ADR 0850): a job a tradesperson spends three hours
9
+ * on. A staff member's **time entry** is the one record of time worked; an entry against a shift
10
+ * (a `staffed` record) is against the roster, an entry against a `workedOn` record is against the
11
+ * job, and an entry against neither is other work. The record keeps no hours of its own: its
12
+ * time is the entries against it, read through the host (the same entries pay counts), never a
13
+ * second "time spent" record, and never in a `rec_*` view.
14
+ *
15
+ * - `live`: the states of the object's `states` in which time may be recorded against it. A
16
+ * record in any other state, or in the Trash, takes no new time.
17
+ * - `title`: a field of this object, shown on the entry, the timesheet and the export.
18
+ * - `kind` (optional): a relation (one record) of this object, shown as the export's kind.
19
+ * - `place` (optional): a field, or `relation.field`, shown beside the entry.
20
+ * - `readers`: a permission of this extension's tools: who reads the time recorded against a
21
+ * record (person, date, minutes and state; never a note).
22
+ * - `partOf` (optional), on a `staffed` object only (a scheduled visit to a site): a relation (one
23
+ * record) to another of this extension's objects that declares `workedOn` (the job); time
24
+ * recorded against the visit also counts to it.
25
+ *
26
+ * Only shipped packs and the org's own extensions declare it: time worked feeds pay.
27
+ */
28
+ export interface WorkedOnSpec {
29
+ live: readonly string[];
30
+ title: string;
31
+ kind?: string;
32
+ place?: string;
33
+ readers: string;
34
+ partOf?: string;
35
+ }
36
+
37
+ const fieldName = z.string().max(40);
38
+
39
+ export const workedOnManifestSchema = z
40
+ .object({
41
+ live: z.array(z.string().min(1).max(100)).min(1).max(20),
42
+ title: fieldName,
43
+ kind: fieldName.optional(),
44
+ place: z.string().max(81).optional(),
45
+ readers: z.string().max(80),
46
+ partOf: fieldName.optional(),
47
+ })
48
+ .strict();
49
+ export type WorkedOnManifest = z.infer<typeof workedOnManifestSchema>;
50
+
51
+ /** Reports problems with `objects.<name>.workedOn` through `contract`. */
52
+ export function checkObjectWorkedOn(
53
+ m: ExtensionManifest,
54
+ name: string,
55
+ object: ObjectManifest,
56
+ contract: (path: string, message: string) => void,
57
+ ): void {
58
+ const declared = object.workedOn;
59
+ if (!declared) return;
60
+ const at = `objects.${name}.workedOn`;
61
+ const own = (o: ObjectManifest, local: string): FieldManifest | undefined =>
62
+ Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
63
+
64
+ // The states in which time may be recorded against it.
65
+ const states = object.states;
66
+ if (!states) {
67
+ contract(`${at}.live`, `${name} must declare states: time is recorded in some of them`);
68
+ } else {
69
+ const field = own(object, states.field);
70
+ const values = new Set((field?.options ?? []).map((o) => o.value));
71
+ for (const state of declared.live) {
72
+ if (!values.has(state)) contract(`${at}.live`, `"${state}" is not a state of ${name}`);
73
+ }
74
+ if (new Set(declared.live).size !== declared.live.length) {
75
+ contract(`${at}.live`, "List each state once");
76
+ }
77
+ }
78
+
79
+ // What it is called, of what kind, and where.
80
+ const title = own(object, declared.title);
81
+ if (!title || title.many) {
82
+ contract(`${at}.title`, `"${declared.title}" must be a field (one value) of ${name}`);
83
+ }
84
+ if (declared.kind !== undefined) {
85
+ const kind = own(object, declared.kind);
86
+ if (kind?.type !== "relation" || kind.many) {
87
+ contract(`${at}.kind`, `"${declared.kind}" must be a relation field (one record) of ${name}`);
88
+ }
89
+ }
90
+ if (declared.place !== undefined) {
91
+ const [first, second, ...rest] = declared.place.split(".");
92
+ const field = first === undefined ? undefined : own(object, first);
93
+ if (!field || rest.length || (second !== undefined && field.type !== "relation")) {
94
+ contract(
95
+ `${at}.place`,
96
+ `"${declared.place}" must be a field of ${name}, or a relation of it and a field of what it names`,
97
+ );
98
+ }
99
+ }
100
+
101
+ // Who reads its time: a permission this extension's tools declare.
102
+ const permissions = new Set(
103
+ Object.values(m.tools).flatMap((t) =>
104
+ t.permission === undefined
105
+ ? []
106
+ : typeof t.permission === "string"
107
+ ? [t.permission]
108
+ : t.permission,
109
+ ),
110
+ );
111
+ if (!declared.readers.startsWith(`${m.id}:`) || !permissions.has(declared.readers)) {
112
+ contract(
113
+ `${at}.readers`,
114
+ `"${declared.readers}" must be a permission of this extension's tools (${m.id}:<word>)`,
115
+ );
116
+ }
117
+
118
+ // What a staffed visit's time also counts to.
119
+ if (declared.partOf !== undefined) {
120
+ if (!object.staffed) {
121
+ contract(`${at}.partOf`, `Only a shift (an object that declares staffed) is part of a job`);
122
+ }
123
+ const via = own(object, declared.partOf);
124
+ const target = via?.type === "relation" && !via.many ? via.to : undefined;
125
+ const local = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
126
+ const job = local && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
127
+ if (!job || local === name || !job.workedOn || job.workedOn.partOf !== undefined) {
128
+ contract(
129
+ `${at}.partOf`,
130
+ `"${declared.partOf}" must be a relation field (one record) to another of this extension's objects that declares workedOn`,
131
+ );
132
+ }
133
+ }
134
+ }
@@ -2,8 +2,8 @@
2
2
  import {
3
3
  DEFAULT_PRODUCT,
4
4
  products
5
- } from "./chunk-2ABSVMGB.js";
6
- import "./chunk-6UWRT4VM.js";
5
+ } from "./chunk-SMJZIK2O.js";
6
+ import "./chunk-ZJSFX4RD.js";
7
7
  import "./chunk-5KSXSK7Y.js";
8
8
  export {
9
9
  DEFAULT_PRODUCT,
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ readRosterFile,
4
+ rosterReportLines,
5
+ runRosterImport
6
+ } from "./chunk-EPDTPGZJ.js";
7
+ import "./chunk-Z4IKF23A.js";
8
+ import {
9
+ hostOf,
10
+ withRunSession
11
+ } from "./chunk-LX2TTLVY.js";
12
+ import "./chunk-U72NEGCT.js";
13
+ import "./chunk-5KSXSK7Y.js";
14
+
15
+ // src/rostering-import/run.ts
16
+ import { existsSync, readFileSync, statSync } from "node:fs";
17
+ import { basename } from "node:path";
18
+ var ROSTER_USAGE = "Usage: cos rostering import <roster.csv> --host <url> [--email <you>] [--apply]";
19
+ async function rosteringImport(positional, flags, io) {
20
+ const path = positional[0];
21
+ if (!path) {
22
+ io.err(
23
+ `${ROSTER_USAGE}
24
+
25
+ The file: a header naming duty, time and length, and weekdays with first_date (a weekly pattern) or date (one shift); optionally staff_email, last_date, zone and place. A dry run unless --apply. See docs/importing.md.`
26
+ );
27
+ return 2;
28
+ }
29
+ const host = hostOf(flags.host, io);
30
+ if (!host) return 2;
31
+ if (!existsSync(path) || !statSync(path).isFile()) {
32
+ io.err(`${path} is not a file`);
33
+ return 2;
34
+ }
35
+ const name = basename(path);
36
+ const file = readRosterFile(readFileSync(path, "utf8"), name);
37
+ if (file.problems.length) {
38
+ io.err(`Nothing was sent: ${file.problems.length} problem(s) in ${name}.`);
39
+ for (const p of file.problems) io.err(` ${p.file}: ${p.message}`);
40
+ return 1;
41
+ }
42
+ io.out(
43
+ `${file.rows.length} row(s) in ${name}.${flags.apply ? "" : " A dry run: nothing is written."}`
44
+ );
45
+ return withRunSession(host, flags.email, io, "cos rostering import", async (on) => {
46
+ try {
47
+ const report = await runRosterImport(file, on, {
48
+ apply: Boolean(flags.apply),
49
+ name,
50
+ progress: (line) => io.out(line)
51
+ });
52
+ for (const line of rosterReportLines(report, name)) {
53
+ (report.problems.length ? io.err : io.out)(line);
54
+ }
55
+ return report.problems.length ? 1 : 0;
56
+ } catch (error) {
57
+ io.err(`Error: ${error.message}`);
58
+ return 1;
59
+ }
60
+ });
61
+ }
62
+ export {
63
+ ROSTER_USAGE,
64
+ rosteringImport
65
+ };
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ coursesReportLines,
4
+ runCoursesImport
5
+ } from "./chunk-2CCHB37H.js";
6
+ import {
7
+ readCoursesFolder
8
+ } from "./chunk-Z4IKF23A.js";
9
+ import {
10
+ confirmer,
11
+ hostOf,
12
+ withRunSession
13
+ } from "./chunk-LX2TTLVY.js";
14
+ import "./chunk-U72NEGCT.js";
15
+ import "./chunk-5KSXSK7Y.js";
16
+
17
+ // src/courses-import/run.ts
18
+ import { createHash } from "node:crypto";
19
+ import {
20
+ closeSync,
21
+ existsSync,
22
+ openSync,
23
+ readdirSync,
24
+ readFileSync,
25
+ readSync,
26
+ statSync
27
+ } from "node:fs";
28
+ import { join, resolve } from "node:path";
29
+ var filesIn = (dir) => existsSync(dir) ? readdirSync(dir).filter((n) => statSync(join(dir, n)).isFile()) : [];
30
+ function readCoursesDisk(dir) {
31
+ const root = resolve(dir);
32
+ if (!existsSync(root) || !statSync(root).isDirectory()) throw new Error(`${dir} is not a folder`);
33
+ const json = (sub) => new Map(
34
+ filesIn(join(root, sub)).filter((n) => n.toLowerCase().endsWith(".json")).map((n) => [n, readFileSync(join(root, sub, n), "utf8")])
35
+ );
36
+ const bytes = (sub) => new Map(
37
+ filesIn(join(root, sub)).map(
38
+ (n) => [n, new Uint8Array(readFileSync(join(root, sub, n)))]
39
+ )
40
+ );
41
+ const text = (name) => existsSync(join(root, name)) ? readFileSync(join(root, name), "utf8") : null;
42
+ return {
43
+ courses: json("courses"),
44
+ bundles: json("bundles"),
45
+ videos: new Map(
46
+ filesIn(join(root, "videos")).map(
47
+ (n) => [n, statSync(join(root, "videos", n)).size]
48
+ )
49
+ ),
50
+ captions: bytes("captions"),
51
+ pictures: bytes("pictures"),
52
+ downloads: bytes("downloads"),
53
+ access: text("access.csv"),
54
+ progress: text("progress.csv")
55
+ };
56
+ }
57
+ function videoOnDisk(path) {
58
+ const size = statSync(path).size;
59
+ const hash = createHash("sha256");
60
+ const fd = openSync(path, "r");
61
+ try {
62
+ const buffer = Buffer.alloc(8 * 1024 * 1024);
63
+ for (let at = 0; at < size; ) {
64
+ const read = readSync(fd, buffer, 0, buffer.length, at);
65
+ if (!read) break;
66
+ hash.update(buffer.subarray(0, read));
67
+ at += read;
68
+ }
69
+ } finally {
70
+ closeSync(fd);
71
+ }
72
+ return {
73
+ size,
74
+ sha256: hash.digest("hex"),
75
+ async read(offset, length) {
76
+ const handle = openSync(path, "r");
77
+ try {
78
+ const out = Buffer.alloc(length);
79
+ const got = readSync(handle, out, 0, length, offset);
80
+ return new Uint8Array(out.subarray(0, got));
81
+ } finally {
82
+ closeSync(handle);
83
+ }
84
+ }
85
+ };
86
+ }
87
+ async function coursesImport(positional, flags, io) {
88
+ const dir = positional[0];
89
+ if (!dir) {
90
+ io.err(
91
+ "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."
92
+ );
93
+ return 2;
94
+ }
95
+ const host = hostOf(flags.host, io);
96
+ if (!host) return 2;
97
+ const plan = readCoursesFolder(readCoursesDisk(dir));
98
+ if (plan.problems.length) {
99
+ io.err(`Nothing was written: ${plan.problems.length} problem(s) in ${dir}.`);
100
+ for (const p of plan.problems) io.err(` ${p.file}: ${p.message}`);
101
+ return 1;
102
+ }
103
+ io.out(
104
+ `${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}.`
105
+ );
106
+ const videos = /* @__PURE__ */ new Map();
107
+ for (const v of plan.videos.values()) {
108
+ if (v.file) {
109
+ io.out(`Reading videos/${v.file}\u2026`);
110
+ videos.set(v.file, videoOnDisk(join(resolve(dir), "videos", v.file)));
111
+ }
112
+ }
113
+ return withRunSession(host, flags.email, io, "cos courses import", async (on) => {
114
+ const report = await runCoursesImport(plan, on, {
115
+ dryRun: Boolean(flags.dryRun),
116
+ publish: Boolean(flags.publish),
117
+ confirm: confirmer(io, flags.yes),
118
+ videoFiles: (name) => videos.get(name),
119
+ waitMs: (flags.wait ?? 10) * 6e4,
120
+ progress: (line) => io.out(line)
121
+ });
122
+ for (const line of coursesReportLines(report, Boolean(flags.dryRun))) {
123
+ (report.problems.length ? io.err : io.out)(line);
124
+ }
125
+ return report.problems.length ? 1 : report.waiting.length ? 3 : 0;
126
+ });
127
+ }
128
+ export {
129
+ coursesImport,
130
+ readCoursesDisk
131
+ };