@open-slide/core 2.0.0 → 2.1.0
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/{build-Cu5_qRyE.js → build-COxfoC_P.js} +1 -1
- package/dist/cli/bin.js +105 -15
- package/dist/{config-BQ6rD2iE.js → config-CNhYSo4C.js} +79 -896
- package/dist/{config-GMR1QUlM.d.ts → config-DHPbCCnt.d.ts} +1 -1
- package/dist/{dev-BK641O5e.js → dev-CujpTS3T.js} +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -3
- package/dist/locale/index.d.ts +5 -2
- package/dist/locale/index.js +2 -2
- package/dist/{preview-BuCI-RMW.js → preview-kU5R6QXJ.js} +1 -1
- package/dist/{types-BcbaKTNY.d.ts → types-BGUi9COY.d.ts} +1 -1
- package/dist/update-CPBitJQq.js +905 -0
- package/dist/vite/index.d.ts +1 -1
- package/dist/vite/index.js +1 -1
- package/dist/{zh-tw-DlZoAc2-.js → zh-tw-BbFb4PZW.js} +519 -1
- package/package.json +4 -4
- package/skills/apply-comments/SKILL.md +1 -0
- package/skills/create-slide/SKILL.md +2 -0
- package/skills/current-slide/SKILL.md +1 -0
- package/skills/slide-authoring/SKILL.md +31 -1
- package/src/app/app.tsx +2 -0
- package/src/app/assets/open-slide.svg +39 -0
- package/src/app/components/command/command-menu.tsx +1 -3
- package/src/app/components/history-provider.tsx +25 -3
- package/src/app/components/inspector/inspector-provider.tsx +179 -21
- package/src/app/components/panel/editor-sidebar.tsx +1 -1
- package/src/app/components/panel/save-card.tsx +6 -2
- package/src/app/components/preview-step-host.tsx +16 -0
- package/src/app/components/sidebar/sidebar.tsx +2 -2
- package/src/app/components/slide-transition-layer.tsx +1 -1
- package/src/app/components/style-panel/style-panel.tsx +3 -2
- package/src/app/components/thumbnail-rail.tsx +37 -0
- package/src/app/favicon.ico +0 -0
- package/src/app/favicon.svg +39 -0
- package/src/app/index.html +2 -1
- package/src/app/lib/keys.ts +3 -0
- package/src/app/lib/locale-store.ts +6 -1
- package/src/app/lib/print-ready.ts +9 -6
- package/src/app/routes/presenter.tsx +1 -16
- package/src/app/routes/preview.tsx +169 -0
- package/src/app/routes/slide.tsx +113 -65
- package/src/locale/es.ts +548 -0
- package/src/locale/index.ts +1 -0
- package/src/locale/types.ts +1 -1
- package/src/app/assets/open-slide.png +0 -0
|
@@ -0,0 +1,905 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import { parse } from "@babel/parser";
|
|
5
|
+
import { randomUUID } from "node:crypto";
|
|
6
|
+
//#region src/http/request-guard.ts
|
|
7
|
+
function headerValue(req, name) {
|
|
8
|
+
const raw = req.headers[name.toLowerCase()];
|
|
9
|
+
return (Array.isArray(raw) ? raw[0] : raw)?.trim() ?? null;
|
|
10
|
+
}
|
|
11
|
+
function firstCommaToken(value) {
|
|
12
|
+
if (!value) return null;
|
|
13
|
+
const [first] = value.split(",", 1);
|
|
14
|
+
return first?.trim() || null;
|
|
15
|
+
}
|
|
16
|
+
function requestProto(req) {
|
|
17
|
+
const forwarded = firstCommaToken(headerValue(req, "x-forwarded-proto"))?.toLowerCase();
|
|
18
|
+
if (forwarded === "http" || forwarded === "https") return forwarded;
|
|
19
|
+
return "encrypted" in req.socket && req.socket.encrypted ? "https" : "http";
|
|
20
|
+
}
|
|
21
|
+
function normalizedOrigin(origin) {
|
|
22
|
+
try {
|
|
23
|
+
const url = new URL(origin);
|
|
24
|
+
return `${url.protocol}//${url.host}`.toLowerCase();
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function validateMutationRequest(req, opts = {}) {
|
|
30
|
+
if (opts.requireJsonBody) {
|
|
31
|
+
if (!(headerValue(req, "content-type")?.toLowerCase())?.startsWith("application/json")) return {
|
|
32
|
+
ok: false,
|
|
33
|
+
status: 415,
|
|
34
|
+
error: "content-type must be application/json"
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (firstCommaToken(headerValue(req, "sec-fetch-site"))?.toLowerCase() === "cross-site") return {
|
|
38
|
+
ok: false,
|
|
39
|
+
status: 403,
|
|
40
|
+
error: "cross-site request blocked"
|
|
41
|
+
};
|
|
42
|
+
const originRaw = headerValue(req, "origin");
|
|
43
|
+
if (!originRaw) return { ok: true };
|
|
44
|
+
if (originRaw.toLowerCase() === "null") return {
|
|
45
|
+
ok: false,
|
|
46
|
+
status: 403,
|
|
47
|
+
error: "opaque origin is not allowed"
|
|
48
|
+
};
|
|
49
|
+
const actualOrigin = normalizedOrigin(originRaw);
|
|
50
|
+
if (!actualOrigin) return {
|
|
51
|
+
ok: false,
|
|
52
|
+
status: 403,
|
|
53
|
+
error: "invalid origin header"
|
|
54
|
+
};
|
|
55
|
+
const host = firstCommaToken(headerValue(req, "x-forwarded-host")) ?? headerValue(req, "host");
|
|
56
|
+
if (!host) return {
|
|
57
|
+
ok: false,
|
|
58
|
+
status: 400,
|
|
59
|
+
error: "missing host header"
|
|
60
|
+
};
|
|
61
|
+
if (actualOrigin !== `${requestProto(req)}://${host}`.toLowerCase()) return {
|
|
62
|
+
ok: false,
|
|
63
|
+
status: 403,
|
|
64
|
+
error: "origin mismatch"
|
|
65
|
+
};
|
|
66
|
+
return { ok: true };
|
|
67
|
+
}
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/editing/slide-ops.ts
|
|
70
|
+
const SLIDE_ID_RE = /^[a-z0-9_-]+$/i;
|
|
71
|
+
/**
|
|
72
|
+
* True when a directory under root already uses this id, including a
|
|
73
|
+
* different case. ext4 keeps `Cover` and `cover` as two folders; macOS
|
|
74
|
+
* and Windows usually collapse them. Listing names catches both.
|
|
75
|
+
*/
|
|
76
|
+
async function slideIdTaken(root, slideId) {
|
|
77
|
+
let names;
|
|
78
|
+
try {
|
|
79
|
+
names = await fs.readdir(root);
|
|
80
|
+
} catch {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
const want = slideId.toLowerCase();
|
|
84
|
+
return names.some((name) => name.toLowerCase() === want);
|
|
85
|
+
}
|
|
86
|
+
function validateSlideName(v) {
|
|
87
|
+
if (typeof v !== "string") return null;
|
|
88
|
+
const trimmed = v.trim();
|
|
89
|
+
if (trimmed.length < 1 || trimmed.length > 80) return null;
|
|
90
|
+
return trimmed;
|
|
91
|
+
}
|
|
92
|
+
function unwrapExpression(node) {
|
|
93
|
+
let current = node;
|
|
94
|
+
while (current && (current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression")) current = current.expression;
|
|
95
|
+
return current;
|
|
96
|
+
}
|
|
97
|
+
function readMetaTitleInSource(source) {
|
|
98
|
+
let ast;
|
|
99
|
+
try {
|
|
100
|
+
ast = parse(source, {
|
|
101
|
+
sourceType: "module",
|
|
102
|
+
plugins: ["typescript", "jsx"],
|
|
103
|
+
errorRecovery: true
|
|
104
|
+
});
|
|
105
|
+
} catch {
|
|
106
|
+
return { kind: "unsupported" };
|
|
107
|
+
}
|
|
108
|
+
const body = ast.program?.body ?? [];
|
|
109
|
+
for (const stmt of body) {
|
|
110
|
+
if (stmt.type !== "ExportNamedDeclaration") continue;
|
|
111
|
+
const decl = stmt.declaration;
|
|
112
|
+
if (decl?.type !== "VariableDeclaration") continue;
|
|
113
|
+
const declarations = decl.declarations ?? [];
|
|
114
|
+
for (const d of declarations) {
|
|
115
|
+
const id = d.id;
|
|
116
|
+
if (id?.type !== "Identifier" || id.name !== "meta") continue;
|
|
117
|
+
const init = unwrapExpression(d.init);
|
|
118
|
+
if (init?.type !== "ObjectExpression") return { kind: "unsupported" };
|
|
119
|
+
const properties = init.properties ?? [];
|
|
120
|
+
for (const property of properties) {
|
|
121
|
+
if (property.type !== "ObjectProperty" || property.computed) continue;
|
|
122
|
+
const key = property.key;
|
|
123
|
+
if ((key?.type === "Identifier" ? key.name : key?.type === "StringLiteral" ? key.value : void 0) !== "title") continue;
|
|
124
|
+
const value = property.value;
|
|
125
|
+
if (value?.type === "StringLiteral" && typeof value.value === "string") return {
|
|
126
|
+
kind: "found",
|
|
127
|
+
title: value.value
|
|
128
|
+
};
|
|
129
|
+
if (value?.type === "TemplateLiteral") {
|
|
130
|
+
const expressions = value.expressions ?? [];
|
|
131
|
+
const firstValue = (value.quasis ?? [])[0]?.value;
|
|
132
|
+
const cooked = firstValue?.cooked;
|
|
133
|
+
const raw = firstValue?.raw;
|
|
134
|
+
if (expressions.length === 0 && typeof (cooked ?? raw) === "string") return {
|
|
135
|
+
kind: "found",
|
|
136
|
+
title: cooked ?? raw
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return { kind: "unsupported" };
|
|
140
|
+
}
|
|
141
|
+
return { kind: "missing" };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { kind: "missing" };
|
|
145
|
+
}
|
|
146
|
+
async function rmSlideDir(slidesRoot, slideId) {
|
|
147
|
+
if (!SLIDE_ID_RE.test(slideId)) return false;
|
|
148
|
+
const dir = path.resolve(slidesRoot, slideId);
|
|
149
|
+
if (!dir.startsWith(slidesRoot + path.sep)) return false;
|
|
150
|
+
try {
|
|
151
|
+
await fs.rm(dir, {
|
|
152
|
+
recursive: true,
|
|
153
|
+
force: true
|
|
154
|
+
});
|
|
155
|
+
return true;
|
|
156
|
+
} catch {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async function duplicateSlideDir(slidesRoot, slideId, desiredId) {
|
|
161
|
+
if (!SLIDE_ID_RE.test(slideId)) return {
|
|
162
|
+
ok: false,
|
|
163
|
+
status: 400,
|
|
164
|
+
error: "invalid slideId"
|
|
165
|
+
};
|
|
166
|
+
const root = path.resolve(slidesRoot);
|
|
167
|
+
const srcDir = path.resolve(root, slideId);
|
|
168
|
+
if (!srcDir.startsWith(root + path.sep)) return {
|
|
169
|
+
ok: false,
|
|
170
|
+
status: 400,
|
|
171
|
+
error: "invalid slideId"
|
|
172
|
+
};
|
|
173
|
+
try {
|
|
174
|
+
await fs.access(path.join(srcDir, "index.tsx"));
|
|
175
|
+
} catch {
|
|
176
|
+
return {
|
|
177
|
+
ok: false,
|
|
178
|
+
status: 404,
|
|
179
|
+
error: "slide not found"
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
let newId;
|
|
183
|
+
if (desiredId !== void 0) {
|
|
184
|
+
if (!SLIDE_ID_RE.test(desiredId)) return {
|
|
185
|
+
ok: false,
|
|
186
|
+
status: 400,
|
|
187
|
+
error: "invalid newId"
|
|
188
|
+
};
|
|
189
|
+
newId = desiredId;
|
|
190
|
+
if (!path.resolve(root, newId).startsWith(root + path.sep)) return {
|
|
191
|
+
ok: false,
|
|
192
|
+
status: 400,
|
|
193
|
+
error: "invalid newId"
|
|
194
|
+
};
|
|
195
|
+
if (await slideIdTaken(root, newId)) return {
|
|
196
|
+
ok: false,
|
|
197
|
+
status: 409,
|
|
198
|
+
error: "slide already exists"
|
|
199
|
+
};
|
|
200
|
+
} else {
|
|
201
|
+
let suffix = 1;
|
|
202
|
+
while (true) {
|
|
203
|
+
newId = suffix === 1 ? `${slideId}-copy` : `${slideId}-copy-${suffix}`;
|
|
204
|
+
if (await slideIdTaken(root, newId)) {
|
|
205
|
+
suffix++;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const dstDir = path.resolve(root, newId);
|
|
212
|
+
if (!dstDir.startsWith(root + path.sep)) return {
|
|
213
|
+
ok: false,
|
|
214
|
+
status: 400,
|
|
215
|
+
error: "invalid newId"
|
|
216
|
+
};
|
|
217
|
+
const srcEntry = path.join(srcDir, "index.tsx");
|
|
218
|
+
let copiedEntrySource;
|
|
219
|
+
try {
|
|
220
|
+
const source = await fs.readFile(srcEntry, "utf8");
|
|
221
|
+
const metaTitle = readMetaTitleInSource(source);
|
|
222
|
+
if (metaTitle.kind === "unsupported") return {
|
|
223
|
+
ok: false,
|
|
224
|
+
status: 422,
|
|
225
|
+
error: "could not update copied slide title"
|
|
226
|
+
};
|
|
227
|
+
const updated = updateMetaTitleInSource(source, `${metaTitle.kind === "found" ? metaTitle.title : slideId} (copy)`);
|
|
228
|
+
if (updated === null) return {
|
|
229
|
+
ok: false,
|
|
230
|
+
status: 422,
|
|
231
|
+
error: "could not update copied slide title"
|
|
232
|
+
};
|
|
233
|
+
copiedEntrySource = updated;
|
|
234
|
+
} catch {
|
|
235
|
+
return {
|
|
236
|
+
ok: false,
|
|
237
|
+
status: 404,
|
|
238
|
+
error: "slide not found"
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
await fs.cp(srcDir, dstDir, {
|
|
243
|
+
recursive: true,
|
|
244
|
+
errorOnExist: true,
|
|
245
|
+
force: false
|
|
246
|
+
});
|
|
247
|
+
await fs.writeFile(path.join(dstDir, "index.tsx"), copiedEntrySource, "utf8");
|
|
248
|
+
return {
|
|
249
|
+
ok: true,
|
|
250
|
+
slideId: newId
|
|
251
|
+
};
|
|
252
|
+
} catch (err) {
|
|
253
|
+
if (err.code === "EEXIST") return {
|
|
254
|
+
ok: false,
|
|
255
|
+
status: 409,
|
|
256
|
+
error: "slide already exists"
|
|
257
|
+
};
|
|
258
|
+
return {
|
|
259
|
+
ok: false,
|
|
260
|
+
status: 500,
|
|
261
|
+
error: String(err.message ?? err)
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function resolveSlideEntry(slidesRoot, slideId) {
|
|
266
|
+
if (!SLIDE_ID_RE.test(slideId)) return null;
|
|
267
|
+
const dir = path.resolve(slidesRoot, slideId);
|
|
268
|
+
if (!dir.startsWith(slidesRoot + path.sep)) return null;
|
|
269
|
+
return path.join(dir, "index.tsx");
|
|
270
|
+
}
|
|
271
|
+
function escapeSingleQuoted(s) {
|
|
272
|
+
return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Rewrite (or insert) the `title` field in the slide module's `export const meta`.
|
|
276
|
+
*
|
|
277
|
+
* Strategy:
|
|
278
|
+
* 1. Find `export const meta` and brace-match its object literal.
|
|
279
|
+
* 2. If the object already has a `title: '...'` entry, replace the literal.
|
|
280
|
+
* 3. If the object exists but has no title, inject a new `title: '...'` line
|
|
281
|
+
* as the first property (preserving the author's surrounding indentation).
|
|
282
|
+
* 4. If there is no `meta` export at all, insert a fresh one right before
|
|
283
|
+
* `export default`.
|
|
284
|
+
*
|
|
285
|
+
* Returns the rewritten source, or `null` if the file shape was too surprising
|
|
286
|
+
* to touch safely (e.g. `export default` missing when we'd need to inject meta).
|
|
287
|
+
*/
|
|
288
|
+
function updateMetaTitleInSource(source, title) {
|
|
289
|
+
const newLiteral = `'${escapeSingleQuoted(title)}'`;
|
|
290
|
+
const metaStart = source.search(/export\s+const\s+meta\b/);
|
|
291
|
+
if (metaStart !== -1) {
|
|
292
|
+
const eqIdx = source.indexOf("=", metaStart);
|
|
293
|
+
if (eqIdx === -1) return null;
|
|
294
|
+
const openBrace = source.indexOf("{", eqIdx);
|
|
295
|
+
if (openBrace === -1) return null;
|
|
296
|
+
let depth = 0;
|
|
297
|
+
let closeBrace = -1;
|
|
298
|
+
for (let i = openBrace; i < source.length; i++) {
|
|
299
|
+
const ch = source[i];
|
|
300
|
+
if (ch === "{") depth++;
|
|
301
|
+
else if (ch === "}") {
|
|
302
|
+
depth--;
|
|
303
|
+
if (depth === 0) {
|
|
304
|
+
closeBrace = i;
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (closeBrace === -1) return null;
|
|
310
|
+
const body = source.slice(openBrace + 1, closeBrace);
|
|
311
|
+
const titleRe = /(^|[\s,{])(title\s*:\s*)(['"`])((?:\\.|(?!\3).)*)\3/;
|
|
312
|
+
const match = body.match(titleRe);
|
|
313
|
+
if (match) {
|
|
314
|
+
const newBody = body.replace(titleRe, `${match[1]}${match[2]}${newLiteral}`);
|
|
315
|
+
return source.slice(0, openBrace + 1) + newBody + source.slice(closeBrace);
|
|
316
|
+
}
|
|
317
|
+
const firstIndentMatch = body.match(/\n([ \t]+)\S/);
|
|
318
|
+
const insertion = `\n${firstIndentMatch ? firstIndentMatch[1] : " "}title: ${newLiteral}${body.replace(/^\s*\n?/, "").trim().length > 0 ? "," : ""}`;
|
|
319
|
+
return source.slice(0, openBrace + 1) + insertion + body + source.slice(closeBrace);
|
|
320
|
+
}
|
|
321
|
+
const exportDefaultIdx = source.search(/export\s+default\b/);
|
|
322
|
+
if (exportDefaultIdx === -1) return null;
|
|
323
|
+
const insertion = `export const meta: SlideMeta = { title: ${newLiteral} };\n\n`;
|
|
324
|
+
return source.slice(0, exportDefaultIdx) + insertion + source.slice(exportDefaultIdx);
|
|
325
|
+
}
|
|
326
|
+
function findDefaultExportArray(source) {
|
|
327
|
+
let ast;
|
|
328
|
+
try {
|
|
329
|
+
ast = parse(source, {
|
|
330
|
+
sourceType: "module",
|
|
331
|
+
plugins: ["typescript", "jsx"],
|
|
332
|
+
errorRecovery: true
|
|
333
|
+
});
|
|
334
|
+
} catch {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
const body = ast.program?.body ?? [];
|
|
338
|
+
for (const node of body) {
|
|
339
|
+
if (node.type !== "ExportDefaultDeclaration") continue;
|
|
340
|
+
let inner = node.declaration;
|
|
341
|
+
while (inner && (inner.type === "TSAsExpression" || inner.type === "TSSatisfiesExpression")) inner = inner.expression;
|
|
342
|
+
if (inner?.type !== "ArrayExpression") return null;
|
|
343
|
+
const arrayStart = inner.start;
|
|
344
|
+
const arrayEnd = inner.end;
|
|
345
|
+
const rawElements = inner.elements ?? [];
|
|
346
|
+
const elements = [];
|
|
347
|
+
for (const el of rawElements) {
|
|
348
|
+
if (!el || typeof el.start !== "number" || typeof el.end !== "number") return null;
|
|
349
|
+
elements.push({
|
|
350
|
+
start: el.start,
|
|
351
|
+
end: el.end
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
return {
|
|
355
|
+
elements,
|
|
356
|
+
arrayStart,
|
|
357
|
+
arrayEnd
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
return null;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Rewrite `export default [...]` so its elements appear in the requested order.
|
|
364
|
+
*
|
|
365
|
+
* `order[i]` is the original index that should land at new position `i`. The
|
|
366
|
+
* function preserves each element's exact source slice (including any inline
|
|
367
|
+
* comments that hug an identifier) and keeps the inter-element separator slots
|
|
368
|
+
* in their original positions, so a 3-page array `[A, B, C]` reordered to
|
|
369
|
+
* `[2, 0, 1]` becomes `[C, A, B]` with the same indentation and trailing
|
|
370
|
+
* commas the author wrote.
|
|
371
|
+
*
|
|
372
|
+
* Returns `null` when the file's default export isn't an array literal, or the
|
|
373
|
+
* order is not a valid permutation of `[0, n-1]`.
|
|
374
|
+
*/
|
|
375
|
+
function reorderDefaultExportPagesInSource(source, order) {
|
|
376
|
+
const found = findDefaultExportArray(source);
|
|
377
|
+
if (!found) return null;
|
|
378
|
+
const { elements, arrayStart, arrayEnd } = found;
|
|
379
|
+
const n = elements.length;
|
|
380
|
+
if (order.length !== n) return null;
|
|
381
|
+
const seen = /* @__PURE__ */ new Set();
|
|
382
|
+
for (const idx of order) {
|
|
383
|
+
if (!Number.isInteger(idx) || idx < 0 || idx >= n) return null;
|
|
384
|
+
if (seen.has(idx)) return null;
|
|
385
|
+
seen.add(idx);
|
|
386
|
+
}
|
|
387
|
+
if (n === 0) return source;
|
|
388
|
+
let identity = true;
|
|
389
|
+
for (let i = 0; i < n; i++) if (order[i] !== i) {
|
|
390
|
+
identity = false;
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
if (identity) return source;
|
|
394
|
+
const prefix = source.slice(arrayStart, elements[0].start);
|
|
395
|
+
const suffix = source.slice(elements[n - 1].end, arrayEnd);
|
|
396
|
+
const separators = [];
|
|
397
|
+
for (let i = 0; i < n - 1; i++) separators.push(source.slice(elements[i].end, elements[i + 1].start));
|
|
398
|
+
const elementText = elements.map((el) => source.slice(el.start, el.end));
|
|
399
|
+
let rebuilt = prefix + elementText[order[0]];
|
|
400
|
+
for (let i = 1; i < n; i++) rebuilt += separators[i - 1] + elementText[order[i]];
|
|
401
|
+
rebuilt += suffix;
|
|
402
|
+
return source.slice(0, arrayStart) + rebuilt + source.slice(arrayEnd);
|
|
403
|
+
}
|
|
404
|
+
function findNotesArray(source) {
|
|
405
|
+
let ast;
|
|
406
|
+
try {
|
|
407
|
+
ast = parse(source, {
|
|
408
|
+
sourceType: "module",
|
|
409
|
+
plugins: ["typescript", "jsx"],
|
|
410
|
+
errorRecovery: true
|
|
411
|
+
});
|
|
412
|
+
} catch {
|
|
413
|
+
return "invalid";
|
|
414
|
+
}
|
|
415
|
+
const body = ast.program?.body ?? [];
|
|
416
|
+
for (const stmt of body) {
|
|
417
|
+
if (stmt.type !== "ExportNamedDeclaration") continue;
|
|
418
|
+
const decl = stmt.declaration;
|
|
419
|
+
if (decl?.type !== "VariableDeclaration") continue;
|
|
420
|
+
const declarations = decl.declarations ?? [];
|
|
421
|
+
for (const d of declarations) {
|
|
422
|
+
const id = d.id;
|
|
423
|
+
if (id?.type !== "Identifier" || id.name !== "notes") continue;
|
|
424
|
+
const init = d.init;
|
|
425
|
+
if (init?.type !== "ArrayExpression") return "invalid";
|
|
426
|
+
const arrayStart = init.start;
|
|
427
|
+
const arrayEnd = init.end;
|
|
428
|
+
if (typeof arrayStart !== "number" || typeof arrayEnd !== "number") return "invalid";
|
|
429
|
+
const rawElements = init.elements ?? [];
|
|
430
|
+
const elementTexts = [];
|
|
431
|
+
for (const el of rawElements) {
|
|
432
|
+
if (el === null) {
|
|
433
|
+
elementTexts.push("undefined");
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
if (el.type === "SpreadElement") return "invalid";
|
|
437
|
+
const start = el.start;
|
|
438
|
+
const end = el.end;
|
|
439
|
+
if (typeof start !== "number" || typeof end !== "number") return "invalid";
|
|
440
|
+
elementTexts.push(source.slice(start, end));
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
arrayStart,
|
|
444
|
+
arrayEnd,
|
|
445
|
+
elementTexts
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return null;
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Reorder `export const notes = [...]` to follow the page-array reorder.
|
|
453
|
+
*
|
|
454
|
+
* `order[i]` is the original page index that should land at new position `i`.
|
|
455
|
+
* The notes array is index-aligned with the pages array but may be shorter
|
|
456
|
+
* (trailing `undefined` slots are routinely trimmed). Missing elements are
|
|
457
|
+
* treated as `undefined`, and trailing `undefined` is trimmed again after
|
|
458
|
+
* reordering to keep the file tidy.
|
|
459
|
+
*
|
|
460
|
+
* Returns the rewritten source, the original source if no `notes` export
|
|
461
|
+
* exists or the reorder is a no-op, or `null` if the `notes` export's shape
|
|
462
|
+
* is too surprising to touch safely.
|
|
463
|
+
*/
|
|
464
|
+
function reorderNotesArrayInSource(source, order) {
|
|
465
|
+
for (const idx of order) if (!Number.isInteger(idx) || idx < 0) return null;
|
|
466
|
+
const found = findNotesArray(source);
|
|
467
|
+
if (found === "invalid") return null;
|
|
468
|
+
if (found === null) return source;
|
|
469
|
+
const { arrayStart, arrayEnd, elementTexts } = found;
|
|
470
|
+
const pick = (i) => i >= 0 && i < elementTexts.length ? elementTexts[i] : "undefined";
|
|
471
|
+
return rebuildNotesArray(source, arrayStart, arrayEnd, order.map(pick));
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Remove the note aligned with the page at `index` so the `notes` export stays
|
|
475
|
+
* index-aligned with `export default [...]` after a page deletion. Mirrors
|
|
476
|
+
* {@link removePageFromDefaultExportInSource}.
|
|
477
|
+
*
|
|
478
|
+
* Returns the rewritten source, the original source if no `notes` export exists
|
|
479
|
+
* or the index falls past the recorded notes, or `null` if the `notes` export's
|
|
480
|
+
* shape is too surprising to touch safely.
|
|
481
|
+
*/
|
|
482
|
+
function removeNotesElementInSource(source, index) {
|
|
483
|
+
if (!Number.isInteger(index) || index < 0) return null;
|
|
484
|
+
const found = findNotesArray(source);
|
|
485
|
+
if (found === "invalid") return null;
|
|
486
|
+
if (found === null) return source;
|
|
487
|
+
const { arrayStart, arrayEnd, elementTexts } = found;
|
|
488
|
+
if (index >= elementTexts.length) return source;
|
|
489
|
+
const next = elementTexts.slice();
|
|
490
|
+
next.splice(index, 1);
|
|
491
|
+
return rebuildNotesArray(source, arrayStart, arrayEnd, next);
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Duplicate the note aligned with the page at `index`, inserting the copy right
|
|
495
|
+
* after it so the `notes` export stays index-aligned with `export default [...]`
|
|
496
|
+
* after a page duplication. Mirrors {@link duplicatePageInDefaultExportInSource}.
|
|
497
|
+
*
|
|
498
|
+
* Returns the rewritten source, the original source if no `notes` export exists
|
|
499
|
+
* or the index falls past the recorded notes (the new slot and everything after
|
|
500
|
+
* it are absent, so nothing shifts), or `null` if the shape is too surprising.
|
|
501
|
+
*/
|
|
502
|
+
function duplicateNotesElementInSource(source, index) {
|
|
503
|
+
if (!Number.isInteger(index) || index < 0) return null;
|
|
504
|
+
const found = findNotesArray(source);
|
|
505
|
+
if (found === "invalid") return null;
|
|
506
|
+
if (found === null) return source;
|
|
507
|
+
const { arrayStart, arrayEnd, elementTexts } = found;
|
|
508
|
+
if (index >= elementTexts.length) return source;
|
|
509
|
+
const next = elementTexts.slice();
|
|
510
|
+
next.splice(index + 1, 0, next[index]);
|
|
511
|
+
return rebuildNotesArray(source, arrayStart, arrayEnd, next);
|
|
512
|
+
}
|
|
513
|
+
function rebuildNotesArray(source, arrayStart, arrayEnd, elements) {
|
|
514
|
+
const trimmed = elements.slice();
|
|
515
|
+
while (trimmed.length > 0 && trimmed[trimmed.length - 1] === "undefined") trimmed.pop();
|
|
516
|
+
const replacement = trimmed.length === 0 ? "[]" : `[\n${trimmed.map((s) => ` ${s},`).join("\n")}\n]`;
|
|
517
|
+
if (replacement === source.slice(arrayStart, arrayEnd)) return source;
|
|
518
|
+
return source.slice(0, arrayStart) + replacement + source.slice(arrayEnd);
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Remove the element at `index` from `export default [...]`.
|
|
522
|
+
*
|
|
523
|
+
* Preserves the source slice of every other element, dropping the separator
|
|
524
|
+
* immediately following the removed element (or the preceding one when the
|
|
525
|
+
* removed element is the last). Returns `null` when the default export isn't
|
|
526
|
+
* an array literal or `index` is out of range.
|
|
527
|
+
*/
|
|
528
|
+
function removePageFromDefaultExportInSource(source, index) {
|
|
529
|
+
const found = findDefaultExportArray(source);
|
|
530
|
+
if (!found) return null;
|
|
531
|
+
const { elements, arrayStart, arrayEnd } = found;
|
|
532
|
+
const n = elements.length;
|
|
533
|
+
if (!Number.isInteger(index) || index < 0 || index >= n) return null;
|
|
534
|
+
if (n === 1) return `${source.slice(0, arrayStart)}[]${source.slice(arrayEnd)}`;
|
|
535
|
+
const prefix = source.slice(arrayStart, elements[0].start);
|
|
536
|
+
const suffix = source.slice(elements[n - 1].end, arrayEnd);
|
|
537
|
+
const separators = [];
|
|
538
|
+
for (let i = 0; i < n - 1; i++) separators.push(source.slice(elements[i].end, elements[i + 1].start));
|
|
539
|
+
const elementText = elements.map((el) => source.slice(el.start, el.end));
|
|
540
|
+
const keptElements = [];
|
|
541
|
+
const keptSeparators = [];
|
|
542
|
+
for (let i = 0; i < n; i++) {
|
|
543
|
+
if (i === index) continue;
|
|
544
|
+
keptElements.push(elementText[i]);
|
|
545
|
+
}
|
|
546
|
+
for (let i = 0; i < n - 1; i++) {
|
|
547
|
+
if (index === n - 1 ? i === n - 2 : i === index) continue;
|
|
548
|
+
keptSeparators.push(separators[i]);
|
|
549
|
+
}
|
|
550
|
+
let rebuilt = prefix + keptElements[0];
|
|
551
|
+
for (let i = 1; i < keptElements.length; i++) rebuilt += keptSeparators[i - 1] + keptElements[i];
|
|
552
|
+
rebuilt += suffix;
|
|
553
|
+
return source.slice(0, arrayStart) + rebuilt + source.slice(arrayEnd);
|
|
554
|
+
}
|
|
555
|
+
function chooseInsertSeparator(prefix, existingSeparators) {
|
|
556
|
+
const sample = existingSeparators.find((s) => s.includes(","));
|
|
557
|
+
if (sample) return sample;
|
|
558
|
+
if (prefix.includes("\n")) {
|
|
559
|
+
const m = prefix.match(/\n([ \t]*)$/);
|
|
560
|
+
return `,\n${m ? m[1] : " "}`;
|
|
561
|
+
}
|
|
562
|
+
return ", ";
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Duplicate the element at `index` in `export default [...]`, inserting the
|
|
566
|
+
* copy immediately after the original. Reuses an existing inter-element
|
|
567
|
+
* separator when one is available so the cloned entry matches the surrounding
|
|
568
|
+
* indentation. Returns `null` when the default export isn't an array literal
|
|
569
|
+
* or `index` is out of range.
|
|
570
|
+
*/
|
|
571
|
+
function duplicatePageInDefaultExportInSource(source, index) {
|
|
572
|
+
const found = findDefaultExportArray(source);
|
|
573
|
+
if (!found) return null;
|
|
574
|
+
const { elements, arrayStart, arrayEnd } = found;
|
|
575
|
+
const n = elements.length;
|
|
576
|
+
if (!Number.isInteger(index) || index < 0 || index >= n) return null;
|
|
577
|
+
const prefix = source.slice(arrayStart, elements[0].start);
|
|
578
|
+
const suffix = source.slice(elements[n - 1].end, arrayEnd);
|
|
579
|
+
const separators = [];
|
|
580
|
+
for (let i = 0; i < n - 1; i++) separators.push(source.slice(elements[i].end, elements[i + 1].start));
|
|
581
|
+
const elementText = elements.map((el) => source.slice(el.start, el.end));
|
|
582
|
+
const insertSep = chooseInsertSeparator(prefix, separators);
|
|
583
|
+
const newElements = [];
|
|
584
|
+
const newSeparators = [];
|
|
585
|
+
for (let i = 0; i < n; i++) {
|
|
586
|
+
newElements.push(elementText[i]);
|
|
587
|
+
if (i === index) {
|
|
588
|
+
newElements.push(elementText[i]);
|
|
589
|
+
newSeparators.push(insertSep);
|
|
590
|
+
}
|
|
591
|
+
if (i < n - 1) newSeparators.push(separators[i]);
|
|
592
|
+
}
|
|
593
|
+
let rebuilt = prefix + newElements[0];
|
|
594
|
+
for (let i = 1; i < newElements.length; i++) rebuilt += newSeparators[i - 1] + newElements[i];
|
|
595
|
+
rebuilt += suffix;
|
|
596
|
+
return source.slice(0, arrayStart) + rebuilt + source.slice(arrayEnd);
|
|
597
|
+
}
|
|
598
|
+
//#endregion
|
|
599
|
+
//#region src/files/short-id.ts
|
|
600
|
+
function shortId(prefix) {
|
|
601
|
+
return `${prefix}-${randomUUID().replace(/-/g, "").slice(0, 8)}`;
|
|
602
|
+
}
|
|
603
|
+
//#endregion
|
|
604
|
+
//#region src/files/folders.ts
|
|
605
|
+
const FOLDER_ID_RE = /^f-[a-f0-9]{8}$/;
|
|
606
|
+
const COLOR_RE = /^#[0-9a-fA-F]{6}$/;
|
|
607
|
+
function foldersManifestPath(slidesRoot) {
|
|
608
|
+
return path.join(slidesRoot, ".folders.json");
|
|
609
|
+
}
|
|
610
|
+
function emptyManifest() {
|
|
611
|
+
return {
|
|
612
|
+
folders: [],
|
|
613
|
+
assignments: {}
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
async function readManifest(file) {
|
|
617
|
+
try {
|
|
618
|
+
const raw = await fs.readFile(file, "utf8");
|
|
619
|
+
const parsed = JSON.parse(raw);
|
|
620
|
+
return {
|
|
621
|
+
folders: Array.isArray(parsed.folders) ? parsed.folders : [],
|
|
622
|
+
assignments: parsed.assignments && typeof parsed.assignments === "object" ? parsed.assignments : {}
|
|
623
|
+
};
|
|
624
|
+
} catch (err) {
|
|
625
|
+
if (err.code === "ENOENT") return emptyManifest();
|
|
626
|
+
throw err;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
async function writeManifest(file, manifest) {
|
|
630
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
631
|
+
await fs.writeFile(file, `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
|
|
632
|
+
}
|
|
633
|
+
function newFolderId() {
|
|
634
|
+
return shortId("f");
|
|
635
|
+
}
|
|
636
|
+
function validateName(v) {
|
|
637
|
+
if (typeof v !== "string") return null;
|
|
638
|
+
const trimmed = v.trim();
|
|
639
|
+
if (trimmed.length < 1 || trimmed.length > 40) return null;
|
|
640
|
+
return trimmed;
|
|
641
|
+
}
|
|
642
|
+
function validateReorder(v, current) {
|
|
643
|
+
if (!Array.isArray(v) || v.length !== current.length) return null;
|
|
644
|
+
const known = new Set(current.map((f) => f.id));
|
|
645
|
+
const seen = /* @__PURE__ */ new Set();
|
|
646
|
+
const out = [];
|
|
647
|
+
for (const id of v) {
|
|
648
|
+
if (typeof id !== "string" || !FOLDER_ID_RE.test(id)) return null;
|
|
649
|
+
if (!known.has(id) || seen.has(id)) return null;
|
|
650
|
+
seen.add(id);
|
|
651
|
+
out.push(id);
|
|
652
|
+
}
|
|
653
|
+
return out;
|
|
654
|
+
}
|
|
655
|
+
function validateIcon(v) {
|
|
656
|
+
if (!v || typeof v !== "object") return null;
|
|
657
|
+
const icon = v;
|
|
658
|
+
if (icon.type === "emoji") {
|
|
659
|
+
if (typeof icon.value !== "string") return null;
|
|
660
|
+
if (icon.value.length < 1 || icon.value.length > 8) return null;
|
|
661
|
+
return {
|
|
662
|
+
type: "emoji",
|
|
663
|
+
value: icon.value
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
if (icon.type === "color") {
|
|
667
|
+
if (typeof icon.value !== "string" || !COLOR_RE.test(icon.value)) return null;
|
|
668
|
+
return {
|
|
669
|
+
type: "color",
|
|
670
|
+
value: icon.value
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
return null;
|
|
674
|
+
}
|
|
675
|
+
//#endregion
|
|
676
|
+
//#region src/vite/routes/context.ts
|
|
677
|
+
function makeContext(opts) {
|
|
678
|
+
const userCwd = opts.userCwd;
|
|
679
|
+
const slidesDir = opts.slidesDir ?? "slides";
|
|
680
|
+
const assetsDir = opts.assetsDir ?? "assets";
|
|
681
|
+
const slidesRoot = path.resolve(userCwd, slidesDir);
|
|
682
|
+
return {
|
|
683
|
+
userCwd,
|
|
684
|
+
slidesDir,
|
|
685
|
+
slidesRoot,
|
|
686
|
+
globalAssetsRoot: path.resolve(userCwd, assetsDir),
|
|
687
|
+
manifestPath: foldersManifestPath(slidesRoot),
|
|
688
|
+
coreVersion: opts.coreVersion
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
async function readBody(req) {
|
|
692
|
+
return await new Promise((resolve, reject) => {
|
|
693
|
+
const chunks = [];
|
|
694
|
+
req.on("data", (c) => chunks.push(c));
|
|
695
|
+
req.on("end", () => {
|
|
696
|
+
const raw = Buffer.concat(chunks).toString("utf8");
|
|
697
|
+
if (!raw) return resolve({});
|
|
698
|
+
try {
|
|
699
|
+
resolve(JSON.parse(raw));
|
|
700
|
+
} catch (e) {
|
|
701
|
+
reject(e);
|
|
702
|
+
}
|
|
703
|
+
});
|
|
704
|
+
req.on("error", reject);
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
function json(res, status, body) {
|
|
708
|
+
res.statusCode = status;
|
|
709
|
+
res.setHeader("content-type", "application/json");
|
|
710
|
+
res.end(JSON.stringify(body));
|
|
711
|
+
}
|
|
712
|
+
function resolveSlideEntryPath(ctx, slideId) {
|
|
713
|
+
return resolveSlideEntry(ctx.slidesRoot, slideId);
|
|
714
|
+
}
|
|
715
|
+
async function readSlideSource(file) {
|
|
716
|
+
try {
|
|
717
|
+
return await fs.readFile(file, "utf8");
|
|
718
|
+
} catch (err) {
|
|
719
|
+
if (err.code === "ENOENT") return null;
|
|
720
|
+
throw err;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
//#endregion
|
|
724
|
+
//#region src/vite/routes/update.ts
|
|
725
|
+
const PKG = "@open-slide/core";
|
|
726
|
+
const CACHE_TTL_MS = 6e5;
|
|
727
|
+
const COMMAND_TIMEOUT_MS = 3e5;
|
|
728
|
+
let cache = null;
|
|
729
|
+
let updateInFlight = null;
|
|
730
|
+
function parseSemver(v) {
|
|
731
|
+
const m = /^v?(\d+)\.(\d+)\.(\d+)/.exec(v.trim());
|
|
732
|
+
if (!m) return null;
|
|
733
|
+
return [
|
|
734
|
+
Number(m[1]),
|
|
735
|
+
Number(m[2]),
|
|
736
|
+
Number(m[3])
|
|
737
|
+
];
|
|
738
|
+
}
|
|
739
|
+
function isOutdated(current, latest) {
|
|
740
|
+
const a = parseSemver(current);
|
|
741
|
+
const b = parseSemver(latest);
|
|
742
|
+
if (!a || !b) return false;
|
|
743
|
+
for (let i = 0; i < 3; i++) {
|
|
744
|
+
if (b[i] > a[i]) return true;
|
|
745
|
+
if (b[i] < a[i]) return false;
|
|
746
|
+
}
|
|
747
|
+
return false;
|
|
748
|
+
}
|
|
749
|
+
async function fetchLatest(now) {
|
|
750
|
+
if (cache && now - cache.at < CACHE_TTL_MS) return cache.latest;
|
|
751
|
+
try {
|
|
752
|
+
const res = await fetch(`https://registry.npmjs.org/${PKG}/latest`, {
|
|
753
|
+
signal: AbortSignal.timeout(3e3),
|
|
754
|
+
headers: { accept: "application/json" }
|
|
755
|
+
});
|
|
756
|
+
if (!res.ok) throw new Error(`registry ${res.status}`);
|
|
757
|
+
const body = await res.json();
|
|
758
|
+
const latest = typeof body.version === "string" ? body.version : null;
|
|
759
|
+
cache = {
|
|
760
|
+
at: now,
|
|
761
|
+
latest
|
|
762
|
+
};
|
|
763
|
+
return latest;
|
|
764
|
+
} catch {
|
|
765
|
+
return cache?.latest ?? null;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
async function fileExists(file) {
|
|
769
|
+
try {
|
|
770
|
+
await fs.access(file);
|
|
771
|
+
return true;
|
|
772
|
+
} catch {
|
|
773
|
+
return false;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
const LOCKFILES = [
|
|
777
|
+
["pnpm-lock.yaml", "pnpm"],
|
|
778
|
+
["yarn.lock", "yarn"],
|
|
779
|
+
["bun.lockb", "bun"],
|
|
780
|
+
["bun.lock", "bun"],
|
|
781
|
+
["package-lock.json", "npm"]
|
|
782
|
+
];
|
|
783
|
+
async function detectPackageManager(cwd) {
|
|
784
|
+
const ua = process.env.npm_config_user_agent ?? "";
|
|
785
|
+
if (ua.startsWith("pnpm")) return "pnpm";
|
|
786
|
+
if (ua.startsWith("yarn")) return "yarn";
|
|
787
|
+
if (ua.startsWith("bun")) return "bun";
|
|
788
|
+
if (ua.startsWith("npm")) return "npm";
|
|
789
|
+
let dir = cwd;
|
|
790
|
+
while (true) {
|
|
791
|
+
for (const [lockfile, pm] of LOCKFILES) if (await fileExists(path.join(dir, lockfile))) return pm;
|
|
792
|
+
const parent = path.dirname(dir);
|
|
793
|
+
if (parent === dir) return "npm";
|
|
794
|
+
dir = parent;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
function updateCommandFor(packageManager) {
|
|
798
|
+
switch (packageManager) {
|
|
799
|
+
case "pnpm": return {
|
|
800
|
+
cmd: "pnpm",
|
|
801
|
+
args: ["add", `${PKG}@latest`]
|
|
802
|
+
};
|
|
803
|
+
case "yarn": return {
|
|
804
|
+
cmd: "yarn",
|
|
805
|
+
args: ["add", `${PKG}@latest`]
|
|
806
|
+
};
|
|
807
|
+
case "bun": return {
|
|
808
|
+
cmd: "bun",
|
|
809
|
+
args: ["add", `${PKG}@latest`]
|
|
810
|
+
};
|
|
811
|
+
case "npm": return {
|
|
812
|
+
cmd: "npm",
|
|
813
|
+
args: ["install", `${PKG}@latest`]
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function localOpenSlideCommand(cwd) {
|
|
818
|
+
const bin = process.platform === "win32" ? "open-slide.cmd" : "open-slide";
|
|
819
|
+
return {
|
|
820
|
+
cmd: path.join(cwd, "node_modules", ".bin", bin),
|
|
821
|
+
args: ["sync:skills"]
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
function formatCommand(spec) {
|
|
825
|
+
return [spec.cmd, ...spec.args].join(" ");
|
|
826
|
+
}
|
|
827
|
+
async function runCommand(spec, cwd) {
|
|
828
|
+
await new Promise((resolve, reject) => {
|
|
829
|
+
const child = spawn(spec.cmd, spec.args, {
|
|
830
|
+
cwd,
|
|
831
|
+
env: process.env,
|
|
832
|
+
shell: process.platform === "win32",
|
|
833
|
+
stdio: [
|
|
834
|
+
"ignore",
|
|
835
|
+
"ignore",
|
|
836
|
+
"pipe"
|
|
837
|
+
]
|
|
838
|
+
});
|
|
839
|
+
let stderr = "";
|
|
840
|
+
const timer = setTimeout(() => {
|
|
841
|
+
child.kill();
|
|
842
|
+
reject(/* @__PURE__ */ new Error(`${formatCommand(spec)} timed out`));
|
|
843
|
+
}, COMMAND_TIMEOUT_MS);
|
|
844
|
+
child.stderr.on("data", (chunk) => {
|
|
845
|
+
stderr += chunk.toString("utf8");
|
|
846
|
+
if (stderr.length > 2e3) stderr = stderr.slice(-2e3);
|
|
847
|
+
});
|
|
848
|
+
child.on("error", (err) => {
|
|
849
|
+
clearTimeout(timer);
|
|
850
|
+
reject(err);
|
|
851
|
+
});
|
|
852
|
+
child.on("close", (code) => {
|
|
853
|
+
clearTimeout(timer);
|
|
854
|
+
if (code === 0) {
|
|
855
|
+
resolve();
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
const detail = stderr.trim();
|
|
859
|
+
reject(new Error(detail || `${formatCommand(spec)} exited with code ${code ?? "unknown"}`));
|
|
860
|
+
});
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
async function updatePackage(ctx) {
|
|
864
|
+
const packageManager = await detectPackageManager(ctx.userCwd);
|
|
865
|
+
const updateCommand = updateCommandFor(packageManager);
|
|
866
|
+
const syncCommand = localOpenSlideCommand(ctx.userCwd);
|
|
867
|
+
await runCommand(updateCommand, ctx.userCwd);
|
|
868
|
+
await runCommand(syncCommand, ctx.userCwd);
|
|
869
|
+
cache = null;
|
|
870
|
+
const latest = await fetchLatest(Date.now());
|
|
871
|
+
return {
|
|
872
|
+
packageManager,
|
|
873
|
+
command: `${formatCommand(updateCommand)} && open-slide sync:skills`,
|
|
874
|
+
latest,
|
|
875
|
+
message: "Updated @open-slide/core and synced skills."
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
function registerUpdateRoutes(server, ctx) {
|
|
879
|
+
server.middlewares.use("/__update-check", async (req, res, next) => {
|
|
880
|
+
if ((req.method ?? "GET") !== "GET") return next();
|
|
881
|
+
const latest = await fetchLatest(Date.now());
|
|
882
|
+
const result = {
|
|
883
|
+
current: ctx.coreVersion,
|
|
884
|
+
latest,
|
|
885
|
+
outdated: latest ? isOutdated(ctx.coreVersion, latest) : false
|
|
886
|
+
};
|
|
887
|
+
res.setHeader("cache-control", "no-store");
|
|
888
|
+
json(res, 200, result);
|
|
889
|
+
});
|
|
890
|
+
server.middlewares.use("/__update-package", async (req, res, next) => {
|
|
891
|
+
if ((req.method ?? "GET") !== "POST") return next();
|
|
892
|
+
const guard = validateMutationRequest(req);
|
|
893
|
+
if (!guard.ok) return json(res, guard.status, { error: guard.error });
|
|
894
|
+
try {
|
|
895
|
+
updateInFlight ??= updatePackage(ctx).finally(() => {
|
|
896
|
+
updateInFlight = null;
|
|
897
|
+
});
|
|
898
|
+
json(res, 200, await updateInFlight);
|
|
899
|
+
} catch (err) {
|
|
900
|
+
json(res, 500, { error: err instanceof Error ? err.message : "update failed" });
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
//#endregion
|
|
905
|
+
export { reorderDefaultExportPagesInSource as C, updateMetaTitleInSource as D, rmSlideDir as E, validateSlideName as O, removePageFromDefaultExportInSource as S, resolveSlideEntry as T, SLIDE_ID_RE as _, readBody as a, duplicateSlideDir as b, FOLDER_ID_RE as c, readManifest as d, validateIcon as f, shortId as g, writeManifest as h, makeContext as i, validateMutationRequest as k, foldersManifestPath as l, validateReorder as m, registerUpdateRoutes as n, readSlideSource as o, validateName as p, json as r, resolveSlideEntryPath as s, detectPackageManager as t, newFolderId as u, duplicateNotesElementInSource as v, reorderNotesArrayInSource as w, removeNotesElementInSource as x, duplicatePageInDefaultExportInSource as y };
|