Skip to content

Commit 7a2b051

Browse files
author
Maël Nison
committed
Adds a preferred-cache-folder option
1 parent fc761e9 commit 7a2b051

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

src/cli/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export function main({
6060
'--modules-folder <path>',
6161
'rather than installing modules into the node_modules folder relative to the cwd, output them here',
6262
);
63+
commander.option('--preferred-cache-folder <path>', 'specify a custom folder to store the yarn cache if possible');
6364
commander.option('--cache-folder <path>', 'specify a custom folder to store the yarn cache');
6465
commander.option('--mutex <type>[:specifier]', 'use a mutex to ensure only one yarn instance is executing');
6566
commander.option('--emoji [bool]', 'enable emoji in output', process.platform === 'darwin');
@@ -324,6 +325,7 @@ export function main({
324325
binLinks: commander.binLinks,
325326
modulesFolder: commander.modulesFolder,
326327
globalFolder: commander.globalFolder,
328+
preferredCacheFolder: commander.preferredCacheFolder,
327329
cacheFolder: commander.cacheFolder,
328330
preferOffline: commander.preferOffline,
329331
captureHar: commander.har,

src/config.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,29 @@ export default class Config {
279279

280280
let cacheRootFolder = opts.cacheFolder || this.getOption('cache-folder', true);
281281

282-
for (let t = 0; t < constants.PREFERRED_MODULE_CACHE_DIRECTORIES.length && !cacheRootFolder; ++t) {
283-
const tentativeCacheFolder = constants.PREFERRED_MODULE_CACHE_DIRECTORIES[t];
284-
285-
try {
286-
await fs.mkdirp(tentativeCacheFolder);
287-
await fs.writeFile(path.join(tentativeCacheFolder, constants.WTEST_FILENAME), `testing write access`);
288-
cacheRootFolder = tentativeCacheFolder;
289-
} catch (error) {
290-
this.reporter.warn(this.reporter.lang('cacheFolderSkipped', tentativeCacheFolder));
282+
if (!cacheRootFolder) {
283+
let preferredCacheFolders = constants.PREFERRED_MODULE_CACHE_DIRECTORIES;
284+
const preferredCacheFolder = opts.preferredCacheFolder || this.getOption('preferred-cache-folder', true);
285+
286+
if (preferredCacheFolder) {
287+
preferredCacheFolders = [preferredCacheFolder].concat(preferredCacheFolders);
288+
}
289+
290+
for (let t = 0; t < preferredCacheFolders.length && !cacheRootFolder; ++t) {
291+
const tentativeCacheFolder = preferredCacheFolders[t];
292+
293+
try {
294+
await fs.mkdirp(tentativeCacheFolder);
295+
// eslint-disable-next-line
296+
await fs.access(tentativeCacheFolder, fs.constants.R_OK | fs.constants.W_OK | fs.constants.X_OK);
297+
cacheRootFolder = tentativeCacheFolder;
298+
} catch (error) {
299+
this.reporter.warn(this.reporter.lang('cacheFolderSkipped', tentativeCacheFolder));
300+
}
301+
302+
if (cacheRootFolder && t > 0) {
303+
this.reporter.warn(this.reporter.lang('cacheFolderSelected', cacheRootFolder));
304+
}
291305
}
292306
}
293307

src/constants.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow */
22

3+
const os = require('os');
34
const path = require('path');
45
const userHome = require('./util/user-home-dir').default;
56

@@ -47,14 +48,17 @@ function getDirectory(category: string): string {
4748
}
4849

4950
function getPreferredCacheDirectories(): Array<string> {
50-
const preferredCacheDirectories = [getDirectory('cache')];
51+
const preferredCacheDirectories = [];
5152

52-
if (process.platform !== 'win32') {
53-
preferredCacheDirectories.unshift('/tmp/.yarn-cache');
53+
if (process.platform === 'darwin') {
54+
preferredCacheDirectories.push(path.join(userHome, 'Library', 'Caches', 'Yarn'));
5455
}
5556

56-
if (process.platform === 'darwin') {
57-
preferredCacheDirectories.unshift(path.join(userHome, 'Library', 'Caches', 'Yarn'));
57+
preferredCacheDirectories.push(getDirectory('cache'));
58+
59+
if (process.platform !== 'win32') {
60+
preferredCacheDirectories.push(path.join(os.tmpdir(), '.yarn-cache'));
61+
preferredCacheDirectories.push('/tmp/.yarn-cache');
5862
}
5963

6064
return preferredCacheDirectories;
@@ -74,7 +78,7 @@ export const LOCKFILE_FILENAME = 'yarn.lock';
7478
export const METADATA_FILENAME = '.yarn-metadata.json';
7579
export const TARBALL_FILENAME = '.yarn-tarball.tgz';
7680
export const CLEAN_FILENAME = '.yarnclean';
77-
export const WTEST_FILENAME = 'yarn-wtest';
81+
export const ACCESS_FILENAME = '.yarn-access';
7882

7983
export const DEFAULT_INDENT = ' ';
8084
export const SINGLE_INSTANCE_PORT = 31997;

src/reporters/lang/en.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ const messages = {
177177
cacheFolderSkipped: 'Skipping preferred cache folder $0 because it is not writable.',
178178
cacheFolderMissing:
179179
"Yarn hasn't been able to find a cache folder. Please use an explicit --cache-folder option to tell it what location to use, or make one of the preferred locations writable.",
180+
cacheFolderSelected: 'Selected the next writable cache folder in the list, will be $0.',
180181

181182
execMissingCommand: 'Missing command name.',
182183

src/util/fs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const globModule = require('glob');
1313
const os = require('os');
1414
const path = require('path');
1515

16+
export const constants = fs.constants;
17+
1618
export const lockQueue = new BlockingQueue('fs lock');
1719

1820
export const readFileBuffer = promisify(fs.readFile);

0 commit comments

Comments
 (0)