Skip to content

Commit

Permalink
Adds a preferred-cache-folder option
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison committed Aug 11, 2017
1 parent fc761e9 commit 7a2b051
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
2 changes: 2 additions & 0 deletions src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function main({
'--modules-folder <path>',
'rather than installing modules into the node_modules folder relative to the cwd, output them here',
);
commander.option('--preferred-cache-folder <path>', 'specify a custom folder to store the yarn cache if possible');
commander.option('--cache-folder <path>', 'specify a custom folder to store the yarn cache');
commander.option('--mutex <type>[:specifier]', 'use a mutex to ensure only one yarn instance is executing');
commander.option('--emoji [bool]', 'enable emoji in output', process.platform === 'darwin');
Expand Down Expand Up @@ -324,6 +325,7 @@ export function main({
binLinks: commander.binLinks,
modulesFolder: commander.modulesFolder,
globalFolder: commander.globalFolder,
preferredCacheFolder: commander.preferredCacheFolder,
cacheFolder: commander.cacheFolder,
preferOffline: commander.preferOffline,
captureHar: commander.har,
Expand Down
32 changes: 23 additions & 9 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,29 @@ export default class Config {

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

for (let t = 0; t < constants.PREFERRED_MODULE_CACHE_DIRECTORIES.length && !cacheRootFolder; ++t) {
const tentativeCacheFolder = constants.PREFERRED_MODULE_CACHE_DIRECTORIES[t];

try {
await fs.mkdirp(tentativeCacheFolder);
await fs.writeFile(path.join(tentativeCacheFolder, constants.WTEST_FILENAME), `testing write access`);
cacheRootFolder = tentativeCacheFolder;
} catch (error) {
this.reporter.warn(this.reporter.lang('cacheFolderSkipped', tentativeCacheFolder));
if (!cacheRootFolder) {
let preferredCacheFolders = constants.PREFERRED_MODULE_CACHE_DIRECTORIES;
const preferredCacheFolder = opts.preferredCacheFolder || this.getOption('preferred-cache-folder', true);

if (preferredCacheFolder) {
preferredCacheFolders = [preferredCacheFolder].concat(preferredCacheFolders);
}

for (let t = 0; t < preferredCacheFolders.length && !cacheRootFolder; ++t) {
const tentativeCacheFolder = preferredCacheFolders[t];

try {
await fs.mkdirp(tentativeCacheFolder);
// eslint-disable-next-line
await fs.access(tentativeCacheFolder, fs.constants.R_OK | fs.constants.W_OK | fs.constants.X_OK);
cacheRootFolder = tentativeCacheFolder;
} catch (error) {
this.reporter.warn(this.reporter.lang('cacheFolderSkipped', tentativeCacheFolder));
}

if (cacheRootFolder && t > 0) {
this.reporter.warn(this.reporter.lang('cacheFolderSelected', cacheRootFolder));
}
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* @flow */

const os = require('os');
const path = require('path');
const userHome = require('./util/user-home-dir').default;

Expand Down Expand Up @@ -47,14 +48,17 @@ function getDirectory(category: string): string {
}

function getPreferredCacheDirectories(): Array<string> {
const preferredCacheDirectories = [getDirectory('cache')];
const preferredCacheDirectories = [];

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

if (process.platform === 'darwin') {
preferredCacheDirectories.unshift(path.join(userHome, 'Library', 'Caches', 'Yarn'));
preferredCacheDirectories.push(getDirectory('cache'));

if (process.platform !== 'win32') {
preferredCacheDirectories.push(path.join(os.tmpdir(), '.yarn-cache'));
preferredCacheDirectories.push('/tmp/.yarn-cache');
}

return preferredCacheDirectories;
Expand All @@ -74,7 +78,7 @@ export const LOCKFILE_FILENAME = 'yarn.lock';
export const METADATA_FILENAME = '.yarn-metadata.json';
export const TARBALL_FILENAME = '.yarn-tarball.tgz';
export const CLEAN_FILENAME = '.yarnclean';
export const WTEST_FILENAME = 'yarn-wtest';
export const ACCESS_FILENAME = '.yarn-access';

export const DEFAULT_INDENT = ' ';
export const SINGLE_INSTANCE_PORT = 31997;
Expand Down
1 change: 1 addition & 0 deletions src/reporters/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const messages = {
cacheFolderSkipped: 'Skipping preferred cache folder $0 because it is not writable.',
cacheFolderMissing:
"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.",
cacheFolderSelected: 'Selected the next writable cache folder in the list, will be $0.',

execMissingCommand: 'Missing command name.',

Expand Down
2 changes: 2 additions & 0 deletions src/util/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const globModule = require('glob');
const os = require('os');
const path = require('path');

export const constants = fs.constants;

export const lockQueue = new BlockingQueue('fs lock');

export const readFileBuffer = promisify(fs.readFile);
Expand Down

0 comments on commit 7a2b051

Please sign in to comment.