UNPKG

@medflyt/test-db-service

Version:

Developer tool to instantly provision test PostgreSQL databases from a template

207 lines 7.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.main = void 0; const assert_never_1 = require("assert-never"); const childProcess = require("child_process"); const path = require("path"); const app_http_server_1 = require("./app_http_server"); const background_server_status_1 = require("./background_server_status"); async function help() { console.log(`Usage: ${process.argv[1]} [start|status|stop]`); console.log(); console.log(" --version Print version and exit"); console.log(" --help Print this help info and exit"); console.log(); console.log(" start (default) Start the server in the background"); console.log(" start-foreground Start the server in the foreground"); console.log(" status Print the status of the running server and exit"); console.log(" stop Exit a server running in the background"); return process.exit(0); } async function main() { const args = process.argv.slice(2); if (args.indexOf("--help") >= 0) { return await help(); } else if (args.indexOf("--version") >= 0) { return await version(); } else if (args.length === 0) { // Default command return await startBackground(new Date()); } else if (args.length !== 1) { return await invalid(args); } else if (args[0] === "start") { return await startBackground(new Date()); } else if (args[0] === "start-foreground") { return await startForeground(); } else if (args[0] === "status") { return await status(); } else if (args[0] === "stop") { return await stop(); } else { return await invalid(args); } } exports.main = main; function getVersion() { // eslint-disable-next-line @typescript-eslint/no-var-requires return require("../package.json").version; } async function version() { console.log(getVersion()); return process.exit(0); } async function startBackground(startTime) { const status = await background_server_status_1.getExistingServerStatus(); switch (status.type) { case "Running": console.log("Server already running"); return process.exit(0); case "NotRunning": return await launchBackground(); case "Faulty": if (new Date().getTime() - startTime.getTime() > 30000) { console.error("Error starting server"); console.error("Existing background server is FAULTY:"); console.error(" " + status.errMessage); return process.exit(2); } else { await delay(100); return await startBackground(startTime); } default: return assert_never_1.assertNever(status); } } async function launchBackground() { // Reference: // <https://github.com/silverwind/daemonize-process/blob/72815a7291dc4e115f0df11107c559e552fe626f/index.js> const id = "_DAEMONIZE_PROCESS"; console.log("Starting server in background..."); const script = process.argv[1]; const child = childProcess.spawn(process.execPath, [script].concat(["start-foreground"]), { env: Object.assign(Object.assign({}, process.env), { [id]: "1" }), cwd: path.parse(process.cwd()).root, stdio: "ignore", detached: true }); child.unref(); // Wait for background process to start: const startTime = new Date(); while (true) { const status = await background_server_status_1.getExistingServerStatus(); if (status.type === "Running") { console.log("Success"); console.log(`Server is listening on: http://localhost:${app_http_server_1.PORT}`); return process.exit(0); } if (child.exitCode !== null) { console.error(`Error starting background server: exit-code: ${child.exitCode}`); console.error("Try running `start-foreground` to diagnose"); return process.exit(1); } if (new Date().getTime() - startTime.getTime() > 30000) { switch (status.type) { case "NotRunning": console.error("Error starting background server: NotRunning after timeout"); console.error("WARNING: Background process may still be alive"); return process.exit(1); case "Faulty": console.error("Error starting background server: HTTP server is FAULTY after timeout:"); console.error(" " + status.errMessage); return process.exit(2); default: return assert_never_1.assertNever(status); } } await delay(50); } } async function startForeground() { try { await app_http_server_1.startHttpServer(`test-db-service ${getVersion()}`); return process.exit(0); } catch (err) { console.error("Error starting server:"); console.error(` ${err.code} ${err.message}`); return process.exit(1); } } async function status() { const status = await background_server_status_1.getExistingServerStatus(); switch (status.type) { case "Running": if (getVersion() === status.version) { console.log("Server is running"); return process.exit(0); } else { console.log("Server is running"); console.warn(`WARNING Running server has different version: ${status.version}`); return process.exit(0); } case "NotRunning": console.log("Server is NOT running"); return process.exit(1); case "Faulty": console.log("Server is FAULTY:"); console.log(" " + status.errMessage); return process.exit(2); default: return assert_never_1.assertNever(status); } } async function stop() { await background_server_status_1.backgroundServerSendShutdown(); const startTime = new Date(); while (true) { const status = await background_server_status_1.getExistingServerStatus(); if (status.type === "NotRunning") { return process.exit(0); } if (new Date().getTime() - startTime.getTime() > 30000) { switch (status.type) { case "Running": console.error("Error stopping server"); console.error("Server is still running"); return process.exit(1); case "Faulty": console.error("Error stopping server"); console.error("Server is FAULTY:"); console.error(" " + status.errMessage); return process.exit(2); default: return assert_never_1.assertNever(status); } } await delay(50); } } async function invalid(args) { console.log(`Invalid args: ${args.join(" ")}`); console.log(); console.log("Run:"); console.log(); console.log(` ${process.argv[1]} --help`); console.log(); console.log("For usage and help"); return process.exit(1); } async function delay(millis) { return await new Promise(resolve => setTimeout(resolve, millis)); } if (require.main === module) { require("source-map-support/register"); // eslint-disable-next-line @typescript-eslint/no-floating-promises main(); } //# sourceMappingURL=main.js.map