nx/packages/tao/src/shared/params.ts
2019-07-24 10:27:47 -04:00

51 lines
1.3 KiB
TypeScript

import { logging } from '@angular-devkit/core';
import { UnsuccessfulWorkflowExecution } from '@angular-devkit/schematics';
export type Schema = {
properties: { [p: string]: any };
required: string[];
description: string;
};
export async function handleErrors(logger: logging.Logger, fn: Function) {
try {
return await fn();
} catch (err) {
if (err instanceof UnsuccessfulWorkflowExecution) {
logger.fatal('The Schematic workflow failed. See above.');
} else {
logger.fatal(err.message);
}
return 1;
}
}
export function convertToCamelCase(parsed: {
[k: string]: any;
}): { [k: string]: any } {
return Object.keys(parsed).reduce(
(m, c) => ({ ...m, [camelCase(c)]: parsed[c] }),
{}
);
}
function camelCase(input: string): string {
if (input.indexOf('-') > 1) {
return input
.toLowerCase()
.replace(/-(.)/g, (match, group1) => group1.toUpperCase());
} else {
return input;
}
}
export function coerceTypes(opts: { [k: string]: any }, schema: 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;
}