|
| 1 | +var startOpts = require("../lib/cli/opts.start.json"); |
| 2 | +var reloadOpts = require("../lib/cli/opts.reload.json"); |
| 3 | +var recipeOpts = require("../lib/cli/opts.recipe.json"); |
| 4 | +var pkg = require("../package.json"); |
| 5 | +var utils = require("../lib/utils"); |
| 6 | + |
| 7 | +/** |
| 8 | + * Handle cli input |
| 9 | + */ |
| 10 | +if (!module.parent) { |
| 11 | + var yargs = require("yargs") |
| 12 | + .command('start [options]', 'Start the server') |
| 13 | + .command('init', 'Create a configuration file') |
| 14 | + .command('reload', 'Send a reload event over HTTP protocol') |
| 15 | + .command('recipe <command> [options]', 'Generate the files for a recipe') |
| 16 | + .version(function () { |
| 17 | + return pkg.version; |
| 18 | + }) |
| 19 | + .epilogue("For help running a certain command, type <command> --help\neg: browser-sync start --help"); |
| 20 | + |
| 21 | + var argv = yargs.argv; |
| 22 | + var command = argv._[0]; |
| 23 | + |
| 24 | + var valid = ['start', 'init', 'reload', 'recipe']; |
| 25 | + if (valid.indexOf(command) > -1) { |
| 26 | + var out = handleIncoming(command, yargs.reset()); |
| 27 | + // console.log(out); |
| 28 | + } else { |
| 29 | + yargs.showHelp(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @param {{cli: object, [whitelist]: array, [cb]: function}} opts |
| 35 | + * @returns {*} |
| 36 | + */ |
| 37 | +function handleCli(opts) { |
| 38 | + |
| 39 | + opts.cb = opts.cb || utils.defaultCallback; |
| 40 | + return require("../lib/cli/command." + opts.cli.input[0])(opts); |
| 41 | +} |
| 42 | + |
| 43 | +module.exports = handleCli; |
| 44 | + |
| 45 | + |
| 46 | +function handleIncoming(command, yargs) { |
| 47 | + if (command === 'start') { |
| 48 | + var out = yargs |
| 49 | + .command('start [options]') |
| 50 | + .options(startOpts) |
| 51 | + .example('browser-sync start -s app', '- Use the App directory to serve files') |
| 52 | + .example('browser-sync start -p www.bbc.co.uk', '- Proxy an existing website') |
| 53 | + .help() |
| 54 | + .argv; |
| 55 | + if (out.help) { |
| 56 | + yargs.showHelp(); |
| 57 | + } else { |
| 58 | + handleCli({cli: {flags: out, input: ["start"]}}); |
| 59 | + } |
| 60 | + } |
| 61 | + if (command === 'init') { |
| 62 | + var out = yargs |
| 63 | + .command('init', 'Generate a configuration file') |
| 64 | + .example('browser-sync init') |
| 65 | + .help() |
| 66 | + .argv; |
| 67 | + if (out.help) { |
| 68 | + yargs.showHelp(); |
| 69 | + } else { |
| 70 | + handleCli({cli: {flags: out, input: ["init"]}}); |
| 71 | + } |
| 72 | + } |
| 73 | + if (command === 'reload') { |
| 74 | + var out = yargs |
| 75 | + .command('reload', 'Send a reload event over HTTP protocol') |
| 76 | + .example('browser-sync reload') |
| 77 | + .example('browser-sync reload --port 4000') |
| 78 | + .example('browser-sync reload --port 4000') |
| 79 | + .help() |
| 80 | + .argv; |
| 81 | + if (out.help) { |
| 82 | + yargs.showHelp(); |
| 83 | + } else { |
| 84 | + handleCli({cli: {flags: out, input: ["reload"]}}); |
| 85 | + } |
| 86 | + } |
| 87 | + if (command === 'recipe') { |
| 88 | + var out = yargs |
| 89 | + .command('recipe <recipe-name> [options]', 'Generate the files for a recipe', function (yargs) { |
| 90 | + console.log(yargs.argv._[1]); |
| 91 | + }) |
| 92 | + .command('recipe ls', 'List all recipes', function (yargs) { |
| 93 | + console.log(yargs.argv._[1]); |
| 94 | + }) |
| 95 | + .example('browser-sync recipe ls', 'list the recipes') |
| 96 | + .example('browser-sync recipe gulp.sass', 'use the gulp.sass recipe') |
| 97 | + .help() |
| 98 | + .argv; |
| 99 | + if (out.help) { |
| 100 | + yargs.showHelp(); |
| 101 | + } else { |
| 102 | + console.log('HERE', out); |
| 103 | + // handleCli({cli: {flags: out, input: ["recipe", out["recipe-name"]]}}); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments