utils.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.devIp = exports.UAParser = exports.easyExtender = exports.serveStatic = exports.connect = exports.portscanner = exports.defaultCallback = exports.arrayify = exports.eachSeries = exports.verifyConfig = exports.getConfigErrors = exports.isMap = exports.isList = exports.willCauseReload = exports.xip = exports.fail = exports.opnWrapper = exports.openBrowser = exports.getUaString = exports.getPort = exports.getPorts = exports._makeUrl = exports.getUrls = exports.getUrl = exports.getUrlOptions = exports.getHostIp = void 0;
  4. const devIp = require("dev-ip");
  5. exports.devIp = devIp;
  6. const portScanner = require("portscanner");
  7. const path = require("path");
  8. const UAParser = require("ua-parser-js");
  9. exports.UAParser = UAParser;
  10. const Immutable = require("immutable");
  11. const immutable_1 = require("immutable");
  12. const _ = require("./lodash.custom");
  13. const parser = new UAParser();
  14. /**
  15. * @param {Object} options
  16. * @returns {String|boolean} - the IP address
  17. * @param devIp
  18. */
  19. function getHostIp(options, devIp) {
  20. if (options) {
  21. var host = options.get("host");
  22. if (host && host !== "localhost") {
  23. return host;
  24. }
  25. if (options.get("detect") === false || !devIp.length) {
  26. return false;
  27. }
  28. }
  29. return devIp.length ? devIp[0] : false;
  30. }
  31. exports.getHostIp = getHostIp;
  32. /**
  33. * Set URL Options
  34. */
  35. function getUrlOptions(options) {
  36. const scheme = options.get("scheme");
  37. const port = options.get("port");
  38. const urls = {};
  39. const listen = options.get("listen");
  40. if (options.get("online") === false || listen) {
  41. const host = listen || "localhost";
  42. urls.local = getUrl(`${scheme}://${host}:${port}`, options);
  43. return Immutable.fromJS(urls);
  44. }
  45. const fn = exports.getHostIp;
  46. const external = xip(fn(options, devIp()), options);
  47. let localhost = "localhost";
  48. if (options.get("xip")) {
  49. localhost = "127.0.0.1";
  50. }
  51. localhost = xip(localhost, options);
  52. return Immutable.fromJS(getUrls(external, localhost, scheme, options));
  53. }
  54. exports.getUrlOptions = getUrlOptions;
  55. /**
  56. * Append a start path if given in options
  57. * @param {String} url
  58. * @param {Object} options
  59. * @returns {String}
  60. */
  61. function getUrl(url, options) {
  62. var prefix = "/";
  63. var startPath = options.get("startPath");
  64. if (startPath) {
  65. if (startPath.charAt(0) === "/") {
  66. prefix = "";
  67. }
  68. url = url + prefix + startPath;
  69. }
  70. return url;
  71. }
  72. exports.getUrl = getUrl;
  73. /**
  74. * @param {String} external
  75. * @param {String} local
  76. * @param {String} scheme
  77. * @param {Object} options
  78. * @returns {{local: string, external: string}}
  79. */
  80. function getUrls(external, local, scheme, options) {
  81. var urls = {
  82. local: getUrl(_makeUrl(scheme, local, options.get("port")), options)
  83. };
  84. if (external !== local) {
  85. urls.external = getUrl(_makeUrl(scheme, external, options.get("port")), options);
  86. }
  87. return urls;
  88. }
  89. exports.getUrls = getUrls;
  90. /**
  91. * @param {String} scheme
  92. * @param {String} host
  93. * @param {Number} port
  94. * @returns {String}
  95. * @private
  96. */
  97. function _makeUrl(scheme, host, port) {
  98. return scheme + "://" + host + ":" + port;
  99. }
  100. exports._makeUrl = _makeUrl;
  101. /**
  102. * Get ports
  103. * @param {Object} options
  104. * @param {Function} cb
  105. */
  106. function getPorts(options, cb) {
  107. var port = options.get("port");
  108. var ports = options.get("ports"); // backwards compatibility
  109. var host = options.get("listen", "localhost"); // backwards compatibility
  110. var max;
  111. if (ports) {
  112. port = ports.get("min");
  113. max = ports.get("max") || null;
  114. }
  115. var fn = exports.getPort;
  116. fn(host, port, max, cb);
  117. }
  118. exports.getPorts = getPorts;
  119. function getPort(host, port, max, cb) {
  120. portScanner.findAPortNotInUse(port, max, {
  121. host: host,
  122. timeout: 1000
  123. }, cb);
  124. }
  125. exports.getPort = getPort;
  126. /**
  127. * @param {String} ua
  128. * @returns {Object}
  129. */
  130. function getUaString(ua) {
  131. return parser.setUA(ua).getBrowser();
  132. }
  133. exports.getUaString = getUaString;
  134. /**
  135. * Open the page in browser
  136. * @param {String} url
  137. * @param {Object} options
  138. * @param {BrowserSync} bs
  139. */
  140. function openBrowser(url, options, bs) {
  141. const open = options.get("open");
  142. const browser = options.get("browser");
  143. if (_.isString(open)) {
  144. if (options.getIn(["urls", open])) {
  145. url = options.getIn(["urls", open]);
  146. }
  147. }
  148. const fn = exports.opnWrapper;
  149. if (open) {
  150. if (browser !== "default") {
  151. if ((0, exports.isList)(browser)) {
  152. browser.forEach(function (browser) {
  153. fn(url, browser, bs);
  154. });
  155. }
  156. else {
  157. fn(url, browser, bs); // single
  158. }
  159. }
  160. else {
  161. fn(url, null, bs);
  162. }
  163. }
  164. }
  165. exports.openBrowser = openBrowser;
  166. /**
  167. * Wrapper for opn module
  168. * @param url
  169. * @param name
  170. * @param bs
  171. */
  172. function opnWrapper(url, name, bs) {
  173. var options = (function () {
  174. if (_.isString(name)) {
  175. return { app: name };
  176. }
  177. if (Immutable.Map.isMap(name)) {
  178. return name.toJS();
  179. }
  180. return {};
  181. })();
  182. var opn = require("opn");
  183. opn(url, options).catch(function () {
  184. bs.events.emit("browser:error");
  185. });
  186. }
  187. exports.opnWrapper = opnWrapper;
  188. /**
  189. * @param {Boolean} kill
  190. * @param {String|Error} [errMessage]
  191. * @param {Function} [cb]
  192. */
  193. function fail(kill, errMessage, cb) {
  194. if (kill) {
  195. if (_.isFunction(cb)) {
  196. if (errMessage.message) {
  197. // Is this an error object?
  198. cb(errMessage);
  199. }
  200. else {
  201. cb(new Error(errMessage));
  202. }
  203. }
  204. process.exit(1);
  205. }
  206. }
  207. exports.fail = fail;
  208. /**
  209. * Add support for xip.io urls
  210. * @param {String} host
  211. * @param {Object} options
  212. * @returns {String}
  213. */
  214. function xip(host, options) {
  215. var suffix = options.get("hostnameSuffix");
  216. if (options.get("xip")) {
  217. return host + ".xip.io";
  218. }
  219. if (suffix) {
  220. return host + suffix;
  221. }
  222. return host;
  223. }
  224. exports.xip = xip;
  225. /**
  226. * Determine if an array of file paths will cause a full page reload.
  227. * @param {Array} needles - filepath such as ["core.css", "index.html"]
  228. * @param {Array} haystack
  229. * @returns {Boolean}
  230. */
  231. function willCauseReload(needles, haystack) {
  232. return needles.some(function (needle) {
  233. return !_.includes(haystack, path.extname(needle).replace(".", ""));
  234. });
  235. }
  236. exports.willCauseReload = willCauseReload;
  237. exports.isList = Immutable.List.isList;
  238. exports.isMap = Immutable.Map.isMap;
  239. /**
  240. * @param {Map} options
  241. * @returns {Array}
  242. */
  243. function getConfigErrors(options) {
  244. var messages = require("./config").errors;
  245. var errors = [];
  246. if (options.get("server") && options.get("proxy")) {
  247. errors.push(messages["server+proxy"]);
  248. }
  249. return errors;
  250. }
  251. exports.getConfigErrors = getConfigErrors;
  252. /**
  253. * @param {Map} options
  254. * @param {Function} [cb]
  255. */
  256. function verifyConfig(options, cb) {
  257. var errors = getConfigErrors(options);
  258. if (errors.length) {
  259. fail(true, errors.join("\n"), cb);
  260. return false;
  261. }
  262. return true;
  263. }
  264. exports.verifyConfig = verifyConfig;
  265. function eachSeries(arr, iterator, callback) {
  266. callback = callback || function () { };
  267. var completed = 0;
  268. var iterate = function () {
  269. iterator(arr[completed], function (err) {
  270. if (err) {
  271. callback(err);
  272. callback = function () { };
  273. }
  274. else {
  275. ++completed;
  276. if (completed >= arr.length) {
  277. callback();
  278. }
  279. else {
  280. iterate();
  281. }
  282. }
  283. });
  284. };
  285. iterate();
  286. }
  287. exports.eachSeries = eachSeries;
  288. /**
  289. * @param {Immutable.List|Array|String} incoming
  290. * @returns {Array}
  291. */
  292. function arrayify(incoming) {
  293. if (immutable_1.List.isList(incoming)) {
  294. return incoming.toArray();
  295. }
  296. return [].concat(incoming).filter(Boolean);
  297. }
  298. exports.arrayify = arrayify;
  299. function defaultCallback(err) {
  300. if (err && err.message) {
  301. console.error(err.message);
  302. }
  303. }
  304. exports.defaultCallback = defaultCallback;
  305. exports.portscanner = portScanner;
  306. exports.connect = require("connect");
  307. exports.serveStatic = require("./server/serve-static-wrapper").default();
  308. exports.easyExtender = require("easy-extender");
  309. //# sourceMappingURL=utils.js.map