Skip to content

Commit

Permalink
Improve error interception
Browse files Browse the repository at this point in the history
  • Loading branch information
t-ski committed Nov 10, 2024
1 parent a455d69 commit a95da1a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
59 changes: 26 additions & 33 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -80,40 +92,21 @@ 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`);

process.exit(0);
})();


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) => {
Expand Down
4 changes: 2 additions & 2 deletions test/repo/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit a95da1a

Please sign in to comment.