Files
traxxx-web/tools/run-tool.js
T

36 lines
1.0 KiB
JavaScript

import { realpathSync } from 'node:fs';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import { knexOwner as knex } from '../src/knex.js';
import redis from '../src/redis.js';
// whether the module was run directly rather than imported, a stand-in for import.meta.main, which needs node 24.2 or 22.18
export function isMain(moduleUrl) {
if (!process.argv[1]) {
return false;
}
try {
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(moduleUrl));
}
catch (_error) {
return false;
}
}
// runs a command line tool and closes the connections afterwards, the knex pool and redis would otherwise keep the process alive
export default async function runTool(tool) {
try {
await tool();
await knex.destroy();
await redis.quit();
}
catch (error) {
// the manticore client rejects with a plain response object rather than an Error, print the server's message
console.error(error instanceof Error ? error : error?.body?.error || error?.body || error);
process.exit(1);
}
}