react-cosmos
Version:
CLI for running React Cosmos inside webpack-powered apps
97 lines (77 loc) • 3.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getFixtureUrls = void 0;
exports.getFixtureUrlsSync = getFixtureUrlsSync;
var _react = require("react-cosmos-shared2/react");
var _url = require("react-cosmos-shared2/url");
var _playgroundHtml = require("./shared/playgroundHtml");
var _getUserModules2 = require("./userDeps/getUserModules");
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); }); }; }
var getFixtureUrls = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(args) {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", getFixtureUrlsSync(args));
case 1:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function getFixtureUrls(_x) {
return _ref.apply(this, arguments);
};
}();
exports.getFixtureUrls = getFixtureUrls;
function getFixtureUrlsSync(_ref2) {
var cosmosConfig = _ref2.cosmosConfig,
_ref2$fullScreen = _ref2.fullScreen,
fullScreen = _ref2$fullScreen === void 0 ? false : _ref2$fullScreen;
var host = getPlaygroundHost(cosmosConfig);
var fixtureUrls = [];
function pushFixtureUrl(fixtureId) {
fixtureUrls.push(createFixtureUrl(host, fixtureId, fullScreen));
}
var _getUserModules = (0, _getUserModules2.getUserModules)(cosmosConfig),
fixtures = _getUserModules.fixtures;
var fixtureList = (0, _react.getFixtureListFromExports)(fixtures);
Object.keys(fixtureList).forEach(function (fixturePath) {
var fixtureItem = fixtureList[fixturePath];
if (fixtureItem.type === 'single') {
pushFixtureUrl({
path: fixturePath
});
} else if (fixtureItem.type === 'multi') {
fixtureItem.fixtureNames.forEach(function (fixtureName) {
pushFixtureUrl({
path: fixturePath,
name: fixtureName
});
});
}
});
return fixtureUrls;
}
function createFixtureUrl(host, fixtureId, fullScreen) {
if (fullScreen) {
var _query = (0, _url.stringifyRendererUrlQuery)({
_fixtureId: fixtureId
});
return "".concat(host, "/").concat(_playgroundHtml.RENDERER_FILENAME, "?").concat(_query);
}
var query = (0, _url.stringifyPlaygroundUrlQuery)({
fixtureId: fixtureId
});
return "".concat(host, "/?").concat(query);
}
function getPlaygroundHost(_ref3) {
var hostname = _ref3.hostname,
port = _ref3.port;
return "".concat(hostname || 'localhost', ":").concat(port);
}