UNPKG

react-cosmos

Version:

CLI for running React Cosmos inside webpack-powered apps

117 lines (92 loc) 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFixtures2 = getFixtures2; var _path = _interopRequireDefault(require("path")); var _FixtureLoader = require("react-cosmos-shared2/FixtureLoader"); var _fixtureTree = require("react-cosmos-shared2/fixtureTree"); var _react = require("react-cosmos-shared2/react"); var _url = require("react-cosmos-shared2/url"); var _url2 = _interopRequireDefault(require("url")); var _playgroundHtml = require("./shared/playgroundHtml"); var _getUserModules2 = require("./userDeps/getUserModules"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } 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 getFixtures2(cosmosConfig) { var fixturesDir = cosmosConfig.fixturesDir, fixtureFileSuffix = cosmosConfig.fixtureFileSuffix, rootDir = cosmosConfig.rootDir; var fixtureInfo = []; var _getUserModules = (0, _getUserModules2.getUserModules)(cosmosConfig), fixtures = _getUserModules.fixtures, decorators = _getUserModules.decorators; var fixtureList = (0, _react.getFixtureListFromExports)(fixtures); var fixtureTree = (0, _fixtureTree.createFixtureTree)({ fixtures: fixtureList, fixturesDir: fixturesDir, fixtureFileSuffix: fixtureFileSuffix }); var flatFixtureTree = (0, _fixtureTree.flattenFixtureTree)(fixtureTree); flatFixtureTree.forEach(function (_ref) { var fileName = _ref.fileName, fixtureId = _ref.fixtureId, name = _ref.name, parents = _ref.parents; var fixtureExport = fixtures[fixtureId.path]; var fixture = fixtureId.name === undefined ? fixtureExport : fixtureExport[fixtureId.name]; var treePath = [].concat(_toConsumableArray(parents), [fileName]); if (name) treePath.push(name); fixtureInfo.push({ absoluteFilePath: _path.default.join(rootDir, fixtureId.path), fileName: fileName, getElement: createFixtureElementGetter(fixture, fixtureId.path, decorators), name: name, parents: parents, playgroundUrl: getPlaygroundUrl(cosmosConfig, fixtureId), relativeFilePath: fixtureId.path, rendererUrl: getRendererUrl(cosmosConfig, fixtureId), treePath: treePath }); }); return fixtureInfo; } function getPlaygroundUrl(cosmosConfig, fixtureId) { var host = getPlaygroundHost(cosmosConfig); var query = (0, _url.stringifyPlaygroundUrlQuery)({ fixtureId: fixtureId }); return "".concat(host, "/?").concat(query); } function getRendererUrl(cosmosConfig, fixtureId) { var publicUrl = cosmosConfig.publicUrl, experimentalRendererUrl = cosmosConfig.experimentalRendererUrl; var query = (0, _url.stringifyRendererUrlQuery)({ _fixtureId: fixtureId }); if (experimentalRendererUrl) return "".concat(experimentalRendererUrl, "?").concat(query); var host = getPlaygroundHost(cosmosConfig); var urlPath = _url2.default.resolve(publicUrl, _playgroundHtml.RENDERER_FILENAME); return "".concat(host).concat(urlPath, "?").concat(query); } function getPlaygroundHost(_ref2) { var hostname = _ref2.hostname, port = _ref2.port, https = _ref2.https; return "".concat(https ? 'https' : 'http', "://").concat(hostname || 'localhost', ":").concat(port); } function createFixtureElementGetter(fixture, fixturePath, decoratorsByPath) { var decorators = (0, _react.getSortedDecoratorsForFixturePath)(fixturePath, decoratorsByPath); return function () { return (0, _FixtureLoader.getDecoratedFixtureElement)(fixture, decorators, { fixtureState: {}, setFixtureState: function setFixtureState() {}, onErrorReset: function onErrorReset() {} }); }; }