UNPKG

@destdefe/dev-utils

Version:

47 lines (37 loc) 1.35 kB
#!/usr/bin/env node const path = require("path"); const exec = require("./exec"); const log = require("./log"); const xargs = require("./xargs"); process.on("unhandledRejection", (err) => { log.error(err); }); process.on("uncaughtException", (err) => { log.error(err); }); function exitHandler(options, exitCode) { process.exit(); } // do something when app is closing process.on("exit", exitHandler.bind(null, { cleanup: true })); // catches ctrl+c event process.on("SIGINT", exitHandler.bind(null, { exit: true })); // catches "kill pid" (for example: nodemon restart) process.on("SIGUSR1", exitHandler.bind(null, { exit: true })); process.on("SIGUSR2", exitHandler.bind(null, { exit: true })); // catches uncaught exceptions process.on("uncaughtException", exitHandler.bind(null, { exit: true })); const xarg = xargs(); const binPathArray = xarg.bin.split("bin"); const scriptDir = path.join(binPathArray[0], 'bin', 'scripts'); const commands = xarg.commands; if (commands[1]) { commands[1] = `${commands[1]}.js`; } else { commands[1] = "index.js"; } const [folder, subFolder, ...rest] = commands; const scriptPath = path.join(scriptDir, folder, subFolder); const commandArgs = rest ? ` ${rest.join(" ")}` : '' const nodeCommand = `node ${scriptPath}${commandArgs}${xarg.args ? ` ${xarg.args.join(" ")}` : ''}`; exec(nodeCommand);