Skip to content

Commit

Permalink
dx: make it easier to launch a server/proxy re: #1253
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Dec 22, 2017
1 parent d45d04c commit 91b7e89
Showing 1 changed file with 134 additions and 43 deletions.
177 changes: 134 additions & 43 deletions 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
Expand All @@ -19,46 +29,101 @@ if (!module.parent) {
.version(function () {
return pkg.version;
})
.epilogue("For help running a certain command, type <command> --help\neg: $0 start --help");
.epilogue([
"For help running a certain command, type <command> --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"] } });
}
}
}
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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 []
}
}

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");
}

0 comments on commit 91b7e89

Please sign in to comment.