Skip to content

Commit

Permalink
add cache-folder config option for setting cache folder location (#2046)
Browse files Browse the repository at this point in the history
* add cache-folder config option for setting cache folder

* unit test for cache-folder config

* test the priorities of different cache folder setting methods

* run the cache config tests in sequence
  • Loading branch information
superxp1412 authored and bestander committed Dec 4, 2016
1 parent 68df0ea commit 8437487
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion __tests__/commands/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function run<T, R>(
binLinks: !!flags.binLinks,
cwd,
globalFolder: path.join(cwd, '.yarn-global'),
cacheFolder: path.join(cwd, '.yarn-cache'),
cacheFolder: flags.cacheFolder || path.join(cwd, '.yarn-cache'),
linkFolder: path.join(cwd, '.yarn-link'),
production: flags.production,
});
Expand Down
32 changes: 32 additions & 0 deletions __tests__/commands/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* @flow */

import type {CLIFunctionReturn} from '../../src/types.js';
import * as reporters from '../../src/reporters/index.js';
import * as configCmd from '../../src/cli/commands/config.js';
import {run as buildRun} from './_helpers.js';
import * as fs from '../../src/util/fs.js';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

const runConfig = buildRun.bind(
null,
reporters.ConsoleReporter,
'',
(args, flags, config, reporter): CLIFunctionReturn => {
return configCmd.run(config, reporter, flags, args);
},
);

test('write cache-folder config into .yarnrc file', (): Promise<void> => {
return runConfig(['set', 'cache-folder', 'folder_dir_for_test'], {}, '', async (config) => {
const configFile = await fs.readFile(config.registries.yarn.homeConfigLoc);
expect(configFile).toContain('folder_dir_for_test');
});
});

test('cache-folder flag has higher priorities than .yarnrc file', (): Promise<void> => {
return runConfig(['set', 'cache-folder', 'set_config_folder_dir'], {'cacheFolder': 'flag_config_folder_dir'},
'', (config) => {
expect(config.cacheFolder).toContain('flag_config_folder_dir');
});
});
11 changes: 6 additions & 5 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ export default class Config {
this._init(opts);

await fs.mkdirp(this.globalFolder);
await fs.mkdirp(this.cacheFolder);
await fs.mkdirp(this.tempFolder);

await fs.mkdirp(this.linkFolder);
this.linkedModules = await fs.readdir(this.linkFolder);

Expand All @@ -204,6 +201,12 @@ export default class Config {
key: String(opts.key || this.getOption('key') || ''),
});

//init & create cacheFolder, tempFolder
this.cacheFolder = String(opts.cacheFolder || this.getOption('cache-folder') || constants.MODULE_CACHE_DIRECTORY);
this.tempFolder = opts.tempFolder || path.join(this.cacheFolder, '.tmp');
await fs.mkdirp(this.cacheFolder);
await fs.mkdirp(this.tempFolder);

if (this.getOption('production') || process.env.NODE_ENV === 'production') {
this.production = true;
}
Expand All @@ -225,9 +228,7 @@ export default class Config {
this.preferOffline = !!opts.preferOffline;
this.modulesFolder = opts.modulesFolder;
this.globalFolder = opts.globalFolder || constants.GLOBAL_MODULE_DIRECTORY;
this.cacheFolder = opts.cacheFolder || constants.MODULE_CACHE_DIRECTORY;
this.linkFolder = opts.linkFolder || constants.LINK_REGISTRY_DIRECTORY;
this.tempFolder = opts.tempFolder || path.join(this.cacheFolder, '.tmp');
this.production = !!opts.production;
this.offline = !!opts.offline;
this.binLinks = !!opts.binLinks;
Expand Down

0 comments on commit 8437487

Please sign in to comment.