react-cosmos
Version:
CLI for running React Cosmos inside webpack-powered apps
137 lines (96 loc) • 5.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.generateExport = generateExport;
var _fsExtra = _interopRequireDefault(require("fs-extra"));
var _path = _interopRequireDefault(require("path"));
var _detectCosmosConfig = require("../config/detectCosmosConfig");
var _playgroundHtml = require("../shared/playgroundHtml");
var _pluginConfigs = require("../shared/pluginConfigs");
var _static = require("../shared/static");
var _utils = require("../shared/utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 generateExport() {
return _generateExport.apply(this, arguments);
}
function _generateExport() {
_generateExport = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var plugins,
cosmosConfig,
exportPath,
_args = arguments;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
plugins = _args.length > 0 && _args[0] !== undefined ? _args[0] : [];
cosmosConfig = (0, _detectCosmosConfig.detectCosmosConfig)(); // Clear previous export (or other files at export path)
exportPath = cosmosConfig.exportPath;
_fsExtra.default.removeSync(exportPath); // Copy static assets first, so that the built index.html overrides the its
// template file (in case the static assets are served from the root path)
copyStaticAssets(cosmosConfig);
_context.next = 7;
return Promise.all(plugins.map(function (plugin) {
return plugin({
cosmosConfig: cosmosConfig
});
}));
case 7:
exportPlaygroundFiles(cosmosConfig);
console.log('[Cosmos] Export complete!');
console.log("Export path: ".concat(cosmosConfig.exportPath));
case 10:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _generateExport.apply(this, arguments);
}
function copyStaticAssets(cosmosConfig) {
var staticPath = cosmosConfig.staticPath;
if (!staticPath) {
return;
}
var exportPath = cosmosConfig.exportPath;
if (exportPath.indexOf(staticPath) !== -1) {
console.warn("[Cosmos] Warning: Can't export static path because it contains the export path! (avoiding infinite loop)");
console.warn('Public path:', staticPath);
console.warn('Export path:', exportPath);
return;
}
if (!_fsExtra.default.existsSync(staticPath)) {
console.log('[Cosmos] Warning: config.staticPath points to missing dir', staticPath);
return;
}
var publicUrl = cosmosConfig.publicUrl;
var exportStaticPath = _path.default.resolve(exportPath, (0, _utils.removeLeadingSlash)(publicUrl));
_fsExtra.default.copySync(staticPath, exportStaticPath);
}
function exportPlaygroundFiles(cosmosConfig) {
var exportPath = cosmosConfig.exportPath;
var pluginConfigs = (0, _pluginConfigs.getPluginConfigs)(cosmosConfig);
pluginConfigs.forEach(function (pluginConfig) {
return exportPlugin(cosmosConfig, pluginConfig);
});
_fsExtra.default.copySync(require.resolve('react-cosmos-playground2/dist'), _path.default.resolve(exportPath, '_playground.js'));
_fsExtra.default.copySync((0, _static.getStaticPath)('favicon.ico'), _path.default.resolve(exportPath, '_cosmos.ico'));
var playgroundHtml = (0, _playgroundHtml.getExportPlaygroundHtml)(cosmosConfig, pluginConfigs);
_fsExtra.default.writeFileSync(_path.default.resolve(exportPath, 'index.html'), playgroundHtml);
}
function exportPlugin(cosmosConfig, pluginConfig) {
var rootDir = cosmosConfig.rootDir,
exportPath = cosmosConfig.exportPath;
var pluginExportDir = _path.default.resolve(exportPath, '_plugin'); // Copy plugin config
var relConfigPath = _path.default.join(pluginConfig.rootDir, 'cosmos.plugin.json');
var absConfigPath = _path.default.resolve(rootDir, relConfigPath);
_fsExtra.default.copySync(absConfigPath, _path.default.resolve(pluginExportDir, relConfigPath)); // Copy UI script
if (pluginConfig.ui) {
var absUiPath = _path.default.resolve(rootDir, pluginConfig.ui);
_fsExtra.default.copySync(absUiPath, _path.default.resolve(pluginExportDir, pluginConfig.ui));
}
}