UNPKG

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