Skip to content
Permalink
Browse files

RFC: Make conventional directories for config and data the default

Following from yarnpkg#5336, this makes the typical directories for XDG and
Windows conventions the default.

This RFC is really carried over from yarnpkg#5336, where we continued to use
the existing locations inside ~/.config and punted on migrating users to
these new locations.

A suggestion from the prior PR include moving the contents of
the existing directories (should they exist), leaving a symlink in
place.

I really like this approach, however there will be a cost to checking to
see if these directories exist and leaving symlinks behind in a
synchronous way to maintain compatibility with how we expose constants
synchronously from a module.

Please chime in!
  • Loading branch information...
Will Binns-Smith authored and wbinnssmith committed Feb 9, 2018
1 parent 19ec98c commit 683b08c38caa34edddb7c983b8cdacbc6dac4072
Showing with 15 additions and 24 deletions.
  1. +6 −6 __tests__/util/user-dirs.js
  2. +9 −18 src/util/user-dirs.js
@@ -17,8 +17,8 @@ describe('getDataDir', () => {
delete process.env.LOCALAPPDATA;
});

test('uses the config dir otherwise', () => {
expect(getDataDir()).toBe(path.join(userHome, '.config', 'yarn'));
test('uses AppData\\Local\\Data otherwise', () => {
expect(getDataDir()).toBe(path.join(userHome, 'AppData', 'Local', 'Yarn', 'Data'));
});
});

@@ -33,8 +33,8 @@ describe('getDataDir', () => {
delete process.env.XDG_DATA_HOME;
});

test('falls back to the config dir', () => {
expect(getDataDir()).toBe(path.join(userHome, '.config', 'yarn'));
test('falls back to .local/share/yarn', () => {
expect(getDataDir()).toBe(path.join(userHome, '.local', 'share', 'yarn'));
});
});
});
@@ -101,8 +101,8 @@ describe('getConfigDir', () => {
delete process.env.LOCALAPPDATA;
});

test('uses the config dir otherwise', () => {
expect(getConfigDir()).toBe(path.join(userHome, '.config', 'yarn'));
test('uses AppData\\Local\\Config otherwise', () => {
expect(getConfigDir()).toBe(path.join(userHome, 'AppData', 'Local', 'Yarn', 'Config'));
});
});

@@ -3,52 +3,43 @@
const path = require('path');
const userHome = require('./user-home-dir').default;

const FALLBACK_CONFIG_DIR = path.join(userHome, '.config', 'yarn');
const FALLBACK_CACHE_DIR = path.join(userHome, '.cache', 'yarn');

export function getDataDir(): string {
if (process.platform === 'win32') {
const WIN32_APPDATA_DIR = getLocalAppDataDir();
return WIN32_APPDATA_DIR == null ? FALLBACK_CONFIG_DIR : path.join(WIN32_APPDATA_DIR, 'Data');
return path.join(getLocalAppDataDir(), 'Data');
} else if (process.env.XDG_DATA_HOME) {
return path.join(process.env.XDG_DATA_HOME, 'yarn');
} else {
// This could arguably be ~/Library/Application Support/Yarn on Macs,
// but that feels unintuitive for a cli tool

// Instead, use our prior fallback. Some day this could be
// path.join(userHome, '.local', 'share', 'yarn')
// or return path.join(WIN32_APPDATA_DIR, 'Data') on win32
return FALLBACK_CONFIG_DIR;
// Instead, always use the typical location of XDG_DATA_HOME
return path.join(userHome, '.local', 'share', 'yarn');
}
}

export function getCacheDir(): string {
if (process.platform === 'win32') {
// process.env.TEMP also exists, but most apps put caches here
return path.join(getLocalAppDataDir() || path.join(userHome, 'AppData', 'Local', 'Yarn'), 'Cache');
return path.join(getLocalAppDataDir(), 'Cache');
} else if (process.env.XDG_CACHE_HOME) {
return path.join(process.env.XDG_CACHE_HOME, 'yarn');
} else if (process.platform === 'darwin') {
return path.join(userHome, 'Library', 'Caches', 'Yarn');
} else {
return FALLBACK_CACHE_DIR;
return path.join(userHome, '.cache', 'yarn');
}
}

export function getConfigDir(): string {
if (process.platform === 'win32') {
// Use our prior fallback. Some day this could be
// return path.join(WIN32_APPDATA_DIR, 'Config')
const WIN32_APPDATA_DIR = getLocalAppDataDir();
return WIN32_APPDATA_DIR == null ? FALLBACK_CONFIG_DIR : path.join(WIN32_APPDATA_DIR, 'Config');
return path.join(getLocalAppDataDir(), 'Config');
} else if (process.env.XDG_CONFIG_HOME) {
return path.join(process.env.XDG_CONFIG_HOME, 'yarn');
} else {
return FALLBACK_CONFIG_DIR;
return path.join(userHome, '.config', 'yarn');
}
}

function getLocalAppDataDir(): ?string {
return process.env.LOCALAPPDATA ? path.join(process.env.LOCALAPPDATA, 'Yarn') : null;
function getLocalAppDataDir(): string {
return path.join(process.env.LOCALAPPDATA || path.join(userHome, 'AppData', 'Local'), 'Yarn');
}

0 comments on commit 683b08c

Please sign in to comment.
You can’t perform that action at this time.