atomic-reactor-cli
Version:
A CLI for creating Atomic Reactor Awesomeness
214 lines (192 loc) • 5.84 kB
JavaScript
/**
* -----------------------------------------------------------------------------
* Imports
* -----------------------------------------------------------------------------
*/
const chalk = require('chalk');
const generator = require('./generator');
const prettier = require('prettier');
const path = require('path');
const op = require('object-path');
const mod = path.dirname(require.main.filename);
const { error, message } = require(`${mod}/lib/messenger`);
/**
* NAME String
* @description Constant defined as the command name. Value passed to the commander.command() function.
* @example $ arcli update
* @see https://www.npmjs.com/package/commander#command-specific-options
* @since 2.0.0
*/
const NAME = 'actinium <update>';
/**
* DESC String
* @description Constant defined as the command description. Value passed to
* the commander.desc() function. This string is also used in the --help flag output.
* @see https://www.npmjs.com/package/commander#automated---help
* @since 2.0.0
*/
const DESC = 'Actinium: Update core';
/**
* CANCELED String
* @description Message sent when the command is canceled
* @since 2.0.0
*/
const CANCELED = 'Actinium update canceled!';
/**
* confirm({ props:Object, params:Object }) Function
* @description Prompts the user to confirm the operation
* @since 2.0.0
*/
const CONFIRM = ({ props, params, msg }) => {
const { prompt } = props;
msg = msg || chalk.white('Proceed?');
return new Promise((resolve, reject) => {
prompt.get(
{
properties: {
confirmed: {
description: `${msg} ${chalk.cyan('(Y/N):')}`,
type: 'string',
required: true,
pattern: /^y|n|Y|N/,
message: ` `,
before: val => {
return String(val).toUpperCase() === 'Y';
},
},
},
},
(error, input = {}) => {
const confirmed = op.get(input, 'confirmed', false);
if (error || confirmed === false) {
reject(error);
} else {
params['confirmed'] = true;
resolve(params);
}
},
);
});
};
/**
* conform(input:Object) Function
* @description Reduces the input object.
* @param input Object The key value pairs to reduce.
* @since 2.0.0
*/
const CONFORM = ({ input, props }) =>
Object.keys(input).reduce((obj, key) => {
let val = input[key];
switch (key) {
default:
obj[key] = val;
break;
}
return obj;
}, {});
/**
* HELP Function
* @description Function called in the commander.on('--help', callback) callback.
* @see https://www.npmjs.com/package/commander#automated---help
* @since 2.0.0
*/
const HELP = () =>
console.log(`
Example:
$ arcli update -h
`);
/**
* FLAGS
* @description Array of flags passed from the commander options.
* @since 2.0.18
*/
const FLAGS = ['overwrite'];
/**
* FLAGS_TO_PARAMS Function
* @description Create an object used by the prompt.override property.
* @since 2.0.18
*/
const FLAGS_TO_PARAMS = ({ opt = {} }) =>
FLAGS.reduce((obj, key) => {
let val = opt[key];
val = typeof val === 'function' ? undefined : val;
if (val) {
obj[key] = val;
}
return obj;
}, {});
/**
* SCHEMA Function
* @description used to describe the input for the prompt function.
* @see https://www.npmjs.com/package/prompt
* @since 2.0.0
*/
const SCHEMA = () => {
return {
properties: {
overwrite: {
description: `${chalk.white(
'Are you sure you want to update?',
)} ${chalk.cyan('(Y/N):')}`,
before: val => {
return String(val).toLowerCase() === 'y';
},
},
},
};
};
/**
* ACTION Function
* @description Function used as the commander.action() callback.
* @see https://www.npmjs.com/package/commander
* @param opt Object The commander options passed into the function.
* @param props Object The CLI props passed from the calling class `orcli.js`.
* @since 2.0.0
*/
const ACTION = ({ opt, props }) => {
const { cwd, prompt } = props;
const schema = SCHEMA({ props });
const ovr = FLAGS_TO_PARAMS({ opt });
prompt.override = ovr;
prompt.start();
return new Promise((resolve, reject) => {
prompt.get(schema, (err, input = {}) => {
if (err) {
prompt.stop();
reject(`${NAME} ${err.message}`);
return;
}
input = { ...ovr, ...input };
const params = CONFORM({ input, props });
resolve(params);
});
})
.then(params => generator({ params, props }))
.then(() => prompt.stop())
.catch(err => {
prompt.stop();
message(op.get(err, 'message', CANCELED));
});
};
/**
* COMMAND Function
* @description Function that executes program.command()
*/
const COMMAND = ({ program, props }) =>
program
.command(NAME)
.description(DESC)
.action(opt => ACTION({ opt, props }))
.option('-o, --overwrite [overwrite]', 'Overwrite existing files.')
.on('--help', HELP);
/**
* Module Constructor
* @description Internal constructor of the module that is being exported.
* @param program Class Commander.program reference.
* @param props Object The CLI props passed from the calling class `arcli.js`.
* @since 2.0.0
*/
module.exports = {
COMMAND,
ID: NAME,
};