|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { parseSha256Sums } from "../src/distribution/checksums"; |
| 4 | +import { renderChocolateyPackageFiles } from "../src/distribution/chocolatey"; |
| 5 | + |
| 6 | +function main(): void { |
| 7 | + const args = process.argv.slice(2); |
| 8 | + const version = readArg(args, "--version") ?? readVersionFromPackageJson(); |
| 9 | + const checksumsPath = path.resolve(readArg(args, "--checksums") ?? "dist/SHA256SUMS"); |
| 10 | + const outputDir = path.resolve(readArg(args, "--output-dir") ?? "packaging/chocolatey"); |
| 11 | + const releaseUrlBase = |
| 12 | + readArg(args, "--release-url-base") ?? |
| 13 | + `https://github.com/echohello-dev/skillet/releases/download/v${version}`; |
| 14 | + |
| 15 | + if (!fs.existsSync(checksumsPath)) { |
| 16 | + throw new Error(`Checksums file not found: ${checksumsPath}`); |
| 17 | + } |
| 18 | + |
| 19 | + const checksums = parseSha256Sums(fs.readFileSync(checksumsPath, "utf8")); |
| 20 | + const files = renderChocolateyPackageFiles({ version, releaseUrlBase, checksumsByArtifact: checksums }); |
| 21 | + |
| 22 | + fs.mkdirSync(path.join(outputDir, "tools"), { recursive: true }); |
| 23 | + fs.writeFileSync(path.join(outputDir, "skillet.nuspec"), files.nuspec); |
| 24 | + fs.writeFileSync(path.join(outputDir, "tools", "chocolateyinstall.ps1"), files.installScript); |
| 25 | + fs.writeFileSync(path.join(outputDir, "tools", "chocolateyuninstall.ps1"), files.uninstallScript); |
| 26 | + console.log(`Wrote Chocolatey package files to ${outputDir}`); |
| 27 | +} |
| 28 | + |
| 29 | +function readArg(args: string[], name: string): string | undefined { |
| 30 | + for (let i = 0; i < args.length; i += 1) { |
| 31 | + const token = args[i]; |
| 32 | + if (token === name) { |
| 33 | + const next = args[i + 1]; |
| 34 | + if (!next || next.startsWith("--")) { |
| 35 | + throw new Error(`Missing value for ${name}`); |
| 36 | + } |
| 37 | + return next; |
| 38 | + } |
| 39 | + |
| 40 | + const prefix = `${name}=`; |
| 41 | + if (token.startsWith(prefix)) { |
| 42 | + const value = token.slice(prefix.length); |
| 43 | + if (value.length === 0) { |
| 44 | + throw new Error(`Missing value for ${name}`); |
| 45 | + } |
| 46 | + return value; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return undefined; |
| 51 | +} |
| 52 | + |
| 53 | +function readVersionFromPackageJson(): string { |
| 54 | + const packageJsonPath = path.resolve("package.json"); |
| 55 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")) as { version?: string }; |
| 56 | + if (!packageJson.version || packageJson.version.length === 0) { |
| 57 | + throw new Error("package.json version is missing"); |
| 58 | + } |
| 59 | + |
| 60 | + return packageJson.version; |
| 61 | +} |
| 62 | + |
| 63 | +main(); |
0 commit comments