Skip to content

Commit

Permalink
Catch Promise rejection on SIGINT (#92)
Browse files Browse the repository at this point in the history
* Catch errors when exiting the changeset flow + Lint'd

* No need to repeat ourselves

* clearer function name

* Changeset was forgotten 🤦

* Swapping console for logger, removed unused code that was added

* Removed the plural in one of the messages

* Removing extra space

* Handling cancelations in the middle of the flow

* No need to repeat ourselves
  • Loading branch information
highvoltag3 authored and Noviny committed Jun 27, 2019
1 parent a700de8 commit e55fa3f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .changeset/serious-pillows-cry/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"releases": [{ "name": "@changesets/cli", "type": "minor" }],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/serious-pillows-cry/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Catch Promise rejection on SIGINT and exit gracefully
43 changes: 34 additions & 9 deletions packages/cli/src/utils/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid from "uuid/v1";
// @ts-ignore it's not worth writing a TS declaration file in this repo for a tiny module we use once like this
import termSize from "term-size";
import { prefix } from "./logger";
import logger, { prefix } from "./logger";
// @ts-ignore
import { prompt } from "enquirer";

Expand All @@ -13,6 +13,11 @@ import { prompt } from "enquirer";

const limit = Math.max(termSize().rows - 5, 10);

let cancelFlow = () => {
logger.success("Cancelled... 👋 ");
process.exit();
};

async function askCheckboxPlus(
message: string,
choices: Array<any>,
Expand All @@ -29,8 +34,13 @@ async function askCheckboxPlus(
multiple: true,
choices,
format,
limit
}).then((responses: any) => responses[name]);
limit,
onCancel: cancelFlow
})
.then((responses: any) => responses[name])
.catch((err: unknown) => {
logger.error(err);
});
}

async function askQuestion(message: string): Promise<string> {
Expand All @@ -42,9 +52,14 @@ async function askQuestion(message: string): Promise<string> {
message,
name,
// @ts-ignore
prefix
prefix,
onCancel: cancelFlow
}
]).then((responses: any) => responses[name]);
])
.then((responses: any) => responses[name])
.catch((err: unknown) => {
logger.error(err);
});
}

async function askConfirm(message: string): Promise<boolean> {
Expand All @@ -57,9 +72,14 @@ async function askConfirm(message: string): Promise<boolean> {
// @ts-ignore
prefix,
type: "confirm",
initial: true
initial: true,
onCancel: cancelFlow
}
]).then((responses: any) => responses[name]);
])
.then((responses: any) => responses[name])
.catch((err: unknown) => {
logger.error(err);
});
}

async function askList(
Expand All @@ -75,9 +95,14 @@ async function askList(
name,
// @ts-ignore
prefix,
type: "select"
type: "select",
onCancel: cancelFlow
}
]).then((responses: any) => responses[name]);
])
.then((responses: any) => responses[name])
.catch((err: unknown) => {
logger.error(err);
});
}

export { askCheckboxPlus, askQuestion, askConfirm, askList };

0 comments on commit e55fa3f

Please sign in to comment.