|
| 1 | +/** |
| 2 | + * ----------------------------------------------------------------------------- |
| 3 | + * Imports |
| 4 | + * ----------------------------------------------------------------------------- |
| 5 | + */ |
| 6 | + |
| 7 | +const chalk = require('chalk'); |
| 8 | +const generator = require('./generator'); |
| 9 | +const prettier = require('prettier'); |
| 10 | +const path = require('path'); |
| 11 | +const op = require('object-path'); |
| 12 | +const mod = path.dirname(require.main.filename); |
| 13 | +const { error, message } = require(`${mod}/lib/messenger`); |
| 14 | + |
| 15 | +/** |
| 16 | + * NAME String |
| 17 | + * @description Constant defined as the command name. Value passed to the commander.command() function. |
| 18 | + * @example $ arcli docs |
| 19 | + * @see https://www.npmjs.com/package/commander#command-specific-options |
| 20 | + * @since 2.0.0 |
| 21 | + */ |
| 22 | +const NAME = 'i18n'; |
| 23 | + |
| 24 | +/** |
| 25 | + * DESC String |
| 26 | + * @description Constant defined as the command description. Value passed to |
| 27 | + * the commander.desc() function. This string is also used in the --help flag output. |
| 28 | + * @see https://www.npmjs.com/package/commander#automated---help |
| 29 | + * @since 2.0.0 |
| 30 | + */ |
| 31 | +const DESC = 'Generate POT file.'; |
| 32 | + |
| 33 | +/** |
| 34 | + * CANCELED String |
| 35 | + * @description Message sent when the command is canceled |
| 36 | + * @since 2.0.0 |
| 37 | + */ |
| 38 | +const CANCELED = 'Action canceled!'; |
| 39 | + |
| 40 | +/** |
| 41 | + * conform(input:Object) Function |
| 42 | + * @description Reduces the input object. |
| 43 | + * @param input Object The key value pairs to reduce. |
| 44 | + * @since 2.0.0 |
| 45 | + */ |
| 46 | +const CONFORM = ({ input, props }) => |
| 47 | + Object.keys(input).reduce((obj, key) => { |
| 48 | + const { cwd } = props; |
| 49 | + let val = input[key]; |
| 50 | + switch (key) { |
| 51 | + default: |
| 52 | + obj[key] = val; |
| 53 | + break; |
| 54 | + } |
| 55 | + |
| 56 | + return obj; |
| 57 | + }, {}); |
| 58 | + |
| 59 | +/** |
| 60 | + * HELP Function |
| 61 | + * @description Function called in the commander.on('--help', callback) callback. |
| 62 | + * @see https://www.npmjs.com/package/commander#automated---help |
| 63 | + * @since 2.0.0 |
| 64 | + */ |
| 65 | +const HELP = () => |
| 66 | + console.log(` |
| 67 | +Example: |
| 68 | + $ arcli i18n -h |
| 69 | +`); |
| 70 | + |
| 71 | +/** |
| 72 | + * FLAGS |
| 73 | + * @description Array of flags passed from the commander options. |
| 74 | + * @since 2.0.18 |
| 75 | + */ |
| 76 | +const FLAGS = []; |
| 77 | + |
| 78 | +/** |
| 79 | + * FLAGS_TO_PARAMS Function |
| 80 | + * @description Create an object used by the prompt.override property. |
| 81 | + * @since 2.0.18 |
| 82 | + */ |
| 83 | +const FLAGS_TO_PARAMS = ({ opt = {} }) => |
| 84 | + FLAGS.reduce((obj, key) => { |
| 85 | + let val = opt[key]; |
| 86 | + val = typeof val === 'function' ? undefined : val; |
| 87 | + |
| 88 | + if (val) { |
| 89 | + obj[key] = val; |
| 90 | + } |
| 91 | + |
| 92 | + return obj; |
| 93 | + }, {}); |
| 94 | + |
| 95 | +/** |
| 96 | + * SCHEMA Function |
| 97 | + * @description used to describe the input for the prompt function. |
| 98 | + * @see https://www.npmjs.com/package/prompt |
| 99 | + * @since 2.0.0 |
| 100 | + */ |
| 101 | +const SCHEMA = ({ props }) => { |
| 102 | + const { prompt } = props; |
| 103 | + |
| 104 | + return { |
| 105 | + properties: {}, |
| 106 | + }; |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * ACTION Function |
| 111 | + * @description Function used as the commander.action() callback. |
| 112 | + * @see https://www.npmjs.com/package/commander |
| 113 | + * @param opt Object The commander options passed into the function. |
| 114 | + * @param props Object The CLI props passed from the calling class `orcli.js`. |
| 115 | + * @since 2.0.0 |
| 116 | + */ |
| 117 | +const ACTION = ({ opt, props }) => { |
| 118 | + console.log(''); |
| 119 | + |
| 120 | + const { cwd, prompt } = props; |
| 121 | + const schema = SCHEMA({ props }); |
| 122 | + const ovr = FLAGS_TO_PARAMS({ opt }); |
| 123 | + |
| 124 | + prompt.override = ovr; |
| 125 | + prompt.start(); |
| 126 | + |
| 127 | + return new Promise((resolve, reject) => { |
| 128 | + prompt.get(schema, (err, input = {}) => { |
| 129 | + if (err) { |
| 130 | + prompt.stop(); |
| 131 | + reject(`${NAME} ${err.message}`); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + input = { ...ovr, ...input }; |
| 136 | + |
| 137 | + resolve(CONFORM({ input, props })); |
| 138 | + }); |
| 139 | + }) |
| 140 | + .then(params => { |
| 141 | + console.log(''); |
| 142 | + return generator({ params, props }); |
| 143 | + }) |
| 144 | + .then(results => { |
| 145 | + console.log(''); |
| 146 | + }) |
| 147 | + .catch(err => { |
| 148 | + prompt.stop(); |
| 149 | + message(op.get(err, 'message', CANCELED)); |
| 150 | + }); |
| 151 | +}; |
| 152 | + |
| 153 | +/** |
| 154 | + * COMMAND Function |
| 155 | + * @description Function that executes program.command() |
| 156 | + */ |
| 157 | +const COMMAND = ({ program, props }) => |
| 158 | + program |
| 159 | + .command(NAME) |
| 160 | + .description(DESC) |
| 161 | + .action(opt => ACTION({ opt, props })) |
| 162 | + .on('--help', HELP); |
| 163 | + |
| 164 | +/** |
| 165 | + * Module Constructor |
| 166 | + * @description Internal constructor of the module that is being exported. |
| 167 | + * @param program Class Commander.program reference. |
| 168 | + * @param props Object The CLI props passed from the calling class `arcli.js`. |
| 169 | + * @since 2.0.0 |
| 170 | + */ |
| 171 | +module.exports = { |
| 172 | + COMMAND, |
| 173 | + NAME, |
| 174 | +}; |
0 commit comments