create-react-native-library
Version:
CLI to scaffold React Native libraries
66 lines (63 loc) • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.assertNpxExists = assertNpxExists;
exports.assertUserInput = assertUserInput;
var _kleur = _interopRequireDefault(require("kleur"));
var _spawn = require("./spawn");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
async function assertNpxExists() {
try {
await (0, _spawn.spawn)('npx', ['--help']);
} catch (error) {
// @ts-expect-error: TS doesn't know about `code`
if (error != null && error.code === 'ENOENT') {
console.log(`Couldn't find ${_kleur.default.blue('npx')}! Please install it by running ${_kleur.default.blue('npm install -g npx')}`);
process.exit(1);
} else {
throw error;
}
}
}
/**
* Makes sure the answers are in expected form and ends the process with error if they are not
*/
function assertUserInput(questions, answers) {
for (const [key, value] of Object.entries(answers)) {
if (value == null) {
continue;
}
const question = questions.find(q => q.name === key);
if (question == null) {
continue;
}
let validation;
// We also need to guard against invalid choices
// If we don't already have a validation message to provide a better error
if ('choices' in question) {
const choices = typeof question.choices === 'function' ? question.choices(undefined, answers) : question.choices;
if (choices && choices.every(choice => choice.value !== value)) {
if (choices.length > 1) {
validation = `Must be one of ${choices.map(choice => _kleur.default.green(choice.value)).join(', ')}`;
} else if (choices[0]) {
validation = `Must be '${_kleur.default.green(choices[0].value)}'`;
} else {
validation = false;
}
}
}
if (validation == null && question.validate) {
validation = question.validate(String(value));
}
if (validation != null && validation !== true) {
let message = `Invalid value ${_kleur.default.red(String(value))} passed for ${_kleur.default.blue(key)}`;
if (typeof validation === 'string') {
message += `: ${validation}`;
}
console.log(message);
process.exit(1);
}
}
}
//# sourceMappingURL=assert.js.map