@open-press/core 0.3.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/LICENSE +21 -0
- package/README.md +36 -0
- package/engine/chrome-pdf.d.mts +34 -0
- package/engine/chrome-pdf.mjs +344 -0
- package/engine/cli.mjs +93 -0
- package/engine/commands/_shared.mjs +170 -0
- package/engine/commands/deploy.mjs +31 -0
- package/engine/commands/dev.mjs +26 -0
- package/engine/commands/export.mjs +8 -0
- package/engine/commands/init.mjs +24 -0
- package/engine/commands/inspect.mjs +35 -0
- package/engine/commands/migrate-to-react.mjs +27 -0
- package/engine/commands/pdf.mjs +26 -0
- package/engine/commands/preview.mjs +26 -0
- package/engine/commands/render.mjs +17 -0
- package/engine/commands/replace.mjs +41 -0
- package/engine/commands/search.mjs +33 -0
- package/engine/commands/typecheck.mjs +5 -0
- package/engine/commands/validate.mjs +17 -0
- package/engine/config.d.mts +40 -0
- package/engine/config.mjs +160 -0
- package/engine/deploy-sync.mjs +15 -0
- package/engine/document-export.mjs +15 -0
- package/engine/file-utils.mjs +106 -0
- package/engine/fonts.mjs +62 -0
- package/engine/init.mjs +90 -0
- package/engine/inspection.mjs +348 -0
- package/engine/issue-report.mjs +44 -0
- package/engine/katex-assets.mjs +45 -0
- package/engine/page-block.mjs +30 -0
- package/engine/page-renderer.mjs +217 -0
- package/engine/pdf-media.mjs +45 -0
- package/engine/public-assets.mjs +19 -0
- package/engine/react/chapter-css.mjs +53 -0
- package/engine/react/comment-endpoint.d.mts +11 -0
- package/engine/react/comment-endpoint.mjs +128 -0
- package/engine/react/comment-marker.mjs +306 -0
- package/engine/react/document-entry.mjs +253 -0
- package/engine/react/document-export.mjs +392 -0
- package/engine/react/mdx-compile.mjs +295 -0
- package/engine/react/measurement-css.mjs +44 -0
- package/engine/react/migrate-to-react.mjs +355 -0
- package/engine/react/pagination-constants.mjs +3 -0
- package/engine/react/pagination.mjs +121 -0
- package/engine/react/project-asset-endpoint.d.mts +10 -0
- package/engine/react/project-asset-endpoint.mjs +379 -0
- package/engine/react/workspace-discovery.mjs +156 -0
- package/engine/source-text-tools.mjs +280 -0
- package/engine/source-workspace.mjs +76 -0
- package/engine/static-server.mjs +493 -0
- package/engine/validation.mjs +172 -0
- package/index.html +13 -0
- package/package.json +86 -0
- package/src/openpress/App.tsx +127 -0
- package/src/openpress/composerMentions.ts +188 -0
- package/src/openpress/core/basePages.tsx +87 -0
- package/src/openpress/core/index.tsx +20 -0
- package/src/openpress/core/types.ts +71 -0
- package/src/openpress/frameScheduler.ts +32 -0
- package/src/openpress/indexes.ts +329 -0
- package/src/openpress/inspector.ts +282 -0
- package/src/openpress/pageRoute.ts +21 -0
- package/src/openpress/pagination.ts +845 -0
- package/src/openpress/projectIdentity.ts +15 -0
- package/src/openpress/projectSources.ts +24 -0
- package/src/openpress/projectWorkspace.tsx +919 -0
- package/src/openpress/publicPage.tsx +469 -0
- package/src/openpress/reactDocumentMetadata.ts +41 -0
- package/src/openpress/readerPageRegistry.ts +41 -0
- package/src/openpress/readerRuntime.ts +230 -0
- package/src/openpress/readerScroll.ts +92 -0
- package/src/openpress/readerState.ts +15 -0
- package/src/openpress/renderer.tsx +91 -0
- package/src/openpress/runtimeMode.ts +22 -0
- package/src/openpress/types.ts +112 -0
- package/src/openpress/workbench.tsx +1299 -0
- package/src/openpress/workbenchPanels.tsx +122 -0
- package/src/openpress/workbenchTypes.ts +4 -0
- package/src/styles/openpress/app-shell.css +251 -0
- package/src/styles/openpress/media-workspace.css +230 -0
- package/src/styles/openpress/print-route.css +186 -0
- package/src/styles/openpress/project-workspace.css +1318 -0
- package/src/styles/openpress/public-viewer.css +983 -0
- package/src/styles/openpress/reader-runtime.css +792 -0
- package/src/styles/openpress/responsive.css +384 -0
- package/src/styles/openpress/workbench-panels.css +558 -0
- package/src/styles/openpress/workbench.css +720 -0
- package/src/styles/openpress.css +14 -0
- package/tsconfig.json +37 -0
- package/vite.config.ts +512 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 quan0715
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @open-press/core
|
|
2
|
+
|
|
3
|
+
Framework runtime, CLI engine, and page primitives for [open-press](https://github.com/quan0715/open-press) — an AI-first fixed-layout document workspace.
|
|
4
|
+
|
|
5
|
+
Most users do **not** install this package directly. Instead, scaffold a workspace with the CLI:
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npx @open-press/cli init my-doc --pack editorial-monograph
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The scaffolded workspace contains a snapshot of this package.
|
|
12
|
+
|
|
13
|
+
## Direct use
|
|
14
|
+
|
|
15
|
+
If you want the runtime primitives in an existing project:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @open-press/core
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
import {
|
|
23
|
+
BasePage,
|
|
24
|
+
BaseCoverPage,
|
|
25
|
+
BaseTocPage,
|
|
26
|
+
BaseBackCoverPage,
|
|
27
|
+
BaseFigure,
|
|
28
|
+
BaseCallout,
|
|
29
|
+
} from "@open-press/core";
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The CLI bin (`open-press`) supports dev / build / preview / validate / pdf / deploy / export commands. It requires a workspace with `openpress.config.mjs` and the surrounding framework files (which the scaffolder installs).
|
|
33
|
+
|
|
34
|
+
## License
|
|
35
|
+
|
|
36
|
+
MIT — see [LICENSE](https://github.com/quan0715/open-press/blob/main/LICENSE).
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ChildProcess } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
export interface ChromeDevToolsClient {
|
|
4
|
+
send(method: string, params?: Record<string, unknown>): Promise<any>;
|
|
5
|
+
close(): void;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface PrintUrlToPdfOptions {
|
|
9
|
+
root: string;
|
|
10
|
+
url: string;
|
|
11
|
+
outPath: string;
|
|
12
|
+
chrome?: string;
|
|
13
|
+
waitForReady?: (client: ChromeDevToolsClient) => Promise<unknown>;
|
|
14
|
+
printOptions?: Record<string, unknown>;
|
|
15
|
+
debuggingPortBase?: number;
|
|
16
|
+
debuggingPortRange?: number;
|
|
17
|
+
profilePrefix?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface EvaluateUrlWithChromeOptions {
|
|
21
|
+
root: string;
|
|
22
|
+
url: string;
|
|
23
|
+
chrome?: string;
|
|
24
|
+
evaluate: (client: ChromeDevToolsClient) => Promise<unknown>;
|
|
25
|
+
emulatedMedia?: "screen" | "print" | "none";
|
|
26
|
+
debuggingPortBase?: number;
|
|
27
|
+
debuggingPortRange?: number;
|
|
28
|
+
profilePrefix?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function printUrlToPdf(options: PrintUrlToPdfOptions): Promise<unknown>;
|
|
32
|
+
export function evaluateUrlWithChrome(options: EvaluateUrlWithChromeOptions): Promise<unknown>;
|
|
33
|
+
export function waitForPrintReady(client: ChromeDevToolsClient): Promise<number>;
|
|
34
|
+
export function stopChildProcess(child: ChildProcess): Promise<void>;
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
|
|
6
|
+
const CHROME_CANDIDATE_PATHS = {
|
|
7
|
+
darwin: [
|
|
8
|
+
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
|
|
9
|
+
"/Applications/Google Chrome Beta.app/Contents/MacOS/Google Chrome Beta",
|
|
10
|
+
"/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
|
|
11
|
+
"/Applications/Chromium.app/Contents/MacOS/Chromium",
|
|
12
|
+
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
|
13
|
+
],
|
|
14
|
+
linux: [
|
|
15
|
+
"/usr/bin/google-chrome",
|
|
16
|
+
"/usr/bin/google-chrome-stable",
|
|
17
|
+
"/usr/bin/chromium",
|
|
18
|
+
"/usr/bin/chromium-browser",
|
|
19
|
+
"/snap/bin/chromium",
|
|
20
|
+
"/opt/google/chrome/chrome",
|
|
21
|
+
],
|
|
22
|
+
win32: [
|
|
23
|
+
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
|
|
24
|
+
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
|
|
25
|
+
],
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const CHROME_BIN_NAMES = {
|
|
29
|
+
darwin: [],
|
|
30
|
+
linux: ["google-chrome", "google-chrome-stable", "chromium", "chromium-browser"],
|
|
31
|
+
win32: ["chrome.exe"],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
let cachedChromePath = null;
|
|
35
|
+
|
|
36
|
+
function resolveChromePath() {
|
|
37
|
+
if (cachedChromePath) return cachedChromePath;
|
|
38
|
+
if (process.env.CHROME) {
|
|
39
|
+
cachedChromePath = process.env.CHROME;
|
|
40
|
+
return cachedChromePath;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const platform = process.platform;
|
|
44
|
+
const candidates = [...(CHROME_CANDIDATE_PATHS[platform] ?? [])];
|
|
45
|
+
|
|
46
|
+
if (platform === "win32") {
|
|
47
|
+
const roots = [process.env.PROGRAMFILES, process.env["PROGRAMFILES(X86)"], process.env.LOCALAPPDATA].filter(Boolean);
|
|
48
|
+
for (const root of roots) {
|
|
49
|
+
candidates.push(`${root}\\Google\\Chrome\\Application\\chrome.exe`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
for (const candidate of candidates) {
|
|
54
|
+
if (existsSync(candidate)) {
|
|
55
|
+
cachedChromePath = candidate;
|
|
56
|
+
return cachedChromePath;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const whichCommand = platform === "win32" ? "where" : "which";
|
|
61
|
+
for (const name of CHROME_BIN_NAMES[platform] ?? []) {
|
|
62
|
+
try {
|
|
63
|
+
const result = spawnSync(whichCommand, [name], { encoding: "utf8" });
|
|
64
|
+
const output = result.stdout?.trim().split(/\r?\n/)[0];
|
|
65
|
+
if (result.status === 0 && output && existsSync(output)) {
|
|
66
|
+
cachedChromePath = output;
|
|
67
|
+
return cachedChromePath;
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
// continue
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const lines = [
|
|
75
|
+
`Cannot locate a Chrome / Chromium executable on ${platform}.`,
|
|
76
|
+
`Set the CHROME environment variable to your Chrome binary path, or install Chrome at one of:`,
|
|
77
|
+
...candidates.map((p) => ` - ${p}`),
|
|
78
|
+
];
|
|
79
|
+
throw new Error(lines.join("\n"));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const DEFAULT_PRINT_OPTIONS = {
|
|
83
|
+
printBackground: true,
|
|
84
|
+
displayHeaderFooter: false,
|
|
85
|
+
preferCSSPageSize: true,
|
|
86
|
+
paperWidth: 8.2677,
|
|
87
|
+
paperHeight: 11.6929,
|
|
88
|
+
marginTop: 0,
|
|
89
|
+
marginRight: 0,
|
|
90
|
+
marginBottom: 0,
|
|
91
|
+
marginLeft: 0,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export async function printUrlToPdf({
|
|
95
|
+
root,
|
|
96
|
+
url,
|
|
97
|
+
outPath,
|
|
98
|
+
chrome,
|
|
99
|
+
waitForReady = waitForPrintReady,
|
|
100
|
+
printOptions = {},
|
|
101
|
+
debuggingPortBase = 9600,
|
|
102
|
+
debuggingPortRange = 300,
|
|
103
|
+
profilePrefix = "chrome-pdf",
|
|
104
|
+
}) {
|
|
105
|
+
chrome ??= resolveChromePath();
|
|
106
|
+
await fs.mkdir(path.dirname(outPath), { recursive: true });
|
|
107
|
+
|
|
108
|
+
const debuggingPort = String(debuggingPortBase + Math.floor(Math.random() * debuggingPortRange));
|
|
109
|
+
const profileDir = path.join(root, ".openpress", "tmp", `${profilePrefix}-${process.pid}-${Date.now()}`);
|
|
110
|
+
await fs.mkdir(profileDir, { recursive: true });
|
|
111
|
+
|
|
112
|
+
const child = spawn(
|
|
113
|
+
chrome,
|
|
114
|
+
[
|
|
115
|
+
"--headless=new",
|
|
116
|
+
"--disable-gpu",
|
|
117
|
+
"--no-sandbox",
|
|
118
|
+
`--remote-debugging-port=${debuggingPort}`,
|
|
119
|
+
`--user-data-dir=${profileDir}`,
|
|
120
|
+
"about:blank",
|
|
121
|
+
],
|
|
122
|
+
{ cwd: root, stdio: ["ignore", "pipe", "pipe"] },
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
const tab = await waitForChromeTab(debuggingPort);
|
|
127
|
+
const client = await connectChromeDevTools(tab.webSocketDebuggerUrl);
|
|
128
|
+
try {
|
|
129
|
+
await client.send("Page.enable");
|
|
130
|
+
await client.send("Runtime.enable");
|
|
131
|
+
await client.send("Emulation.setEmulatedMedia", { media: "print" });
|
|
132
|
+
await client.send("Page.navigate", { url });
|
|
133
|
+
const readyResult = await waitForReady(client);
|
|
134
|
+
const result = await client.send("Page.printToPDF", {
|
|
135
|
+
...DEFAULT_PRINT_OPTIONS,
|
|
136
|
+
...printOptions,
|
|
137
|
+
});
|
|
138
|
+
await fs.writeFile(outPath, Buffer.from(String(result.data ?? ""), "base64"));
|
|
139
|
+
return readyResult;
|
|
140
|
+
} finally {
|
|
141
|
+
client.close();
|
|
142
|
+
}
|
|
143
|
+
} finally {
|
|
144
|
+
await stopChildProcess(child);
|
|
145
|
+
await cleanupChromeProfile(profileDir);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function evaluateUrlWithChrome({
|
|
150
|
+
root,
|
|
151
|
+
url,
|
|
152
|
+
chrome,
|
|
153
|
+
evaluate,
|
|
154
|
+
emulatedMedia,
|
|
155
|
+
debuggingPortBase = 9900,
|
|
156
|
+
debuggingPortRange = 300,
|
|
157
|
+
profilePrefix = "chrome-eval",
|
|
158
|
+
}) {
|
|
159
|
+
chrome ??= resolveChromePath();
|
|
160
|
+
|
|
161
|
+
const debuggingPort = String(debuggingPortBase + Math.floor(Math.random() * debuggingPortRange));
|
|
162
|
+
const profileDir = path.join(root, ".openpress", "tmp", `${profilePrefix}-${process.pid}-${Date.now()}`);
|
|
163
|
+
await fs.mkdir(profileDir, { recursive: true });
|
|
164
|
+
|
|
165
|
+
const child = spawn(
|
|
166
|
+
chrome,
|
|
167
|
+
[
|
|
168
|
+
"--headless=new",
|
|
169
|
+
"--disable-gpu",
|
|
170
|
+
"--no-sandbox",
|
|
171
|
+
`--remote-debugging-port=${debuggingPort}`,
|
|
172
|
+
`--user-data-dir=${profileDir}`,
|
|
173
|
+
"about:blank",
|
|
174
|
+
],
|
|
175
|
+
{ cwd: root, stdio: ["ignore", "pipe", "pipe"] },
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
const tab = await waitForChromeTab(debuggingPort);
|
|
180
|
+
const client = await connectChromeDevTools(tab.webSocketDebuggerUrl);
|
|
181
|
+
try {
|
|
182
|
+
await client.send("Page.enable");
|
|
183
|
+
await client.send("Runtime.enable");
|
|
184
|
+
if (emulatedMedia) {
|
|
185
|
+
await client.send("Emulation.setEmulatedMedia", { media: emulatedMedia });
|
|
186
|
+
}
|
|
187
|
+
await client.send("Page.navigate", { url });
|
|
188
|
+
return await evaluate(client);
|
|
189
|
+
} finally {
|
|
190
|
+
client.close();
|
|
191
|
+
}
|
|
192
|
+
} finally {
|
|
193
|
+
await stopChildProcess(child);
|
|
194
|
+
await cleanupChromeProfile(profileDir);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export async function waitForPrintReady(client) {
|
|
199
|
+
const deadline = Date.now() + 30000;
|
|
200
|
+
while (Date.now() < deadline) {
|
|
201
|
+
const result = await client.send("Runtime.evaluate", {
|
|
202
|
+
returnByValue: true,
|
|
203
|
+
awaitPromise: true,
|
|
204
|
+
expression: `Promise.resolve().then(async () => {
|
|
205
|
+
const root = document.querySelector('[data-openpress-print-document="true"]');
|
|
206
|
+
const ready = root?.getAttribute('data-openpress-pagination') === 'ready';
|
|
207
|
+
if (!ready) return 0;
|
|
208
|
+
|
|
209
|
+
await document.fonts?.ready;
|
|
210
|
+
await Promise.all(Array.from(document.images).map(async (img) => {
|
|
211
|
+
if (!img.complete) {
|
|
212
|
+
await new Promise((resolve) => {
|
|
213
|
+
const settle = () => {
|
|
214
|
+
img.removeEventListener('load', settle);
|
|
215
|
+
img.removeEventListener('error', settle);
|
|
216
|
+
resolve();
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
img.addEventListener('load', settle, { once: true });
|
|
220
|
+
img.addEventListener('error', settle, { once: true });
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
await img.decode?.().catch(() => undefined);
|
|
225
|
+
}));
|
|
226
|
+
|
|
227
|
+
await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)));
|
|
228
|
+
|
|
229
|
+
const pages = Array.from(document.querySelectorAll('.openpress-public-page > .openpress-html-page'));
|
|
230
|
+
const contentFitsPageBody = (body) => {
|
|
231
|
+
const bodyBottom = body.getBoundingClientRect().bottom;
|
|
232
|
+
const contentBottom = Array.from(body.children).reduce((bottom, child) => {
|
|
233
|
+
if (getComputedStyle(child).display === 'none') return bottom;
|
|
234
|
+
const marginBottom = Number.parseFloat(getComputedStyle(child).marginBottom) || 0;
|
|
235
|
+
return Math.max(bottom, child.getBoundingClientRect().bottom + marginBottom);
|
|
236
|
+
}, body.getBoundingClientRect().top);
|
|
237
|
+
return contentBottom <= bodyBottom + 1;
|
|
238
|
+
};
|
|
239
|
+
const bodyOverflowSafe = pages.every((page) => {
|
|
240
|
+
const body = page.querySelector('.page-body');
|
|
241
|
+
return !body || contentFitsPageBody(body);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
return pages.length > 0 && bodyOverflowSafe ? pages.length : 0;
|
|
245
|
+
})`,
|
|
246
|
+
});
|
|
247
|
+
const count = Number(result.result?.value ?? 0);
|
|
248
|
+
if (count > 0) return count;
|
|
249
|
+
await delay(100);
|
|
250
|
+
}
|
|
251
|
+
throw new Error("Timed out waiting for OpenPress pagination before PDF export.");
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export async function stopChildProcess(child) {
|
|
255
|
+
if (child.exitCode !== null || child.signalCode !== null) return;
|
|
256
|
+
child.kill();
|
|
257
|
+
const closed = await waitForChildClose(child, 2500);
|
|
258
|
+
if (closed || child.exitCode !== null || child.signalCode !== null) return;
|
|
259
|
+
child.kill("SIGKILL");
|
|
260
|
+
await waitForChildClose(child, 2500);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function waitForChromeTab(debuggingPort) {
|
|
264
|
+
const deadline = Date.now() + 10000;
|
|
265
|
+
let lastError;
|
|
266
|
+
while (Date.now() < deadline) {
|
|
267
|
+
try {
|
|
268
|
+
const response = await fetch(`http://127.0.0.1:${debuggingPort}/json/list`);
|
|
269
|
+
const tabs = await response.json();
|
|
270
|
+
const tab = tabs.find((item) => item.type === "page" && item.webSocketDebuggerUrl);
|
|
271
|
+
if (tab) return tab;
|
|
272
|
+
} catch (error) {
|
|
273
|
+
lastError = error;
|
|
274
|
+
}
|
|
275
|
+
await delay(100);
|
|
276
|
+
}
|
|
277
|
+
throw new Error(`Timed out waiting for Chrome DevTools${lastError ? `: ${lastError.message}` : ""}`);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async function connectChromeDevTools(webSocketDebuggerUrl) {
|
|
281
|
+
const socket = new WebSocket(webSocketDebuggerUrl);
|
|
282
|
+
const pending = new Map();
|
|
283
|
+
let nextId = 0;
|
|
284
|
+
|
|
285
|
+
await new Promise((resolve, reject) => {
|
|
286
|
+
socket.addEventListener("open", resolve, { once: true });
|
|
287
|
+
socket.addEventListener("error", reject, { once: true });
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
socket.addEventListener("message", (event) => {
|
|
291
|
+
const message = JSON.parse(String(event.data));
|
|
292
|
+
if (!message.id || !pending.has(message.id)) return;
|
|
293
|
+
const callbacks = pending.get(message.id);
|
|
294
|
+
pending.delete(message.id);
|
|
295
|
+
if (message.error) callbacks?.reject(new Error(message.error.message ?? "Chrome DevTools command failed."));
|
|
296
|
+
else callbacks?.resolve(message.result ?? {});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
return {
|
|
300
|
+
send(method, params = {}) {
|
|
301
|
+
return new Promise((resolve, reject) => {
|
|
302
|
+
const id = ++nextId;
|
|
303
|
+
pending.set(id, { resolve, reject });
|
|
304
|
+
socket.send(JSON.stringify({ id, method, params }));
|
|
305
|
+
});
|
|
306
|
+
},
|
|
307
|
+
close() {
|
|
308
|
+
socket.close();
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function waitForChildClose(child, timeoutMs) {
|
|
314
|
+
if (child.exitCode !== null || child.signalCode !== null) return Promise.resolve(true);
|
|
315
|
+
return new Promise((resolve) => {
|
|
316
|
+
let settled = false;
|
|
317
|
+
const finish = (closed) => {
|
|
318
|
+
if (settled) return;
|
|
319
|
+
settled = true;
|
|
320
|
+
clearTimeout(timer);
|
|
321
|
+
child.off("close", onClose);
|
|
322
|
+
child.off("error", onError);
|
|
323
|
+
resolve(closed);
|
|
324
|
+
};
|
|
325
|
+
const onClose = () => finish(true);
|
|
326
|
+
const onError = () => finish(true);
|
|
327
|
+
const timer = setTimeout(() => finish(false), timeoutMs);
|
|
328
|
+
child.once("close", onClose);
|
|
329
|
+
child.once("error", onError);
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async function cleanupChromeProfile(profileDir) {
|
|
334
|
+
try {
|
|
335
|
+
await fs.rm(profileDir, { recursive: true, force: true, maxRetries: 8, retryDelay: 150 });
|
|
336
|
+
} catch (error) {
|
|
337
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
338
|
+
console.warn(`Could not remove temporary Chrome profile ${profileDir}: ${message}`);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function delay(ms) {
|
|
343
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
344
|
+
}
|
package/engine/cli.mjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import * as deployCmd from "./commands/deploy.mjs";
|
|
4
|
+
import * as devCmd from "./commands/dev.mjs";
|
|
5
|
+
import * as exportCmd from "./commands/export.mjs";
|
|
6
|
+
import * as initCmd from "./commands/init.mjs";
|
|
7
|
+
import * as inspectCmd from "./commands/inspect.mjs";
|
|
8
|
+
import * as migrateToReactCmd from "./commands/migrate-to-react.mjs";
|
|
9
|
+
import * as pdfCmd from "./commands/pdf.mjs";
|
|
10
|
+
import * as previewCmd from "./commands/preview.mjs";
|
|
11
|
+
import * as replaceCmd from "./commands/replace.mjs";
|
|
12
|
+
import * as renderCmd from "./commands/render.mjs";
|
|
13
|
+
import * as searchCmd from "./commands/search.mjs";
|
|
14
|
+
import * as typecheckCmd from "./commands/typecheck.mjs";
|
|
15
|
+
import * as validateCmd from "./commands/validate.mjs";
|
|
16
|
+
import { parseOptions } from "./commands/_shared.mjs";
|
|
17
|
+
import { loadConfig } from "./config.mjs";
|
|
18
|
+
import { listStylePackSkills } from "./init.mjs";
|
|
19
|
+
import { discoverWorkspace } from "./validation.mjs";
|
|
20
|
+
|
|
21
|
+
const COMMANDS = {
|
|
22
|
+
init: initCmd,
|
|
23
|
+
"migrate-to-react": migrateToReactCmd,
|
|
24
|
+
validate: validateCmd,
|
|
25
|
+
inspect: inspectCmd,
|
|
26
|
+
search: searchCmd,
|
|
27
|
+
replace: replaceCmd,
|
|
28
|
+
export: exportCmd,
|
|
29
|
+
render: renderCmd,
|
|
30
|
+
dev: devCmd,
|
|
31
|
+
preview: previewCmd,
|
|
32
|
+
typecheck: typecheckCmd,
|
|
33
|
+
pdf: pdfCmd,
|
|
34
|
+
deploy: deployCmd,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const args = process.argv.slice(2);
|
|
38
|
+
const command = args.shift();
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const code = await main(command, args);
|
|
42
|
+
process.exitCode = code;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
45
|
+
process.exitCode = 1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function main(commandName, argv) {
|
|
49
|
+
if (!commandName || ["-h", "--help"].includes(commandName)) {
|
|
50
|
+
await printHelp();
|
|
51
|
+
return commandName ? 0 : 2;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const handler = COMMANDS[commandName];
|
|
55
|
+
if (!handler) {
|
|
56
|
+
console.error(`Unknown command: ${commandName}`);
|
|
57
|
+
await printHelp();
|
|
58
|
+
return 2;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (handler.needsWorkspace === false) {
|
|
62
|
+
return handler.run({ argv });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const options = parseOptions(argv);
|
|
66
|
+
const root = await discoverWorkspace(options.path ?? ".");
|
|
67
|
+
const config = await loadConfig(root);
|
|
68
|
+
return handler.run({ root, config, options, recurse: main });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function printHelp() {
|
|
72
|
+
const packs = await listStylePackSkills();
|
|
73
|
+
const skillList = packs.length ? packs.join(" | ") : "(none installed)";
|
|
74
|
+
console.log(`Usage: node engine/cli.mjs <command> [path] [options]
|
|
75
|
+
|
|
76
|
+
Commands:
|
|
77
|
+
init <target> [--skill <name>] [--force]
|
|
78
|
+
migrate-to-react [path] [--dry-run] [--force] [--json]
|
|
79
|
+
validate
|
|
80
|
+
inspect [--json] [--no-build] [--dry-run]
|
|
81
|
+
search [path] <query> [--json] [--scope content|all]
|
|
82
|
+
replace [path] <from> <to> [--json] [--apply] [--scope content|all]
|
|
83
|
+
export
|
|
84
|
+
render --renderer react [--dry-run]
|
|
85
|
+
preview --renderer react [--host 127.0.0.1] [--port 5173] [--no-build] [--dry-run]
|
|
86
|
+
dev --renderer react [--host 127.0.0.1] [--port 5173] [--no-build] [--dry-run]
|
|
87
|
+
typecheck
|
|
88
|
+
pdf [--output <outputDir>/<pdf.filename>] [--no-build] [--dry-run]
|
|
89
|
+
deploy --confirm [--dry-run]
|
|
90
|
+
|
|
91
|
+
Style packs available for \`init --skill\`: ${skillList}
|
|
92
|
+
`);
|
|
93
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { printUrlToPdf, stopChildProcess, waitForPrintReady } from "../chrome-pdf.mjs";
|
|
6
|
+
import { loadConfig, publicPdfHref } from "../config.mjs";
|
|
7
|
+
import { exportDocument } from "../document-export.mjs";
|
|
8
|
+
import { optimizePdfMediaForStaticRoot } from "../pdf-media.mjs";
|
|
9
|
+
|
|
10
|
+
const ENGINE_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
11
|
+
const STATIC_SERVER = path.join(ENGINE_DIR, "static-server.mjs");
|
|
12
|
+
|
|
13
|
+
export function parseOptions(argv) {
|
|
14
|
+
const options = {};
|
|
15
|
+
const positional = [];
|
|
16
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
17
|
+
const value = argv[i];
|
|
18
|
+
if (value === "--renderer") options.renderer = argv[++i];
|
|
19
|
+
else if (value === "--host") options.host = argv[++i];
|
|
20
|
+
else if (value === "--port") options.port = argv[++i];
|
|
21
|
+
else if (value === "--dry-run") options.dryRun = true;
|
|
22
|
+
else if (value === "--force") options.force = true;
|
|
23
|
+
else if (value === "--confirm") options.confirm = true;
|
|
24
|
+
else if (value === "--json") options.json = true;
|
|
25
|
+
else if (value === "--no-build") options.noBuild = true;
|
|
26
|
+
else if (value === "--apply") options.apply = true;
|
|
27
|
+
else if (value === "--include-code") options.includeCode = true;
|
|
28
|
+
else if (value === "--case-sensitive") options.caseSensitive = true;
|
|
29
|
+
else if (value === "--scope") options.scope = argv[++i];
|
|
30
|
+
else if (value === "--source") options.source = argv[++i];
|
|
31
|
+
else if (value === "--output") options.output = argv[++i];
|
|
32
|
+
else if (value.startsWith("--")) throw new Error(`Unknown option: ${value}`);
|
|
33
|
+
else positional.push(value);
|
|
34
|
+
}
|
|
35
|
+
options.path = positional[0];
|
|
36
|
+
options.positional = positional;
|
|
37
|
+
return options;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function parseInitOptions(argv) {
|
|
41
|
+
const options = { force: false };
|
|
42
|
+
const positional = [];
|
|
43
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
44
|
+
const value = argv[i];
|
|
45
|
+
if (value === "--skill") options.skill = argv[++i];
|
|
46
|
+
else if (value === "--force") options.force = true;
|
|
47
|
+
else if (value.startsWith("--")) throw new Error(`Unknown option: ${value}`);
|
|
48
|
+
else positional.push(value);
|
|
49
|
+
}
|
|
50
|
+
options.target = positional[0];
|
|
51
|
+
return options;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function formatDisplayPath(absolutePath) {
|
|
55
|
+
const relative = path.relative(process.cwd(), absolutePath);
|
|
56
|
+
if (!relative || relative.startsWith("..")) return absolutePath;
|
|
57
|
+
return relative;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function runCommand(commandName, commandArgs, cwd) {
|
|
61
|
+
const result = spawnSync(commandName, commandArgs, { cwd, stdio: "inherit" });
|
|
62
|
+
return result.status ?? 1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function buildReactStatic({ root, noBuild = false, recurse, silent = false }) {
|
|
66
|
+
if (noBuild) return 0;
|
|
67
|
+
if (!silent) {
|
|
68
|
+
return await recurse("render", [root, "--renderer", "react"]);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
await exportDocument(root);
|
|
72
|
+
const result = spawnSync("npx", ["vite", "build", "--config", "vite.config.ts"], {
|
|
73
|
+
cwd: root,
|
|
74
|
+
encoding: "utf8",
|
|
75
|
+
});
|
|
76
|
+
return result.status ?? 1;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function buildReactPdf({
|
|
80
|
+
root,
|
|
81
|
+
config,
|
|
82
|
+
outPath,
|
|
83
|
+
host = "127.0.0.1",
|
|
84
|
+
port = "5185",
|
|
85
|
+
noBuild = false,
|
|
86
|
+
recurse,
|
|
87
|
+
}) {
|
|
88
|
+
config ??= await loadConfig(root);
|
|
89
|
+
outPath ??= config.paths.pdf;
|
|
90
|
+
const renderCode = await buildReactStatic({ root, noBuild, recurse });
|
|
91
|
+
if (renderCode !== 0) throw new Error(`React render failed with exit code ${renderCode}`);
|
|
92
|
+
await optimizePdfMediaForStaticRoot(config.paths.outputDir);
|
|
93
|
+
await fs.mkdir(path.dirname(outPath), { recursive: true });
|
|
94
|
+
|
|
95
|
+
const server = await startStaticServer(root, config, host, port);
|
|
96
|
+
try {
|
|
97
|
+
const pageCount = await printUrlToPdf({
|
|
98
|
+
root,
|
|
99
|
+
url: `http://${host}:${port}/?print=1`,
|
|
100
|
+
outPath,
|
|
101
|
+
waitForReady: waitForPrintReady,
|
|
102
|
+
debuggingPortBase: 9300,
|
|
103
|
+
debuggingPortRange: 600,
|
|
104
|
+
profilePrefix: "chrome-pdf",
|
|
105
|
+
});
|
|
106
|
+
console.log(`${pageCount} OpenPress pages printed to PDF`);
|
|
107
|
+
} finally {
|
|
108
|
+
await stopChildProcess(server);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { pdfPath: outPath };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function startStaticServer(root, config, host, port) {
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
const child = spawn("node", [STATIC_SERVER, config.outputDir, "--host", host, "--port", port, "--workspace", "."], {
|
|
117
|
+
cwd: root,
|
|
118
|
+
shell: false,
|
|
119
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
120
|
+
});
|
|
121
|
+
let settled = false;
|
|
122
|
+
let stderr = "";
|
|
123
|
+
const timer = setTimeout(() => {
|
|
124
|
+
if (settled) return;
|
|
125
|
+
settled = true;
|
|
126
|
+
child.kill();
|
|
127
|
+
reject(new Error(`Timed out waiting for OpenPress static server on ${host}:${port}`));
|
|
128
|
+
}, 10000);
|
|
129
|
+
|
|
130
|
+
child.stdout.on("data", (chunk) => {
|
|
131
|
+
const text = String(chunk);
|
|
132
|
+
if (!settled && text.includes("OpenPress static preview:")) {
|
|
133
|
+
settled = true;
|
|
134
|
+
clearTimeout(timer);
|
|
135
|
+
resolve(child);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
child.stderr.on("data", (chunk) => {
|
|
139
|
+
stderr += String(chunk);
|
|
140
|
+
});
|
|
141
|
+
child.on("error", (error) => {
|
|
142
|
+
if (settled) return;
|
|
143
|
+
settled = true;
|
|
144
|
+
clearTimeout(timer);
|
|
145
|
+
reject(error);
|
|
146
|
+
});
|
|
147
|
+
child.on("close", (code) => {
|
|
148
|
+
if (settled) return;
|
|
149
|
+
settled = true;
|
|
150
|
+
clearTimeout(timer);
|
|
151
|
+
reject(new Error(`OpenPress static server exited with code ${code ?? 1}: ${stderr}`));
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export async function writePdfStageDeployConfig(root, source, config) {
|
|
157
|
+
const deployRoot = path.resolve(root, source);
|
|
158
|
+
const openpressDir = path.join(deployRoot, "openpress");
|
|
159
|
+
await fs.mkdir(openpressDir, { recursive: true });
|
|
160
|
+
await fs.writeFile(
|
|
161
|
+
path.join(openpressDir, "deploy.json"),
|
|
162
|
+
`${JSON.stringify({ pdf: publicPdfHref(config), deployed_at: new Date().toISOString() }, null, 2)}\n`,
|
|
163
|
+
"utf8",
|
|
164
|
+
);
|
|
165
|
+
await fs.writeFile(
|
|
166
|
+
path.join(deployRoot, "_headers"),
|
|
167
|
+
`${publicPdfHref(config)}\n Content-Type: application/pdf\n Content-Disposition: inline; filename="${config.pdf.filename}"\n`,
|
|
168
|
+
"utf8",
|
|
169
|
+
);
|
|
170
|
+
}
|