react-cosmos
Version:
CLI for running React Cosmos inside webpack-powered apps
166 lines (131 loc) • 6.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.userDepsFile = userDepsFile;
var _chokidar = require("chokidar");
var _fs = require("fs");
var _lodash = require("lodash");
var _path = _interopRequireDefault(require("path"));
var _util = _interopRequireDefault(require("util.promisify"));
var _cli = require("../shared/cli");
var _generateUserDepsModule = require("../userDeps/generateUserDepsModule");
var _shared = require("../userDeps/shared");
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 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 writeFileAsync = (0, _util.default)(_fs.writeFile);
function userDepsFile(_x) {
return _userDepsFile.apply(this, arguments);
}
function _userDepsFile() {
_userDepsFile = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(args) {
var cosmosConfig, watcher;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (shouldGenerateUserDepsFile(args)) {
_context.next = 2;
break;
}
return _context.abrupt("return");
case 2:
cosmosConfig = args.cosmosConfig;
_context.next = 5;
return generateUserDepsFile(cosmosConfig);
case 5:
_context.next = 7;
return startFixtureFileWatcher(cosmosConfig);
case 7:
watcher = _context.sent;
return _context.abrupt("return", function () {
watcher.close();
});
case 9:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _userDepsFile.apply(this, arguments);
}
function shouldGenerateUserDepsFile(_ref) {
var cosmosConfig = _ref.cosmosConfig,
platformType = _ref.platformType;
return platformType === 'native' || cosmosConfig.experimentalRendererUrl !== null || // CLI support for --external-userdeps flag (useful with react-native-web)
Boolean((0, _cli.getCliArgs)().externalUserdeps);
}
var DEBOUNCE_INTERVAL = 50;
function startFixtureFileWatcher(_x2) {
return _startFixtureFileWatcher.apply(this, arguments);
}
function _startFixtureFileWatcher() {
_startFixtureFileWatcher = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(cosmosConfig) {
var fixturesDir, fixtureFileSuffix, FILE_PATTERNS;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
fixturesDir = cosmosConfig.fixturesDir, fixtureFileSuffix = cosmosConfig.fixtureFileSuffix;
FILE_PATTERNS = [].concat(_toConsumableArray((0, _shared.getFixturePatterns)(fixturesDir, fixtureFileSuffix)), _toConsumableArray((0, _shared.getDecoratorPatterns)()));
return _context2.abrupt("return", new Promise(function (resolve) {
var watcher = (0, _chokidar.watch)(FILE_PATTERNS, {
ignored: (0, _shared.getIgnorePatterns)(),
ignoreInitial: true,
cwd: cosmosConfig.rootDir
}).on('ready', function () {
return resolve(watcher);
}).on('all', (0, _lodash.debounce)(function () {
return generateUserDepsFile(cosmosConfig);
}, DEBOUNCE_INTERVAL));
}));
case 3:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
return _startFixtureFileWatcher.apply(this, arguments);
}
function generateUserDepsFile(_x3) {
return _generateUserDepsFile.apply(this, arguments);
}
function _generateUserDepsFile() {
_generateUserDepsFile = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(cosmosConfig) {
var userDepsFilePath, port, rendererConfig, userDepsModule, relUserDepsFilePath;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
userDepsFilePath = cosmosConfig.userDepsFilePath, port = cosmosConfig.port;
rendererConfig = {
port: port
};
userDepsModule = (0, _generateUserDepsModule.generateUserDepsModule)({
cosmosConfig: cosmosConfig,
rendererConfig: rendererConfig,
relativeToDir: _path.default.dirname(userDepsFilePath)
});
_context3.next = 5;
return writeFileAsync(userDepsFilePath, userDepsModule, 'utf8');
case 5:
relUserDepsFilePath = _path.default.relative(process.cwd(), userDepsFilePath);
console.log("[Cosmos] Generated ".concat(relUserDepsFilePath));
case 7:
case "end":
return _context3.stop();
}
}
}, _callee3);
}));
return _generateUserDepsFile.apply(this, arguments);
}