Skip to content

Commit 91b7e89

Browse files
committed
dx: make it easier to launch a server/proxy re: #1253
1 parent d45d04c commit 91b7e89

File tree

1 file changed

+134
-43
lines changed

1 file changed

+134
-43
lines changed

bin/browser-sync.js

Lines changed: 134 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
#!/usr/bin/env node
2-
var startOpts = require("../lib/cli/opts.start.json");
2+
var startOpts = require("../lib/cli/opts.start.json");
33
var reloadOpts = require("../lib/cli/opts.reload.json");
44
var recipeOpts = require("../lib/cli/opts.recipe.json");
5-
var pkg = require("../package.json");
6-
var utils = require("../lib/utils");
7-
var resolve = require("path").resolve;
5+
var pkg = require("../package.json");
6+
var utils = require("../lib/utils");
7+
var resolve = require("path").resolve;
88
var existsSync = require("fs").existsSync;
9+
var logger = require("../lib/logger").logger;
10+
var compile = require("eazy-logger").compile;
11+
12+
var BsErrorLevels = {
13+
Fatal: "Fatal"
14+
};
15+
16+
var BsErrorTypes = {
17+
PathNotFound: "PathNotFound"
18+
};
919

1020
/**
1121
* Handle cli input
@@ -19,46 +29,101 @@ if (!module.parent) {
1929
.version(function () {
2030
return pkg.version;
2131
})
22-
.epilogue("For help running a certain command, type <command> --help\neg: $0 start --help");
32+
.epilogue([
33+
"For help running a certain command, type <command> --help",
34+
" $0 start --help",
35+
"",
36+
"You can run a static server by providing a path(s) directly",
37+
" $0 app/src app/tmp",
38+
"",
39+
"If the directory contains a 'index.html' file, you can omit any input",
40+
" $0",
41+
"",
42+
"You can run the proxy in this manner too",
43+
" $0 https://example.com",
44+
"",
45+
"To run a proxy, whilst also serving static files",
46+
compile(" $0 https://example.com htdocs/themes/example")
47+
].join("\n"));
2348

24-
var argv = yargs.argv;
25-
var input = argv._;
49+
var argv = yargs.argv;
50+
var input = argv._;
2651
var command = input[0];
27-
var valid = ["start", "init", "reload", "recipe"];
52+
var valid = ["start", "init", "reload", "recipe"];
2853

54+
if (argv.help) {
55+
return yargs.showHelp();
56+
}
57+
2958
if (valid.indexOf(command) > -1) {
30-
handleIncoming(command, yargs.reset());
59+
return handleIncoming(command, yargs.reset());
60+
}
61+
62+
if (input.length) {
63+
return handleNoCommand(argv, input);
64+
}
65+
66+
if (existsSync("index.html")) {
67+
return handleNoCommand(argv, ["."]);
68+
}
69+
70+
yargs.showHelp();
71+
}
72+
73+
function handleNoCommand(argv, input) {
74+
var paths = input.map(function (path) {
75+
var resolved = resolve(path);
76+
var isUrl = /^https?:\/\//.test(path);
77+
return {
78+
isUrl: isUrl,
79+
userInput: path,
80+
resolved: resolved,
81+
errors: isUrl ? [] : pathErrors(path, resolved)
82+
}
83+
});
84+
85+
var withErrors = paths.filter(function (item) {
86+
return item.errors.length
87+
});
88+
89+
var withoutErrors = paths.filter(function (item) {
90+
return item.errors.length === 0
91+
});
92+
93+
if (withErrors.length) {
94+
withErrors.forEach(function (item) {
95+
logger.unprefixed("error", printErrors(item.errors));
96+
});
97+
process.exit(1);
3198
} else {
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-
}
99+
var ssPaths = withoutErrors
100+
.filter(function (item) {
101+
return item.isUrl === false
102+
})
103+
.map(function (item) {
104+
return item.resolved
41105
});
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-
}
106+
107+
var urls = withoutErrors
108+
.filter(function (item) {
109+
return item.isUrl === true
110+
})
111+
.map(function (item) {
112+
return item.userInput
113+
});
114+
115+
if (urls.length) {
116+
var proxy = urls[0];
117+
var config = Object.assign({}, argv, {
118+
proxy: proxy,
119+
serveStatic: ssPaths
120+
});
121+
handleCli({ cli: { flags: config, input: ["start"] } });
60122
} else {
61-
yargs.showHelp();
123+
var config = Object.assign({}, argv, {
124+
server: { baseDir: ssPaths }
125+
});
126+
handleCli({ cli: { flags: config, input: ["start"] } });
62127
}
63128
}
64129
}
@@ -67,7 +132,7 @@ if (!module.parent) {
67132
* @param {{cli: object, [whitelist]: array, [cb]: function}} opts
68133
* @returns {*}
69134
*/
70-
function handleCli(opts) {
135+
function handleCli (opts) {
71136

72137
opts.cb = opts.cb || utils.defaultCallback;
73138
return require("../lib/cli/command." + opts.cli.input[0])(opts);
@@ -79,7 +144,7 @@ module.exports = handleCli;
79144
* @param {string} command
80145
* @param {object} yargs
81146
*/
82-
function handleIncoming(command, yargs) {
147+
function handleIncoming (command, yargs) {
83148
var out;
84149
if (command === "start") {
85150
out = yargs
@@ -120,16 +185,42 @@ function handleIncoming(command, yargs) {
120185
return yargs.showHelp();
121186
}
122187

123-
handleCli({cli: {flags: out, input: out._}});
188+
handleCli({ cli: { flags: out, input: out._ } });
124189
}
125190

126-
function pathErrors(input, resolved) {
191+
function pathErrors (input, resolved) {
127192
if (!existsSync(resolved)) {
128193
return [
129194
{
130-
type: 'PATH_DOES_NOT_EXIST'
195+
type: BsErrorTypes.PathNotFound,
196+
level: BsErrorLevels.Fatal,
197+
errors: [{
198+
error: new Error("Path not found: " + input),
199+
meta: function () {
200+
return [
201+
"Your Input: {yellow:" + input + "}",
202+
"CWD: {yellow:" + process.cwd() + "}",
203+
"Resolved to: {yellow:" + resolved + "}"
204+
];
205+
}
206+
}]
131207
}
132208
]
133209
}
134210
return []
135-
}
211+
}
212+
213+
function printErrors (errors) {
214+
return errors.map(function (error) {
215+
return [
216+
"Error Type: {bold:" + error.type + "}",
217+
"Error Level: {bold:" + error.level + "}",
218+
error.errors.map(function (item) {
219+
return [
220+
"Error Message: " + item.error.message,
221+
item.meta ? item.meta().join("\n") : ""
222+
].filter(Boolean).join("\n")
223+
}),
224+
].join("\n");
225+
}).join("\n\n");
226+
}

0 commit comments

Comments
 (0)