'use strict';
const { execFileSync } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const RELEASE = process.argv.includes('--release');
const CLI_DIR = path.resolve(__dirname, '..');
const ROOT = path.resolve(CLI_DIR, '..');
const TARGET = process.env.TURNDB_CLI_TARGET ?? 'x86_64-unknown-linux-gnu';
const SLICE = process.env.TURNDB_CLI_SLICE ?? 'linux-x64-gnu';
const DIST = path.join(CLI_DIR, 'dist');
function run(cmd, args, opts = {}) {
execFileSync(cmd, args, { stdio: 'inherit', ...opts });
}
const platformDir = path.join(CLI_DIR, 'npm', SLICE);
if (!fs.existsSync(platformDir)) {
throw new Error(`no platform package for slice ${SLICE} at ${platformDir}`);
}
run('cargo', ['build', '--profile', 'native-release', '--target', TARGET, '--bin', 'turndb'], {
cwd: ROOT,
});
const built = path.join(ROOT, 'target', TARGET, 'native-release', 'turndb');
if (!fs.existsSync(built)) throw new Error(`cargo did not produce ${built}`);
fs.copyFileSync(built, path.join(platformDir, 'turndb'));
fs.chmodSync(path.join(platformDir, 'turndb'), 0o755);
for (const file of ['LICENSE', 'NOTICE', 'THIRD_PARTY_LICENSES.html']) {
fs.copyFileSync(path.join(ROOT, file), path.join(platformDir, file));
fs.copyFileSync(path.join(ROOT, file), path.join(CLI_DIR, file));
}
fs.copyFileSync(path.join(CLI_DIR, 'README.md'), path.join(platformDir, 'README.md'));
const size = fs.statSync(path.join(platformDir, 'turndb')).size;
console.log(`${SLICE}: turndb binary ${size} bytes`);
fs.rmSync(DIST, { recursive: true, force: true });
fs.mkdirSync(DIST, { recursive: true });
const manifests = [path.join(CLI_DIR, 'package.json'), path.join(platformDir, 'package.json')];
const originals = manifests.map((file) => fs.readFileSync(file, 'utf8'));
try {
if (RELEASE) {
for (const file of manifests) {
const json = JSON.parse(fs.readFileSync(file, 'utf8'));
delete json.private;
fs.writeFileSync(file, `${JSON.stringify(json, null, 2)}\n`);
}
}
run('npm', ['pack', platformDir, '--pack-destination', DIST]);
run('npm', ['pack', CLI_DIR, '--pack-destination', DIST]);
} finally {
manifests.forEach((file, i) => fs.writeFileSync(file, originals[i]));
}
const selector = JSON.parse(fs.readFileSync(path.join(CLI_DIR, 'package.json'), 'utf8'));
const platform = JSON.parse(fs.readFileSync(path.join(platformDir, 'package.json'), 'utf8'));
const pinned = selector.optionalDependencies?.[platform.name];
if (pinned !== platform.version) {
throw new Error(
`selector pins ${platform.name}@${pinned} but the packed platform package is `
+ `${platform.version}; the lockstep version sync did not cover one of them`,
);
}
console.log(`packed ${fs.readdirSync(DIST).sort().join(', ')}`);