api
Version:
Magical SDK generation from an OpenAPI definition 🪄
47 lines • 1.7 kB
JavaScript
// oxlint-disable no-param-reassign
import prompts from 'prompts';
import isCI from './isCI.js';
/**
* Runs a check before every prompt renders to make sure that prompt is not being run in a CI
* environment.
*/
function onRender() {
if (isCI()) {
process.stdout.write('\n');
process.stdout.write('Yikes! Looks like we were about to prompt you for something in a CI environment. Are you missing an argument?');
process.stdout.write('\n\n');
process.stdout.write('Try running `api <command> --help` to get more information.');
process.stdout.write('\n\n');
process.exit(1);
}
}
/**
* The `prompts` library doesn't always interpret CTRL+C and release the terminal back to the user
* so we need handle this ourselves. This function is just a simple overload of the main `prompts`
* import that we use.
*
* @see {@link https://github.com/terkelg/prompts/issues/252}
*/
export default async function promptTerminal(questions, options) {
if (Array.isArray(questions)) {
questions = questions.map(question => ({ onRender, ...question }));
}
else {
questions.onRender = onRender;
}
return prompts(questions, {
/**
* The CTRL+C handler discussed above.
* @see {@link https://github.com/terkelg/prompts#optionsoncancel}
*/
onCancel: () => {
// If we don't re-enable the terminal cursor before exiting the program, the cursor will
// remain hidden.
process.stdout.write('\x1B[?25h');
process.stdout.write('\n');
process.exit(1);
},
...options,
});
}
//# sourceMappingURL=prompt.js.map