1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getRcConfigForCwd = getRcConfigForCwd;
|
7 | exports.getRcArgs = getRcArgs;
|
8 |
|
9 | var _fs;
|
10 |
|
11 | function _load_fs() {
|
12 | return _fs = require('fs');
|
13 | }
|
14 |
|
15 | var _path;
|
16 |
|
17 | function _load_path() {
|
18 | return _path = require('path');
|
19 | }
|
20 |
|
21 | var _commander;
|
22 |
|
23 | function _load_commander() {
|
24 | return _commander = _interopRequireDefault(require('commander'));
|
25 | }
|
26 |
|
27 | var _lockfile;
|
28 |
|
29 | function _load_lockfile() {
|
30 | return _lockfile = require('./lockfile');
|
31 | }
|
32 |
|
33 | var _rc;
|
34 |
|
35 | function _load_rc() {
|
36 | return _rc = _interopRequireWildcard(require('./util/rc.js'));
|
37 | }
|
38 |
|
39 | function _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 |
|
41 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
42 |
|
43 |
|
44 | const PATH_KEYS = new Set(['yarn-path', 'cache-folder', 'global-folder', 'modules-folder', 'cwd', 'offline-cache-folder']);
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function 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 |
|
69 | function loadRcFile(fileText, filePath) {
|
70 | var _parse = (0, (_lockfile || _load_lockfile()).parse)(fileText, 'yarnrc');
|
71 |
|
72 | const values = _parse.object;
|
73 |
|
74 |
|
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 |
|
86 | function buildRcArgs(cwd, args) {
|
87 | const config = getRcConfigForCwd(cwd, args);
|
88 |
|
89 | const argsForCommands = new Map();
|
90 |
|
91 | for (const key in config) {
|
92 |
|
93 |
|
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 |
|
104 | const args = argsForCommands.get(commandName) || [];
|
105 | argsForCommands.set(commandName, args);
|
106 |
|
107 |
|
108 | const option = (_commander || _load_commander()).default.optionFor(`--${arg}`);
|
109 |
|
110 |
|
111 | if (!option || option.optional || option.required) {
|
112 | args.push(`--${arg}`, value);
|
113 | } else if (value === true) {
|
114 |
|
115 | args.push(`--${arg}`);
|
116 | }
|
117 | }
|
118 |
|
119 | return argsForCommands;
|
120 | }
|
121 |
|
122 |
|
123 | function 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 |
|
136 | function getRcArgs(commandName, args, previousCwds = []) {
|
137 |
|
138 | const origCwd = extractCwdArg(args) || process.cwd();
|
139 |
|
140 |
|
141 | const argMap = buildRcArgs(origCwd, args);
|
142 |
|
143 |
|
144 | const newArgs = [...(argMap.get('*') || []), ...(argMap.get(commandName) || [])];
|
145 |
|
146 |
|
147 | const newCwd = extractCwdArg(newArgs);
|
148 | if (newCwd && newCwd !== origCwd) {
|
149 |
|
150 | if (previousCwds.indexOf(newCwd) !== -1) {
|
151 | throw new Error(`Recursive .yarnrc files specifying --cwd flags. Bailing out.`);
|
152 | }
|
153 |
|
154 |
|
155 | return getRcArgs(commandName, newArgs, previousCwds.concat(origCwd));
|
156 | }
|
157 |
|
158 | return newArgs;
|
159 | } |
\ | No newline at end of file |