From d45d04ce4222055c0da436f216fa725eb55d1c67 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Fri, 22 Dec 2017 07:44:58 +0000 Subject: [PATCH] adding cleaner cli commands --- bin/browser-sync.js | 48 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/bin/browser-sync.js b/bin/browser-sync.js index 1f9d6f396..c50b6ec1a 100755 --- a/bin/browser-sync.js +++ b/bin/browser-sync.js @@ -4,6 +4,8 @@ var reloadOpts = require("../lib/cli/opts.reload.json"); var recipeOpts = require("../lib/cli/opts.recipe.json"); var pkg = require("../package.json"); var utils = require("../lib/utils"); +var resolve = require("path").resolve; +var existsSync = require("fs").existsSync; /** * Handle cli input @@ -20,13 +22,44 @@ if (!module.parent) { .epilogue("For help running a certain command, type --help\neg: $0 start --help"); var argv = yargs.argv; - var command = argv._[0]; + var input = argv._; + var command = input[0]; var valid = ["start", "init", "reload", "recipe"]; if (valid.indexOf(command) > -1) { handleIncoming(command, yargs.reset()); } else { - yargs.showHelp(); + if (input.length) { + const paths = input.map(function(path) { + var resolved = resolve(path); + return { + isUrl: false, + userInput: path, + resolved: resolved, + errors: pathErrors(path, resolved) + } + }); + var withErrors = paths.filter(function(item) { return item.errors.length }); + var withoutErrors = paths.filter(function(item) { return item.errors.length === 0 }); + if (withErrors.length) { + withErrors.forEach(function(item) { + console.log(item); + }) + } else { + var ssPaths = withoutErrors + .filter(function(item) { return item.isUrl === false }) + .map(function(item) { return item.resolved }); + + var config = Object.assign({}, argv, { + server: { + baseDir: ssPaths + } + }); + handleCli({cli: {flags: config, input: ['start']}}); + } + } else { + yargs.showHelp(); + } } } @@ -89,3 +122,14 @@ function handleIncoming(command, yargs) { handleCli({cli: {flags: out, input: out._}}); } + +function pathErrors(input, resolved) { + if (!existsSync(resolved)) { + return [ + { + type: 'PATH_DOES_NOT_EXIST' + } + ] + } + return [] +} \ No newline at end of file