Skip to content

Commit

Permalink
refactor: replace onState with onCancel
Browse files Browse the repository at this point in the history
I was looking through the docs and saw that onState was injected into every question in the question array and would run on every keystroke, and saw that onCancel existed and would look a lot cleaner in our prompt wrapper.
  • Loading branch information
kanadgupta committed Aug 19, 2022
1 parent fb60913 commit 16f6117
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src/lib/promptWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,13 @@ export default async function promptTerminal<T extends string = string>(
process.stdout.write('\x1B[?25h');
};

const onState = (state: { aborted: boolean }) => {
if (state.aborted) {
// If we don't re-enable the terminal cursor before exiting the program, the cursor will
// remain hidden.
enableTerminalCursor();
process.stdout.write('\n\n');
process.stdout.write('Thanks for using rdme! See you soon ✌️');
process.stdout.write('\n\n');
process.exit(1);
}
const onCancel = () => {
enableTerminalCursor();
process.stdout.write('\n');
process.stdout.write('Thanks for using rdme! See you soon ✌️');
process.stdout.write('\n\n');
process.exit(1);
};

if (Array.isArray(questions)) {
// eslint-disable-next-line no-param-reassign
questions = questions.map(question => ({ ...question, onState }));
} else {
// eslint-disable-next-line no-param-reassign
questions.onState = onState;
}

return prompts(questions, options);
return prompts(questions, { onCancel, ...options });
}

0 comments on commit 16f6117

Please sign in to comment.