react-cosmos
Version:
CLI for running React Cosmos inside webpack-powered apps
133 lines (107 loc) • 6.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createCosmosConfig = createCosmosConfig;
var _path = _interopRequireDefault(require("path"));
var _cli = require("../shared/cli");
var _resolveModule = require("./resolveModule");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function createCosmosConfig(rootDir) {
var _cosmosConfigInput$di, _cosmosConfigInput$ex;
var cosmosConfigInput = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _objectSpread(_objectSpread({}, cosmosConfigInput), {}, {
rootDir: rootDir,
exportPath: getExportPath(cosmosConfigInput, rootDir),
disablePlugins: (_cosmosConfigInput$di = cosmosConfigInput.disablePlugins) !== null && _cosmosConfigInput$di !== void 0 ? _cosmosConfigInput$di : true,
fixtureFileSuffix: getFixtureFileSuffix(cosmosConfigInput),
fixturesDir: getFixturesDir(cosmosConfigInput),
globalImports: getGlobalImports(cosmosConfigInput, rootDir),
hostname: getHostname(cosmosConfigInput),
https: getHttps(cosmosConfigInput),
httpsOptions: getHttpsOptions(cosmosConfigInput, rootDir),
port: getPort(cosmosConfigInput),
publicUrl: getPublicUrl(cosmosConfigInput),
staticPath: getStaticPath(cosmosConfigInput, rootDir),
userDepsFilePath: getUserDepsFilePath(cosmosConfigInput, rootDir),
watchDirs: getWatchDirs(cosmosConfigInput, rootDir),
experimentalRendererUrl: (_cosmosConfigInput$ex = cosmosConfigInput.experimentalRendererUrl) !== null && _cosmosConfigInput$ex !== void 0 ? _cosmosConfigInput$ex : null,
ui: cosmosConfigInput.ui || {}
});
}
function getExportPath(cosmosConfigInput, rootDir) {
var _cosmosConfigInput$ex2 = cosmosConfigInput.exportPath,
exportPath = _cosmosConfigInput$ex2 === void 0 ? 'cosmos-export' : _cosmosConfigInput$ex2;
return _path.default.resolve(rootDir, exportPath);
}
function getStaticPath(cosmosConfigInput, rootDir) {
var _cosmosConfigInput$st = cosmosConfigInput.staticPath,
staticPath = _cosmosConfigInput$st === void 0 ? null : _cosmosConfigInput$st;
return staticPath && _path.default.resolve(rootDir, staticPath);
}
function getHttps(cosmosConfigInput) {
var _cosmosConfigInput$ht = cosmosConfigInput.https,
https = _cosmosConfigInput$ht === void 0 ? false : _cosmosConfigInput$ht;
return https;
}
function getHttpsOptions(cosmosConfigInput, rootDir) {
var _cosmosConfigInput$ht2, _cosmosConfigInput$ht3;
if ((_cosmosConfigInput$ht2 = cosmosConfigInput.httpsOptions) !== null && _cosmosConfigInput$ht2 !== void 0 && _cosmosConfigInput$ht2.keyPath && (_cosmosConfigInput$ht3 = cosmosConfigInput.httpsOptions) !== null && _cosmosConfigInput$ht3 !== void 0 && _cosmosConfigInput$ht3.certPath) {
return {
keyPath: _path.default.resolve(rootDir, cosmosConfigInput.httpsOptions.keyPath),
certPath: _path.default.resolve(rootDir, cosmosConfigInput.httpsOptions.certPath)
};
}
return null;
}
function getPublicUrl(_ref) {
var _ref$publicUrl = _ref.publicUrl,
publicUrl = _ref$publicUrl === void 0 ? '/' : _ref$publicUrl;
return publicUrl;
}
function getFixturesDir(_ref2) {
var _ref2$fixturesDir = _ref2.fixturesDir,
fixturesDir = _ref2$fixturesDir === void 0 ? '__fixtures__' : _ref2$fixturesDir;
return fixturesDir;
}
function getFixtureFileSuffix(_ref3) {
var _ref3$fixtureFileSuff = _ref3.fixtureFileSuffix,
fixtureFileSuffix = _ref3$fixtureFileSuff === void 0 ? 'fixture' : _ref3$fixtureFileSuff;
return fixtureFileSuffix;
}
function getWatchDirs(cosmosConfigInput, rootDir) {
var _cosmosConfigInput$wa = cosmosConfigInput.watchDirs,
watchDirs = _cosmosConfigInput$wa === void 0 ? ['.'] : _cosmosConfigInput$wa;
return watchDirs.map(function (watchDir) {
return _path.default.resolve(rootDir, watchDir);
});
}
function getUserDepsFilePath(cosmosConfigInput, rootDir) {
var _cosmosConfigInput$us = cosmosConfigInput.userDepsFilePath,
userDepsFilePath = _cosmosConfigInput$us === void 0 ? 'cosmos.userdeps.js' : _cosmosConfigInput$us;
return _path.default.resolve(rootDir, userDepsFilePath);
}
function getHostname(_ref4) {
var _ref4$hostname = _ref4.hostname,
hostname = _ref4$hostname === void 0 ? null : _ref4$hostname;
return hostname;
}
function getPort(cosmosConfigInput) {
var cliArgs = (0, _cli.getCliArgs)();
if (typeof cliArgs.port === 'number') {
return cliArgs.port;
}
var _cosmosConfigInput$po = cosmosConfigInput.port,
port = _cosmosConfigInput$po === void 0 ? 5000 : _cosmosConfigInput$po;
return port;
}
function getGlobalImports(cosmosConfigInput, rootDir) {
var _cosmosConfigInput$gl = cosmosConfigInput.globalImports,
globalImports = _cosmosConfigInput$gl === void 0 ? [] : _cosmosConfigInput$gl;
return globalImports.map(function (globalImport) {
return (0, _resolveModule.resolveModule)(rootDir, globalImport);
});
}