JezK
Edit File: rc.js
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.getRcArgs = getRcArgs; exports.getRcConfigForCwd = getRcConfigForCwd; exports.getRcConfigForFolder = getRcConfigForFolder; function _toConsumableArray2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); _toConsumableArray2 = function _toConsumableArray2() { return data; }; return data; } function _fs() { var data = require("fs"); _fs = function _fs() { return data; }; return data; } function _path() { var data = require("path"); _path = function _path() { return data; }; return data; } function _lockfile() { var data = require("./lockfile"); _lockfile = function _lockfile() { return data; }; return data; } function rcUtil() { var data = _interopRequireWildcard(require("./util/rc.js")); rcUtil = function rcUtil() { return data; }; return data; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var _require = require('commander'), Command = _require.Command; var commander = new Command(); // Keys that will get resolved relative to the path of the rc file they belong to var PATH_KEYS = new Set(['yarn-path', 'cache-folder', 'global-folder', 'modules-folder', 'cwd', 'offline-cache-folder']); // given a cwd, load all .yarnrc files relative to it function getRcConfigForCwd(cwd, args) { var config = {}; if (args.indexOf('--no-default-rc') === -1) { Object.assign(config, rcUtil().findRc('yarn', cwd, function (fileText, filePath) { return loadRcFile(fileText, filePath); })); } for (var index = args.indexOf('--use-yarnrc'); index !== -1; index = args.indexOf('--use-yarnrc', index + 1)) { var value = args[index + 1]; if (value && value.charAt(0) !== '-') { Object.assign(config, loadRcFile((0, _fs().readFileSync)(value, 'utf8'), value)); } } return config; } function getRcConfigForFolder(cwd) { var filePath = (0, _path().resolve)(cwd, '.yarnrc'); if (!(0, _fs().existsSync)(filePath)) { return {}; } var fileText = (0, _fs().readFileSync)(filePath, 'utf8'); return loadRcFile(fileText, filePath); } function loadRcFile(fileText, filePath) { var _parse = (0, _lockfile().parse)(fileText, filePath), values = _parse.object; if (filePath.match(/\.yml$/) && typeof values.yarnPath === 'string') { values = { 'yarn-path': values.yarnPath }; } // some keys reference directories so keep their relativity for (var _key in values) { if (PATH_KEYS.has(_key.replace(/^(--)?([^.]+\.)*/, ''))) { values[_key] = (0, _path().resolve)((0, _path().dirname)(filePath), values[_key]); } } return values; } // get the built of arguments of a .yarnrc chain of the passed cwd function buildRcArgs(cwd, args) { var config = getRcConfigForCwd(cwd, args); var argsForCommands = new Map(); for (var _key2 in config) { // args can be prefixed with the command name they're meant for, eg. // `--install.check-files true` var keyMatch = _key2.match(/^--(?:([^.]+)\.)?(.*)$/); if (!keyMatch) { continue; } var commandName = keyMatch[1] || '*'; var arg = keyMatch[2]; var value = config[_key2]; // create args for this command name if we didn't previously have them var _args = argsForCommands.get(commandName) || []; argsForCommands.set(commandName, _args); // turn config value into appropriate cli flag var option = commander._findOption("--".concat(arg)); // If commander doesn't recognize the option or it takes a value after it if (!option || option.optional || option.required) { _args.push("--".concat(arg), value); } else if (value === true) { // we can't force remove an arg from cli _args.push("--".concat(arg)); } } return argsForCommands; } // extract the value of a --cwd arg if present function extractCwdArg(args) { for (var i = 0, I = args.length; i < I; ++i) { var arg = args[i]; if (arg === '--') { return null; } else if (arg === '--cwd') { return args[i + 1]; } } return null; } // get a list of arguments from .yarnrc that apply to this commandName function getRcArgs(commandName, args) { var previousCwds = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; // for the cwd, use the --cwd arg if it was passed or else use process.cwd() var origCwd = extractCwdArg(args) || process.cwd(); // get a map of command names and their arguments var argMap = buildRcArgs(origCwd, args); // concat wildcard arguments and arguments meant for this specific command var newArgs = [].concat((0, _toConsumableArray2()["default"])(argMap.get('*') || []), (0, _toConsumableArray2()["default"])(argMap.get(commandName) || [])); // check if the .yarnrc args specified a cwd var newCwd = extractCwdArg(newArgs); if (newCwd && newCwd !== origCwd) { // ensure that we don't enter into a loop if (previousCwds.indexOf(newCwd) !== -1) { throw new Error("Recursive .yarnrc files specifying --cwd flags. Bailing out."); } // if we have a new cwd then let's refetch the .yarnrc args relative to it return getRcArgs(commandName, newArgs, previousCwds.concat(origCwd)); } return newArgs; } //# sourceMappingURL=rc.js.map