Repository navigation
Expand file tree
/
Copy pathpull-en-site-options.ts
More file actions
74 lines (65 loc) · 2.08 KB
/
Copy pathpull-en-site-options.ts
File metadata and controls
74 lines (65 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env tsx
/**
* pull-en-site-options.ts
* Usage: npx tsx scripts/pull-en-site-options.ts [--dry-run]
*
* Pulls site options from the EN Publish site via `ob publish-site-options`
* and saves them to en/site-options.json.
*/
import fs from "fs";
import path from "path";
import { execSync } from "child_process";
const ROOT = path.resolve(import.meta.dirname, "..");
const EN_DIR = path.join(ROOT, "en");
const JSON_PATH = path.join(EN_DIR, "site-options.json");
const dryRun = process.argv.includes("--dry-run");
interface SiteOptions {
siteName?: string;
indexFile?: string;
logo?: string;
showNavigation?: boolean;
showGraph?: boolean;
showOutline?: boolean;
showSearch?: boolean;
showBacklinks?: boolean;
showHoverPreview?: boolean;
showThemeToggle?: boolean;
defaultTheme?: string;
readableLineLength?: boolean;
strictLineBreaks?: boolean;
hideTitle?: boolean;
slidingWindow?: boolean;
navigationOrdering?: string[];
navigationHiddenItems?: string[];
}
function parseSiteOptions(raw: string): SiteOptions {
const opts: SiteOptions = {};
for (const line of raw.split("\n")) {
const m = line.match(/^\s{2}(\w+): (.+)$/);
if (!m) continue;
const [, key, val] = m;
if (val === "true" || val === "false") {
(opts as Record<string, unknown>)[key] = val === "true";
} else if (val.startsWith("[")) {
(opts as Record<string, unknown>)[key] = JSON.parse(val);
} else {
(opts as Record<string, unknown>)[key] = val.replace(/^"|"$/g, "");
}
}
return opts;
}
console.log("Pulling site options from EN...");
const raw = execSync(`ob publish-site-options --path ${JSON.stringify(EN_DIR)}`, { encoding: "utf8" });
const opts = parseSiteOptions(raw);
if (Object.keys(opts).length === 0) {
console.error("Error: no site options returned from EN. Is the site linked?");
process.exit(1);
}
const json = JSON.stringify(opts, null, 2) + "\n";
if (dryRun) {
console.log("[DRY RUN] Would write to en/site-options.json:\n");
console.log(json);
} else {
fs.writeFileSync(JSON_PATH, json);
console.log(`Saved to ${JSON_PATH}`);
}