UNPKG

react-cosmos

Version:

CLI for running React Cosmos inside webpack-powered apps

209 lines (161 loc) 8.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.startDevServer = startDevServer; var _path = _interopRequireDefault(require("path")); var _detectCosmosConfig = require("../config/detectCosmosConfig"); var _pluginConfigs = require("../shared/pluginConfigs"); var _static = require("../shared/static"); var _app = require("./app"); var _httpServer = require("./httpServer"); var _messageHandler = require("./messageHandler"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function startDevServer(_x) { return _startDevServer.apply(this, arguments); } function _startDevServer() { _startDevServer = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(platformType) { var plugins, cosmosConfig, pluginConfigs, app, pluginCleanupCallbacks, httpServer, msgHandler, cleanUp, _cleanUp, _iterator, _step, plugin, pluginReturn, _args2 = arguments; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _cleanUp = function _cleanUp3() { _cleanUp = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return Promise.all(pluginCleanupCallbacks.map(function (cleanup) { return cleanup(); })); case 2: _context.next = 4; return httpServer.stop(); case 4: msgHandler.cleanUp(); case 5: case "end": return _context.stop(); } } }, _callee); })); return _cleanUp.apply(this, arguments); }; cleanUp = function _cleanUp2() { return _cleanUp.apply(this, arguments); }; plugins = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : []; cosmosConfig = (0, _detectCosmosConfig.detectCosmosConfig)(); logCosmosConfigInfo(); pluginConfigs = (0, _pluginConfigs.getPluginConfigs)(cosmosConfig); logPluginInfo(pluginConfigs); app = (0, _app.createApp)(platformType, cosmosConfig, pluginConfigs); if (cosmosConfig.staticPath) { (0, _static.serveStaticDir)(app, cosmosConfig.staticPath, cosmosConfig.publicUrl); } pluginCleanupCallbacks = []; _context2.next = 12; return (0, _httpServer.createHttpServer)(cosmosConfig, app); case 12: httpServer = _context2.sent; _context2.next = 15; return httpServer.start(); case 15: msgHandler = (0, _messageHandler.createMessageHandler)(httpServer.server); _context2.prev = 16; _iterator = _createForOfIteratorHelper(plugins); _context2.prev = 18; _iterator.s(); case 20: if ((_step = _iterator.n()).done) { _context2.next = 28; break; } plugin = _step.value; _context2.next = 24; return plugin({ cosmosConfig: cosmosConfig, platformType: platformType, httpServer: httpServer.server, expressApp: app, sendMessage: msgHandler.sendMessage }); case 24: pluginReturn = _context2.sent; if (typeof pluginReturn === 'function') { pluginCleanupCallbacks.push(pluginReturn); } case 26: _context2.next = 20; break; case 28: _context2.next = 33; break; case 30: _context2.prev = 30; _context2.t0 = _context2["catch"](18); _iterator.e(_context2.t0); case 33: _context2.prev = 33; _iterator.f(); return _context2.finish(33); case 36: _context2.next = 42; break; case 38: _context2.prev = 38; _context2.t1 = _context2["catch"](16); cleanUp(); throw _context2.t1; case 42: return _context2.abrupt("return", cleanUp); case 43: case "end": return _context2.stop(); } } }, _callee2, null, [[16, 38], [18, 30, 33, 36]]); })); return _startDevServer.apply(this, arguments); } function logCosmosConfigInfo() { var cosmosConfigPath = (0, _detectCosmosConfig.detectCosmosConfigPath)(); if (!cosmosConfigPath) { console.log("[Cosmos] Using default cosmos config"); return; } var relConfigPath = _path.default.relative(process.cwd(), cosmosConfigPath); console.log("[Cosmos] Using cosmos config found at ".concat(relConfigPath)); } function logPluginInfo(pluginConfigs) { var pluginCount = pluginConfigs.length; if (pluginCount > 0) { var pluginLabel = pluginCount === 1 ? 'plugin' : 'plugins'; var pluginNames = pluginConfigs.map(function (p) { return p.name; }).join(', '); console.log("[Cosmos] Found ".concat(pluginCount, " ").concat(pluginLabel, ": ").concat(pluginNames)); } }