UNPKG

4.75 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getRcConfigForCwd = getRcConfigForCwd;
7exports.getRcArgs = getRcArgs;
8
9var _fs;
10
11function _load_fs() {
12 return _fs = require('fs');
13}
14
15var _path;
16
17function _load_path() {
18 return _path = require('path');
19}
20
21var _commander;
22
23function _load_commander() {
24 return _commander = _interopRequireDefault(require('commander'));
25}
26
27var _lockfile;
28
29function _load_lockfile() {
30 return _lockfile = require('./lockfile');
31}
32
33var _rc;
34
35function _load_rc() {
36 return _rc = _interopRequireWildcard(require('./util/rc.js'));
37}
38
39function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
40
41function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
42
43// Keys that will get resolved relative to the path of the rc file they belong to
44const PATH_KEYS = new Set(['yarn-path', 'cache-folder', 'global-folder', 'modules-folder', 'cwd', 'offline-cache-folder']);
45
46// given a cwd, load all .yarnrc files relative to it
47
48
49function getRcConfigForCwd(cwd, args) {
50 const config = {};
51
52 if (args.indexOf('--no-default-rc') === -1) {
53 Object.assign(config, (_rc || _load_rc()).findRc('yarn', cwd, (fileText, filePath) => {
54 return loadRcFile(fileText, filePath);
55 }));
56 }
57
58 for (let index = args.indexOf('--use-yarnrc'); index !== -1; index = args.indexOf('--use-yarnrc', index + 1)) {
59 const value = args[index + 1];
60
61 if (value && value.charAt(0) !== '-') {
62 Object.assign(config, loadRcFile((0, (_fs || _load_fs()).readFileSync)(value).toString(), value));
63 }
64 }
65
66 return config;
67}
68
69function loadRcFile(fileText, filePath) {
70 var _parse = (0, (_lockfile || _load_lockfile()).parse)(fileText, 'yarnrc');
71
72 const values = _parse.object;
73
74 // some keys reference directories so keep their relativity
75
76 for (const key in values) {
77 if (PATH_KEYS.has(key.replace(/^(--)?([^.]+\.)*/, ''))) {
78 values[key] = (0, (_path || _load_path()).resolve)((0, (_path || _load_path()).dirname)(filePath), values[key]);
79 }
80 }
81
82 return values;
83}
84
85// get the built of arguments of a .yarnrc chain of the passed cwd
86function buildRcArgs(cwd, args) {
87 const config = getRcConfigForCwd(cwd, args);
88
89 const argsForCommands = new Map();
90
91 for (const key in config) {
92 // args can be prefixed with the command name they're meant for, eg.
93 // `--install.check-files true`
94 const keyMatch = key.match(/^--(?:([^.]+)\.)?(.*)$/);
95 if (!keyMatch) {
96 continue;
97 }
98
99 const commandName = keyMatch[1] || '*';
100 const arg = keyMatch[2];
101 const value = config[key];
102
103 // create args for this command name if we didn't previously have them
104 const args = argsForCommands.get(commandName) || [];
105 argsForCommands.set(commandName, args);
106
107 // turn config value into appropriate cli flag
108 const option = (_commander || _load_commander()).default.optionFor(`--${arg}`);
109
110 // If commander doesn't recognize the option or it takes a value after it
111 if (!option || option.optional || option.required) {
112 args.push(`--${arg}`, value);
113 } else if (value === true) {
114 // we can't force remove an arg from cli
115 args.push(`--${arg}`);
116 }
117 }
118
119 return argsForCommands;
120}
121
122// extract the value of a --cwd arg if present
123function extractCwdArg(args) {
124 for (let i = 0, I = args.length; i < I; ++i) {
125 const arg = args[i];
126 if (arg === '--') {
127 return null;
128 } else if (arg === '--cwd') {
129 return args[i + 1];
130 }
131 }
132 return null;
133}
134
135// get a list of arguments from .yarnrc that apply to this commandName
136function getRcArgs(commandName, args, previousCwds = []) {
137 // for the cwd, use the --cwd arg if it was passed or else use process.cwd()
138 const origCwd = extractCwdArg(args) || process.cwd();
139
140 // get a map of command names and their arguments
141 const argMap = buildRcArgs(origCwd, args);
142
143 // concat wildcard arguments and arguments meant for this specific command
144 const newArgs = [...(argMap.get('*') || []), ...(argMap.get(commandName) || [])];
145
146 // check if the .yarnrc args specified a cwd
147 const newCwd = extractCwdArg(newArgs);
148 if (newCwd && newCwd !== origCwd) {
149 // ensure that we don't enter into a loop
150 if (previousCwds.indexOf(newCwd) !== -1) {
151 throw new Error(`Recursive .yarnrc files specifying --cwd flags. Bailing out.`);
152 }
153
154 // if we have a new cwd then let's refetch the .yarnrc args relative to it
155 return getRcArgs(commandName, newArgs, previousCwds.concat(origCwd));
156 }
157
158 return newArgs;
159}
\No newline at end of file