1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getRcConfigForCwd = getRcConfigForCwd;
|
7 | exports.getRcConfigForFolder = getRcConfigForFolder;
|
8 | exports.getRcArgs = getRcArgs;
|
9 |
|
10 | var _fs;
|
11 |
|
12 | function _load_fs() {
|
13 | return _fs = require('fs');
|
14 | }
|
15 |
|
16 | var _path;
|
17 |
|
18 | function _load_path() {
|
19 | return _path = require('path');
|
20 | }
|
21 |
|
22 | var _commander;
|
23 |
|
24 | function _load_commander() {
|
25 | return _commander = _interopRequireDefault(require('commander'));
|
26 | }
|
27 |
|
28 | var _lockfile;
|
29 |
|
30 | function _load_lockfile() {
|
31 | return _lockfile = require('./lockfile');
|
32 | }
|
33 |
|
34 | var _rc;
|
35 |
|
36 | function _load_rc() {
|
37 | return _rc = _interopRequireWildcard(require('./util/rc.js'));
|
38 | }
|
39 |
|
40 | 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; } }
|
41 |
|
42 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
43 |
|
44 |
|
45 | const PATH_KEYS = new Set(['yarn-path', 'cache-folder', 'global-folder', 'modules-folder', 'cwd', 'offline-cache-folder']);
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | function 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 |
|
70 | function 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 |
|
80 | function 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 |
|
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 |
|
101 | function buildRcArgs(cwd, args) {
|
102 | const config = getRcConfigForCwd(cwd, args);
|
103 |
|
104 | const argsForCommands = new Map();
|
105 |
|
106 | for (const key in config) {
|
107 |
|
108 |
|
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 |
|
119 | const args = argsForCommands.get(commandName) || [];
|
120 | argsForCommands.set(commandName, args);
|
121 |
|
122 |
|
123 | const option = (_commander || _load_commander()).default.optionFor(`--${arg}`);
|
124 |
|
125 |
|
126 | if (!option || option.optional || option.required) {
|
127 | args.push(`--${arg}`, value);
|
128 | } else if (value === true) {
|
129 |
|
130 | args.push(`--${arg}`);
|
131 | }
|
132 | }
|
133 |
|
134 | return argsForCommands;
|
135 | }
|
136 |
|
137 |
|
138 | function 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 |
|
151 | function getRcArgs(commandName, args, previousCwds = []) {
|
152 |
|
153 | const origCwd = extractCwdArg(args) || process.cwd();
|
154 |
|
155 |
|
156 | const argMap = buildRcArgs(origCwd, args);
|
157 |
|
158 |
|
159 | const newArgs = [...(argMap.get('*') || []), ...(argMap.get(commandName) || [])];
|
160 |
|
161 |
|
162 | const newCwd = extractCwdArg(newArgs);
|
163 | if (newCwd && newCwd !== origCwd) {
|
164 |
|
165 | if (previousCwds.indexOf(newCwd) !== -1) {
|
166 | throw new Error(`Recursive .yarnrc files specifying --cwd flags. Bailing out.`);
|
167 | }
|
168 |
|
169 |
|
170 | return getRcArgs(commandName, newArgs, previousCwds.concat(origCwd));
|
171 | }
|
172 |
|
173 | return newArgs;
|
174 | } |
\ | No newline at end of file |