UNPKG

@mason-api/cli

Version:

CLI assistant for Mason builder

49 lines (42 loc) 1.59 kB
#!/usr/bin/env node import Yargs from 'yargs'; import BabelPolyfill from 'babel-polyfill'; const yargs = Yargs // eslint-disable-line .usage('Usage: $0 <command> [options]') .example('$0 html 3030 4040 default') .commandDir('commands') .command('$0', 'default', () => {}, (argv) => { setTimeout(() => { // guide is a default command if `mason` is called without positional arguments if (argv._.length === 0) { yargs.exec(`guide ${process.argv.slice(2).join(' ')}`); // when arguments are provided but no command was matched, assume `generate` command } else { yargs.exec(`generate ${process.argv.slice(2).join(' ')}`); } }, 10); }) .option('verbose', { alias: 'v', describe: 'Output additional information', }) .option('api-key', { alias: 'k', describe: 'Key for authentication, can be set via $MASON_API_KEY', }); global.yargs = yargs; yargs.exec = (input) => { yargs.parse(input, (err, args, output) => { if (output) { // Remove types (e.g. [string], [boolean]) from the output output = output.replace('Positionals:', 'Arguments:'); output = output.replace(/\[\w+\]\n/g, '\n'); output = output.replace(/(?:\n\s+)?\[default:\s*(.*?)\]\n/g, '\n'); output = output.replace(/\[choices:\s*([\s\S]+?)\]\n/g, (m, choices) => ` (${choices.replace(/"/g, '').replace(/\s+/g, ' ')})\n`); output = output.replace(/\n\s*--version.*?\n/, '\n'); // Show the modified output console.log(output); } }); }; yargs.exec(process.argv.slice(2));