-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Ability to pass command CLI arguments via configuration in …
….yarnrc (#3033) This PR opens the way to share configuration between multiple projects via the use of the .yarnrc files. The current design it pretty simple: it simply adds the supported options from the yarnrc file at the beginning of the command line. Some notes: The yarnrc is currently parsed synchronously. I believe this is not an issue, since it's quite literally the very first thing we need to do when booting the application (even before the command line can be parsed), so there's really nothing we could run concurrently anyway. The yarnrc is read in another file (https://github.com/yarnpkg/yarn/blob/master/src/registries/yarn-registry.js). I haven't changed how it works there, since I wanted my first draft to have as few changes as possible, but it's something that should probably be fixed so that the code only use one path to manage the yarnrc files.
- Loading branch information
Showing
10 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
__tests__/fixtures/lifecycle-scripts/yarnrc-cli-relative/.yarnrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--cache-folder foobar/hello/world |
2 changes: 2 additions & 0 deletions
2
__tests__/fixtures/lifecycle-scripts/yarnrc-cli-relative/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--cache-folder /tmp/foobar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* @flow */ | ||
|
||
import parse from './lockfile/parse.js'; | ||
|
||
const rc = require('rc'); | ||
|
||
// Keys that will get resolved relative to the path of the rc file they belong to | ||
const PATH_KEYS = [ | ||
'cache-folder', | ||
'global-folder', | ||
'modules-folder', | ||
]; | ||
|
||
let rcConfCache; | ||
let rcArgsCache; | ||
|
||
const buildRcConf = () => rc('yarn', {}, [], (fileText) => { | ||
const values = parse(fileText, 'yarnrc'); | ||
const keys = Object.keys(values); | ||
|
||
// Unfortunately, the "rc" module we use doesn't tell us the file path :( | ||
// cf https://github.com/dominictarr/rc/issues/61 | ||
|
||
for (const key of keys) { | ||
for (const pathKey of PATH_KEYS) { | ||
if (key.replace(/^(--)?([^.]+\.)+/, '') === pathKey) { | ||
// values[key] = resolve(dirname(filePath), values[key]); | ||
if (!values[key].startsWith('/')) { | ||
delete values[keys]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return values; | ||
}); | ||
|
||
export function getRcConf(): { [string]: Array<string> } { | ||
if (!rcConfCache) { | ||
rcConfCache = buildRcConf(); | ||
} | ||
|
||
return rcConfCache; | ||
} | ||
|
||
const buildRcArgs = () => Object.keys(getRcConf()).reduce((argLists, key) => { | ||
const miniparse = key.match(/^--(?:([^.]+)\.)?(.*)$/); | ||
|
||
if (!miniparse) { | ||
return argLists; | ||
} | ||
|
||
const namespace = miniparse[1] || '*'; | ||
const arg = miniparse[2]; | ||
const value = getRcConf()[key]; | ||
|
||
if (!argLists[namespace]) { | ||
argLists[namespace] = []; | ||
} | ||
|
||
if (typeof value === 'string') { | ||
argLists[namespace] = argLists[namespace].concat([`--${arg}`, value]); | ||
} else { | ||
argLists[namespace] = argLists[namespace].concat([`--${arg}`]); | ||
} | ||
|
||
return argLists; | ||
}, {}); | ||
|
||
export function getRcArgs(command: string): Array<string> { | ||
if (!rcArgsCache) { | ||
rcArgsCache = buildRcArgs(); | ||
} | ||
|
||
let result = rcArgsCache['*'] || []; | ||
|
||
if (command !== '*') { | ||
result = result.concat(rcArgsCache[command] || []); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function clearRcCache() { | ||
rcConfCache = null; | ||
rcArgsCache = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters