@composable-os/cli 0.0.0-stage → 0.1.0-beta.11
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/LICENSE +109 -0
- package/README.md +24 -2
- package/dist/accounts-UHYLCLEY.js +45 -0
- package/dist/blocks-5IFU25J6.js +233 -0
- package/dist/chunk-2ABSVMGB.js +13 -0
- package/dist/chunk-2IVEVPMY.js +669 -0
- package/dist/chunk-2TWV3SVB.js +349 -0
- package/dist/chunk-2XC5JLBO.js +1931 -0
- package/dist/chunk-324WJXNP.js +320 -0
- package/dist/chunk-5KSXSK7Y.js +47 -0
- package/dist/chunk-6ELZRNUU.js +116 -0
- package/dist/chunk-6UWRT4VM.js +220 -0
- package/dist/chunk-6WGFA2IV.js +643 -0
- package/dist/chunk-6XTU2MTP.js +11278 -0
- package/dist/chunk-7CQGMVDO.js +63 -0
- package/dist/chunk-7D4GKZOX.js +615 -0
- package/dist/chunk-7OGCMQXG.js +284 -0
- package/dist/chunk-DHS2CV2M.js +139 -0
- package/dist/chunk-DU6OIFOU.js +2358 -0
- package/dist/chunk-E4WCYTCT.js +347 -0
- package/dist/chunk-E64SG24J.js +88 -0
- package/dist/chunk-FJIZGS5Q.js +698 -0
- package/dist/chunk-GYTEW66E.js +50 -0
- package/dist/chunk-HDIOVFAZ.js +544 -0
- package/dist/chunk-K34UKWEI.js +298 -0
- package/dist/chunk-KGHA67HG.js +765 -0
- package/dist/chunk-LAAL4YGR.js +365 -0
- package/dist/chunk-LO6WGU5Y.js +26189 -0
- package/dist/chunk-MBIKVRAC.js +1425 -0
- package/dist/chunk-P6UWZ6QQ.js +426 -0
- package/dist/chunk-PDD2ETVF.js +1302 -0
- package/dist/chunk-UB4I2QKR.js +31575 -0
- package/dist/chunk-WHKJGF36.js +2418 -0
- package/dist/chunk-XIDLW3EP.js +15471 -0
- package/dist/chunk-YFTLIEWU.js +504 -0
- package/dist/chunk-YNUZRTPH.js +182 -0
- package/dist/chunk-ZAWES2UD.js +9364 -0
- package/dist/chunk-ZZFWMVJO.js +704 -0
- package/dist/cli-5UNY5G5L.js +136 -0
- package/dist/cli-UYZMVQMR.js +24534 -0
- package/dist/cli.js +25 -0
- package/dist/client-repo-SE4EKG6W.js +55 -0
- package/dist/create.js +25 -0
- package/dist/fields-FTWWMD7N.js +50 -0
- package/dist/grants-LOW6TSWD.js +32 -0
- package/dist/invoke-6KVT6WJI.js +41 -0
- package/dist/moves-E2HKYCTK.js +41 -0
- package/dist/order-U4KZLLSJ.js +19 -0
- package/dist/person-UHLVBVLR.js +67 -0
- package/dist/plan-on-sale-TJPFCNL7.js +26 -0
- package/dist/platform/migrations/0001_init.sql +59 -0
- package/dist/platform/migrations/0002_users_settings.sql +34 -0
- package/dist/platform/migrations/0003_people_organisations.sql +225 -0
- package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
- package/dist/platform/migrations/0005_views_audit.sql +79 -0
- package/dist/platform/migrations/0006_search.sql +223 -0
- package/dist/platform/migrations/0007_crm_views.sql +141 -0
- package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
- package/dist/platform/migrations/0010_approval_queue.sql +90 -0
- package/dist/platform/migrations/0011_connected_apps.sql +36 -0
- package/dist/platform/migrations/0020_extensions.sql +373 -0
- package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
- package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
- package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
- package/dist/platform/migrations/0061_audit_retention.sql +95 -0
- package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
- package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
- package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
- package/dist/platform/migrations/0065_storage_stats.sql +13 -0
- package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
- package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
- package/dist/platform/migrations/0100_stage_history.sql +54 -0
- package/dist/platform/migrations/0110_integrations.sql +103 -0
- package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
- package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
- package/dist/platform/migrations/0130_automations.sql +113 -0
- package/dist/platform/migrations/0131_notifications.sql +29 -0
- package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
- package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
- package/dist/platform/migrations/0160_releases.sql +41 -0
- package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
- package/dist/platform/migrations/0170_automation_connections.sql +32 -0
- package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
- package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
- package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
- package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
- package/dist/platform/migrations/0210_daily_digest.sql +33 -0
- package/dist/platform/migrations/0220_entitlements.sql +13 -0
- package/dist/platform/migrations/0221_support_grants.sql +33 -0
- package/dist/platform/migrations/0250_judge.sql +11 -0
- package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
- package/dist/platform/migrations/0252_judge_usage.sql +14 -0
- package/dist/platform/migrations/0255_summary_checks.sql +9 -0
- package/dist/platform/migrations/0256_outbound_check.sql +9 -0
- package/dist/platform/migrations/0260_ai_usage.sql +20 -0
- package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
- package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
- package/dist/platform/migrations/0320_duplicates.sql +41 -0
- package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
- package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
- package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
- package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
- package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
- package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
- package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
- package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
- package/dist/platform/migrations/0362_voice_usage.sql +17 -0
- package/dist/platform/migrations/0365_routines.sql +38 -0
- package/dist/platform/migrations/0370_approval_execution.sql +20 -0
- package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
- package/dist/platform/migrations/0380_rec_views.sql +94 -0
- package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
- package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
- package/dist/platform/migrations/0402_erase_requests.sql +34 -0
- package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
- package/dist/platform/migrations/0411_device_sessions.sql +33 -0
- package/dist/platform/migrations/0420_transition_history.sql +100 -0
- package/dist/platform/migrations/0421_orders.sql +134 -0
- package/dist/platform/migrations/0422_order_payments.sql +109 -0
- package/dist/platform/migrations/0440_capacity.sql +100 -0
- package/dist/platform/migrations/0441_capacity_views.sql +40 -0
- package/dist/platform/migrations/0450_order_grants.sql +21 -0
- package/dist/platform/migrations/0451_pack_roles.sql +18 -0
- package/dist/platform/migrations/0460_content.sql +59 -0
- package/dist/platform/migrations/0461_media.sql +52 -0
- package/dist/platform/migrations/0470_publish_versions.sql +36 -0
- package/dist/platform/migrations/0471_permission_grants.sql +18 -0
- package/dist/platform/migrations/0490_staff_invites.sql +40 -0
- package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
- package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
- package/dist/platform/migrations/0500_published_media_field.sql +25 -0
- package/dist/platform/migrations/0501_import_keys.sql +23 -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/migrations/0580_subscription_grants.sql +21 -0
- package/dist/platform/migrations/0581_once_per_person.sql +34 -0
- package/dist/platform/migrations/0600_tool_actions_ran_at_queue.sql +22 -0
- package/dist/platform/migrations/0610_waitlists.sql +54 -0
- package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
- package/dist/platform/migrations/0650_person_links.sql +90 -0
- package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
- package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
- package/dist/platform/sdk/allowance.ts +522 -0
- package/dist/platform/sdk/automation-check.ts +534 -0
- package/dist/platform/sdk/automation.ts +743 -0
- package/dist/platform/sdk/behaviour.ts +215 -0
- package/dist/platform/sdk/capacity.ts +303 -0
- package/dist/platform/sdk/contract.ts +909 -0
- package/dist/platform/sdk/document.ts +935 -0
- package/dist/platform/sdk/field-types.ts +111 -0
- package/dist/platform/sdk/formula/ast.ts +72 -0
- package/dist/platform/sdk/formula/compile.ts +454 -0
- package/dist/platform/sdk/formula/index.ts +4 -0
- package/dist/platform/sdk/formula/parser.ts +417 -0
- package/dist/platform/sdk/index.ts +36 -0
- package/dist/platform/sdk/manifest.ts +1785 -0
- package/dist/platform/sdk/media.ts +108 -0
- package/dist/platform/sdk/permissions.ts +60 -0
- package/dist/platform/sdk/recurring.ts +321 -0
- package/dist/platform/sdk/renamed.ts +63 -0
- package/dist/platform/sdk/rich-text-fixture.ts +68 -0
- package/dist/platform/sdk/rich-text.ts +221 -0
- package/dist/platform/sdk/runtime.ts +410 -0
- package/dist/platform/sdk/sellable.ts +1138 -0
- package/dist/platform/sdk/slug.ts +25 -0
- package/dist/platform/sdk/states.ts +598 -0
- package/dist/platform/sdk/testing.ts +847 -0
- package/dist/platform/sdk/unique.ts +81 -0
- package/dist/products-2H5DR232.js +11 -0
- package/dist/run-7P4RQALH.js +648 -0
- package/dist/run-PO3W5BFA.js +508 -0
- package/dist/run-ZCGFMEZF.js +309 -0
- package/dist/src-CRO35DYY.js +46 -0
- package/dist/src-I3IJ4NKC.js +354 -0
- package/dist/token-MOJUA2U6.js +70 -0
- package/dist/token-util-TENRN4V7.js +6 -0
- package/dist/visibility-TVXZ67FD.js +41 -0
- package/package.json +33 -4
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
chunks,
|
|
4
|
+
importRecords,
|
|
5
|
+
outcomeLines,
|
|
6
|
+
sha256
|
|
7
|
+
} from "./chunk-7CQGMVDO.js";
|
|
8
|
+
import {
|
|
9
|
+
LOOPBACK_PATH,
|
|
10
|
+
confirmer,
|
|
11
|
+
sessionHost,
|
|
12
|
+
signInForRun
|
|
13
|
+
} from "./chunk-E4WCYTCT.js";
|
|
14
|
+
import {
|
|
15
|
+
IMAGE_SCHEMA,
|
|
16
|
+
RICH_TEXT_SCHEMA,
|
|
17
|
+
SLUG_MAX,
|
|
18
|
+
SLUG_RE,
|
|
19
|
+
mediaProblem,
|
|
20
|
+
slugify,
|
|
21
|
+
validateDocument
|
|
22
|
+
} from "./chunk-LO6WGU5Y.js";
|
|
23
|
+
import "./chunk-5KSXSK7Y.js";
|
|
24
|
+
|
|
25
|
+
// src/content-import/run.ts
|
|
26
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
27
|
+
import { join, resolve } from "node:path";
|
|
28
|
+
|
|
29
|
+
// src/content-import/format.ts
|
|
30
|
+
var POST_OBJECT = "content.post";
|
|
31
|
+
var CATEGORY_OBJECT = "content.category";
|
|
32
|
+
var STATUSES = ["draft", "scheduled", "published"];
|
|
33
|
+
var POST_FIELDS = {
|
|
34
|
+
key: "the post's import key (default: the file's name without .json)",
|
|
35
|
+
title: "text, required",
|
|
36
|
+
slug: "the address: lowercase words joined by hyphens (default: made from the title)",
|
|
37
|
+
excerpt: "text",
|
|
38
|
+
body: "rich text (RICH_TEXT_SCHEMA), a picture named by `file` where it has `media`",
|
|
39
|
+
cover: "{ file, alt, focal? }",
|
|
40
|
+
date: "ISO 8601 date and time: the date readers are shown, kept when published",
|
|
41
|
+
status: "draft | scheduled | published (default draft)",
|
|
42
|
+
category: "the category's name",
|
|
43
|
+
byline: "text: who wrote it, as readers see it",
|
|
44
|
+
seoTitle: "text",
|
|
45
|
+
seoDescription: "text",
|
|
46
|
+
seoImage: "{ file, alt, focal? }",
|
|
47
|
+
noindex: "true to ask search engines not to list it"
|
|
48
|
+
};
|
|
49
|
+
var PICTURE = "$picture";
|
|
50
|
+
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
51
|
+
var DATETIME_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?(Z|[+-]\d{2}:\d{2})$/;
|
|
52
|
+
var KEY_RE = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,199}$/;
|
|
53
|
+
function withPictures(value, id) {
|
|
54
|
+
if (Array.isArray(value)) return value.map((v) => withPictures(v, id));
|
|
55
|
+
if (!isObject(value)) return value;
|
|
56
|
+
if (typeof value[PICTURE] === "string" && Object.keys(value).length === 1) {
|
|
57
|
+
return id(value[PICTURE]);
|
|
58
|
+
}
|
|
59
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, withPictures(v, id)]));
|
|
60
|
+
}
|
|
61
|
+
var placeholderMedia = (index) => `med_import${index}`;
|
|
62
|
+
function readFolder(files) {
|
|
63
|
+
const problems = [];
|
|
64
|
+
const posts = [];
|
|
65
|
+
const categories = /* @__PURE__ */ new Map();
|
|
66
|
+
const used = /* @__PURE__ */ new Map();
|
|
67
|
+
const keys = /* @__PURE__ */ new Map();
|
|
68
|
+
const slugs = /* @__PURE__ */ new Map();
|
|
69
|
+
const picturesProblems = /* @__PURE__ */ new Set();
|
|
70
|
+
for (const [file, text] of [...files.posts].sort(([a], [b]) => a.localeCompare(b))) {
|
|
71
|
+
const fault = (message) => problems.push({ file, message });
|
|
72
|
+
let raw;
|
|
73
|
+
try {
|
|
74
|
+
raw = JSON.parse(text);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
fault(`Not JSON: ${error.message}`);
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (!isObject(raw)) {
|
|
80
|
+
fault("A post is a JSON object");
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
for (const name of Object.keys(raw)) {
|
|
84
|
+
if (!Object.hasOwn(POST_FIELDS, name)) {
|
|
85
|
+
fault(`Unknown field "${name}" (${Object.keys(POST_FIELDS).join(", ")})`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const key = typeof raw.key === "string" ? raw.key : file.replace(/\.json$/i, "");
|
|
89
|
+
if (!KEY_RE.test(key)) fault(`The key "${key}" is not 1 to 200 letters, digits or ._:-`);
|
|
90
|
+
const other = keys.get(key);
|
|
91
|
+
if (other) fault(`The key "${key}" is ${other}'s too`);
|
|
92
|
+
keys.set(key, file);
|
|
93
|
+
const values = {};
|
|
94
|
+
const pictures = [];
|
|
95
|
+
const text_ = (name, max, required = false) => {
|
|
96
|
+
const v = raw[name];
|
|
97
|
+
if (v === void 0 || v === null) {
|
|
98
|
+
if (required) fault(`${name} is required`);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (typeof v !== "string" || required && !v.trim()) fault(`${name} is text`);
|
|
102
|
+
else if (v.length > max) fault(`${name} is at most ${max} characters`);
|
|
103
|
+
else values[name] = v;
|
|
104
|
+
};
|
|
105
|
+
text_("title", 1e3, true);
|
|
106
|
+
text_("excerpt", 1e3);
|
|
107
|
+
text_("byline", 1e3);
|
|
108
|
+
text_("seoTitle", 1e3);
|
|
109
|
+
text_("seoDescription", 1e3);
|
|
110
|
+
if (raw.slug !== void 0) {
|
|
111
|
+
if (typeof raw.slug !== "string" || raw.slug.length > SLUG_MAX || !SLUG_RE.test(raw.slug)) {
|
|
112
|
+
fault("slug is lowercase letters and digits in words joined by hyphens");
|
|
113
|
+
} else {
|
|
114
|
+
values.slug = raw.slug;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const slug = values.slug ?? slugify(String(raw.title ?? ""));
|
|
118
|
+
if (slug) {
|
|
119
|
+
const holder = slugs.get(slug);
|
|
120
|
+
if (holder) fault(`The address "${slug}" is ${holder}'s too`);
|
|
121
|
+
slugs.set(slug, file);
|
|
122
|
+
}
|
|
123
|
+
const picture = (given, path) => {
|
|
124
|
+
if (typeof given !== "string" || !given || given.includes("/") || given.includes("\\")) {
|
|
125
|
+
fault(`${path}: a picture is a file name in pictures/`);
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const bytes = files.pictures.get(given);
|
|
129
|
+
if (!bytes) {
|
|
130
|
+
fault(`${path}: pictures/${given} is not there`);
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
const problem = mediaProblem(bytes);
|
|
134
|
+
if (problem) {
|
|
135
|
+
if (!picturesProblems.has(given))
|
|
136
|
+
problems.push({ file: `pictures/${given}`, message: problem });
|
|
137
|
+
picturesProblems.add(given);
|
|
138
|
+
fault(`${path}: pictures/${given} cannot be uploaded`);
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
used.set(given, bytes);
|
|
142
|
+
pictures.push(given);
|
|
143
|
+
return { [PICTURE]: given };
|
|
144
|
+
};
|
|
145
|
+
for (const name of ["cover", "seoImage"]) {
|
|
146
|
+
const v = raw[name];
|
|
147
|
+
if (v === void 0 || v === null) continue;
|
|
148
|
+
if (!isObject(v)) {
|
|
149
|
+
fault(`${name} is { file, alt, focal? }`);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const { file: pictureFile, ...rest } = v;
|
|
153
|
+
const ref = picture(pictureFile, name);
|
|
154
|
+
const candidate = { media: placeholderMedia(0), ...rest };
|
|
155
|
+
const checked = validateDocument(IMAGE_SCHEMA, candidate);
|
|
156
|
+
if (!checked.ok) {
|
|
157
|
+
for (const issue of checked.issues) {
|
|
158
|
+
fault(`${name}.${issue.path.join(".")}: ${issue.message}`);
|
|
159
|
+
}
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (ref) values[name] = { ...rest, media: ref };
|
|
163
|
+
}
|
|
164
|
+
if (raw.body !== void 0 && raw.body !== null) {
|
|
165
|
+
let n = 0;
|
|
166
|
+
const placed = JSON.parse(JSON.stringify(raw.body), function(_k, v) {
|
|
167
|
+
if (isObject(v) && v.type === "image" && typeof v.file === "string" && !("media" in v)) {
|
|
168
|
+
const { file: f, ...rest } = v;
|
|
169
|
+
const ref = picture(f, `body (picture ${f})`);
|
|
170
|
+
n += 1;
|
|
171
|
+
return { ...rest, media: ref ?? placeholderMedia(n) };
|
|
172
|
+
}
|
|
173
|
+
return v;
|
|
174
|
+
});
|
|
175
|
+
const check = withPictures(placed, () => placeholderMedia(0));
|
|
176
|
+
const checked = validateDocument(RICH_TEXT_SCHEMA, check, { maxBytes: 524288 });
|
|
177
|
+
if (!checked.ok) {
|
|
178
|
+
for (const issue of checked.issues) {
|
|
179
|
+
fault(`body.${issue.path.join(".")}: ${issue.message}`);
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
values.body = placed;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
let date = null;
|
|
186
|
+
if (raw.date !== void 0 && raw.date !== null) {
|
|
187
|
+
if (typeof raw.date !== "string" || !DATETIME_RE.test(raw.date) || Number.isNaN(Date.parse(raw.date))) {
|
|
188
|
+
fault("date is an ISO 8601 date and time, with its zone (2024-05-01T09:00:00Z)");
|
|
189
|
+
} else {
|
|
190
|
+
date = new Date(raw.date).toISOString();
|
|
191
|
+
values.date = date;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const status = raw.status ?? "draft";
|
|
195
|
+
if (!STATUSES.includes(status)) fault(`status is one of ${STATUSES.join(", ")}`);
|
|
196
|
+
if (status === "scheduled" && !date) fault("A scheduled post has the date it goes live");
|
|
197
|
+
if (raw.noindex !== void 0) {
|
|
198
|
+
if (typeof raw.noindex !== "boolean") fault("noindex is true or false");
|
|
199
|
+
else values.noindex = raw.noindex;
|
|
200
|
+
}
|
|
201
|
+
let category = null;
|
|
202
|
+
if (raw.category !== void 0 && raw.category !== null) {
|
|
203
|
+
const name = typeof raw.category === "string" ? raw.category.trim() : "";
|
|
204
|
+
const categorySlug = slugify(name);
|
|
205
|
+
if (!name || !categorySlug || name.length > 1e3) fault("category is the category's name");
|
|
206
|
+
else {
|
|
207
|
+
category = `category:${categorySlug}`;
|
|
208
|
+
if (!categories.has(category)) {
|
|
209
|
+
categories.set(category, { key: category, values: { name, slug: categorySlug } });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
posts.push({ file, key, status, date, values, category, pictures });
|
|
214
|
+
}
|
|
215
|
+
return { posts, categories: [...categories.values()], pictures: used, problems };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/content-import/engine.ts
|
|
219
|
+
function resolved(post, media, category) {
|
|
220
|
+
const values = withPictures(post.values, media);
|
|
221
|
+
if (post.category) {
|
|
222
|
+
const id = category(post.category);
|
|
223
|
+
if (id) values.category = id;
|
|
224
|
+
}
|
|
225
|
+
return values;
|
|
226
|
+
}
|
|
227
|
+
async function unpublishedNote(host, post, result) {
|
|
228
|
+
if (result?.id && (result.outcome === "unchanged" || result.outcome === "updated")) {
|
|
229
|
+
const record = await host.call("record.get", {
|
|
230
|
+
object: POST_OBJECT,
|
|
231
|
+
id: result.id
|
|
232
|
+
});
|
|
233
|
+
const state = record.values.publishState;
|
|
234
|
+
const same = post.status === "published" ? state === "published" : state === "scheduled" && record.values.publishAt === post.date;
|
|
235
|
+
if (same) {
|
|
236
|
+
return result.outcome === "unchanged" ? null : post.status === "published" ? "Published here already: the change is saved, not published (pass --publish to publish it)" : "Scheduled here already: the change is saved, not published (pass --publish to schedule it)";
|
|
237
|
+
}
|
|
238
|
+
if (state === "published" || state === "scheduled") {
|
|
239
|
+
return "Already " + state + " here; left as it is (pass --publish to publish or schedule it as the source has it)";
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return post.status === "scheduled" ? `Scheduled for ${post.date} in the source: imported as a draft (pass --publish to schedule it)` : "Published in the source: imported as a draft (pass --publish to publish it)";
|
|
243
|
+
}
|
|
244
|
+
async function runImport(plan, host, options = {}) {
|
|
245
|
+
const report = {
|
|
246
|
+
problems: [...plan.problems],
|
|
247
|
+
records: [],
|
|
248
|
+
pictures: { uploaded: 0, already: 0 },
|
|
249
|
+
published: [],
|
|
250
|
+
notes: [],
|
|
251
|
+
wroteNothing: true
|
|
252
|
+
};
|
|
253
|
+
if (report.problems.length) return report;
|
|
254
|
+
const now = (options.now ?? (() => /* @__PURE__ */ new Date()))();
|
|
255
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
256
|
+
for (const [file, bytes] of plan.pictures) hashes.set(file, await sha256(bytes));
|
|
257
|
+
const known = /* @__PURE__ */ new Map();
|
|
258
|
+
for (const part of chunks([...new Set(hashes.values())], 200)) {
|
|
259
|
+
if (!part.length) continue;
|
|
260
|
+
const found = await host.call("media.findByHash", {
|
|
261
|
+
sha256: part
|
|
262
|
+
});
|
|
263
|
+
for (const item of found.items) known.set(item.sha256, item.id);
|
|
264
|
+
}
|
|
265
|
+
const dryCategories = await importRecords(
|
|
266
|
+
host,
|
|
267
|
+
CATEGORY_OBJECT,
|
|
268
|
+
plan.categories.map((c) => ({ key: c.key, file: null, values: c.values })),
|
|
269
|
+
{ dryRun: true, checkReferences: false }
|
|
270
|
+
);
|
|
271
|
+
const placeholders = new Map(
|
|
272
|
+
[...plan.pictures.keys()].map((f, i) => [
|
|
273
|
+
f,
|
|
274
|
+
known.get(hashes.get(f)) ?? placeholderMedia(i)
|
|
275
|
+
])
|
|
276
|
+
);
|
|
277
|
+
const dryCategoryIds = new Map(dryCategories.map((c) => [c.key, c.id]));
|
|
278
|
+
const dryPosts = await importRecords(
|
|
279
|
+
host,
|
|
280
|
+
POST_OBJECT,
|
|
281
|
+
plan.posts.map((post) => ({
|
|
282
|
+
key: post.key,
|
|
283
|
+
file: post.file,
|
|
284
|
+
values: resolved(
|
|
285
|
+
post,
|
|
286
|
+
(f) => placeholders.get(f) ?? placeholderMedia(0),
|
|
287
|
+
(key) => dryCategoryIds.get(key) ?? "rec_importplaceholder"
|
|
288
|
+
)
|
|
289
|
+
})),
|
|
290
|
+
{ dryRun: true, checkReferences: false }
|
|
291
|
+
);
|
|
292
|
+
for (const result of [...dryCategories, ...dryPosts]) {
|
|
293
|
+
if (result.outcome === "invalid") {
|
|
294
|
+
for (const message of result.problems) {
|
|
295
|
+
report.problems.push({ file: result.file ?? `category ${result.key}`, message });
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
for (const post of plan.posts) {
|
|
300
|
+
if (post.status === "draft" || options.publish) continue;
|
|
301
|
+
const note = await unpublishedNote(
|
|
302
|
+
host,
|
|
303
|
+
post,
|
|
304
|
+
dryPosts.find((r) => r.key === post.key)
|
|
305
|
+
);
|
|
306
|
+
if (note) report.notes.push({ file: post.file, message: note });
|
|
307
|
+
}
|
|
308
|
+
if (report.problems.length || options.dryRun) {
|
|
309
|
+
report.records = [...dryCategories, ...dryPosts];
|
|
310
|
+
return report;
|
|
311
|
+
}
|
|
312
|
+
if (options.publish && options.confirm) {
|
|
313
|
+
const live = plan.posts.filter((p) => p.status !== "draft");
|
|
314
|
+
const summary = `Publish ${live.filter((p) => p.status === "published").length} post(s) and schedule ${live.filter((p) => p.status === "scheduled").length}, as you, with their own dates`;
|
|
315
|
+
if (!await options.confirm(summary)) {
|
|
316
|
+
report.notes.push({ file: "", message: "Stopped before writing: not confirmed" });
|
|
317
|
+
return report;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
const ids = /* @__PURE__ */ new Map();
|
|
321
|
+
for (const [file, bytes] of plan.pictures) {
|
|
322
|
+
const hash = hashes.get(file);
|
|
323
|
+
let id = known.get(hash);
|
|
324
|
+
if (id) report.pictures.already += 1;
|
|
325
|
+
else {
|
|
326
|
+
report.wroteNothing = false;
|
|
327
|
+
const uploaded = await host.upload(bytes, file);
|
|
328
|
+
id = uploaded.id;
|
|
329
|
+
known.set(hash, id);
|
|
330
|
+
if (uploaded.existing) report.pictures.already += 1;
|
|
331
|
+
else report.pictures.uploaded += 1;
|
|
332
|
+
}
|
|
333
|
+
ids.set(file, id);
|
|
334
|
+
}
|
|
335
|
+
const categories = await importRecords(
|
|
336
|
+
host,
|
|
337
|
+
CATEGORY_OBJECT,
|
|
338
|
+
plan.categories.map((c) => ({ key: c.key, file: null, values: c.values })),
|
|
339
|
+
{ dryRun: false, checkReferences: true }
|
|
340
|
+
);
|
|
341
|
+
const categoryIds = new Map(categories.map((c) => [c.key, c.id]));
|
|
342
|
+
const posts = await importRecords(
|
|
343
|
+
host,
|
|
344
|
+
POST_OBJECT,
|
|
345
|
+
plan.posts.map((post) => ({
|
|
346
|
+
key: post.key,
|
|
347
|
+
file: post.file,
|
|
348
|
+
values: resolved(
|
|
349
|
+
post,
|
|
350
|
+
(f) => ids.get(f),
|
|
351
|
+
(key) => categoryIds.get(key) ?? null
|
|
352
|
+
)
|
|
353
|
+
})),
|
|
354
|
+
{ dryRun: false, checkReferences: true }
|
|
355
|
+
);
|
|
356
|
+
report.records = [...categories, ...posts];
|
|
357
|
+
if (report.records.some((r) => r.outcome === "created" || r.outcome === "updated")) {
|
|
358
|
+
report.wroteNothing = false;
|
|
359
|
+
}
|
|
360
|
+
if (options.publish) {
|
|
361
|
+
for (const post of plan.posts) {
|
|
362
|
+
const result = posts.find((r) => r.key === post.key);
|
|
363
|
+
if (!result?.id || result.outcome === "conflict" || result.outcome === "invalid") continue;
|
|
364
|
+
if (post.status === "draft") continue;
|
|
365
|
+
const id = result.id;
|
|
366
|
+
const record = await host.call("record.get", {
|
|
367
|
+
object: POST_OBJECT,
|
|
368
|
+
id
|
|
369
|
+
});
|
|
370
|
+
const state = record.values.publishState;
|
|
371
|
+
const due = post.status === "scheduled" && post.date && Date.parse(post.date) > now.getTime();
|
|
372
|
+
if (due) {
|
|
373
|
+
if (state === "scheduled" && record.values.publishAt === post.date) {
|
|
374
|
+
report.published.push({ file: post.file, id, as: "unchanged" });
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
await host.call("record.schedulePublish", {
|
|
378
|
+
object: POST_OBJECT,
|
|
379
|
+
id,
|
|
380
|
+
at: post.date,
|
|
381
|
+
note: "Imported"
|
|
382
|
+
});
|
|
383
|
+
report.wroteNothing = false;
|
|
384
|
+
report.published.push({ file: post.file, id, as: "scheduled" });
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (post.status === "scheduled") {
|
|
388
|
+
report.notes.push({
|
|
389
|
+
file: post.file,
|
|
390
|
+
message: `Scheduled for ${post.date}, which has passed: published`
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
if (state === "published" && typeof record.values.publishedVersion === "number") {
|
|
394
|
+
const diff = await host.call("record.compareVersions", {
|
|
395
|
+
object: POST_OBJECT,
|
|
396
|
+
id,
|
|
397
|
+
from: record.values.publishedVersion
|
|
398
|
+
});
|
|
399
|
+
if (!diff.changes.length) {
|
|
400
|
+
report.published.push({ file: post.file, id, as: "unchanged" });
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
await host.call("record.publish", { object: POST_OBJECT, id, note: "Imported" });
|
|
405
|
+
report.wroteNothing = false;
|
|
406
|
+
report.published.push({ file: post.file, id, as: "published" });
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return report;
|
|
410
|
+
}
|
|
411
|
+
function reportLines(report, dryRun) {
|
|
412
|
+
const lines = [];
|
|
413
|
+
if (report.problems.length) {
|
|
414
|
+
lines.push(`Nothing was written: ${report.problems.length} problem(s).`);
|
|
415
|
+
for (const p of report.problems) lines.push(` ${p.file}: ${p.message}`);
|
|
416
|
+
return lines;
|
|
417
|
+
}
|
|
418
|
+
lines.push(...outcomeLines(report.records, dryRun));
|
|
419
|
+
if (!dryRun) {
|
|
420
|
+
lines.push(
|
|
421
|
+
`Pictures: ${report.pictures.uploaded} uploaded, ${report.pictures.already} already in the library.`
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
for (const p of report.published) {
|
|
425
|
+
if (p.as !== "unchanged") lines.push(` ${p.file}: ${p.as}`);
|
|
426
|
+
}
|
|
427
|
+
for (const note of report.notes)
|
|
428
|
+
lines.push(` ${note.file ? `${note.file}: ` : ""}${note.message}`);
|
|
429
|
+
return lines;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// src/content-import/run.ts
|
|
433
|
+
function readImportFolder(dir) {
|
|
434
|
+
const root = resolve(dir);
|
|
435
|
+
if (!existsSync(root) || !statSync(root).isDirectory()) {
|
|
436
|
+
throw new Error(`${dir} is not a folder`);
|
|
437
|
+
}
|
|
438
|
+
const posts = /* @__PURE__ */ new Map();
|
|
439
|
+
for (const name of readdirSync(root)) {
|
|
440
|
+
if (name.toLowerCase().endsWith(".json"))
|
|
441
|
+
posts.set(name, readFileSync(join(root, name), "utf8"));
|
|
442
|
+
}
|
|
443
|
+
const pictures = /* @__PURE__ */ new Map();
|
|
444
|
+
const folder = join(root, "pictures");
|
|
445
|
+
if (existsSync(folder)) {
|
|
446
|
+
for (const name of readdirSync(folder)) {
|
|
447
|
+
const path = join(folder, name);
|
|
448
|
+
if (statSync(path).isFile()) pictures.set(name, new Uint8Array(readFileSync(path)));
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return { posts, pictures };
|
|
452
|
+
}
|
|
453
|
+
async function contentImport(positional, flags, io) {
|
|
454
|
+
const dir = positional[0];
|
|
455
|
+
if (!dir) {
|
|
456
|
+
io.err(
|
|
457
|
+
`Usage: cos content import <folder> --host <https://host> [--email <you>] [--dry-run] [--publish] [--yes]
|
|
458
|
+
|
|
459
|
+
A post file's fields: ${Object.entries(
|
|
460
|
+
POST_FIELDS
|
|
461
|
+
).map(([k, v]) => `${k} (${v})`).join("; ")}. See docs/importing.md.`
|
|
462
|
+
);
|
|
463
|
+
return 2;
|
|
464
|
+
}
|
|
465
|
+
const host = flags.host?.replace(/\/+$/, "");
|
|
466
|
+
if (!host || !/^https?:\/\//.test(host)) {
|
|
467
|
+
io.err("--host is the host's address, e.g. https://admin.example.com");
|
|
468
|
+
return 2;
|
|
469
|
+
}
|
|
470
|
+
const plan = readFolder(readImportFolder(dir));
|
|
471
|
+
if (plan.problems.length) {
|
|
472
|
+
io.err(`Nothing was written: ${plan.problems.length} problem(s) in ${dir}.`);
|
|
473
|
+
for (const p of plan.problems) io.err(` ${p.file}: ${p.message}`);
|
|
474
|
+
return 1;
|
|
475
|
+
}
|
|
476
|
+
io.out(
|
|
477
|
+
`${plan.posts.length} post(s), ${plan.categories.length} categor${plan.categories.length === 1 ? "y" : "ies"} and ${plan.pictures.size} picture(s) in ${dir}.`
|
|
478
|
+
);
|
|
479
|
+
let session;
|
|
480
|
+
try {
|
|
481
|
+
session = await signInForRun(host, flags.email, io, "cos content import");
|
|
482
|
+
} catch (error) {
|
|
483
|
+
io.err(`Could not sign in: ${error.message}`);
|
|
484
|
+
return 1;
|
|
485
|
+
}
|
|
486
|
+
try {
|
|
487
|
+
const report = await runImport(
|
|
488
|
+
plan,
|
|
489
|
+
sessionHost(host, () => session.headers()),
|
|
490
|
+
{
|
|
491
|
+
dryRun: Boolean(flags.dryRun),
|
|
492
|
+
publish: Boolean(flags.publish),
|
|
493
|
+
confirm: confirmer(io, flags.yes)
|
|
494
|
+
}
|
|
495
|
+
);
|
|
496
|
+
for (const line of reportLines(report, Boolean(flags.dryRun))) {
|
|
497
|
+
(report.problems.length ? io.err : io.out)(line);
|
|
498
|
+
}
|
|
499
|
+
return report.problems.length ? 1 : 0;
|
|
500
|
+
} finally {
|
|
501
|
+
await session.signOut();
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
export {
|
|
505
|
+
LOOPBACK_PATH,
|
|
506
|
+
contentImport,
|
|
507
|
+
readImportFolder
|
|
508
|
+
};
|