@speedscale/proxymock
Version:
A free desktop CLI that automatically creates mocks and tests from watching your app run.
75 lines (63 loc) • 1.82 kB
JavaScript
import { spawn } from "child_process";
import { existsSync } from "fs";
import {
getPackageVersion,
getInstalledVersion,
} from "../lib/version.js";
import { binaryPath } from "../lib/constants.js";
import logger from "../lib/log.js";
import install from "../lib/install.js";
/**
* Spawns the proxymock binary with the provided command line arguments
* Exits the process with the same code as proxymock
*/
function runProxyMock() {
const child = spawn(binaryPath, process.argv.slice(2), {
stdio: "inherit",
env: process.env,
});
child.on("error", (err) => {
logger.error(`Failed to start proxymock: ${err.message}`);
process.exit(1);
});
child.on("exit", (code) => {
process.exit(code || 0);
});
}
/**
* Installs proxymock and runs it, exiting on failure
*/
async function installAndRun() {
try {
await install();
} catch (error) {
logger.error(`Installation failed: ${error.message}`);
process.exit(1);
}
runProxyMock();
}
/**
* Main entry point - ensures the correct version of proxymock is installed
* before executing it. Performs version checking and automatic reinstallation
* if the installed version doesn't match the npm package version.
*/
async function main() {
const npmVersion = getPackageVersion();
const expectedVersion = `v${npmVersion}`;
// Check if proxymock is installed
if (!existsSync(binaryPath)) {
await installAndRun();
return;
}
// Binary exists, check if version matches
const installedVersion = await getInstalledVersion();
if (installedVersion === null || installedVersion !== expectedVersion) {
// Version unknown or mismatch, reinstall version from package
await installAndRun();
return;
}
// Version matches, run directly
runProxyMock();
}
main();