@dooc-dev/dooc 0.0.3 → 0.0.4

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/cli.js CHANGED
@@ -1,36 +1,35 @@
1
1
  #!/usr/bin/env node
2
- // CLI entry shared by npx and mise. Root defaults to cwd; relative paths resolve against cwd.
3
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
+ // CLI entry shared by npx and mise. The manifest defaults to ./dooc.yaml; project paths use its parent directory.
3
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
4
4
  import { basename, dirname, join, resolve } from "node:path";
5
5
  import { parseArgs } from "node:util";
6
+ import { projectRoot } from "./compiler/manifest.js";
6
7
  import { prebuilt } from "./viewer/assets.js";
7
8
  process.env.NODE_ENV = prebuilt ? "production" : "development";
8
9
  const USAGE = `usage: dooc <command>
9
10
 
10
- edit [root] [port] open dooc in edit mode
11
- view [root] [port] open dooc in view mode
12
- --viewer-plugins <module> inject an external viewer plugin module
13
- build html [root] [out-dir] build bundle.html (default <root>/dist)
14
- build zip [root] [out-dir] build bundle.zip (default <root>/dist)
15
- --url_preview embed|network URL handling for either format (default: embed)
16
- check [root] [options] JSON consistency report (check --help for options)
17
- plugin pack <built-dir> [zip] package bundled JS and assets as a plugin ZIP
18
- plugin add <zip> [root] [port] install a local plugin and print login links
19
- plugin login <id> [root] [port] open a local login flow for an installed plugin
20
- font add <file> [root] register a font in the project (.dooc/fonts/)
21
- font list [root] list registered project fonts
22
- font remove <name> [root] remove a registered project font
23
- mcp [root] stdio MCP server (all read / write operations)
24
- --git-origin <module> inject gitOrigin from an external viewer plugin module
25
- help, --help, -h show this help`;
11
+ edit [dooc.yaml] [port] open dooc in edit mode
12
+ view [dooc.yaml] [port] open dooc in view mode
13
+ --viewer-plugins <module> inject an external viewer plugin module
14
+ build html [dooc.yaml] [out-dir] build bundle.html (default <project>/dist)
15
+ build zip [dooc.yaml] [out-dir] build bundle.zip (default <project>/dist)
16
+ --url_preview embed|network URL handling for either format (default: embed)
17
+ check [dooc.yaml] [options] JSON consistency report (check --help for options)
18
+ plugin pack <built-dir> [zip] package bundled JS and assets as a plugin ZIP
19
+ plugin add <zip> [dooc.yaml] [port] install a local plugin and print login links
20
+ plugin login <id> [dooc.yaml] [port] open a local login flow for an installed plugin
21
+ font add <file> [dooc.yaml] register a font in the project (.dooc/fonts/)
22
+ font list [dooc.yaml] list registered project fonts
23
+ font remove <name> [dooc.yaml] remove a registered project font
24
+ mcp [dooc.yaml] stdio MCP server (all read / write operations)
25
+ --git-origin <module> inject gitOrigin from an external viewer plugin module
26
+ help, --help, -h show this help
27
+
28
+ Default manifest: ./dooc.yaml. Paths and .dooc/ are relative to its directory.`;
26
29
  const fail = (message) => {
27
30
  console.error(message);
28
31
  process.exit(1);
29
32
  };
30
- const docsRoot = (arg) => {
31
- const root = resolve(arg ?? ".");
32
- return existsSync(root) ? root : fail(`${root} does not exist`);
33
- };
34
33
  const writeOut = (out, data) => {
35
34
  mkdirSync(dirname(out), { recursive: true });
36
35
  writeFileSync(out, data);
@@ -47,13 +46,13 @@ switch (command) {
47
46
  const [action, value, rootArg, portArg] = positionals;
48
47
  if (values.help) {
49
48
  console.log(`usage: dooc plugin pack <built-dir> [output.zip]
50
- dooc plugin add <zip> [root] [port]
51
- dooc plugin login <plugin-id> [root] [port]
49
+ dooc plugin add <zip> [dooc.yaml] [port]
50
+ dooc plugin login <plugin-id> [dooc.yaml] [port]
52
51
 
53
52
  ZIP root: package.json with name, version, type: module and main pointing to bundled .js/.mjs.
54
53
  Optional icon: path to a bundled SVG asset up to 64 KiB.
55
54
  Include relative assets (SVG, JSON, etc.); dependencies must already be bundled. Limit: 16 MiB / 256 files.
56
- Default output: <built-dir>.zip. Installed ZIPs: <root>/.dooc/plugins/.
55
+ Default output: <built-dir>.zip. Installed ZIPs: <project>/.dooc/plugins/.
57
56
  Personal logins and plugin approvals: ~/.dooc/plugins/ (override with DOOC_USER_DIR).
58
57
  Add/login starts a local editor and prints browser login links. Keep it running while signing in.
59
58
  Tokens stay on this computer and never enter exported documents.`);
@@ -74,7 +73,7 @@ Tokens stay on this computer and never enter exported documents.`);
74
73
  const port = Number(portArg ?? 5179);
75
74
  if (!Number.isInteger(port) || port < 1024 || port > 65535)
76
75
  throw new Error("Choose a local port between 1024 and 65535");
77
- process.env.DOCS_ROOT = docsRoot(rootArg);
76
+ process.env.DOCS_ROOT = projectRoot(rootArg);
78
77
  process.env.DOCS_HMR = "1";
79
78
  const { startServer } = await import("./viewer/serve.js");
80
79
  if (action === "add") {
@@ -94,7 +93,7 @@ Tokens stay on this computer and never enter exported documents.`);
94
93
  }
95
94
  }
96
95
  else
97
- throw new Error("usage: dooc plugin pack <built-dir> [zip] | add <zip> [root] [port] | login <id> [root] [port]");
96
+ throw new Error("usage: dooc plugin pack <built-dir> [zip] | add <zip> [dooc.yaml] [port] | login <id> [dooc.yaml] [port]");
98
97
  }
99
98
  catch (error) {
100
99
  fail(error instanceof Error ? error.message : String(error));
@@ -110,22 +109,22 @@ Tokens stay on this computer and never enter exported documents.`);
110
109
  options: { help: { type: "boolean", short: "h" } },
111
110
  });
112
111
  if (values.help) {
113
- console.log("usage: dooc font add <file> [root] | list [root] | remove <name> [root]\n\nFonts are copied to <root>/.dooc/fonts/ and included in ZIP and HTML bundles.\nSupported formats: TTF, OTF, WOFF, WOFF2. Existing names are never overwritten.\nReload the viewer after changing registered fonts. Default root: current directory.");
112
+ console.log("usage: dooc font add <file> [dooc.yaml] | list [dooc.yaml] | remove <name> [dooc.yaml]\n\nFonts are copied to <project>/.dooc/fonts/ and included in ZIP and HTML bundles.\nSupported formats: TTF, OTF, WOFF, WOFF2. Existing names are never overwritten.\nReload the viewer after changing registered fonts. Default manifest: ./dooc.yaml.");
114
113
  break;
115
114
  }
116
115
  const [action, value, rootArg] = positionals;
117
116
  if (action === "list" && positionals.length <= 2) {
118
- console.log(JSON.stringify(listProjectFonts(docsRoot(value)), null, 2));
117
+ console.log(JSON.stringify(listProjectFonts(projectRoot(value)), null, 2));
119
118
  }
120
119
  else if (action === "add" && value && positionals.length <= 3) {
121
- console.log(addProjectFont(docsRoot(rootArg), resolve(value)));
120
+ console.log(addProjectFont(projectRoot(rootArg), resolve(value)));
122
121
  }
123
122
  else if (action === "remove" && value && positionals.length <= 3) {
124
- removeProjectFont(docsRoot(rootArg), value);
123
+ removeProjectFont(projectRoot(rootArg), value);
125
124
  console.log(`Removed font: ${value}`);
126
125
  }
127
126
  else
128
- throw new Error("usage: dooc font add <file> [root] | list [root] | remove <name> [root]");
127
+ throw new Error("usage: dooc font add <file> [dooc.yaml] | list [dooc.yaml] | remove <name> [dooc.yaml]");
129
128
  }
130
129
  catch (error) {
131
130
  fail(error instanceof Error ? error.message : String(error));
@@ -147,7 +146,7 @@ Tokens stay on this computer and never enter exported documents.`);
147
146
  },
148
147
  });
149
148
  if (values.help) {
150
- console.log(`usage: dooc check [root] [options]
149
+ console.log(`usage: dooc check [dooc.yaml] [options]
151
150
 
152
151
  --only render,languages,themes select checks (default: all three)
153
152
  manifest validity is always checked
@@ -173,8 +172,8 @@ Node API: import { checkSite } from "@dooc-dev/dooc/check".`);
173
172
  break;
174
173
  }
175
174
  if (positionals.length > 1)
176
- throw new Error("check accepts at most one root path");
177
- report = checkSite(resolve(positionals[0] ?? "."), {
175
+ throw new Error("check accepts at most one dooc.yaml path");
176
+ report = checkSite(projectRoot(positionals[0]), {
178
177
  ...(values.only
179
178
  ? {
180
179
  only: values.only.flatMap((value) => value.split(",")),
@@ -210,16 +209,21 @@ Node API: import { checkSite } from "@dooc-dev/dooc/check".`);
210
209
  }
211
210
  case "view":
212
211
  case "edit": {
213
- const { positionals, values } = parseArgs({
214
- args,
215
- allowPositionals: true,
216
- options: { "viewer-plugins": { type: "string" } },
217
- });
218
- process.env.DOCS_ROOT = docsRoot(positionals[0]);
219
- process.env.DOCS_HMR = "1";
220
- // serve.ts reads DOCS_ROOT at import time, so import it after the env is set.
221
- const { startServer } = await import("./viewer/serve.js");
222
- await startServer(command === "view" ? "preview" : "edit", Number(positionals[1] ?? 5179), values["viewer-plugins"] ?? process.env.DOOC_VIEWER_PLUGINS);
212
+ try {
213
+ const { positionals, values } = parseArgs({
214
+ args,
215
+ allowPositionals: true,
216
+ options: { "viewer-plugins": { type: "string" } },
217
+ });
218
+ process.env.DOCS_ROOT = projectRoot(positionals[0]);
219
+ process.env.DOCS_HMR = "1";
220
+ // serve.ts reads DOCS_ROOT at import time, so import it after the env is set.
221
+ const { startServer } = await import("./viewer/serve.js");
222
+ await startServer(command === "view" ? "preview" : "edit", Number(positionals[1] ?? 5179), values["viewer-plugins"] ?? process.env.DOOC_VIEWER_PLUGINS);
223
+ }
224
+ catch (error) {
225
+ fail(error instanceof Error ? error.message : String(error));
226
+ }
223
227
  break;
224
228
  }
225
229
  case "build": {
@@ -233,11 +237,11 @@ Node API: import { checkSite } from "@dooc-dev/dooc/check".`);
233
237
  },
234
238
  });
235
239
  if (values.help) {
236
- console.log(`usage: dooc build html [root] [out-dir] [--url_preview embed|network]
237
- dooc build zip [root] [out-dir] [--url_preview embed|network]
240
+ console.log(`usage: dooc build html [dooc.yaml] [out-dir] [--url_preview embed|network]
241
+ dooc build zip [dooc.yaml] [out-dir] [--url_preview embed|network]
238
242
 
239
243
  A bundle is dooc.yaml and its managed content. Builds only the selected format.
240
- Default output directory: <root>/dist. Source files are never rewritten.
244
+ Default output directory: <project>/dist. Source files are never rewritten.
241
245
  --url_preview embed snapshot URL content and assets into the selected artifact (default)
242
246
  --url_preview network preserve URL references without downloading them at build time
243
247
 
@@ -256,11 +260,11 @@ Use --url_preview network with either format to build without downloading URL co
256
260
  if (format !== "html" && format !== "zip")
257
261
  throw new Error("build format must be html | zip");
258
262
  if (positionals.length > 3)
259
- throw new Error(`build ${format} accepts a root and an output directory`);
263
+ throw new Error(`build ${format} accepts a dooc.yaml path and an output directory`);
260
264
  const urlPreview = values.url_preview;
261
265
  if (urlPreview !== "embed" && urlPreview !== "network")
262
266
  throw new Error("--url_preview must be embed | network");
263
- const root = docsRoot(rootArg);
267
+ const root = projectRoot(rootArg);
264
268
  const out = join(resolve(outArg ?? join(root, "dist")), `bundle.${format}`);
265
269
  const { buildBundle } = await import("./viewer/build.js");
266
270
  const bundle = await buildBundle(root, { format, urlPreview });
@@ -283,17 +287,17 @@ Use --url_preview network with either format to build without downloading URL co
283
287
  },
284
288
  });
285
289
  if (values.help) {
286
- console.log("usage: dooc mcp [root] [--git-origin <module>]\n\nExposes docs, files, Statics, fonts, check, build and Git over stdio.\nBuild accepts format=html|zip and url_preview=embed|network (default embed).\nThe external module's default export must contain gitOrigin (the same provider used by edit/view).\nOnly gitOrigin is used; viewer setup and data plugins are not started. Authentication stays in the provider.");
290
+ console.log("usage: dooc mcp [dooc.yaml] [--git-origin <module>]\n\nExposes docs, files, Statics, fonts, check, build and Git over stdio.\nBuild accepts format=html|zip and url_preview=embed|network (default embed).\nThe external module's default export must contain gitOrigin (the same provider used by edit/view).\nOnly gitOrigin is used; viewer setup and data plugins are not started. Authentication stays in the provider.");
287
291
  break;
288
292
  }
289
293
  if (positionals.length > 1)
290
- throw new Error("mcp accepts at most one root path");
294
+ throw new Error("mcp accepts at most one dooc.yaml path");
291
295
  const { startMcp } = await import("./mcp.js");
292
296
  const { loadViewerPlugins } = await import("./viewer/plugin-config.js");
293
297
  const { gitOrigin } = await loadViewerPlugins(values["git-origin"]);
294
298
  if (values["git-origin"] && !gitOrigin)
295
299
  throw new Error("Module must provide gitOrigin");
296
- await startMcp(docsRoot(positionals[0]), { gitOrigin });
300
+ await startMcp(projectRoot(positionals[0]), { gitOrigin });
297
301
  }
298
302
  catch (error) {
299
303
  fail(error instanceof Error ? error.message : String(error));
@@ -5,6 +5,7 @@ export interface ProjectFile {
5
5
  size: number;
6
6
  extension: string;
7
7
  }
8
+ export declare const completeFilePaths: (root: string, prefix: unknown) => string[];
8
9
  export declare const projectFiles: (root: string) => ProjectFile[];
9
10
  export declare const filesSnapshot: (root: string) => {
10
11
  files: ProjectFile[];
@@ -18,6 +18,25 @@ import { themeNames, themeVariants } from "./theme-variants.js";
18
18
  // Project settings and translations have their own layout, outside file restructuring.
19
19
  const reserved = new Set([MANIFEST_FILE, "docs.config.mjs", "node_modules", "dist", "locales"]);
20
20
  export const managedFile = (file) => localFile(file) && !reserved.has(file.split("/")[0] ?? "");
21
+ export const completeFilePaths = (root, prefix) => {
22
+ if (typeof prefix !== "string" || prefix.length > 4096)
23
+ throw new Error("Invalid file prefix");
24
+ const slash = prefix.lastIndexOf("/");
25
+ const directory = prefix.slice(0, slash + 1);
26
+ if (prefix && !managedFile(prefix.replace(/\/$/, "")))
27
+ throw new Error("Expected a relative project path");
28
+ const path = directory ? textfilePath(root, directory) : root;
29
+ if (!existsSync(path))
30
+ return [];
31
+ return readdirSync(path, { withFileTypes: true })
32
+ .filter((entry) => !entry.isSymbolicLink() &&
33
+ (entry.isFile() || entry.isDirectory()) &&
34
+ managedFile(directory + entry.name) &&
35
+ (directory + entry.name).toLowerCase().startsWith(prefix.toLowerCase()))
36
+ .map((entry) => directory + entry.name + (entry.isDirectory() ? "/" : ""))
37
+ .sort((a, b) => Number(b.endsWith("/")) - Number(a.endsWith("/")) || a.localeCompare(b))
38
+ .slice(0, 200);
39
+ };
21
40
  export const projectFiles = (root) => {
22
41
  const result = [];
23
42
  const walk = (directory) => {
@@ -1,4 +1,6 @@
1
1
  import { type DocNode, type ParsedManifest } from "../core/content-metadata.js";
2
+ /** CLI paths select a manifest; every project path is relative to its parent. */
3
+ export declare const projectRoot: (input?: string) => string;
2
4
  export declare const parseManifestText: (raw: string) => ParsedManifest;
3
5
  /** Empty tree if missing (a new docs root with zero docs). */
4
6
  export declare const readManifest: (root: string) => ParsedManifest;
@@ -1,9 +1,16 @@
1
1
  // Reads/writes dooc.yaml (node-dependent). Tree validation and manipulation live in
2
2
  // core/content-metadata.ts.
3
- import { existsSync, readFileSync, writeFileSync } from "node:fs";
4
- import { join } from "node:path";
3
+ import { existsSync, readFileSync, statSync, writeFileSync } from "node:fs";
4
+ import { basename, dirname, join, resolve } from "node:path";
5
5
  import { parse } from "yaml";
6
6
  import { MANIFEST_FILE, parseManifest, serializeManifest, } from "../core/content-metadata.js";
7
+ /** CLI paths select a manifest; every project path is relative to its parent. */
8
+ export const projectRoot = (input = MANIFEST_FILE) => {
9
+ const file = resolve(input);
10
+ if (basename(file) !== MANIFEST_FILE || !existsSync(file) || !statSync(file).isFile())
11
+ throw new Error(`Specify an existing dooc.yaml file: ${file}`);
12
+ return dirname(file);
13
+ };
7
14
  export const parseManifestText = (raw) => {
8
15
  let value;
9
16
  try {