'use strict';
const { execFileSync } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const { npmCommand } = require('../../scripts/npm-command.cjs');
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');
const SELECTOR_ONLY = process.argv.includes('--selector-only');
const PACK_SELECTOR = SELECTOR_ONLY || process.env.TURNDB_CLI_PACK_SELECTOR !== '0';
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 executable = TARGET.includes('windows') ? 'turndb.exe' : 'turndb';
const built = path.join(ROOT, 'target', TARGET, 'native-release', executable);
if (!fs.existsSync(built)) throw new Error(`cargo did not produce ${built}`);
fs.copyFileSync(built, path.join(platformDir, executable));
if (process.platform !== 'win32') fs.chmodSync(path.join(platformDir, executable), 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, executable)).size;
console.log(`${SLICE}: turndb binary ${size} bytes`);
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`);
}
}
if (!SELECTOR_ONLY) {
const npm = npmCommand(['pack', platformDir, '--pack-destination', DIST]);
run(npm.file, npm.args);
}
if (PACK_SELECTOR) {
const npm = npmCommand(['pack', CLI_DIR, '--pack-destination', DIST]);
run(npm.file, npm.args);
}
} 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'));
for (const [name, pinned] of Object.entries(selector.optionalDependencies ?? {})) {
const slice = name.replace('@turndb/cli-', '');
const manifestPath = path.join(CLI_DIR, 'npm', slice, 'package.json');
if (!fs.existsSync(manifestPath)) {
throw new Error(`selector pins ${name} but cli/npm/${slice} does not exist`);
}
const declared = JSON.parse(fs.readFileSync(manifestPath, 'utf8')).version;
if (pinned !== declared) {
throw new Error(
`selector pins ${name}@${pinned} but that package declares ${declared}; `
+ 'the lockstep version sync did not cover one of them',
);
}
}
if (selector.optionalDependencies?.[platform.name] === undefined) {
throw new Error(`the selector does not pin ${platform.name}, so this slice is unreachable`);
}
const artifactFiles = [
path.join(DIST, `turndb-cli-${SLICE}-${platform.version}.tgz`),
...(PACK_SELECTOR ? [path.join(DIST, `turndb-cli-${selector.version}.tgz`)] : []),
];
run(
process.platform === 'win32' ? 'python' : 'python3',
[
path.join(ROOT, 'scripts', 'release-artifacts.py'),
'create',
'--component', `cli-${SLICE}`,
'--version', selector.version,
'--output', path.join(DIST, `cli-manifest-${SLICE}.json`),
...artifactFiles,
],
{ cwd: ROOT },
);
console.log(`packed ${fs.readdirSync(DIST).sort().join(', ')}`);