#!/usr/bin/env node
import { existsSync, readFileSync, writeFileSync, unlinkSync } from "fs";
import { join } from "path";
import { execSync } from "child_process";
const cwd = process.cwd();
const progressFile = join(cwd, "test-progress.json");
const reset = process.argv.includes("--reset");
let progress = { lastAttempted: "" };
if (existsSync(progressFile) && !reset) {
try {
progress = JSON.parse(readFileSync(progressFile, "utf8"));
} catch (error) {
console.error(`Invalid test-progress.json, resetting: ${error.message}`);
unlinkSync(progressFile);
}
}
const steps = [
{
name: "workspace",
command:
"npm run compile:test --if-present --workspace=@bitcoin-computer/swap2 && BCN_CHAIN=LTC BCN_NETWORK=regtest POSTGRES_HOST=127.0.0.1 BITCOIN_RPC_HOST=127.0.0.1 BCN_ZMQ_URL=tcp://127.0.0.1:28332 mocha --config .mocharc.json",
},
{
name: "monorepo",
command: "npm run test:mocha:all --prefix ./monorepo",
},
{
name: "lib-secret",
command: "npm run test --workspace=@bitcoin-computer/lib-secret",
},
{
name: "vite-test",
command: "npm run test:puppeteer:build",
},
{
name: "browser-test",
command: "npm run test:browser",
},
];
let startIndex = 0;
if (!reset && progress.lastAttempted) {
const lastIndex = steps.findIndex(
(step) => step.name === progress.lastAttempted
);
if (lastIndex >= 0 && lastIndex < steps.length - 1) {
startIndex = lastIndex + 1;
} else {
// If last step was attempted or progress is invalid, reset
if (existsSync(progressFile)) unlinkSync(progressFile);
}
}
const stdio = "inherit";
// Clear existing test-results.json files
try {
execSync("find . -name 'test-results.json' -type f -delete", { cwd, stdio });
} catch (error) {
console.error(`Error clearing test-results.json files: ${error.message}`);
}
// Run steps
for (let i = startIndex; i < steps.length; i++) {
const { name, command } = steps[i];
console.log(`Running step: ${name}`);
// Save progress before running the step
progress.lastAttempted = name;
writeFileSync(progressFile, JSON.stringify(progress, null, 2));
try {
execSync(command, { cwd, stdio });
} catch (error) {
console.error(`Step ${name} failed: ${error.message}`);
process.exit(1);
}
}
// If all steps completed, clear progress
if (existsSync(progressFile)) {
unlinkSync(progressFile);
}
Some points to refine the
test:reruncommand:tscto command for testing lib-secretstepsto be an array of commandsCompile testtobuild test