bin.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/usr/bin/env node
  2. "use strict";
  3. var __assign = (this && this.__assign) || function () {
  4. __assign = Object.assign || function(t) {
  5. for (var s, i = 1, n = arguments.length; i < n; i++) {
  6. s = arguments[i];
  7. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  8. t[p] = s[p];
  9. }
  10. return t;
  11. };
  12. return __assign.apply(this, arguments);
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. var startOpts = require("../cli-options/opts.start.json");
  16. var reloadOpts = require("../cli-options/opts.reload.json");
  17. var recipeOpts = require("../cli-options/opts.recipe.json");
  18. var pkg = require("../package.json");
  19. var utils = require("./utils");
  20. var path_1 = require("path");
  21. var fs_1 = require("fs");
  22. var logger_1 = require("./logger");
  23. var eazy_logger_1 = require("eazy-logger");
  24. var cli_options_1 = require("./cli/cli-options");
  25. var BsErrorLevels;
  26. (function (BsErrorLevels) {
  27. BsErrorLevels["Fatal"] = "Fatal";
  28. })(BsErrorLevels = exports.BsErrorLevels || (exports.BsErrorLevels = {}));
  29. var BsErrorTypes;
  30. (function (BsErrorTypes) {
  31. BsErrorTypes["PathNotFound"] = "PathNotFound";
  32. BsErrorTypes["HostAndListenIncompatible"] = "HostAndListenIncompatible";
  33. })(BsErrorTypes = exports.BsErrorTypes || (exports.BsErrorTypes = {}));
  34. /**
  35. * Handle cli input
  36. */
  37. if (!module.parent) {
  38. runFromCli();
  39. }
  40. function runFromCli() {
  41. var yargs = require("yargs")
  42. .command("start", "Start the server")
  43. .command("init", "Create a configuration file")
  44. .command("reload", "Send a reload event over HTTP protocol")
  45. .command("recipe", "Generate the files for a recipe")
  46. .version(function () { return pkg.version; })
  47. .epilogue([
  48. "For help running a certain command, type <command> --help",
  49. " $0 start --help",
  50. "",
  51. "You can run a static server by providing a path(s) directly",
  52. " $0 app/src app/tmp",
  53. "",
  54. "If the directory contains a 'index.html' file, you can omit any input",
  55. " $0",
  56. "",
  57. "You can run the proxy in this manner too",
  58. " $0 https://example.com",
  59. "",
  60. "To run a proxy, whilst also serving static files",
  61. eazy_logger_1.compile(" $0 https://example.com htdocs/themes/example")
  62. ].join("\n"));
  63. var argv = yargs.argv;
  64. var input = argv._;
  65. var command = input[0];
  66. var valid = ["start", "init", "reload", "recipe"];
  67. if (valid.indexOf(command) > -1) {
  68. return handleIncoming(command, yargs.reset());
  69. }
  70. if (input.length) {
  71. return handleNoCommand(argv, input, yargs);
  72. }
  73. if (fs_1.existsSync("index.html")) {
  74. return handleNoCommand(argv, ["."], yargs);
  75. }
  76. yargs.showHelp();
  77. }
  78. /**
  79. * Feature: If no command was specified, try to do the 'right thing'
  80. *
  81. * If paths were given, start the server
  82. * eg: browser-sync app/code app/design
  83. * is equal to: browser-sync start --server app/code app/design
  84. *
  85. * eg: browser-sync http://example.com
  86. * is equal to: browser-sync start --proxy http://example.com
  87. *
  88. * eg: browser-sync http://example.com themes/example
  89. * is equal to: browser-sync start --proxy http://example.com --ss themes/example
  90. *
  91. * @param argv
  92. * @param input
  93. * @returns {any}
  94. */
  95. function handleNoCommand(argv, input, yargs) {
  96. var processed = processStart(yargs);
  97. var paths = input.map(function (path) {
  98. var resolved = path_1.resolve(path);
  99. var isUrl = /^https?:\/\//.test(path);
  100. return {
  101. isUrl: isUrl,
  102. userInput: path,
  103. resolved: resolved,
  104. errors: isUrl ? [] : pathErrors(path, resolved)
  105. };
  106. });
  107. var withErrors = paths.filter(function (item) { return item.errors.length; });
  108. var withoutErrors = paths.filter(function (item) { return item.errors.length === 0; });
  109. if (withErrors.length) {
  110. withErrors.forEach(function (item) {
  111. logger_1.logger.unprefixed("error", cli_options_1.printErrors(item.errors));
  112. });
  113. return process.exit(1);
  114. }
  115. var serveStaticPaths = withoutErrors
  116. .filter(function (item) { return item.isUrl === false; })
  117. .map(function (item) { return item.resolved; });
  118. var urls = withoutErrors
  119. .filter(function (item) { return item.isUrl === true; })
  120. .map(function (item) { return item.userInput; });
  121. /**
  122. * If a URL was given, switch to proxy mode and use
  123. * any other paths as serveStatic options
  124. */
  125. if (urls.length) {
  126. var proxy = urls[0];
  127. var config_1 = __assign({}, processed, { proxy: proxy, serveStatic: serveStaticPaths });
  128. return handleCli({ cli: { flags: config_1, input: ["start"] } });
  129. }
  130. /**
  131. * if NO urls were given switch directly to server mode
  132. * @type {{server: {baseDir: any}}}
  133. */
  134. var config = __assign({}, processed, { server: { baseDir: serveStaticPaths } });
  135. return handleCli({ cli: { flags: config, input: ["start"] } });
  136. }
  137. /**
  138. * @param {{cli: object, [whitelist]: array, [cb]: function}} opts
  139. * @returns {*}
  140. */
  141. function handleCli(opts) {
  142. opts.cb = opts.cb || utils.defaultCallback;
  143. var m = require("./cli/command." + opts.cli.input[0]);
  144. if (m.default) {
  145. return m.default(opts);
  146. }
  147. return m(opts);
  148. }
  149. exports.default = handleCli;
  150. function processStart(yargs) {
  151. return yargs
  152. .usage("Usage: $0 start [options]")
  153. .options(startOpts)
  154. .example("$0 start -s app", "- Use the App directory to serve files")
  155. .example("$0 start -p www.bbc.co.uk", "- Proxy an existing website")
  156. .default('cwd', function () { return process.cwd(); })
  157. .help().argv;
  158. }
  159. /**
  160. * @param {string} command
  161. * @param {object} yargs
  162. * @param cwd
  163. */
  164. function handleIncoming(command, yargs) {
  165. var out;
  166. if (command === "start") {
  167. out = processStart(yargs);
  168. }
  169. if (command === "init") {
  170. out = yargs
  171. .usage("Usage: $0 init")
  172. .example("$0 init")
  173. .default('cwd', function () { return process.cwd(); })
  174. .help().argv;
  175. }
  176. if (command === "reload") {
  177. out = yargs
  178. .usage("Usage: $0 reload")
  179. .options(reloadOpts)
  180. .example("$0 reload")
  181. .example("$0 reload --port 4000")
  182. .default('cwd', function () { return process.cwd(); })
  183. .help().argv;
  184. }
  185. if (command === "recipe") {
  186. out = yargs
  187. .usage("Usage: $0 recipe <recipe-name>")
  188. .option(recipeOpts)
  189. .example("$0 recipe ls", "list the recipes")
  190. .example("$0 recipe gulp.sass", "use the gulp.sass recipe")
  191. .default('cwd', function () { return process.cwd(); })
  192. .help().argv;
  193. }
  194. if (out.help) {
  195. return yargs.showHelp();
  196. }
  197. handleCli({ cli: { flags: out, input: out._ } });
  198. }
  199. function pathErrors(input, resolved) {
  200. if (!fs_1.existsSync(resolved)) {
  201. return [
  202. {
  203. type: BsErrorTypes.PathNotFound,
  204. level: BsErrorLevels.Fatal,
  205. errors: [
  206. {
  207. error: new Error("Path not found: " + input),
  208. meta: function () {
  209. return [
  210. "Your Input: {yellow:" + input + "}",
  211. "CWD: {yellow:" + process.cwd() + "}",
  212. "Resolved to: {yellow:" + resolved + "}"
  213. ];
  214. }
  215. }
  216. ]
  217. }
  218. ];
  219. }
  220. return [];
  221. }
  222. //# sourceMappingURL=bin.js.map