| 1 | import { execFileSync } from "node:child_process"; |
| 2 | import { cpSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | import { dirname, join } from "node:path"; |
| 4 | import { fileURLToPath } from "node:url"; |
| 5 | import { publishPackages } from "./publish.mjs"; |
| 6 | |
| 7 | const HERE = dirname(fileURLToPath(import.meta.url)); |
| 8 | const ROOT = join(HERE, ".."); |
| 9 | const STAGE = join(HERE, ".stage"); |
| 10 | |
| 11 | const TARGETS = [ |
| 12 | { node: "darwin-arm64", goos: "darwin", goarch: "arm64" }, |
| 13 | { node: "darwin-x64", goos: "darwin", goarch: "amd64" }, |
| 14 | { node: "linux-arm64", goos: "linux", goarch: "arm64" }, |
| 15 | { node: "linux-x64", goos: "linux", goarch: "amd64" }, |
| 16 | { node: "win32-arm64", goos: "windows", goarch: "arm64" }, |
| 17 | { node: "win32-x64", goos: "windows", goarch: "amd64" }, |
| 18 | ]; |
| 19 | |
| 20 | const tag = process.argv[2] ?? process.env.GITHUB_REF_NAME; |
| 21 | if (!tag) { |
| 22 | console.error("usage: node npm/build.mjs <tag> (e.g. v1.0.0 or npm-v1.0.0)"); |
| 23 | process.exit(1); |
| 24 | } |
| 25 | // npm ships on its own `npm-v*` tag (release-npm.yml); also accept a bare `v*`. |
| 26 | const version = tag.replace(/^(npm-)?v/, ""); |
| 27 | const binaryVersion = `v${version}`; |
| 28 | const publish = process.argv.includes("--publish"); |
| 29 | const candidateSha = execFileSync("git", ["rev-parse", "HEAD"], { |
| 30 | cwd: ROOT, |
| 31 | encoding: "utf8", |
| 32 | }).trim(); |
| 33 | const gitCommit = candidateSha.slice(0, 12); |
| 34 | // Real UTC build clock for version --verbose/--json (not VCS commit time). |
| 35 | const buildTimeUTC = new Date().toISOString().replace(/\.\d{3}Z$/, "Z"); |
| 36 | |
| 37 | rmSync(STAGE, { recursive: true, force: true }); |
| 38 | mkdirSync(STAGE, { recursive: true }); |
| 39 | |
| 40 | const subPackages = []; |
| 41 | for (const t of TARGETS) { |
| 42 | const name = `@reasonix/cli-${t.node}`; |
| 43 | const dir = join(STAGE, `cli-${t.node}`); |
| 44 | const exe = t.goos === "windows" ? "reasonix.exe" : "reasonix"; |
| 45 | mkdirSync(join(dir, "bin"), { recursive: true }); |
| 46 | |
| 47 | console.log(`build ${t.goos}/${t.goarch} -> ${name}`); |
| 48 | execFileSync( |
| 49 | "go", |
| 50 | [ |
| 51 | "build", |
| 52 | "-trimpath", |
| 53 | "-ldflags", |
| 54 | `-s -w -X main.version=${binaryVersion} -X main.gitCommit=${gitCommit} -X main.buildTimeUTC=${buildTimeUTC} -X reasonix/internal/productdocs.linkedVersion=${binaryVersion} -X reasonix/internal/productdocs.linkedRevision=${candidateSha}`, |
| 55 | "-o", |
| 56 | join(dir, "bin", exe), |
| 57 | "./cmd/reasonix", |
| 58 | ], |
| 59 | { |
| 60 | cwd: ROOT, |
| 61 | stdio: "inherit", |
| 62 | env: { ...process.env, CGO_ENABLED: "0", GOOS: t.goos, GOARCH: t.goarch }, |
| 63 | }, |
| 64 | ); |
| 65 | |
| 66 | writeFileSync( |
| 67 | join(dir, "package.json"), |
| 68 | `${JSON.stringify( |
| 69 | { |
| 70 | name, |
| 71 | version, |
| 72 | description: `reasonix prebuilt binary for ${t.node}.`, |
| 73 | os: [t.goos === "windows" ? "win32" : t.goos], |
| 74 | cpu: [t.goarch === "amd64" ? "x64" : "arm64"], |
| 75 | files: ["bin/"], |
| 76 | license: "MIT", |
| 77 | repository: { |
| 78 | type: "git", |
| 79 | url: "git+https://github.com/esengine/DeepSeek-Reasonix.git", |
| 80 | }, |
| 81 | reasonixCandidateSha: candidateSha, |
| 82 | }, |
| 83 | null, |
| 84 | 2, |
| 85 | )}\n`, |
| 86 | ); |
| 87 | subPackages.push({ name, dir }); |
| 88 | } |
| 89 | |
| 90 | const mainDir = join(STAGE, "reasonix"); |
| 91 | mkdirSync(mainDir, { recursive: true }); |
| 92 | cpSync(join(HERE, "reasonix", "bin"), join(mainDir, "bin"), { recursive: true }); |
| 93 | cpSync(join(ROOT, "README.md"), join(mainDir, "README.md")); |
| 94 | |
| 95 | const mainPkg = JSON.parse( |
| 96 | readFileSync(join(HERE, "reasonix", "package.json"), "utf8"), |
| 97 | ); |
| 98 | mainPkg.version = version; |
| 99 | mainPkg.reasonixCandidateSha = candidateSha; |
| 100 | for (const key of Object.keys(mainPkg.optionalDependencies)) { |
| 101 | mainPkg.optionalDependencies[key] = version; |
| 102 | } |
| 103 | writeFileSync( |
| 104 | join(mainDir, "package.json"), |
| 105 | `${JSON.stringify(mainPkg, null, 2)}\n`, |
| 106 | ); |
| 107 | |
| 108 | if (!publish) { |
| 109 | console.log(`\nstaged ${version} in ${STAGE} (dry run; pass --publish to publish)`); |
| 110 | process.exit(0); |
| 111 | } |
| 112 | |
| 113 | // Publish every immutable package before advancing the public channel. Recovery |
| 114 | // reuses packages that already prove the same candidate SHA, fills only missing |
| 115 | // packages, and never moves latest/next/canary back to an older version. |
| 116 | publishPackages({ |
| 117 | packages: [...subPackages, { name: "reasonix", dir: mainDir }], |
| 118 | version, |
| 119 | candidateSha, |
| 120 | }); |
| 121 |