Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | import { remove, readdir, writeFile, ensureDir, createReadStream, pathExists, readJSON, } from "fs-extra"; import Path from "path"; import AdmZip from "adm-zip"; import spawn from "../../src/lib/helpers/cli/spawn"; import { createHash } from "crypto"; const buildToolsRoot = Path.join( Path.normalize(`${__dirname}/../../`), "buildTools", ); const lockPath = Path.join(buildToolsRoot, "dependencies.lock"); const dependencies = { "darwin-arm64": { gbdk: { url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/gbdk-next/gbdk-macos-arm64.tar.gz", type: "targz", }, }, "darwin-x64": { gbdk: { url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/gbdk-next/gbdk-macos.tar.gz", type: "targz", }, }, "linux-x64": { gbdk: { url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/gbdk-next/gbdk-linux64.tar.gz", type: "targz", }, }, "linux-arm64": { gbdk: { url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/gbdk-next/gbdk-linux-arm64.tar.gz", type: "targz", }, }, "win32-ia32": { gbdk: { url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/gbdk-next/gbdk-win32.zip", type: "zip", }, }, "win32-x64": { gbdk: { url: "https://github.com/gbdk-2020/gbdk-2020/releases/download/gbdk-next/gbdk-win64.zip", type: "zip", }, }, } as const; type Arch = keyof typeof dependencies; const archs = Object.keys(dependencies) as Array<Arch>; const localArch = `${process.platform}-${process.arch}`; const fetchAll = process.argv.includes("--all"); const updateLock = process.argv.includes("--update-lock"); const why = process.argv.includes("--why"); const fetchArch = process.argv .find((arg) => arg.startsWith("--arch=")) ?.replace("--arch=", "") ?? localArch; const extractTarGz = async ( archivePath: string, outputDir: string, ): Promise<void> => { console.log(`Extract tar to "${outputDir}"`); const res = spawn("tar", ["-zxf", archivePath, "-C", outputDir], {}, {}); await res.completed; console.log("✅ Done"); }; const extractZip = async ( archivePath: string, outputDir: string, ): Promise<void> => { console.log(`Extract zip to "${outputDir}"`); const zip = new AdmZip(archivePath); await zip.extractAllTo(outputDir, true); console.log("✅ Done"); }; export const sha256File = async (filePath: string): Promise<string> => new Promise((resolve, reject) => { const hash = createHash("sha256"); const stream = createReadStream(filePath); stream.on("error", reject); hash.on("error", reject); stream .pipe(hash) .setEncoding("hex") .on("finish", () => { resolve(hash.read()); }); }); const listFilesRecursive = async ( root: string, dir = root, result: string[] = [], ): Promise<string[]> => { const entries = await readdir(dir, { withFileTypes: true }); for (const e of entries) { const full = Path.join(dir, e.name); if (e.isDirectory()) { await listFilesRecursive(root, full, result); } else { result.push(Path.relative(root, full)); } } return result; }; const hashFolder = async (root: string): Promise<Record<string, string>> => { const files = await listFilesRecursive(root); const hashes: Record<string, string> = {}; for (const file of files) { const full = Path.join(root, file); hashes[file] = await sha256File(full); } return hashes; }; const diffFolders = async (oldDir: string, newDir: string) => { const oldHashes = await hashFolder(oldDir); const newHashes = await hashFolder(newDir); const added: string[] = []; const removed: string[] = []; const modified: string[] = []; const oldFiles = new Set(Object.keys(oldHashes)); const newFiles = new Set(Object.keys(newHashes)); for (const file of newFiles) { if (!oldFiles.has(file)) { added.push(file); } else Iif (oldHashes[file] !== newHashes[file]) { modified.push(file); } } for (const file of oldFiles) { Iif (!newFiles.has(file)) { removed.push(file); } } return { added, removed, modified }; }; export const fetchGBDKDependency = async ( arch: Arch, expectedChecksum?: string, ) => { console.log(`Fetching GBDK for arch=${arch}`); const { url, type } = dependencies[arch].gbdk; console.log(`URL=${url}`); const response = await fetch(url); const buffer = await response.arrayBuffer(); // Get a Buffer from the response const data = Buffer.from(buffer); const tmpPath = Path.join(buildToolsRoot, "tmp.data"); await writeFile(tmpPath, data); console.log(`Written to "${tmpPath}"`); const checksum = await sha256File(tmpPath); Iif (expectedChecksum && checksum !== expectedChecksum && !updateLock) { Iif (why) { console.log("⚠️ Checksum mismatch. Investigating differences..."); const tmpExtract = Path.join(buildToolsRoot, `tmp_extract_${arch}`); await ensureDir(tmpExtract); if (type === "targz") { await extractTarGz(tmpPath, tmpExtract); } else { await extractZip(tmpPath, tmpExtract); } const existingDir = Path.join(buildToolsRoot, arch); if (await pathExists(existingDir)) { const diff = await diffFolders(existingDir, tmpExtract); Iif (diff.added.length) console.log("\n➕ Added files:"); diff.added.forEach((f) => console.log(" +", f)); Iif (diff.removed.length) console.log("\n➖ Removed files:"); diff.removed.forEach((f) => console.log(" -", f)); Iif (diff.modified.length) console.log("\n✏️ Modified files:"); diff.modified.forEach((f) => console.log(" *", f)); } else { console.log("No existing directory to compare against."); } await remove(tmpExtract); } throw new Error( `Checksum mismatch for ${arch}. Expected: ${expectedChecksum}, Got: ${checksum}.`, ); } const gbdkArchPath = Path.join(buildToolsRoot, arch); await ensureDir(gbdkArchPath); if (type === "targz") { await extractTarGz(tmpPath, gbdkArchPath); } else { await extractZip(tmpPath, gbdkArchPath); } await remove(tmpPath); return checksum; }; const main = async () => { let lockFile: { gbdk: Record<string, string> } = { gbdk: {} }; Iif (!updateLock && (await pathExists(lockPath))) { lockFile = await readJSON(lockPath); } await ensureDir(buildToolsRoot); for (const arch of archs) { Iif (fetchAll || arch === fetchArch) { const checksum = await fetchGBDKDependency(arch, lockFile.gbdk[arch]); lockFile.gbdk[arch] = checksum; } } await writeFile(lockPath, JSON.stringify(lockFile, null, 2)); }; main().catch((e) => { console.error(`❌ Error: `, e); Iif ( e instanceof Error && e.message && e.message.includes("Checksum mismatch") ) { console.log(""); console.log("A new release of GBDK may have been published."); console.log("To update lock file with new checksums run:"); console.log(""); console.log(`${process.argv.join(" ")} --update-lock`); console.log(""); } process.exit(1); }); |