Skip to content

Commit

Permalink
Feature: Add ability to filter yarn cache (fixes #3200) (#4571)
Browse files Browse the repository at this point in the history
  • Loading branch information
philquinn authored and arcanis committed Oct 3, 2017
1 parent 4fa9f71 commit 1276e3f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
66 changes: 66 additions & 0 deletions __tests__/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,72 @@ test('ls with scoped package', async (): Promise<void> => {
});
});

test('ls with filter that matches cache', async (): Promise<void> => {
await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise<void> => {
const out = new stream.PassThrough();
const reporter = new reporters.JSONReporter({stdout: out});
await run(config, reporter, {pattern: 'dummy'}, ['list']);
const stdout = String(out.read());
expect(stdout).toContain('dummy');
expect(stdout).toContain('0.0.0');
});
});

test('ls with filter that matches cache with wildcard', async (): Promise<void> => {
await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise<void> => {
const out = new stream.PassThrough();
const reporter = new reporters.JSONReporter({stdout: out});
await run(config, reporter, {pattern: 'dum*'}, ['list']);
const stdout = String(out.read());
expect(stdout).toContain('dummy');
expect(stdout).toContain('0.0.0');
});
});

test('ls with multiple patterns, one matching', async (): Promise<void> => {
await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise<void> => {
const out = new stream.PassThrough();
const reporter = new reporters.JSONReporter({stdout: out});
await run(config, reporter, {pattern: 'dum|dummy'}, ['list']);
const stdout = String(out.read());
expect(stdout).toContain('dummy');
expect(stdout).toContain('0.0.0');
});
});

test('ls with pattern that only partially matches', async (): Promise<void> => {
await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise<void> => {
const out = new stream.PassThrough();
const reporter = new reporters.JSONReporter({stdout: out});
await run(config, reporter, {pattern: 'dum'}, ['list']);
const stdout = String(out.read());
expect(stdout).toContain('dummy');
expect(stdout).toContain('0.0.0');
});
});

test('ls with filter that does not match', async (): Promise<void> => {
await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise<void> => {
const out = new stream.PassThrough();
const reporter = new reporters.JSONReporter({stdout: out});
await run(config, reporter, {pattern: 'noMatch'}, ['list']);
const stdout = String(out.read());
expect(stdout).not.toContain('dummy');
expect(stdout).not.toContain('0.0.0');
});
});

test('ls filter by pattern with scoped package', async (): Promise<void> => {
await runInstall({}, 'install-from-authed-private-registry', async (config): Promise<void> => {
const out = new stream.PassThrough();
const reporter = new reporters.JSONReporter({stdout: out});
await run(config, reporter, {pattern: '@types/*'}, ['list']);
const stdout = String(out.read());
expect(stdout).toContain('@types/lodash');
expect(stdout).toContain('4.14.37');
});
});

test('dir', async (): Promise<void> => {
await runCache(['dir'], {}, '', (config, reporter, stdout) => {
expect(stdout).toContain(JSON.stringify(config.cacheFolder));
Expand Down
13 changes: 12 additions & 1 deletion src/cli/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as fs from '../../util/fs.js';
import {METADATA_FILENAME} from '../../constants';

const path = require('path');
const micromatch = require('micromatch');

export function hasWrapper(flags: Object, args: Array<string>): boolean {
return args[0] !== 'dir';
Expand All @@ -32,6 +33,9 @@ async function list(config: Config, reporter: Reporter, flags: Object, args: Arr
packagesMetadata.push(...(await readCacheMetadata(loc)));
} else {
const {registry, package: manifest, remote} = await config.readPackageMetadata(loc);
if (flags.pattern && !micromatch.contains(manifest.name, flags.pattern)) {
continue;
}
packagesMetadata.push([manifest.name, manifest.version, registry, (remote && remote.resolved) || '']);
}
}
Expand All @@ -44,7 +48,7 @@ async function list(config: Config, reporter: Reporter, flags: Object, args: Arr
reporter.table(['Name', 'Version', 'Registry', 'Resolved'], body);
}

export const {run, setFlags, examples} = buildSubCommands('cache', {
const {run, setFlags: _setFlags, examples} = buildSubCommands('cache', {
async ls(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<void> {
reporter.warn(`\`yarn cache ls\` is deprecated. Please use \`yarn cache list\`.`);
await list(config, reporter, flags, args);
Expand Down Expand Up @@ -116,3 +120,10 @@ export const {run, setFlags, examples} = buildSubCommands('cache', {
}
},
});

export {run, examples};

export function setFlags(commander: Object) {
_setFlags(commander);
commander.option('--pattern [pattern]', 'filter cached packages by pattern');
}

0 comments on commit 1276e3f

Please sign in to comment.