|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { readdirSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { basename, join, relative } from "node:path"; |
| 4 | +import { parseArgs } from "node:util"; |
| 5 | +import { defaultLocale, locales, rtlLocales } from "../src/i18n/config"; |
| 6 | + |
| 7 | +if (process.env.SKIP_PREPARE_BUILD === "true") { |
| 8 | + console.log("Skipping: SKIP_PREPARE_BUILD is set"); |
| 9 | + process.exit(0); |
| 10 | +} |
| 11 | + |
| 12 | +const { values: args } = parseArgs({ |
| 13 | + options: { |
| 14 | + "limit-posts": { type: "string" }, |
| 15 | + "skip-og": { type: "boolean", default: false }, |
| 16 | + "limit-locales": { type: "boolean", default: false }, |
| 17 | + "base-branch": { type: "string" }, |
| 18 | + "base-ref": { type: "string" }, |
| 19 | + }, |
| 20 | +}); |
| 21 | + |
| 22 | +const limitPosts = args["limit-posts"] ?? process.env.LIMIT_POSTS; |
| 23 | +const skipOg = args["skip-og"] || process.env.SKIP_OG === "true"; |
| 24 | +const limitLocales = |
| 25 | + args["limit-locales"] || process.env.LIMIT_LOCALES === "true"; |
| 26 | +const baseBranch = args["base-branch"] ?? process.env.BASE_BRANCH; |
| 27 | +const baseRefArg = args["base-ref"] ?? process.env.BASE_REF; |
| 28 | + |
| 29 | +if (baseBranch && baseRefArg) { |
| 30 | + console.error("Cannot use both --base-branch and --base-ref"); |
| 31 | + process.exit(1); |
| 32 | +} |
| 33 | + |
| 34 | +const BLOG_DIR = "src/content/blog"; |
| 35 | +const OG_ROUTE = "src/pages/open-graph/[...route].ts"; |
| 36 | +const I18N_CONFIG = "src/i18n/config.ts"; |
| 37 | +const I18N_DIR = "src/i18n/translations"; |
| 38 | + |
| 39 | +function git(...gitArgs: string[]): string { |
| 40 | + return execFileSync("git", gitArgs, { encoding: "utf-8" }).trim(); |
| 41 | +} |
| 42 | + |
| 43 | +function resolveRef(ref: string): string | undefined { |
| 44 | + try { |
| 45 | + return git("rev-parse", "--verify", ref); |
| 46 | + } catch { |
| 47 | + // ref not found, try fetching |
| 48 | + } |
| 49 | + |
| 50 | + const remote = git("remote").split("\n").filter(Boolean)[0]; |
| 51 | + if (!remote) return undefined; |
| 52 | + |
| 53 | + try { |
| 54 | + if (git("rev-parse", "--is-shallow-repository") === "true") { |
| 55 | + git("fetch", "--unshallow", remote); |
| 56 | + } |
| 57 | + |
| 58 | + git("fetch", remote, ref); |
| 59 | + return "FETCH_HEAD"; |
| 60 | + } catch { |
| 61 | + return undefined; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function getChangedFiles( |
| 66 | + ref: string, |
| 67 | + useMergeBase: boolean, |
| 68 | + ...dirs: string[] |
| 69 | +): string[] { |
| 70 | + const resolved = resolveRef(ref); |
| 71 | + if (!resolved) return []; |
| 72 | + |
| 73 | + try { |
| 74 | + return git( |
| 75 | + "diff", |
| 76 | + "--name-only", |
| 77 | + ...(useMergeBase ? ["--merge-base"] : []), |
| 78 | + resolved, |
| 79 | + "HEAD", |
| 80 | + "--", |
| 81 | + ...dirs, |
| 82 | + ) |
| 83 | + .split("\n") |
| 84 | + .filter(Boolean); |
| 85 | + } catch (e) { |
| 86 | + console.warn("Could not detect changed files"); |
| 87 | + console.warn(e instanceof Error ? e.message : e); |
| 88 | + return []; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function changedFilesUnder(dir: string): string[] { |
| 93 | + return changed.filter((f) => f.startsWith(dir + "/")); |
| 94 | +} |
| 95 | + |
| 96 | +function serializeI18nConfig(activeLocales: Set<string>): string { |
| 97 | + const entries = Object.entries(locales) |
| 98 | + .filter(([key]) => activeLocales.has(key)) |
| 99 | + .map(([key, val]) => ` ${key}: "${val}",`); |
| 100 | + |
| 101 | + const rtl = rtlLocales.map((l) => `"${l}"`).join(", "); |
| 102 | + |
| 103 | + return [ |
| 104 | + `export const defaultLocale = "${defaultLocale}";`, |
| 105 | + "export const locales = {", |
| 106 | + ...entries, |
| 107 | + "};", |
| 108 | + `export const rtlLocales = [${rtl}];`, |
| 109 | + "", |
| 110 | + ].join("\n"); |
| 111 | +} |
| 112 | + |
| 113 | +const baseRef = baseBranch ?? baseRefArg; |
| 114 | +const useMergeBase = Boolean(baseBranch); |
| 115 | + |
| 116 | +let changed: string[] = []; |
| 117 | +if (baseRef) { |
| 118 | + const mode = useMergeBase ? "merge-base" : "direct"; |
| 119 | + console.log(`Diff: ${mode} against ${baseRef}`); |
| 120 | + changed = getChangedFiles(baseRef, useMergeBase, BLOG_DIR, I18N_DIR); |
| 121 | + console.log( |
| 122 | + `Diff: ${changed.length} file${changed.length !== 1 ? "s" : ""} changed`, |
| 123 | + ); |
| 124 | +} else { |
| 125 | + console.log("Diff: skipped"); |
| 126 | +} |
| 127 | + |
| 128 | +// Limit blog posts, keeping edited ones |
| 129 | +if (limitPosts) { |
| 130 | + const limit = Number(limitPosts); |
| 131 | + if (!Number.isFinite(limit) || limit < 1) { |
| 132 | + console.error(`Invalid --limit-posts value: ${limitPosts}`); |
| 133 | + process.exit(1); |
| 134 | + } |
| 135 | + |
| 136 | + const isPost = (f: string) => /^\d/.test(f) && f.endsWith(".md"); |
| 137 | + |
| 138 | + const allPosts = readdirSync(BLOG_DIR).filter(isPost).sort().reverse(); |
| 139 | + const keep = new Set(allPosts.slice(0, limit)); |
| 140 | + |
| 141 | + for (const file of changedFilesUnder(BLOG_DIR)) { |
| 142 | + if (isPost(basename(file))) keep.add(basename(file)); |
| 143 | + } |
| 144 | + |
| 145 | + let removed = 0; |
| 146 | + for (const file of allPosts) { |
| 147 | + if (keep.has(file)) continue; |
| 148 | + rmSync(join(BLOG_DIR, file)); |
| 149 | + removed++; |
| 150 | + } |
| 151 | + |
| 152 | + console.log( |
| 153 | + `Blog: kept ${allPosts.length - removed} of ${allPosts.length} posts`, |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +// Limit locales |
| 158 | +if (limitLocales) { |
| 159 | + const keepLocales = new Set([defaultLocale]); |
| 160 | + |
| 161 | + for (const file of changedFilesUnder(I18N_DIR)) { |
| 162 | + const locale = relative(I18N_DIR, file).split("/")[0]; |
| 163 | + if (locale && locale !== defaultLocale) keepLocales.add(locale); |
| 164 | + } |
| 165 | + |
| 166 | + writeFileSync(I18N_CONFIG, serializeI18nConfig(keepLocales)); |
| 167 | + console.log(`Locales: building ${Array.from(keepLocales).join(" ")}`); |
| 168 | +} |
| 169 | + |
| 170 | +// OpenGraph removal |
| 171 | +if (skipOg) { |
| 172 | + rmSync(OG_ROUTE, { force: true }); |
| 173 | + console.log(`OpenGraph: removed ${OG_ROUTE}`); |
| 174 | +} |
0 commit comments