From 91b7e89155bc6b3bfd3860f3105b7e76396acf86 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Fri, 22 Dec 2017 10:13:44 +0000 Subject: [PATCH] dx: make it easier to launch a server/proxy re: #1253 --- bin/browser-sync.js | 177 +++++++++++++++++++++++++++++++++----------- 1 file changed, 134 insertions(+), 43 deletions(-) diff --git a/bin/browser-sync.js b/bin/browser-sync.js index c50b6ec1a..c9f814330 100755 --- a/bin/browser-sync.js +++ b/bin/browser-sync.js @@ -1,11 +1,21 @@ #!/usr/bin/env node -var startOpts = require("../lib/cli/opts.start.json"); +var startOpts = require("../lib/cli/opts.start.json"); 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 pkg = require("../package.json"); +var utils = require("../lib/utils"); +var resolve = require("path").resolve; var existsSync = require("fs").existsSync; +var logger = require("../lib/logger").logger; +var compile = require("eazy-logger").compile; + +var BsErrorLevels = { + Fatal: "Fatal" +}; + +var BsErrorTypes = { + PathNotFound: "PathNotFound" +}; /** * Handle cli input @@ -19,46 +29,101 @@ if (!module.parent) { .version(function () { return pkg.version; }) - .epilogue("For help running a certain command, type --help\neg: $0 start --help"); + .epilogue([ + "For help running a certain command, type --help", + " $0 start --help", + "", + "You can run a static server by providing a path(s) directly", + " $0 app/src app/tmp", + "", + "If the directory contains a 'index.html' file, you can omit any input", + " $0", + "", + "You can run the proxy in this manner too", + " $0 https://example.com", + "", + "To run a proxy, whilst also serving static files", + compile(" $0 https://example.com htdocs/themes/example") + ].join("\n")); - var argv = yargs.argv; - var input = argv._; + var argv = yargs.argv; + var input = argv._; var command = input[0]; - var valid = ["start", "init", "reload", "recipe"]; + var valid = ["start", "init", "reload", "recipe"]; + if (argv.help) { + return yargs.showHelp(); + } + if (valid.indexOf(command) > -1) { - handleIncoming(command, yargs.reset()); + return handleIncoming(command, yargs.reset()); + } + + if (input.length) { + return handleNoCommand(argv, input); + } + + if (existsSync("index.html")) { + return handleNoCommand(argv, ["."]); + } + + yargs.showHelp(); +} + +function handleNoCommand(argv, input) { + var paths = input.map(function (path) { + var resolved = resolve(path); + var isUrl = /^https?:\/\//.test(path); + return { + isUrl: isUrl, + userInput: path, + resolved: resolved, + errors: isUrl ? [] : 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) { + logger.unprefixed("error", printErrors(item.errors)); + }); + process.exit(1); } else { - 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 ssPaths = withoutErrors + .filter(function (item) { + return item.isUrl === false + }) + .map(function (item) { + return item.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']}}); - } + + var urls = withoutErrors + .filter(function (item) { + return item.isUrl === true + }) + .map(function (item) { + return item.userInput + }); + + if (urls.length) { + var proxy = urls[0]; + var config = Object.assign({}, argv, { + proxy: proxy, + serveStatic: ssPaths + }); + handleCli({ cli: { flags: config, input: ["start"] } }); } else { - yargs.showHelp(); + var config = Object.assign({}, argv, { + server: { baseDir: ssPaths } + }); + handleCli({ cli: { flags: config, input: ["start"] } }); } } } @@ -67,7 +132,7 @@ if (!module.parent) { * @param {{cli: object, [whitelist]: array, [cb]: function}} opts * @returns {*} */ -function handleCli(opts) { +function handleCli (opts) { opts.cb = opts.cb || utils.defaultCallback; return require("../lib/cli/command." + opts.cli.input[0])(opts); @@ -79,7 +144,7 @@ module.exports = handleCli; * @param {string} command * @param {object} yargs */ -function handleIncoming(command, yargs) { +function handleIncoming (command, yargs) { var out; if (command === "start") { out = yargs @@ -120,16 +185,42 @@ function handleIncoming(command, yargs) { return yargs.showHelp(); } - handleCli({cli: {flags: out, input: out._}}); + handleCli({ cli: { flags: out, input: out._ } }); } -function pathErrors(input, resolved) { +function pathErrors (input, resolved) { if (!existsSync(resolved)) { return [ { - type: 'PATH_DOES_NOT_EXIST' + type: BsErrorTypes.PathNotFound, + level: BsErrorLevels.Fatal, + errors: [{ + error: new Error("Path not found: " + input), + meta: function () { + return [ + "Your Input: {yellow:" + input + "}", + "CWD: {yellow:" + process.cwd() + "}", + "Resolved to: {yellow:" + resolved + "}" + ]; + } + }] } ] } return [] -} \ No newline at end of file +} + +function printErrors (errors) { + return errors.map(function (error) { + return [ + "Error Type: {bold:" + error.type + "}", + "Error Level: {bold:" + error.level + "}", + error.errors.map(function (item) { + return [ + "Error Message: " + item.error.message, + item.meta ? item.meta().join("\n") : "" + ].filter(Boolean).join("\n") + }), + ].join("\n"); + }).join("\n\n"); +}