|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { |
| 3 | + mkdtempSync, |
| 4 | + readFileSync, |
| 5 | + readdirSync, |
| 6 | + rmSync, |
| 7 | + statSync, |
| 8 | +} from "node:fs"; |
| 9 | +import { tmpdir } from "node:os"; |
| 10 | +import path from "node:path"; |
| 11 | +import { fileURLToPath } from "node:url"; |
| 12 | +import { loadEnv } from "vite"; |
| 13 | + |
| 14 | +const desktopRoot = path.resolve( |
| 15 | + path.dirname(fileURLToPath(import.meta.url)), |
| 16 | + "..", |
| 17 | +); |
| 18 | +const vitePackageJsonPath = fileURLToPath( |
| 19 | + import.meta.resolve("vite/package.json"), |
| 20 | +); |
| 21 | +const vitePackage = JSON.parse(readFileSync(vitePackageJsonPath, "utf8")); |
| 22 | +const viteEntrypoint = path.resolve( |
| 23 | + path.dirname(vitePackageJsonPath), |
| 24 | + vitePackage.bin.vite, |
| 25 | +); |
| 26 | + |
| 27 | +function buildVariant({ internal, output }) { |
| 28 | + const env = { |
| 29 | + ...process.env, |
| 30 | + // Pin both children explicitly. Deleting the OSS value lets Vite reload |
| 31 | + // `=1` from .env.local or a mode-specific env file. |
| 32 | + VITE_BUZZ_BESTIE: internal ? "1" : "0", |
| 33 | + }; |
| 34 | + |
| 35 | + const result = spawnSync( |
| 36 | + process.execPath, |
| 37 | + [viteEntrypoint, "build", "--outDir", output, "--emptyOutDir"], |
| 38 | + { |
| 39 | + cwd: desktopRoot, |
| 40 | + env, |
| 41 | + stdio: "inherit", |
| 42 | + }, |
| 43 | + ); |
| 44 | + if (result.error) throw result.error; |
| 45 | + if (result.status !== 0) { |
| 46 | + throw new Error( |
| 47 | + `${internal ? "internal" : "OSS"} desktop build failed with status ${result.status}`, |
| 48 | + ); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function emittedText(root) { |
| 53 | + const chunks = []; |
| 54 | + const visit = (candidate) => { |
| 55 | + const stat = statSync(candidate); |
| 56 | + if (stat.isDirectory()) { |
| 57 | + for (const child of readdirSync(candidate)) { |
| 58 | + visit(path.join(candidate, child)); |
| 59 | + } |
| 60 | + return; |
| 61 | + } |
| 62 | + if (/\.(?:css|html|js|json)$/u.test(candidate)) { |
| 63 | + chunks.push(readFileSync(candidate, "utf8")); |
| 64 | + } |
| 65 | + }; |
| 66 | + visit(root); |
| 67 | + return chunks.join("\n"); |
| 68 | +} |
| 69 | + |
| 70 | +export function assertArtifactContract({ ossOutput, internalOutput }) { |
| 71 | + const ossText = emittedText(ossOutput); |
| 72 | + const internalText = emittedText(internalOutput); |
| 73 | + const protectedContent = /\bbestie\b|chief of staff|builtin:bestie/iu; |
| 74 | + const internalManifestMarker = |
| 75 | + "Try a personal agent that is always close at hand"; |
| 76 | + |
| 77 | + if (protectedContent.test(ossText)) { |
| 78 | + throw new Error( |
| 79 | + "Official OSS desktop artifact contains protected Bestie/Chief content", |
| 80 | + ); |
| 81 | + } |
| 82 | + if (!internalText.includes(internalManifestMarker)) { |
| 83 | + throw new Error( |
| 84 | + "Protected internal desktop artifact is missing the Bestie manifest", |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** Resolve the requested output with the same precedence used by Vite config. */ |
| 90 | +export function selectInternalVariant({ processEnv, modeEnv }) { |
| 91 | + return (processEnv.VITE_BUZZ_BESTIE ?? modeEnv.VITE_BUZZ_BESTIE) === "1"; |
| 92 | +} |
| 93 | + |
| 94 | +/** Build and inspect both graphs, leaving the requested variant in dist. */ |
| 95 | +export function buildArtifactMatrix({ |
| 96 | + selectedInternalVariant, |
| 97 | + selectedOutput, |
| 98 | + alternateOutput, |
| 99 | + build = buildVariant, |
| 100 | +}) { |
| 101 | + // Build the unselected variant outside dist first, then leave the requested |
| 102 | + // variant in dist for Vite/Tauri's ordinary packaging contract. |
| 103 | + build({ |
| 104 | + internal: !selectedInternalVariant, |
| 105 | + output: alternateOutput, |
| 106 | + }); |
| 107 | + build({ |
| 108 | + internal: selectedInternalVariant, |
| 109 | + output: selectedOutput, |
| 110 | + }); |
| 111 | + |
| 112 | + assertArtifactContract({ |
| 113 | + ossOutput: selectedInternalVariant ? alternateOutput : selectedOutput, |
| 114 | + internalOutput: selectedInternalVariant ? selectedOutput : alternateOutput, |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +function main() { |
| 119 | + const selectedInternalVariant = selectInternalVariant({ |
| 120 | + processEnv: process.env, |
| 121 | + modeEnv: loadEnv("production", desktopRoot, ""), |
| 122 | + }); |
| 123 | + const scratchRoot = mkdtempSync( |
| 124 | + path.join(tmpdir(), "buzz-protected-feature-artifacts-"), |
| 125 | + ); |
| 126 | + const selectedOutput = process.env.BUZZ_PROTECTED_BUILD_OUTPUT |
| 127 | + ? path.resolve(process.env.BUZZ_PROTECTED_BUILD_OUTPUT) |
| 128 | + : path.join(desktopRoot, "dist"); |
| 129 | + const alternateOutput = path.join(scratchRoot, "alternate"); |
| 130 | + |
| 131 | + try { |
| 132 | + buildArtifactMatrix({ |
| 133 | + selectedInternalVariant, |
| 134 | + selectedOutput, |
| 135 | + alternateOutput, |
| 136 | + }); |
| 137 | + } finally { |
| 138 | + rmSync(scratchRoot, { recursive: true, force: true }); |
| 139 | + } |
| 140 | + |
| 141 | + console.log( |
| 142 | + `Protected feature artifact matrix passed; dist contains the ${selectedInternalVariant ? "internal" : "OSS"} variant.`, |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +if ( |
| 147 | + process.argv[1] && |
| 148 | + path.resolve(process.argv[1]) === fileURLToPath(import.meta.url) |
| 149 | +) { |
| 150 | + main(); |
| 151 | +} |
0 commit comments