diff --git a/lib/cli.js b/lib/cli.js index 06c9658..af63e6e 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -13,6 +13,21 @@ const registryClient = "NPM"; // TODO: Extend? const readline = createInterface(process.stdin, process.stdout); +const terminateWithError = (err) => { + const printStacktrace = args.includes("--stacktrace"); + + console.error(`\x1b[31m${ + (err.message && (!printStacktrace || !err.stack)) + ? (err.message.replace(/^(\w+)?Error: */, "")) + : `${err}${err.stack ? `\n${err.stack}` : ""}` + }\x1b[0m`); + + process.exit(1); +}; +process.on("uncaughtException", terminateWithError); +process.on("unhandledRejection", terminateWithError); + + if(args[0] === "help") { console.log(readFileSync(join(__dirname, "../help.txt")).toString()); @@ -25,11 +40,8 @@ if(args[0] === "help") { const incrementType = (await prompt("Type of increment (major|\x1b[4mminor\x1b[24m|patch)")) || "minor"; const rootPackagePath = resolve(args.find(arg => !/^-[^/]+$/.test(arg)) ?? "."); if(!isDryRun - && util.exec("git status --porcelain", rootPackagePath, true)) { - terminateWithError("Git working directory not clean"); - - process.exit(1); - } + && util.exec("git status --porcelain", rootPackagePath, true)) + throw "Git working directory not clean"; const packages = []; try { @@ -68,9 +80,9 @@ if(args[0] === "help") { const nextTag = packages[0].nextTag; const confirmation = await prompt(`Are you sure to release ${nextTag}? (y/\x1b[4mn\x1b[24m)`); - (confirmation !== "y") - && terminateWithError("Release aborted."); - + if(confirmation !== "y") + throw "Release aborted."; + packages .map((package, i) => { if(!i) return package; @@ -80,21 +92,14 @@ if(args[0] === "help") { }) .forEach(package => { const clientArgIndex = args.indexOf("--client"); - try { - api.release(package, ~clientArgIndex ? args[clientArgIndex + 1] : "npm", { - dry: isDryRun - }); - } catch(err) { - terminateWithError(err); - } + + api.release(package, ~clientArgIndex ? args[clientArgIndex + 1] : "npm", { + dry: isDryRun + }); }); - try { - args.includes("--github") - && api.referToGithub(rootPackagePath, nextTag); - } catch(err) { - terminateWithError(err); - } + args.includes("--github") + && api.referToGithub(rootPackagePath, nextTag); console.log(`\x1b[33mReleased ${nextTag} to ${registryClient}.\x1b[0m`); @@ -102,18 +107,6 @@ if(args[0] === "help") { })(); -function terminateWithError(err) { - const printStacktrace = args.includes("--stacktrace"); - - console.error(`\x1b[31m${ - (err.message && (!printStacktrace || !err.stack)) - ? (err.message.replace(/^(\w+)?Error: */, "")) - : `${err.message}${err.stack ? `\n${err.stack}` : ""}` - }\x1b[0m`); - - process.exit(1); -} - function prompt(question) { return new Promise((resolve) => { readline.question(`\x1b[34m${question.trim()}\x1b[0m `, (answer) => { diff --git a/test/repo/package.json b/test/repo/package.json index 3c796d4..5e511f2 100644 --- a/test/repo/package.json +++ b/test/repo/package.json @@ -1,8 +1,8 @@ { "name": "@example.org/example", - "version": "1.0.0", + "version": "0.5.0", "repository": { "type": "git", "url": "git+https://github.com/rapidjs-org/releasing.git" } -} +} \ No newline at end of file