-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathsetup-sites.ts
More file actions
81 lines (65 loc) · 2.63 KB
/
setup-sites.ts
File metadata and controls
81 lines (65 loc) · 2.63 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
75
76
77
78
79
80
81
#!/usr/bin/env tsx
/**
* setup-sites.ts
* Links all help-* Obsidian Publish sites to their local locale directories.
*
* Usage:
* npx tsx scripts/setup-sites.ts [--dry-run]
*
* Mirrors the "Link publish sites" step in the GitHub Actions workflows:
* - Lists all sites via `ob publish-list-sites`
* - Matches "help" → en, "help-<locale>" → <locale>
* - Runs `ob publish-setup --site <id> --path <localePath>` for each match
*/
import { execSync } from "child_process";
import fs from "fs";
import path from "path";
const ROOT = path.resolve(import.meta.dirname, "..");
const SCRIPTS_DIR = import.meta.dirname;
// Build a map from lowercase locale code → actual directory name from locales.json
const localesJson: Array<{ code: string; dir?: string }> = JSON.parse(
fs.readFileSync(path.join(SCRIPTS_DIR, "locales.json"), "utf8")
);
const localeDir = new Map<string, string>();
for (const entry of localesJson) {
const dir = entry.dir ?? entry.code;
localeDir.set(dir.toLowerCase(), dir);
}
const dryRun = process.argv.includes("--dry-run");
// ── Parse ob publish-list-sites ───────────────────────────────────────────────
const raw = execSync("ob publish-list-sites", { encoding: "utf8" });
// Each site line looks like: <32-hex-id> "slug"
const SITE_RE = /^\s+([0-9a-f]{32})\s+"(help(?:-[^"]+)?)"$/;
interface Site { id: string; slug: string; locale: string; }
const sites: Site[] = [];
for (const line of raw.split("\n")) {
const m = line.match(SITE_RE);
if (!m) continue;
const [, id, slug] = m;
const locale = slug === "help" ? "en" : slug.slice("help-".length);
sites.push({ id, slug, locale });
}
if (sites.length === 0) {
console.log("No help-* sites found.");
process.exit(0);
}
console.log(`Found ${sites.length} help site(s)${dryRun ? " [DRY RUN]" : ""}:\n`);
let linked = 0, skipped = 0;
for (const { id, slug, locale } of sites) {
// Resolve actual directory name from locales.json (handles mixed-case like zh-TW)
const actualLocale = localeDir.get(locale.toLowerCase()) ?? locale;
const localePath = path.join(ROOT, actualLocale);
if (!fs.existsSync(localePath)) {
console.log(` SKIP "${slug}" — no ${actualLocale}/ directory`);
skipped++;
continue;
}
const cmd = `ob publish-setup --site ${id} --path ${JSON.stringify(localePath)}`;
console.log(` LINK "${slug}" (${id}) → ${actualLocale}/`);
if (!dryRun) {
execSync(cmd, { stdio: "inherit" });
}
linked++;
}
console.log(`\nLinked: ${linked} Skipped: ${skipped}`);
if (dryRun) console.log("[DRY RUN] No changes written.");