Skip to content

Commit d45d04c

Browse files
committed
adding cleaner cli commands
1 parent 8344d0b commit d45d04c

1 file changed

Lines changed: 46 additions & 2 deletions

File tree

bin/browser-sync.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var reloadOpts = require("../lib/cli/opts.reload.json");
44
var recipeOpts = require("../lib/cli/opts.recipe.json");
55
var pkg = require("../package.json");
66
var utils = require("../lib/utils");
7+
var resolve = require("path").resolve;
8+
var existsSync = require("fs").existsSync;
79

810
/**
911
* Handle cli input
@@ -20,13 +22,44 @@ if (!module.parent) {
2022
.epilogue("For help running a certain command, type <command> --help\neg: $0 start --help");
2123

2224
var argv = yargs.argv;
23-
var command = argv._[0];
25+
var input = argv._;
26+
var command = input[0];
2427
var valid = ["start", "init", "reload", "recipe"];
2528

2629
if (valid.indexOf(command) > -1) {
2730
handleIncoming(command, yargs.reset());
2831
} else {
29-
yargs.showHelp();
32+
if (input.length) {
33+
const paths = input.map(function(path) {
34+
var resolved = resolve(path);
35+
return {
36+
isUrl: false,
37+
userInput: path,
38+
resolved: resolved,
39+
errors: pathErrors(path, resolved)
40+
}
41+
});
42+
var withErrors = paths.filter(function(item) { return item.errors.length });
43+
var withoutErrors = paths.filter(function(item) { return item.errors.length === 0 });
44+
if (withErrors.length) {
45+
withErrors.forEach(function(item) {
46+
console.log(item);
47+
})
48+
} else {
49+
var ssPaths = withoutErrors
50+
.filter(function(item) { return item.isUrl === false })
51+
.map(function(item) { return item.resolved });
52+
53+
var config = Object.assign({}, argv, {
54+
server: {
55+
baseDir: ssPaths
56+
}
57+
});
58+
handleCli({cli: {flags: config, input: ['start']}});
59+
}
60+
} else {
61+
yargs.showHelp();
62+
}
3063
}
3164
}
3265

@@ -89,3 +122,14 @@ function handleIncoming(command, yargs) {
89122

90123
handleCli({cli: {flags: out, input: out._}});
91124
}
125+
126+
function pathErrors(input, resolved) {
127+
if (!existsSync(resolved)) {
128+
return [
129+
{
130+
type: 'PATH_DOES_NOT_EXIST'
131+
}
132+
]
133+
}
134+
return []
135+
}

0 commit comments

Comments
 (0)