|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import util from 'node:util'; |
| 4 | +import process from 'node:process'; |
| 5 | +import fs from 'node:fs/promises'; |
| 6 | +import child_process from 'node:child_process'; |
| 7 | +import events from 'node:events'; |
| 8 | +import path from 'node:path'; |
| 9 | +import url from 'node:url'; |
| 10 | + |
| 11 | +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); |
| 12 | + |
| 13 | +/** Resolves to the root of this repository */ |
| 14 | +function resolveRoot(...paths) { |
| 15 | + return path.resolve(__dirname, '..', '..', ...paths); |
| 16 | +} |
| 17 | + |
| 18 | +async function parseArguments() { |
| 19 | + const pkg = JSON.parse(await fs.readFile(resolveRoot('package.json'), 'utf8')); |
| 20 | + |
| 21 | + const options = { |
| 22 | + 'kerberos_use_rtld': { type: 'boolean', default: false }, |
| 23 | + help: { short: 'h', type: 'boolean', default: false } |
| 24 | + }; |
| 25 | + |
| 26 | + const args = util.parseArgs({ args: process.argv.slice(2), options, allowPositionals: false }); |
| 27 | + |
| 28 | + if (args.values.help) { |
| 29 | + console.log( |
| 30 | + `${path.basename(process.argv[1])} ${[...Object.keys(options)] |
| 31 | + .filter(k => k !== 'help') |
| 32 | + .map(k => `[--${k}=${options[k].type}]`) |
| 33 | + .join(' ')}` |
| 34 | + ); |
| 35 | + process.exit(0); |
| 36 | + } |
| 37 | + |
| 38 | + return { |
| 39 | + kerberos_use_rtld: !!args.values.kerberos_use_rtld, |
| 40 | + pkg |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +/** `xtrace` style command runner, uses spawn so that stdio is inherited */ |
| 45 | +async function run(command, args = [], options = {}) { |
| 46 | + const commandDetails = `+ ${command} ${args.join(' ')}${options.cwd ? ` (in: ${options.cwd})` : ''}`; |
| 47 | + console.error(commandDetails); |
| 48 | + const proc = child_process.spawn(command, args, { |
| 49 | + shell: process.platform === 'win32', |
| 50 | + stdio: 'inherit', |
| 51 | + cwd: resolveRoot('.'), |
| 52 | + ...options |
| 53 | + }); |
| 54 | + await events.once(proc, 'exit'); |
| 55 | + |
| 56 | + if (proc.exitCode != 0) throw new Error(`CRASH(${proc.exitCode}): ${commandDetails}`); |
| 57 | +} |
| 58 | + |
| 59 | +async function buildBindings(args, pkg) { |
| 60 | + await fs.rm(resolveRoot('build'), { force: true, recursive: true }); |
| 61 | + await fs.rm(resolveRoot('prebuilds'), { force: true, recursive: true }); |
| 62 | + |
| 63 | + // install with "ignore-scripts" so that we don't attempt to download a prebuild |
| 64 | + await run('npm', ['install', '--ignore-scripts']); |
| 65 | + // The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code) |
| 66 | + // it will also produce `./prebuilds/kerberos-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`. |
| 67 | + |
| 68 | + let gypDefines = process.env.GYP_DEFINES ?? ''; |
| 69 | + if (args.kerberos_use_rtld) { |
| 70 | + gypDefines += ' kerberos_use_rtld=true'; |
| 71 | + } |
| 72 | + |
| 73 | + gypDefines = gypDefines.trim(); |
| 74 | + const prebuildOptions = |
| 75 | + gypDefines.length > 0 |
| 76 | + ? { env: { ...process.env, GYP_DEFINES: gypDefines } } |
| 77 | + : undefined; |
| 78 | + |
| 79 | + await run('npm', ['run', 'prebuild'], prebuildOptions); |
| 80 | + |
| 81 | + await run('npm', ['run', 'prepare']); |
| 82 | + |
| 83 | + if (process.platform === 'darwin' && process.arch === 'arm64') { |
| 84 | + // The "arm64" build is actually a universal binary |
| 85 | + const armTar = `kerberos-v${pkg.version}-napi-v4-darwin-arm64.tar.gz`; |
| 86 | + const x64Tar = `kerberos-v${pkg.version}-napi-v4-darwin-x64.tar.gz`; |
| 87 | + await fs.copyFile(resolveRoot('prebuilds', armTar), resolveRoot('prebuilds', x64Tar)); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +async function main() { |
| 92 | + const { pkg, ...args } = await parseArguments(); |
| 93 | + console.log(args); |
| 94 | + await buildBindings(args, pkg); |
| 95 | +} |
| 96 | + |
| 97 | +await main(); |
0 commit comments