Skip to content

Commit

Permalink
Parses strategy correctly (+ tests).
Browse files Browse the repository at this point in the history
Fixes #22
  • Loading branch information
Sergio Cinos committed Dec 11, 2018
1 parent f3118ff commit da7783e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 8 additions & 4 deletions __fixtures__/yarn.lock
Expand Up @@ -2,10 +2,14 @@
# yarn lockfile v1


lodash@>=1.0.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-1.3.1.tgz#a4663b53686b895ff074e2ba504dfb76a8e2b770"
library@>=1.0.0, library@>=1.1.0:
version "3.0.0"
resolved "https://example.net/library@^3.0.0"

lodash@>=2.0.0:
library@^2.0.0:
version "2.1.0"
resolved "https://example.net/library@^2.1.0"

lodash@>=1.0.0, lodash@>=2.0.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
14 changes: 14 additions & 0 deletions __tests__/cli.js
Expand Up @@ -92,3 +92,17 @@ test('line endings are retained', async () => {
await writeFile(yarnLockFilePath, oldFileContent, 'utf8');
}
});

test.only('uses fewer strategy', async () => {
const { stdout, stderr } = await execFile(process.execPath, [
cliFilePath,
'--print',
'-s',
'fewer',
yarnLockFilePath,
]);
expect(stdout).not.toContain('library@>=1.0.0:');
expect(stdout).toContain('library@>=1.0.0, library@>=1.1.0, library@^2.0.0:');
expect(stdout).toContain('resolved "https://example.net/library@^2.1.0"');
expect(stderr).toBe('');
});
10 changes: 7 additions & 3 deletions cli.js
Expand Up @@ -12,7 +12,6 @@ commander
.option(
'-s, --strategy <strategy>',
'deduplication strategy. Valid values: fewer, highest. Default is "highest"',
/^(fewer|highest)$/i,
'highest'
)
.option('-l, --list', 'do not change yarn.lock, just output the diagnosis')
Expand All @@ -24,20 +23,25 @@ commander
.option('--print', 'instead of saving the deduplicated yarn.lock, print the result in stdout');
commander.parse(process.argv);
if (!commander.args.length) commander.help();
if (commander.strategy !== 'highest' && commander.strategy !== 'fewer') {
console.error(`Invalid strategy ${commander.strategy}`);
commander.help();
}

const file = commander.args[0];

try {
const yarnLock = fs.readFileSync(file, 'utf8');
const useMostCommon = commander.strategy === 'fewer';

if (commander.list) {
listDuplicates(yarnLock, {
useMostCommon: commander.mostCommon,
useMostCommon,
includePackages: commander.packages,
}).forEach(logLine => console.log(logLine));
} else {
let dedupedYarnLock = fixDuplicates(yarnLock, {
useMostCommon: commander.mostCommon,
useMostCommon,
includePackages: commander.packages,
});

Expand Down

0 comments on commit da7783e

Please sign in to comment.