@yolkai/nx-tao
Version:
130 lines (129 loc) • 4.31 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const levenshtein = require("fast-levenshtein");
function handleErrors(logger, isVerbose, fn) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield fn();
}
catch (err) {
if (err instanceof schematics_1.UnsuccessfulWorkflowExecution) {
logger.fatal('The Schematic workflow failed. See above.');
}
else {
logger.fatal(err.message);
}
if (isVerbose && err.stack) {
logger.info(err.stack);
}
return 1;
}
});
}
exports.handleErrors = handleErrors;
function convertToCamelCase(parsed) {
return Object.keys(parsed).reduce((m, c) => (Object.assign({}, m, { [camelCase(c)]: parsed[c] })), {});
}
exports.convertToCamelCase = convertToCamelCase;
function camelCase(input) {
if (input.indexOf('-') > 1) {
return input
.toLowerCase()
.replace(/-(.)/g, (match, group1) => group1.toUpperCase());
}
else {
return input;
}
}
/**
* Coerces (and replaces) options identified as 'boolean' or 'number' in the Schema
*
* @param opts The options to check
* @param schema The schema definition with types to check against
*
*/
function coerceTypes(opts, schema) {
Object.keys(opts).forEach(k => {
if (schema.properties[k] && schema.properties[k].type == 'boolean') {
opts[k] = opts[k] === true || opts[k] === 'true';
}
else if (schema.properties[k] && schema.properties[k].type == 'number') {
opts[k] = Number(opts[k]);
}
});
return opts;
}
exports.coerceTypes = coerceTypes;
/**
* Converts any options passed in with short aliases to their full names if found
* Unmatched options are added to opts['--']
*
* @param opts The options passed in by the user
* @param schema The schema definition to check against
*/
function convertAliases(opts, schema) {
return Object.keys(opts).reduce((acc, k) => {
if (schema.properties[k]) {
acc[k] = opts[k];
}
else {
const found = Object.entries(schema.properties).find(([_, d]) => d.alias === k);
if (found) {
acc[found[0]] = opts[k];
}
else {
if (!acc['--']) {
acc['--'] = [];
}
acc['--'].push({
name: k,
possible: []
});
}
}
return acc;
}, {});
}
exports.convertAliases = convertAliases;
/**
* Tries to find what the user meant by unmatched commands
*
* @param opts The options passed in by the user
* @param schema The schema definition to check against
*
*/
function lookupUnmatched(opts, schema) {
if (opts['--']) {
const props = Object.keys(schema.properties);
opts['--'].forEach(unmatched => {
unmatched.possible = props.filter(p => levenshtein.get(p, unmatched.name) < 3);
});
}
return opts;
}
exports.lookupUnmatched = lookupUnmatched;
/**
* Converts aliases and coerces types according to the schema
*
* @param opts The options to check
* @param schema The schema definition to validate against
*
* @remarks
*
* Unmatched options are added to opts['--']
* and listed along with possible schema matches
*
*/
function validateOptions(opts, schema) {
return lookupUnmatched(convertAliases(coerceTypes(opts, schema), schema), schema);
}
exports.validateOptions = validateOptions;