|
| 1 | +import { execSync } from "node:child_process"; |
| 2 | +import { copyFileSync, mkdtempSync, readdirSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +const REPO = "Z-Wave-Alliance/z-wave-stack-binaries"; |
| 7 | +const OUTPUT_DIR = "zwave_stack/bin"; |
| 8 | + |
| 9 | +const BINARIES = [ |
| 10 | + { |
| 11 | + pattern: /^ZW_zwave_ncp_serial_api_controller_.*_REALTIME_DEBUG\.elf$/, |
| 12 | + output: "ZW_zwave_ncp_serial_api_controller.elf", |
| 13 | + }, |
| 14 | + { |
| 15 | + pattern: /^ZW_zwave_ncp_serial_api_end_device_.*_REALTIME_DEBUG\.elf$/, |
| 16 | + output: "ZW_zwave_ncp_serial_api_end_device.elf", |
| 17 | + }, |
| 18 | +]; |
| 19 | + |
| 20 | +const tempDir = mkdtempSync(join(tmpdir(), "zwave-stack-")); |
| 21 | + |
| 22 | +try { |
| 23 | + console.log("Downloading latest Z-Wave stack binaries..."); |
| 24 | + execSync( |
| 25 | + `gh release download --repo ${REPO} --pattern "*Linux.tar.gz" -D "${tempDir}"`, |
| 26 | + { stdio: "inherit" }, |
| 27 | + ); |
| 28 | + |
| 29 | + const tarball = readdirSync(tempDir).find((f) => f.endsWith(".tar.gz")); |
| 30 | + if (!tarball) { |
| 31 | + throw new Error("No tarball found in downloaded files"); |
| 32 | + } |
| 33 | + |
| 34 | + console.log(`Extracting ${tarball}...`); |
| 35 | + execSync(`tar -xzf "${join(tempDir, tarball)}" -C "${tempDir}"`, { |
| 36 | + stdio: "inherit", |
| 37 | + }); |
| 38 | + |
| 39 | + const binDir = join(tempDir, "bin"); |
| 40 | + const files = readdirSync(binDir); |
| 41 | + |
| 42 | + for (const { pattern, output } of BINARIES) { |
| 43 | + const match = files.find((f) => pattern.test(f)); |
| 44 | + if (!match) { |
| 45 | + throw new Error(`No file matching ${pattern} found`); |
| 46 | + } |
| 47 | + |
| 48 | + const src = join(binDir, match); |
| 49 | + const dest = join(OUTPUT_DIR, output); |
| 50 | + console.log(`Copying ${match} -> ${output}`); |
| 51 | + copyFileSync(src, dest); |
| 52 | + } |
| 53 | + |
| 54 | + console.log("Done!"); |
| 55 | +} finally { |
| 56 | + rmSync(tempDir, { recursive: true, force: true }); |
| 57 | +} |
0 commit comments