react-cosmos
Version:
CLI for running React Cosmos inside webpack-powered apps
57 lines (38 loc) • 1.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.detectCosmosConfig = detectCosmosConfig;
exports.detectCosmosConfigPath = detectCosmosConfigPath;
var _path = _interopRequireDefault(require("path"));
var _cli = require("../shared/cli");
var _fs = require("../shared/fs");
var _createCosmosConfig = require("./createCosmosConfig");
var _getCosmosConfigAtPath = require("./getCosmosConfigAtPath");
var _shared = require("./shared");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function detectCosmosConfig() {
var cosmosConfigPath = detectCosmosConfigPath();
return cosmosConfigPath ? (0, _getCosmosConfigAtPath.getCosmosConfigAtPath)(cosmosConfigPath) : (0, _createCosmosConfig.createCosmosConfig)(detectRootDir());
}
function detectCosmosConfigPath() {
var rootDir = detectRootDir();
var cliArgs = (0, _cli.getCliArgs)(); // CLI support for --config path/to/cosmos.config.json
if (typeof cliArgs.config === 'string') {
if (_path.default.extname(cliArgs.config) !== '.json') throw new Error("[Cosmos] Invalid config file type: ".concat(cliArgs.config, " (must be .json)"));
var absPath = _path.default.resolve(rootDir, cliArgs.config);
if (!(0, _fs.fileExists)(absPath)) throw new Error("[Cosmos] Config not found at path: ".concat(cliArgs.config));
return absPath;
}
var defaultConfigPath = _path.default.join(rootDir, 'cosmos.config.json');
return (0, _fs.moduleExists)(defaultConfigPath) ? defaultConfigPath : null;
}
function detectRootDir() {
var cliArgs = (0, _cli.getCliArgs)(); // CLI support for --root-dir path/to/project
if (typeof cliArgs.rootDir === 'string') {
var absPath = _path.default.resolve((0, _shared.getCurrentDir)(), cliArgs.rootDir);
if (!(0, _fs.dirExists)(absPath)) throw new Error("[Cosmos] Dir not found at path: ".concat(cliArgs.rootDir));
return absPath;
}
return (0, _shared.getCurrentDir)();
}