|
| 1 | +import { readFileSync, writeFileSync, existsSync, rmSync, readdirSync, mkdirSync, watch as watchFs } from "node:fs"; |
| 2 | +import { join, dirname } from "node:path"; |
| 3 | + |
| 4 | +const SRC = join(import.meta.dir, "src"); |
| 5 | +const ENTRY = join(SRC, "app.ts"); |
| 6 | + |
| 7 | +type Settings = { nameId: string }; |
| 8 | + |
| 9 | +function getSettings(): Settings { |
| 10 | + return JSON.parse(readFileSync(join(SRC, "settings.json"), "utf-8")); |
| 11 | +} |
| 12 | + |
| 13 | +function getJsOutputName(): string { |
| 14 | + return `${getSettings().nameId}.js`; |
| 15 | +} |
| 16 | + |
| 17 | +const argv = process.argv.slice(2); |
| 18 | +const minify = argv.includes("--minify") || argv.includes("-m"); |
| 19 | +const watch = argv.includes("--watch") || argv.includes("-w"); |
| 20 | + |
| 21 | +function parseOutDir(): string | undefined { |
| 22 | + for (let i = 0; i < argv.length; i++) { |
| 23 | + if (argv[i] === "--out" || argv[i] === "-o") { |
| 24 | + return argv[i + 1]; |
| 25 | + } |
| 26 | + if (argv[i].startsWith("--out=")) { |
| 27 | + return argv[i].slice(6); |
| 28 | + } |
| 29 | + } |
| 30 | + return undefined; |
| 31 | +} |
| 32 | + |
| 33 | +async function getOutDir(): Promise<string> { |
| 34 | + const out = parseOutDir(); |
| 35 | + if (out) return join(import.meta.dir, out); |
| 36 | + const proc = Bun.spawn(["spicetify", "-c"], { stdout: "pipe", stderr: "pipe" }); |
| 37 | + const text = await new Response(proc.stdout).text(); |
| 38 | + const ok = await proc.exited; |
| 39 | + if (ok !== 0) { |
| 40 | + const err = await new Response(proc.stderr).text(); |
| 41 | + console.error("spicetify -c failed:", err || "spicetify not found or not configured"); |
| 42 | + process.exit(1); |
| 43 | + } |
| 44 | + return join(dirname(text.trim()), "Extensions"); |
| 45 | +} |
| 46 | + |
| 47 | +function getStyleId(): string { |
| 48 | + return getSettings().nameId.replace(/-/g, "D"); |
| 49 | +} |
| 50 | + |
| 51 | +function postProcess(outDir: string): void { |
| 52 | + // Bun outputs using entry basename (app.js); we want nameId.js like spicetify-creator |
| 53 | + const jsFromBuild = join(outDir, "app.js"); |
| 54 | + const jsOut = join(outDir, getJsOutputName()); |
| 55 | + if (!existsSync(jsFromBuild)) return; |
| 56 | + |
| 57 | + let js = readFileSync(jsFromBuild, "utf-8"); |
| 58 | + if (jsOut !== jsFromBuild) rmSync(jsFromBuild); |
| 59 | + |
| 60 | + const styleId = getStyleId(); |
| 61 | + |
| 62 | + const files = readdirSync(outDir, { withFileTypes: true }); |
| 63 | + for (const f of files) { |
| 64 | + if (f.isFile() && f.name.endsWith(".css")) { |
| 65 | + const cssPath = join(outDir, f.name); |
| 66 | + const css = readFileSync(cssPath, "utf-8"); |
| 67 | + rmSync(cssPath); |
| 68 | + const escaped = css.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$/g, "\\$"); |
| 69 | + js += ` |
| 70 | +(async () => { |
| 71 | + if (!document.getElementById(\`${styleId}\`)) { |
| 72 | + var el = document.createElement('style'); |
| 73 | + el.id = \`${styleId}\`; |
| 74 | + el.textContent = (String.raw\`${escaped}\`).trim(); |
| 75 | + document.head.appendChild(el); |
| 76 | + } |
| 77 | +})(); |
| 78 | +`; |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const wrapped = `(async function() { |
| 84 | + while (!Spicetify.React || !Spicetify.ReactDOM) { |
| 85 | + await new Promise(resolve => setTimeout(resolve, 10)); |
| 86 | + } |
| 87 | + ${js} |
| 88 | +})(); |
| 89 | +`; |
| 90 | + writeFileSync(jsOut, wrapped); |
| 91 | +} |
| 92 | + |
| 93 | +async function runBuild(outDir: string): Promise<void> { |
| 94 | + if (!existsSync(outDir)) { |
| 95 | + mkdirSync(outDir, { recursive: true }); |
| 96 | + } |
| 97 | + |
| 98 | + const result = await Bun.build({ |
| 99 | + entrypoints: [ENTRY], |
| 100 | + outdir: outDir, |
| 101 | + target: "browser", |
| 102 | + minify, |
| 103 | + naming: "[name].[ext]", |
| 104 | + root: ".", |
| 105 | + }); |
| 106 | + |
| 107 | + if (!result.success) { |
| 108 | + console.error("Build failed:"); |
| 109 | + for (const msg of result.logs) { |
| 110 | + console.error(msg); |
| 111 | + } |
| 112 | + process.exit(1); |
| 113 | + } |
| 114 | + |
| 115 | + postProcess(outDir); |
| 116 | + console.log("Build succeeded."); |
| 117 | +} |
| 118 | + |
| 119 | +async function main(): Promise<void> { |
| 120 | + const outDir = await getOutDir(); |
| 121 | + |
| 122 | + if (watch) { |
| 123 | + await runBuild(outDir); |
| 124 | + let debounce: ReturnType<typeof setTimeout> | null = null; |
| 125 | + watchFs(SRC, { recursive: true }, () => { |
| 126 | + if (debounce) clearTimeout(debounce); |
| 127 | + debounce = setTimeout(async () => { |
| 128 | + debounce = null; |
| 129 | + await runBuild(outDir); |
| 130 | + }, 100); |
| 131 | + }); |
| 132 | + console.log("Watching..."); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + await runBuild(outDir); |
| 137 | +} |
| 138 | + |
| 139 | +main(); |
0 commit comments