command.start.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var path = require("path");
  4. var fs_1 = require("fs");
  5. var immutable_1 = require("immutable");
  6. var utils = require("../utils");
  7. var cli_options_1 = require("./cli-options");
  8. var _ = require("../lodash.custom");
  9. /**
  10. * $ browser-sync start <options>
  11. *
  12. * This commands starts the Browsersync servers
  13. * & Optionally UI.
  14. *
  15. * @param opts
  16. * @returns {Function}
  17. */
  18. function default_1(opts) {
  19. var flags = preprocessFlags(opts.cli.flags);
  20. var cwd = flags.cwd || process.cwd();
  21. var maybepkg = path.resolve(cwd, "package.json");
  22. var input = flags;
  23. if (flags.config) {
  24. var maybeconf = path.resolve(cwd, flags.config);
  25. if (fs_1.existsSync(maybeconf)) {
  26. var conf = require(maybeconf);
  27. input = _.merge({}, conf, flags);
  28. }
  29. else {
  30. utils.fail(true, new Error("Configuration file '" + flags.config + "' not found"), opts.cb);
  31. }
  32. }
  33. else {
  34. if (fs_1.existsSync(maybepkg)) {
  35. var pkg = require(maybepkg);
  36. if (pkg["browser-sync"]) {
  37. console.log("> Configuration obtained from package.json");
  38. input = _.merge({}, pkg["browser-sync"], flags);
  39. }
  40. }
  41. }
  42. return require("../")
  43. .create("cli")
  44. .init(input, opts.cb);
  45. }
  46. exports.default = default_1;
  47. /**
  48. * @param flags
  49. * @returns {*}
  50. */
  51. function preprocessFlags(flags) {
  52. return [
  53. stripUndefined,
  54. legacyFilesArgs,
  55. removeWatchBooleanWhenFalse
  56. ].reduce(function (flags, fn) { return fn.call(null, flags); }, flags);
  57. }
  58. /**
  59. * Incoming undefined values are problematic as
  60. * they interfere with Immutable.Map.mergeDeep
  61. * @param subject
  62. * @returns {*}
  63. */
  64. function stripUndefined(subject) {
  65. return Object.keys(subject).reduce(function (acc, key) {
  66. var value = subject[key];
  67. if (typeof value === "undefined") {
  68. return acc;
  69. }
  70. acc[key] = value;
  71. return acc;
  72. }, {});
  73. }
  74. /**
  75. * @param flags
  76. * @returns {*}
  77. */
  78. function legacyFilesArgs(flags) {
  79. if (flags.files && flags.files.length) {
  80. flags.files = flags.files.reduce(function (acc, item) { return acc.concat(cli_options_1.explodeFilesArg(item)); }, []);
  81. }
  82. return flags;
  83. }
  84. /**
  85. * `watch` is a CLI boolean so should be removed if false to
  86. * allow config to set watch: true
  87. * @param flags
  88. * @returns {any}
  89. */
  90. function removeWatchBooleanWhenFalse(flags) {
  91. if (flags.watch === false) {
  92. return immutable_1.fromJS(flags)
  93. .delete("watch")
  94. .toJS();
  95. }
  96. return flags;
  97. }
  98. //# sourceMappingURL=command.start.js.map