@open-press/cli 1.0.0 → 1.1.1
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/README.md +11 -12
- package/dist/cli.js +298 -79
- package/package.json +9 -7
- package/template/core/AGENTS.md +0 -130
- package/template/core/CHANGELOG.md +0 -218
- package/template/core/README.md +0 -43
- package/template/core/engine/cli.mjs +0 -96
- package/template/core/engine/commands/_shared.mjs +0 -199
- package/template/core/engine/commands/deploy.mjs +0 -31
- package/template/core/engine/commands/dev.mjs +0 -49
- package/template/core/engine/commands/doctor.mjs +0 -229
- package/template/core/engine/commands/export.mjs +0 -8
- package/template/core/engine/commands/image.mjs +0 -29
- package/template/core/engine/commands/inspect.mjs +0 -35
- package/template/core/engine/commands/pdf.mjs +0 -26
- package/template/core/engine/commands/preview.mjs +0 -26
- package/template/core/engine/commands/render.mjs +0 -17
- package/template/core/engine/commands/replace.mjs +0 -41
- package/template/core/engine/commands/search.mjs +0 -33
- package/template/core/engine/commands/skills-sync.mjs +0 -71
- package/template/core/engine/commands/typecheck.mjs +0 -67
- package/template/core/engine/commands/upgrade.mjs +0 -159
- package/template/core/engine/commands/validate.mjs +0 -17
- package/template/core/engine/document-export.mjs +0 -15
- package/template/core/engine/output/chrome-pdf.d.mts +0 -34
- package/template/core/engine/output/chrome-pdf.mjs +0 -450
- package/template/core/engine/output/deploy-sync.mjs +0 -15
- package/template/core/engine/output/fonts.mjs +0 -62
- package/template/core/engine/output/katex-assets.mjs +0 -45
- package/template/core/engine/output/page-block.mjs +0 -30
- package/template/core/engine/output/pdf-media.mjs +0 -45
- package/template/core/engine/output/public-assets.mjs +0 -19
- package/template/core/engine/output/static-server.mjs +0 -571
- package/template/core/engine/react/caption-numbering.mjs +0 -73
- package/template/core/engine/react/comment-endpoint.d.mts +0 -11
- package/template/core/engine/react/comment-endpoint.mjs +0 -102
- package/template/core/engine/react/comment-marker.mjs +0 -374
- package/template/core/engine/react/document-entry.mjs +0 -331
- package/template/core/engine/react/document-export.mjs +0 -512
- package/template/core/engine/react/http-json.mjs +0 -24
- package/template/core/engine/react/mdx-compile.mjs +0 -629
- package/template/core/engine/react/measurement-css.mjs +0 -157
- package/template/core/engine/react/object-entities.mjs +0 -204
- package/template/core/engine/react/pagination/allocator.mjs +0 -167
- package/template/core/engine/react/pagination/regions.mjs +0 -81
- package/template/core/engine/react/pagination-constants.mjs +0 -3
- package/template/core/engine/react/pagination.mjs +0 -9
- package/template/core/engine/react/pipeline/allocate.mjs +0 -217
- package/template/core/engine/react/pipeline/final-render.mjs +0 -94
- package/template/core/engine/react/pipeline/frame-measurement.mjs +0 -306
- package/template/core/engine/react/pipeline/press-tree.mjs +0 -135
- package/template/core/engine/react/press-tree-inspection.mjs +0 -172
- package/template/core/engine/react/project-asset-endpoint.d.mts +0 -10
- package/template/core/engine/react/project-asset-endpoint.mjs +0 -361
- package/template/core/engine/react/section-css.mjs +0 -56
- package/template/core/engine/react/source-edit-endpoint.d.mts +0 -10
- package/template/core/engine/react/source-edit-endpoint.mjs +0 -75
- package/template/core/engine/react/sources/heading-numbering.mjs +0 -132
- package/template/core/engine/react/sources/mdx-resolver.mjs +0 -439
- package/template/core/engine/react/style-discovery.mjs +0 -160
- package/template/core/engine/runtime/config.d.mts +0 -48
- package/template/core/engine/runtime/config.mjs +0 -172
- package/template/core/engine/runtime/file-utils.mjs +0 -114
- package/template/core/engine/runtime/file-walk.mjs +0 -22
- package/template/core/engine/runtime/inspection.mjs +0 -328
- package/template/core/engine/runtime/issue-report.mjs +0 -44
- package/template/core/engine/runtime/page-geometry.mjs +0 -131
- package/template/core/engine/runtime/path-utils.mjs +0 -20
- package/template/core/engine/runtime/source-text-tools.d.mts +0 -102
- package/template/core/engine/runtime/source-text-tools.mjs +0 -832
- package/template/core/engine/runtime/source-workspace.mjs +0 -168
- package/template/core/engine/runtime/validation.mjs +0 -183
- package/template/core/index.html +0 -13
- package/template/core/openpress.config.mjs +0 -8
- package/template/core/package.json +0 -89
- package/template/core/src/main.tsx +0 -16
- package/template/core/src/openpress/app/OpenPressApp.tsx +0 -296
- package/template/core/src/openpress/app/OpenPressRuntime.tsx +0 -102
- package/template/core/src/openpress/app/WorkspaceGalleryPage.tsx +0 -219
- package/template/core/src/openpress/app/index.ts +0 -2
- package/template/core/src/openpress/core/Frame.tsx +0 -91
- package/template/core/src/openpress/core/FrameContext.tsx +0 -26
- package/template/core/src/openpress/core/MdxArea.tsx +0 -34
- package/template/core/src/openpress/core/Press.tsx +0 -55
- package/template/core/src/openpress/core/Workspace.tsx +0 -36
- package/template/core/src/openpress/core/cn.ts +0 -4
- package/template/core/src/openpress/core/index.tsx +0 -47
- package/template/core/src/openpress/core/primitives.tsx +0 -91
- package/template/core/src/openpress/core/types.ts +0 -236
- package/template/core/src/openpress/core/useSource.ts +0 -28
- package/template/core/src/openpress/document-model/anchorMapModel.ts +0 -27
- package/template/core/src/openpress/document-model/documentIndexes.ts +0 -329
- package/template/core/src/openpress/document-model/documentTypes.ts +0 -147
- package/template/core/src/openpress/document-model/index.ts +0 -7
- package/template/core/src/openpress/document-model/objectEntityModel.ts +0 -55
- package/template/core/src/openpress/document-model/projectIdentityModel.ts +0 -15
- package/template/core/src/openpress/document-model/reactDocumentMetadataModel.ts +0 -27
- package/template/core/src/openpress/document-model/workspaceManifestModel.ts +0 -57
- package/template/core/src/openpress/manuscript/index.tsx +0 -238
- package/template/core/src/openpress/mdx/index.ts +0 -96
- package/template/core/src/openpress/numbering/index.ts +0 -294
- package/template/core/src/openpress/reader/PageThumbnailsPanel.tsx +0 -168
- package/template/core/src/openpress/reader/PublicReaderPage.tsx +0 -267
- package/template/core/src/openpress/reader/ReaderNavigationPanel.tsx +0 -123
- package/template/core/src/openpress/reader/index.ts +0 -11
- package/template/core/src/openpress/reader/pageViewportScaleModel.ts +0 -73
- package/template/core/src/openpress/reader/readerPageRegistry.ts +0 -41
- package/template/core/src/openpress/reader/readerPageRoute.ts +0 -21
- package/template/core/src/openpress/reader/readerScroll.ts +0 -92
- package/template/core/src/openpress/reader/readerStateModel.ts +0 -15
- package/template/core/src/openpress/reader/readerTypes.ts +0 -4
- package/template/core/src/openpress/reader/usePageViewportScale.ts +0 -119
- package/template/core/src/openpress/reader/usePanelState.ts +0 -56
- package/template/core/src/openpress/reader/useReaderHashSync.ts +0 -61
- package/template/core/src/openpress/reader/useReaderKeyboardNav.ts +0 -48
- package/template/core/src/openpress/reader/useReaderRuntime.ts +0 -146
- package/template/core/src/openpress/reader/useReaderScrollAnchor.ts +0 -64
- package/template/core/src/openpress/shared/Panel.tsx +0 -77
- package/template/core/src/openpress/shared/frameScheduler.ts +0 -32
- package/template/core/src/openpress/shared/index.ts +0 -4
- package/template/core/src/openpress/shared/numberUtils.ts +0 -3
- package/template/core/src/openpress/shared/runtimeMode.ts +0 -11
- package/template/core/src/openpress/workbench/Workbench.tsx +0 -506
- package/template/core/src/openpress/workbench/actions/DeploymentControl.tsx +0 -157
- package/template/core/src/openpress/workbench/actions/ExportImageControl.tsx +0 -96
- package/template/core/src/openpress/workbench/actions/PageZoomControl.tsx +0 -182
- package/template/core/src/openpress/workbench/actions/SearchControl.tsx +0 -345
- package/template/core/src/openpress/workbench/actions/deploymentStatusModel.ts +0 -112
- package/template/core/src/openpress/workbench/actions/index.ts +0 -6
- package/template/core/src/openpress/workbench/actions/useDeploymentWorkbench.ts +0 -136
- package/template/core/src/openpress/workbench/dialog/WorkbenchDialog.tsx +0 -72
- package/template/core/src/openpress/workbench/dialog/index.ts +0 -1
- package/template/core/src/openpress/workbench/document/components/DocumentPanel.tsx +0 -127
- package/template/core/src/openpress/workbench/document/components/InlineSourceEditorLayer.tsx +0 -207
- package/template/core/src/openpress/workbench/document/components/ReaderStage.tsx +0 -9
- package/template/core/src/openpress/workbench/document/hooks/useDocumentWorkbenchModel.ts +0 -34
- package/template/core/src/openpress/workbench/document/hooks/useInlineDocumentEditor.ts +0 -525
- package/template/core/src/openpress/workbench/document/index.ts +0 -10
- package/template/core/src/openpress/workbench/index.ts +0 -2
- package/template/core/src/openpress/workbench/inspector/InlineInspectorLayer.tsx +0 -459
- package/template/core/src/openpress/workbench/inspector/index.ts +0 -5
- package/template/core/src/openpress/workbench/inspector/inlineCommentModel.ts +0 -125
- package/template/core/src/openpress/workbench/inspector/inspectorGeometryModel.ts +0 -160
- package/template/core/src/openpress/workbench/inspector/inspectorModel.ts +0 -408
- package/template/core/src/openpress/workbench/inspector/useInspectorComments.ts +0 -254
- package/template/core/src/openpress/workbench/mentions/MentionSuggestionList.tsx +0 -41
- package/template/core/src/openpress/workbench/mentions/index.ts +0 -2
- package/template/core/src/openpress/workbench/mentions/useComposerMentions.ts +0 -185
- package/template/core/src/openpress/workbench/panels/Panel.tsx +0 -1
- package/template/core/src/openpress/workbench/panels/PendingCommentsPanel.tsx +0 -80
- package/template/core/src/openpress/workbench/panels/WorkbenchControlPanel.tsx +0 -29
- package/template/core/src/openpress/workbench/panels/index.ts +0 -3
- package/template/core/src/openpress/workbench/project/ProjectEntryPanel.tsx +0 -525
- package/template/core/src/openpress/workbench/project/ProjectPreviewDialog.tsx +0 -35
- package/template/core/src/openpress/workbench/project/index.ts +0 -2
- package/template/core/src/openpress/workbench/project/projectPreviewTypes.ts +0 -11
- package/template/core/src/openpress/workbench/project/projectSourceModel.ts +0 -24
- package/template/core/src/openpress/workbench/shell/WorkbenchShell.tsx +0 -167
- package/template/core/src/openpress/workbench/shell/index.ts +0 -1
- package/template/core/src/openpress/workbench/workbenchFormatters.ts +0 -120
- package/template/core/src/openpress/workbench/workbenchTypes.ts +0 -35
- package/template/core/src/styles/openpress/app-shell.css +0 -251
- package/template/core/src/styles/openpress/media-workspace.css +0 -230
- package/template/core/src/styles/openpress/print-route.css +0 -184
- package/template/core/src/styles/openpress/project-preview-panel.css +0 -924
- package/template/core/src/styles/openpress/public-viewer.css +0 -688
- package/template/core/src/styles/openpress/reader-runtime.css +0 -989
- package/template/core/src/styles/openpress/responsive.css +0 -245
- package/template/core/src/styles/openpress/workbench-panels.css +0 -707
- package/template/core/src/styles/openpress/workbench.css +0 -1255
- package/template/core/src/styles/openpress/workspace-gallery.css +0 -300
- package/template/core/src/styles/openpress.css +0 -15
- package/template/core/src/vite-env.d.ts +0 -9
- package/template/core/tsconfig.json +0 -40
- package/template/core/vite.config.ts +0 -584
|
@@ -1,584 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath, URL } from "node:url";
|
|
2
|
-
import { spawn } from "node:child_process";
|
|
3
|
-
import fs from "node:fs/promises";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
6
|
-
import { defineConfig } from "vite";
|
|
7
|
-
import react from "@vitejs/plugin-react";
|
|
8
|
-
import { loadConfig, publicPdfHref } from "./engine/runtime/config.mjs";
|
|
9
|
-
import { searchSourceText } from "./engine/runtime/source-text-tools.mjs";
|
|
10
|
-
import { handleCommentRequest } from "./engine/react/comment-endpoint.mjs";
|
|
11
|
-
import { handleProjectAssetRequest } from "./engine/react/project-asset-endpoint.mjs";
|
|
12
|
-
import { handleSourceEditRequest } from "./engine/react/source-edit-endpoint.mjs";
|
|
13
|
-
|
|
14
|
-
const frameworkRoot = fileURLToPath(new URL("./", import.meta.url));
|
|
15
|
-
const workspaceRoot = process.env.OPENPRESS_WORKSPACE_ROOT
|
|
16
|
-
? path.resolve(process.env.OPENPRESS_WORKSPACE_ROOT)
|
|
17
|
-
: frameworkRoot;
|
|
18
|
-
const sourceRoot = path.join(frameworkRoot, "src");
|
|
19
|
-
const openpressCliPath = path.join(frameworkRoot, "engine", "cli.mjs");
|
|
20
|
-
const staticServerPath = path.join(frameworkRoot, "engine", "output", "static-server.mjs");
|
|
21
|
-
const openpressCoreEntry = path.join(frameworkRoot, "src", "openpress", "core", "index.tsx");
|
|
22
|
-
const openpressMdxEntry = path.join(frameworkRoot, "src", "openpress", "mdx", "index.ts");
|
|
23
|
-
const openpressManuscriptEntry = path.join(frameworkRoot, "src", "openpress", "manuscript", "index.tsx");
|
|
24
|
-
const openpressNumberingEntry = path.join(frameworkRoot, "src", "openpress", "numbering", "index.ts");
|
|
25
|
-
const openpressConfig = await loadConfig(workspaceRoot);
|
|
26
|
-
const outputDir = openpressConfig.paths.outputDir;
|
|
27
|
-
const reactDocumentRoot = openpressConfig.paths.documentRoot;
|
|
28
|
-
const reactDocumentComponentsRoot = openpressConfig.paths.componentsDir;
|
|
29
|
-
const reactDocumentEntry = path.join(reactDocumentRoot, "index.tsx");
|
|
30
|
-
const activeContentDir = await fileExists(reactDocumentEntry)
|
|
31
|
-
? path.join(reactDocumentRoot, "chapters")
|
|
32
|
-
: openpressConfig.paths.sourceDir;
|
|
33
|
-
|
|
34
|
-
// Workspace directories — Vite resolves these at build time so that
|
|
35
|
-
// `import.meta.glob("@workspace/content/**")` and friends follow the active
|
|
36
|
-
// OpenPress authoring source instead of a hardcoded `document/` prefix.
|
|
37
|
-
const workspaceAliases = {
|
|
38
|
-
"@workspace/content": activeContentDir,
|
|
39
|
-
"@workspace/media": openpressConfig.paths.mediaDir,
|
|
40
|
-
"@workspace/components": openpressConfig.paths.componentsDir,
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// Relative paths displayed back to the user (e.g. "document/content/").
|
|
44
|
-
// Resolved at build time so the React app does not hardcode `document/`.
|
|
45
|
-
function relativeFromWorkspace(absolute: string) {
|
|
46
|
-
const rel = path.relative(workspaceRoot, absolute).replaceAll("\\", "/");
|
|
47
|
-
return rel.endsWith("/") ? rel : `${rel}`;
|
|
48
|
-
}
|
|
49
|
-
const workspaceDefines = {
|
|
50
|
-
__OPENPRESS_CONTENT_PATH__: JSON.stringify(relativeFromWorkspace(activeContentDir)),
|
|
51
|
-
__OPENPRESS_MEDIA_PATH__: JSON.stringify(relativeFromWorkspace(openpressConfig.paths.mediaDir)),
|
|
52
|
-
__OPENPRESS_COMPONENTS_PATH__: JSON.stringify(relativeFromWorkspace(openpressConfig.paths.componentsDir)),
|
|
53
|
-
__OPENPRESS_PDF_HREF__: JSON.stringify(publicPdfHref(openpressConfig)),
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export default defineConfig({
|
|
57
|
-
base: "./",
|
|
58
|
-
cacheDir: path.join(workspaceRoot, ".openpress", "vite-client"),
|
|
59
|
-
plugins: [openpressLocalDeployPlugin(), react()],
|
|
60
|
-
define: workspaceDefines,
|
|
61
|
-
resolve: {
|
|
62
|
-
dedupe: ["react", "react-dom", "@mdx-js/react"],
|
|
63
|
-
alias: {
|
|
64
|
-
// Subpaths must come before the base path so resolution matches longest first.
|
|
65
|
-
"@open-press/core/mdx": openpressMdxEntry,
|
|
66
|
-
"@open-press/core/manuscript": openpressManuscriptEntry,
|
|
67
|
-
"@open-press/core/numbering": openpressNumberingEntry,
|
|
68
|
-
"@open-press/core": openpressCoreEntry,
|
|
69
|
-
"@/components": reactDocumentComponentsRoot,
|
|
70
|
-
"@": sourceRoot,
|
|
71
|
-
...workspaceAliases,
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
optimizeDeps: {
|
|
75
|
-
include: [
|
|
76
|
-
"@mdx-js/react",
|
|
77
|
-
"lucide-react",
|
|
78
|
-
"react",
|
|
79
|
-
"react-dom",
|
|
80
|
-
"react-dom/client",
|
|
81
|
-
"react/jsx-dev-runtime",
|
|
82
|
-
"react/jsx-runtime",
|
|
83
|
-
],
|
|
84
|
-
},
|
|
85
|
-
build: {
|
|
86
|
-
outDir: outputDir,
|
|
87
|
-
emptyOutDir: true,
|
|
88
|
-
rollupOptions: {
|
|
89
|
-
output: {
|
|
90
|
-
entryFileNames: "assets/[name]-[hash]-openpress.js",
|
|
91
|
-
chunkFileNames: "assets/[name]-[hash]-openpress.js",
|
|
92
|
-
assetFileNames: "assets/[name]-[hash]-openpress[extname]",
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
server: {
|
|
97
|
-
host: "127.0.0.1",
|
|
98
|
-
port: 5173,
|
|
99
|
-
fs: {
|
|
100
|
-
allow: Array.from(new Set([frameworkRoot, workspaceRoot])),
|
|
101
|
-
},
|
|
102
|
-
watch: {
|
|
103
|
-
ignored: ["**/.openpress/tmp/**", `**/${openpressConfig.outputDir}/**`],
|
|
104
|
-
},
|
|
105
|
-
},
|
|
106
|
-
preview: {
|
|
107
|
-
host: "127.0.0.1",
|
|
108
|
-
port: 5173,
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
function openpressLocalDeployPlugin() {
|
|
113
|
-
return {
|
|
114
|
-
name: "openpress-local-deploy-endpoint",
|
|
115
|
-
configureServer(server: { middlewares: { use: (path: string, handler: (req: IncomingMessage, res: ServerResponse) => void) => void } }) {
|
|
116
|
-
server.middlewares.use("/__openpress/local-pdf-export", (req, res) => {
|
|
117
|
-
void handleLocalPdfExportRequest(req, res);
|
|
118
|
-
});
|
|
119
|
-
server.middlewares.use("/__openpress/local-pdf-file", (req, res) => {
|
|
120
|
-
void handleLocalPdfFileRequest(req, res);
|
|
121
|
-
});
|
|
122
|
-
server.middlewares.use("/__openpress/status", (req, res) => {
|
|
123
|
-
void handleLocalStatusRequest(req, res);
|
|
124
|
-
});
|
|
125
|
-
server.middlewares.use("/__openpress/search", (req, res) => {
|
|
126
|
-
void handleLocalSearchRequest(req, res);
|
|
127
|
-
});
|
|
128
|
-
server.middlewares.use("/__openpress/source-edit", (req, res) => {
|
|
129
|
-
void handleSourceEditRequest(req, res, { root: workspaceRoot });
|
|
130
|
-
});
|
|
131
|
-
server.middlewares.use("/__openpress/deploy", (req, res) => {
|
|
132
|
-
void handleLocalDeployRequest(req, res);
|
|
133
|
-
});
|
|
134
|
-
server.middlewares.use("/__openpress/comment", (req, res) => {
|
|
135
|
-
void handleCommentRequest(req, res, { root: workspaceRoot });
|
|
136
|
-
});
|
|
137
|
-
server.middlewares.use("/__openpress/media-upload", (req, res) => {
|
|
138
|
-
void handleLocalMediaUploadRequest(req, res);
|
|
139
|
-
});
|
|
140
|
-
server.middlewares.use("/__openpress/project-asset", (req, res) => {
|
|
141
|
-
void handleProjectAssetRequest(req, res, { root: workspaceRoot });
|
|
142
|
-
});
|
|
143
|
-
server.middlewares.use("/openpress/media", (req, res) => {
|
|
144
|
-
void handleLocalMediaFileRequest(req, res);
|
|
145
|
-
});
|
|
146
|
-
},
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async function handleLocalMediaUploadRequest(req: IncomingMessage, res: ServerResponse) {
|
|
151
|
-
if (req.method !== "POST") {
|
|
152
|
-
writeJson(res, 405, { ok: false, message: "Media upload endpoint requires POST." });
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const rawFileName = headerValue(req.headers["x-openpress-file-name"]);
|
|
157
|
-
const decodedFileName = rawFileName ? safeDecodeURIComponent(rawFileName) : "";
|
|
158
|
-
const fileName = sanitizeMediaFileName(decodedFileName);
|
|
159
|
-
if (!fileName) {
|
|
160
|
-
writeJson(res, 400, { ok: false, message: "Media upload requires a valid file name." });
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
if (!isAllowedMediaFile(fileName)) {
|
|
164
|
-
writeJson(res, 400, { ok: false, message: "Only png, jpg, jpeg, gif, svg, and webp files can be uploaded." });
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
try {
|
|
169
|
-
const body = await readRequestBuffer(req, 30 * 1024 * 1024);
|
|
170
|
-
if (body.length === 0) {
|
|
171
|
-
writeJson(res, 400, { ok: false, message: "Uploaded media file is empty." });
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
await fs.mkdir(openpressConfig.paths.mediaDir, { recursive: true });
|
|
175
|
-
const uniqueFileName = await uniqueMediaFileName(openpressConfig.paths.mediaDir, fileName);
|
|
176
|
-
const targetPath = path.join(openpressConfig.paths.mediaDir, uniqueFileName);
|
|
177
|
-
await fs.writeFile(targetPath, body);
|
|
178
|
-
const relativePath = relativeFromWorkspace(targetPath);
|
|
179
|
-
writeJson(res, 200, {
|
|
180
|
-
ok: true,
|
|
181
|
-
asset: {
|
|
182
|
-
fileName: uniqueFileName,
|
|
183
|
-
src: `/openpress/media/${encodeURIComponent(uniqueFileName)}`,
|
|
184
|
-
path: relativePath,
|
|
185
|
-
mention: `@media/${uniqueFileName}`,
|
|
186
|
-
},
|
|
187
|
-
});
|
|
188
|
-
} catch (error) {
|
|
189
|
-
writeJson(res, 500, { ok: false, message: error instanceof Error ? error.message : String(error) });
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async function handleLocalMediaFileRequest(req: IncomingMessage, res: ServerResponse) {
|
|
194
|
-
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
195
|
-
writeJson(res, 405, { ok: false, message: "Media file endpoint requires GET." });
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
try {
|
|
200
|
-
const requestUrl = new URL(req.url ?? "/", "http://localhost");
|
|
201
|
-
const fileName = sanitizeMediaFileName(safeDecodeURIComponent(requestUrl.pathname.replace(/^\/openpress\/media\/?/, "").replace(/^\/+/, "")));
|
|
202
|
-
if (!fileName) {
|
|
203
|
-
writeJson(res, 404, { ok: false, message: "Media file not found." });
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
const targetPath = path.join(openpressConfig.paths.mediaDir, fileName);
|
|
207
|
-
const resolvedTarget = path.resolve(targetPath);
|
|
208
|
-
const mediaRoot = path.resolve(openpressConfig.paths.mediaDir);
|
|
209
|
-
if (!resolvedTarget.startsWith(`${mediaRoot}${path.sep}`) && resolvedTarget !== mediaRoot) {
|
|
210
|
-
writeJson(res, 403, { ok: false, message: "Forbidden." });
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
const body = await fs.readFile(resolvedTarget);
|
|
214
|
-
res.writeHead(200, {
|
|
215
|
-
"Content-Type": mediaMimeType(fileName),
|
|
216
|
-
"Cache-Control": "no-store",
|
|
217
|
-
});
|
|
218
|
-
if (req.method === "HEAD") {
|
|
219
|
-
res.end();
|
|
220
|
-
} else {
|
|
221
|
-
res.end(body);
|
|
222
|
-
}
|
|
223
|
-
} catch {
|
|
224
|
-
writeJson(res, 404, { ok: false, message: "Media file not found." });
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async function handleLocalPdfExportRequest(req: IncomingMessage, res: ServerResponse) {
|
|
229
|
-
if (req.method !== "POST") {
|
|
230
|
-
writeJson(res, 405, { ok: false, message: "Local PDF export endpoint requires POST." });
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const result = await runLocalPdfExport();
|
|
235
|
-
const exists = await fileExists(openpressConfig.paths.pdf);
|
|
236
|
-
writeJson(res, result.code === 0 && exists ? 200 : 500, {
|
|
237
|
-
ok: result.code === 0 && exists,
|
|
238
|
-
code: result.code,
|
|
239
|
-
pdf: `/__openpress/local-pdf-file?ts=${Date.now()}`,
|
|
240
|
-
command: openpressCliCommand(["pdf", "."]),
|
|
241
|
-
stdout: result.stdout,
|
|
242
|
-
stderr: result.stderr,
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
async function handleLocalPdfFileRequest(req: IncomingMessage, res: ServerResponse) {
|
|
247
|
-
if (req.method !== "GET") {
|
|
248
|
-
writeJson(res, 405, { ok: false, message: "Local PDF file endpoint requires GET." });
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
try {
|
|
253
|
-
const body = await fs.readFile(openpressConfig.paths.pdf);
|
|
254
|
-
res.writeHead(200, {
|
|
255
|
-
"Content-Type": "application/pdf",
|
|
256
|
-
"Content-Disposition": `inline; filename="${openpressConfig.pdf.filename}"`,
|
|
257
|
-
"Cache-Control": "no-store",
|
|
258
|
-
});
|
|
259
|
-
res.end(body);
|
|
260
|
-
} catch {
|
|
261
|
-
writeJson(res, 404, { ok: false, message: "Local PDF has not been generated yet." });
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
async function handleLocalStatusRequest(req: IncomingMessage, res: ServerResponse) {
|
|
266
|
-
if (req.method !== "GET") {
|
|
267
|
-
writeJson(res, 405, { ok: false, message: "Status endpoint requires GET." });
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const deployConfigured = isLocalDeployConfigured();
|
|
272
|
-
const deploymentInfo = deployConfigured
|
|
273
|
-
? await readLocalDeploymentInfo()
|
|
274
|
-
: { deployed_at: undefined, pdf: publicPdfHref(openpressConfig), public_url: undefined };
|
|
275
|
-
const dirty = deployConfigured ? await isLocalDeploymentDirty(deploymentInfo.deployed_at) : false;
|
|
276
|
-
writeJson(res, 200, {
|
|
277
|
-
ok: true,
|
|
278
|
-
deployed_at: deploymentInfo.deployed_at,
|
|
279
|
-
pdf: deploymentInfo.pdf,
|
|
280
|
-
public_url: deploymentInfo.public_url,
|
|
281
|
-
dirty,
|
|
282
|
-
deploy_configured: deployConfigured,
|
|
283
|
-
deploy_adapter: openpressConfig.deploy.adapter,
|
|
284
|
-
deploy_source: openpressConfig.deploy.source,
|
|
285
|
-
deploy_project_name: openpressConfig.deploy.projectName,
|
|
286
|
-
deploy_setup_message: localDeploySetupMessage(),
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
async function handleLocalSearchRequest(req: IncomingMessage, res: ServerResponse) {
|
|
291
|
-
if (req.method !== "GET") {
|
|
292
|
-
writeJson(res, 405, { ok: false, message: "Search endpoint requires GET." });
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const requestUrl = new URL(req.url ?? "/", "http://localhost");
|
|
297
|
-
const query = (requestUrl.searchParams.get("q") ?? "").trim();
|
|
298
|
-
if (!query) {
|
|
299
|
-
writeJson(res, 400, { ok: false, message: "Search query is required." });
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
try {
|
|
304
|
-
const report = await searchSourceText({
|
|
305
|
-
config: openpressConfig,
|
|
306
|
-
query,
|
|
307
|
-
scope: searchScopeFrom(requestUrl.searchParams),
|
|
308
|
-
caseSensitive: requestUrl.searchParams.get("caseSensitive") === "true",
|
|
309
|
-
});
|
|
310
|
-
writeJson(res, 200, { ok: true, ...report });
|
|
311
|
-
} catch (error) {
|
|
312
|
-
writeJson(res, 500, { ok: false, message: error instanceof Error ? error.message : String(error) });
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
async function handleLocalDeployRequest(req: IncomingMessage, res: ServerResponse) {
|
|
317
|
-
if (req.method !== "POST") {
|
|
318
|
-
writeJson(res, 405, { ok: false, message: "Deploy endpoint requires POST." });
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
if (!isLocalDeployConfigured()) {
|
|
323
|
-
writeJson(res, 400, {
|
|
324
|
-
ok: false,
|
|
325
|
-
code: 2,
|
|
326
|
-
message: localDeploySetupMessage(),
|
|
327
|
-
deploy_configured: false,
|
|
328
|
-
deploy_adapter: openpressConfig.deploy.adapter,
|
|
329
|
-
deploy_source: openpressConfig.deploy.source,
|
|
330
|
-
deploy_project_name: openpressConfig.deploy.projectName,
|
|
331
|
-
command: openpressCliCommand(["deploy", ".", "--confirm"]),
|
|
332
|
-
});
|
|
333
|
-
return;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
const result = await runLocalDeploy();
|
|
337
|
-
const deployedUrl = extractDeployUrl(result.stdout);
|
|
338
|
-
if (result.code === 0 && deployedUrl) {
|
|
339
|
-
await writeLocalDeploymentPublicUrl(deployedUrl);
|
|
340
|
-
}
|
|
341
|
-
const deploymentInfo = await readLocalDeploymentInfo();
|
|
342
|
-
const publicUrl = deployedUrl ?? deploymentInfo.public_url;
|
|
343
|
-
writeJson(res, result.code === 0 ? 200 : 500, {
|
|
344
|
-
ok: result.code === 0,
|
|
345
|
-
code: result.code,
|
|
346
|
-
deployed_at: deploymentInfo.deployed_at,
|
|
347
|
-
pdf: deployedUrl ? `${deployedUrl}/${openpressConfig.pdf.filename}` : deploymentInfo.pdf,
|
|
348
|
-
public_url: publicUrl,
|
|
349
|
-
dirty: false,
|
|
350
|
-
command: openpressCliCommand(["deploy", ".", "--confirm"]),
|
|
351
|
-
stdout: result.stdout,
|
|
352
|
-
stderr: result.stderr,
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
function runLocalPdfExport() {
|
|
357
|
-
return new Promise<{ code: number; stdout: string; stderr: string }>((resolve) => {
|
|
358
|
-
const child = spawn("node", [openpressCliPath, "pdf", "."], {
|
|
359
|
-
cwd: workspaceRoot,
|
|
360
|
-
shell: false,
|
|
361
|
-
});
|
|
362
|
-
let stdout = "";
|
|
363
|
-
let stderr = "";
|
|
364
|
-
child.stdout.on("data", (chunk) => {
|
|
365
|
-
stdout += String(chunk);
|
|
366
|
-
});
|
|
367
|
-
child.stderr.on("data", (chunk) => {
|
|
368
|
-
stderr += String(chunk);
|
|
369
|
-
});
|
|
370
|
-
child.on("error", (error) => {
|
|
371
|
-
resolve({ code: 1, stdout, stderr: `${stderr}${error.message}\n` });
|
|
372
|
-
});
|
|
373
|
-
child.on("close", (code) => {
|
|
374
|
-
resolve({ code: code ?? 1, stdout, stderr });
|
|
375
|
-
});
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
function runLocalDeploy() {
|
|
380
|
-
return new Promise<{ code: number; stdout: string; stderr: string }>((resolve) => {
|
|
381
|
-
const child = spawn("node", [openpressCliPath, "deploy", ".", "--confirm"], {
|
|
382
|
-
cwd: workspaceRoot,
|
|
383
|
-
shell: false,
|
|
384
|
-
});
|
|
385
|
-
let stdout = "";
|
|
386
|
-
let stderr = "";
|
|
387
|
-
child.stdout.on("data", (chunk) => {
|
|
388
|
-
stdout += String(chunk);
|
|
389
|
-
});
|
|
390
|
-
child.stderr.on("data", (chunk) => {
|
|
391
|
-
stderr += String(chunk);
|
|
392
|
-
});
|
|
393
|
-
child.on("error", (error) => {
|
|
394
|
-
resolve({ code: 1, stdout, stderr: `${stderr}${error.message}\n` });
|
|
395
|
-
});
|
|
396
|
-
child.on("close", (code) => {
|
|
397
|
-
resolve({ code: code ?? 1, stdout, stderr });
|
|
398
|
-
});
|
|
399
|
-
});
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
function isLocalDeployConfigured() {
|
|
403
|
-
if (openpressConfig.deploy.adapter === "cloudflare-pages") {
|
|
404
|
-
return typeof openpressConfig.deploy.projectName === "string" && openpressConfig.deploy.projectName.trim().length > 0;
|
|
405
|
-
}
|
|
406
|
-
return true;
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
function localDeploySetupMessage() {
|
|
410
|
-
if (isLocalDeployConfigured()) return undefined;
|
|
411
|
-
if (openpressConfig.deploy.adapter === "cloudflare-pages") {
|
|
412
|
-
return "Cloudflare Pages deployment requires `deploy.projectName` in openpress.config.mjs.";
|
|
413
|
-
}
|
|
414
|
-
return `Deployment adapter \`${openpressConfig.deploy.adapter}\` is not configured.`;
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
function searchScopeFrom(searchParams: URLSearchParams) {
|
|
418
|
-
return searchParams.get("scope") === "all" ? "all" : "content";
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
async function fileExists(filePath: string) {
|
|
422
|
-
try {
|
|
423
|
-
await fs.access(filePath);
|
|
424
|
-
return true;
|
|
425
|
-
} catch {
|
|
426
|
-
return false;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
async function readLocalDeploymentInfo() {
|
|
431
|
-
try {
|
|
432
|
-
const text = await fs.readFile(openpressConfig.paths.deployMetadata, "utf8");
|
|
433
|
-
const deployConfig = JSON.parse(text) as { deployed_at?: unknown; pdf?: unknown; public_url?: unknown };
|
|
434
|
-
return {
|
|
435
|
-
deployed_at: typeof deployConfig.deployed_at === "string" ? deployConfig.deployed_at : undefined,
|
|
436
|
-
pdf: typeof deployConfig.pdf === "string" ? deployConfig.pdf : publicPdfHref(openpressConfig),
|
|
437
|
-
public_url: typeof deployConfig.public_url === "string" ? deployConfig.public_url : undefined,
|
|
438
|
-
};
|
|
439
|
-
} catch {
|
|
440
|
-
return { deployed_at: undefined, pdf: publicPdfHref(openpressConfig), public_url: undefined };
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
async function writeLocalDeploymentPublicUrl(publicUrl: string) {
|
|
445
|
-
let deployConfig: Record<string, unknown> = {};
|
|
446
|
-
try {
|
|
447
|
-
deployConfig = JSON.parse(await fs.readFile(openpressConfig.paths.deployMetadata, "utf8")) as Record<string, unknown>;
|
|
448
|
-
} catch {
|
|
449
|
-
deployConfig = {};
|
|
450
|
-
}
|
|
451
|
-
await fs.mkdir(path.dirname(openpressConfig.paths.deployMetadata), { recursive: true });
|
|
452
|
-
await fs.writeFile(
|
|
453
|
-
openpressConfig.paths.deployMetadata,
|
|
454
|
-
`${JSON.stringify({ ...deployConfig, pdf: `${publicUrl}/${openpressConfig.pdf.filename}`, public_url: publicUrl }, null, 2)}\n`,
|
|
455
|
-
"utf8",
|
|
456
|
-
);
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
async function isLocalDeploymentDirty(deployedAt: string | undefined) {
|
|
460
|
-
if (!deployedAt) return false;
|
|
461
|
-
const deployedTime = new Date(deployedAt).getTime();
|
|
462
|
-
if (Number.isNaN(deployedTime)) return false;
|
|
463
|
-
const newestSourceMtime = await findNewestLocalSourceMtime(getLocalDeploymentSourcePaths());
|
|
464
|
-
return newestSourceMtime > deployedTime + 1000;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
function getLocalDeploymentSourcePaths() {
|
|
468
|
-
return [
|
|
469
|
-
openpressConfig.paths.sourceDir,
|
|
470
|
-
openpressConfig.paths.mediaDir,
|
|
471
|
-
openpressConfig.paths.themeDir,
|
|
472
|
-
openpressConfig.paths.designDoc,
|
|
473
|
-
openpressConfig.paths.componentsDir,
|
|
474
|
-
path.join(frameworkRoot, "src"),
|
|
475
|
-
path.join(workspaceRoot, "index.html"),
|
|
476
|
-
path.join(workspaceRoot, "package.json"),
|
|
477
|
-
path.join(workspaceRoot, "openpress.config.mjs"),
|
|
478
|
-
openpressConfig.configPath,
|
|
479
|
-
path.join(workspaceRoot, "vite.config.ts"),
|
|
480
|
-
];
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
function openpressCliCommand(args: string[]) {
|
|
484
|
-
const relativeCliPath = path.relative(workspaceRoot, openpressCliPath).replaceAll("\\", "/");
|
|
485
|
-
const displayCliPath = relativeCliPath && !relativeCliPath.startsWith("../") ? relativeCliPath : openpressCliPath;
|
|
486
|
-
return `node ${displayCliPath} ${args.join(" ")}`;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
async function findNewestLocalSourceMtime(paths: string[]) {
|
|
490
|
-
const times = await Promise.all(paths.map((sourcePath) => findNewestLocalMtime(sourcePath)));
|
|
491
|
-
return Math.max(0, ...times);
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
async function findNewestLocalMtime(sourcePath: string): Promise<number> {
|
|
495
|
-
try {
|
|
496
|
-
const stat = await fs.stat(sourcePath);
|
|
497
|
-
if (!stat.isDirectory()) return stat.mtimeMs;
|
|
498
|
-
const entries = await fs.readdir(sourcePath, { withFileTypes: true });
|
|
499
|
-
const times = await Promise.all(entries.map((entry) => findNewestLocalMtime(path.join(sourcePath, entry.name))));
|
|
500
|
-
return Math.max(stat.mtimeMs, ...times);
|
|
501
|
-
} catch {
|
|
502
|
-
return 0;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
function headerValue(value: string | string[] | undefined) {
|
|
507
|
-
return Array.isArray(value) ? value[0] : value;
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
function safeDecodeURIComponent(value: string) {
|
|
511
|
-
try {
|
|
512
|
-
return decodeURIComponent(value);
|
|
513
|
-
} catch {
|
|
514
|
-
return value;
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
function sanitizeMediaFileName(value: string) {
|
|
519
|
-
const baseName = path.basename(value).trim();
|
|
520
|
-
if (!baseName) return "";
|
|
521
|
-
const ext = path.extname(baseName);
|
|
522
|
-
const stem = path.basename(baseName, ext)
|
|
523
|
-
.replace(/[\\/:*?"<>|#%{}^~[\]`]/g, "-")
|
|
524
|
-
.replace(/\s+/g, "-")
|
|
525
|
-
.replace(/-+/g, "-")
|
|
526
|
-
.replace(/^-|-$/g, "");
|
|
527
|
-
if (!stem || !ext) return "";
|
|
528
|
-
return `${stem}${ext.toLowerCase()}`;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
function isAllowedMediaFile(fileName: string) {
|
|
532
|
-
return /\.(png|jpe?g|gif|svg|webp)$/i.test(fileName);
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
function mediaMimeType(fileName: string) {
|
|
536
|
-
const ext = path.extname(fileName).toLowerCase();
|
|
537
|
-
if (ext === ".png") return "image/png";
|
|
538
|
-
if (ext === ".jpg" || ext === ".jpeg") return "image/jpeg";
|
|
539
|
-
if (ext === ".gif") return "image/gif";
|
|
540
|
-
if (ext === ".svg") return "image/svg+xml";
|
|
541
|
-
if (ext === ".webp") return "image/webp";
|
|
542
|
-
return "application/octet-stream";
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
async function uniqueMediaFileName(mediaDir: string, fileName: string) {
|
|
546
|
-
const ext = path.extname(fileName);
|
|
547
|
-
const stem = path.basename(fileName, ext);
|
|
548
|
-
let candidate = fileName;
|
|
549
|
-
let counter = 2;
|
|
550
|
-
while (await fileExists(path.join(mediaDir, candidate))) {
|
|
551
|
-
candidate = `${stem}-${counter}${ext}`;
|
|
552
|
-
counter += 1;
|
|
553
|
-
}
|
|
554
|
-
return candidate;
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
function readRequestBuffer(req: IncomingMessage, maxBytes: number) {
|
|
558
|
-
return new Promise<Buffer>((resolve, reject) => {
|
|
559
|
-
const chunks: Buffer[] = [];
|
|
560
|
-
let total = 0;
|
|
561
|
-
req.on("data", (chunk: Buffer | string) => {
|
|
562
|
-
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
563
|
-
total += buffer.length;
|
|
564
|
-
if (total > maxBytes) {
|
|
565
|
-
reject(new Error("Uploaded media file is too large."));
|
|
566
|
-
req.destroy();
|
|
567
|
-
return;
|
|
568
|
-
}
|
|
569
|
-
chunks.push(buffer);
|
|
570
|
-
});
|
|
571
|
-
req.on("end", () => resolve(Buffer.concat(chunks)));
|
|
572
|
-
req.on("error", reject);
|
|
573
|
-
});
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
function writeJson(res: ServerResponse, status: number, body: unknown) {
|
|
577
|
-
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
|
|
578
|
-
res.end(`${JSON.stringify(body, null, 2)}\n`);
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
function extractDeployUrl(output: string) {
|
|
582
|
-
const match = output.match(/https:\/\/[^\s]+\.pages\.dev/);
|
|
583
|
-
return match?.[0]?.replace(/\/$/, "");
|
|
584
|
-
}
|