From 7fcf4dd4d95cdfa8856cea8a5daa29d48eb2d70b Mon Sep 17 00:00:00 2001 From: Priyantha Lankapura <403912+lankaapura@users.noreply.github.com> Date: Mon, 5 Feb 2018 00:11:21 +0530 Subject: [PATCH] 'Cache clean' with multiple packages at once (#5297) * clean multiple modules from cache. fixes https://github.com/yarnpkg/yarn/issues/5273 * add a test for 'cache clean' with multiple package names --- __tests__/commands/cache.js | 14 ++++++++++++++ src/cli/commands/cache.js | 24 +++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/__tests__/commands/cache.js b/__tests__/commands/cache.js index 7e0bf9598e..937641b65a 100644 --- a/__tests__/commands/cache.js +++ b/__tests__/commands/cache.js @@ -147,3 +147,17 @@ test('clean with package name', async (): Promise => { expect(files.length).toEqual(1); // Only .tmp folder left }); }); + +test('clean with multiple package names', async (): Promise => { + await runInstall({}, 'install-production', async (config): Promise => { + let files = await fs.readdir(config.cacheFolder); + expect(files.length).toEqual(3); + + const reporter = new BufferReporter(); + + await run(config, reporter, {}, ['clean', 'is-array', 'left-pad']); + expect(await fs.exists(config.cacheFolder)).toBeTruthy(); + files = await fs.readdir(config.cacheFolder); + expect(files.length).toEqual(1); // Only .tmp folder left + }); +}); diff --git a/src/cli/commands/cache.js b/src/cli/commands/cache.js index 547fd9e726..f20bb911b9 100644 --- a/src/cli/commands/cache.js +++ b/src/cli/commands/cache.js @@ -91,20 +91,22 @@ const {run, setFlags: _setFlags, examples} = buildSubCommands('cache', { const activity = reporter.activity(); if (args.length > 0) { - // Clear named package from cache - const folders = await getPackageCachefolders(args[0]); + for (const arg of args) { + // Clear named package from cache + const folders = await getPackageCachefolders(arg); + + if (folders.length === 0) { + activity.end(); + reporter.warn(reporter.lang('couldntClearPackageFromCache', arg)); + continue; + } - if (folders.length === 0) { + for (const folder of folders) { + await fs.unlink(folder); + } activity.end(); - reporter.warn(reporter.lang('couldntClearPackageFromCache', args[0])); - return; + reporter.success(reporter.lang('clearedPackageFromCache', arg)); } - - for (const folder of folders) { - await fs.unlink(folder); - } - activity.end(); - reporter.success(reporter.lang('clearedPackageFromCache', args[0])); } else { // Clear all cache await fs.unlink(config._cacheRootFolder);