Skip to content

Commit

Permalink
Adds tests, change syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison committed Apr 5, 2017
1 parent 697e58e commit f663d36
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--cache-folder foobar/hello/world
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions __tests__/fixtures/lifecycle-scripts/yarnrc-cli/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--cache-folder /foobar
2 changes: 2 additions & 0 deletions __tests__/fixtures/lifecycle-scripts/yarnrc-cli/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
10 changes: 10 additions & 0 deletions __tests__/lifecycle-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ async function execCommand(cmd: string, packageName: string, env = process.env):
});
}

test('should add the yarnrc values to the command line', async () => {
let stdout = await execCommand('cache dir', 'yarnrc-cli');
expect(stdout).toMatch(/^\/foobar\/v[0-9]+$/);
});

test('should resolve the yarnrc values relative to where the file lives', async () => {
let stdout = await execCommand('cache dir', 'yarnrc-cli');
expect(stdout).toMatch(/^(\/[^\/]+)+\/foobar\/hello\/world\/v[0-9]+$/);
});

test('should expose `npm_config_argv` environment variable to lifecycle scripts for back compatibility with npm (#684)',
async () => {
const env = Object.assign({}, process.env);
Expand Down
5 changes: 2 additions & 3 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,13 @@ if (args.indexOf('--help') >= 0 || args.indexOf('-h') >= 0) {
process.exit(1);
}

// parse flags
args.unshift(commandName);
args = [commandName].concat(getRcArgs(commandName), args);

if (ARGS_THAT_SHARE_NAMES_WITH_OPTIONS.indexOf(commandName) >= 0 && args[0] === commandName) {
args.shift();
}

commander.parse(getRcArgs().concat(startArgs).concat(args));
commander.parse(startArgs.concat(args));
commander.args = commander.args.concat(endArgs);

if (command) {
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export default class Config {
networkConcurrency: this.networkConcurrency,
});
this._cacheRootFolder = String(
opts.cacheFolder ||
opts.cacheRootFolder ||
this.getOption('cache-folder') ||
constants.MODULE_CACHE_DIRECTORY,
);
Expand Down
2 changes: 1 addition & 1 deletion src/lockfile/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function* tokenise(input: string): Iterator<Token> {
} else if (input[0] === ',') {
yield buildToken(TOKEN_TYPES.comma);
chop++;
} else if (/^[a-zA-Z]/g.test(input)) {
} else if (/^[a-zA-Z\/-]/g.test(input)) {
let name = "";
for (let i = 0; i < input.length; i++) {
const char = input[i];
Expand Down
94 changes: 31 additions & 63 deletions src/rc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,22 @@ import rc from 'rc';

import parse from './lockfile/parse.js';

// Keys that will be propagated into the command line as arguments
const ARG_KEYS = [
'production',

'offline',
'prefer-offline',
'proxy',
'https-proxy',
'network-concurrency',
'har',

'strict-semver',
'ignore-engines',
'ignore-optional',
'ignore-platform',
'ignore-scripts',
'skip-integrity-check',

'no-lockfile',
'pure-lockfile',
'frozen-lockfile',

'no-bin-links',
'link-duplicates',
'flat',

'cache-folder',
'modules-folder',
'global-folder',

'mutex',

'cli.no-emoji',
'cli.verbose',

'unsafe.force',
];

// 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',
];

// Map old keys to more recent ones
const ALIAS_KEYS = {
//'yarn-offline-mirror': 'mirror-path'
//'skip-integrity-check': 'ignore-integrity'
};

const buildRcConf = () => rc('yarn', {}, [], fileText => {
const values = parse(fileText, null);
const keys = Object.keys(values);

for (let key of Object.keys(ALIAS_KEYS)) {
if (Object.prototype.hasOwnProperty.call(values, key)) {
values[ALIAS_KEYS[key]] = values[key];
}
}

for (let key of PATH_KEYS) {
if (Object.prototype.hasOwnProperty.call(values, key)) {
values[key] = resolve(dirname(filePath), values[key]);
for (let key of keys) {
for (let pathKey of PATH_KEYS) {
if (key === pathKey || key.endsWith(`.${pathKey}`)) {
values[key] = resolve(dirname(filePath), values[key]);
}
}
}

Expand All @@ -79,26 +32,41 @@ export function getRcConf() {
return getRcConf.cache;
}

const buildRcArgs = () => ARG_KEYS.reduce((args, key) => {
const buildRcArgs = () => Object.keys(getRcConf()).reduce((argLists, key) => {
if (!key.startsWith(`--`))
return argLists;

const [, namespace = `*`, arg] = key.match(/^--(?:([^.]+)\.)?(.*)$/);
const value = getRcConf()[key];

// some arg keys (such as unsafe.force) are namespaced in the yarnrc
const arg = key.replace(/^[^.]\./, ``);
if (!argLists[namespace]) {
argLists[namespace] = [];
}

if (typeof value === 'string') {
args = args.concat([ `--${arg}`, value ]);
} else if (typeof value === 'boolean') {
args = args.concat([ `--${arg}` ]);
argLists[namespace] = argLists[namespace].concat([`--${arg}`, value]);
} else {
argLists[namespace] = argLists[namespace].concat([`--${arg}`]);
}

return args;
}, []);
console.error(argLists);

export function getRcArgs() {
return argLists;
}, {});

export function getRcArgs(command) {
if (!getRcArgs.cache)
getRcArgs.cache = buildRcArgs();

return getRcArgs.cache;
let result = getRcArgs.cache;

if (typeof command !== 'undefined')
result = result['*'] || [];

if (command !== '*')
result = result.concat(result[command] || []);

return result;
}

export function clearRcCache() {
Expand Down

0 comments on commit f663d36

Please sign in to comment.