From 550a74fa7e50027fb57a607d54d25648b30059c3 Mon Sep 17 00:00:00 2001 From: Tim van der Lippe Date: Tue, 28 Feb 2017 22:17:59 +0100 Subject: [PATCH] Modify user-home-dir to function to inject user-home --- __tests__/commands/_helpers.js | 7 ++++--- src/constants.js | 7 ++++--- src/registries/npm-registry.js | 4 ++-- src/registries/yarn-registry.js | 4 ++-- src/util/user-home-dir.js | 14 ++++++++++---- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/__tests__/commands/_helpers.js b/__tests__/commands/_helpers.js index 5e369f6a47..29e6b02dfa 100644 --- a/__tests__/commands/_helpers.js +++ b/__tests__/commands/_helpers.js @@ -9,6 +9,7 @@ import {run as check} from '../../src/cli/commands/check.js'; import * as fs from '../../src/util/fs.js'; import {Install} from '../../src/cli/commands/install.js'; import Config from '../../src/config.js'; +import {userHomeDir, setUserHomeDir} from '../../src/util/user-home-dir'; const stream = require('stream'); const path = require('path'); @@ -101,8 +102,8 @@ export async function run( // remove the lockfile if we create one and it didn't exist before const lockfile = await createLockfile(cwd); - const homeFolderLocation = process.env.YARN_HOME_FOLDER; - process.env.YARN_HOME_FOLDER = path.join(cwd, '.yarn-home'); + const homeFolderLocation = userHomeDir(); + setUserHomeDir(path.join(cwd, '.yarn-home')); // create directories await fs.mkdirp(path.join(cwd, '.yarn-global')); @@ -134,6 +135,6 @@ export async function run( } catch (err) { throw new Error(`${err && err.stack} \nConsole output:\n ${out}`); } finally { - process.env.YARN_HOME_FOLDER = homeFolderLocation; + setUserHomeDir(homeFolderLocation); } } diff --git a/src/constants.js b/src/constants.js index 48d7854b35..2454ff556e 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,7 +1,8 @@ /* @flow */ +import {userHomeDir} from './util/user-home-dir'; + const path = require('path'); -const userHome = require('./util/user-home-dir').default; type Env = { [key: string]: ? string @@ -42,12 +43,12 @@ function getDirectory(category: string): string { } // otherwise use ~/.{category}/yarn - return path.join(userHome, `.${category}`, 'yarn'); + return path.join(userHomeDir(), `.${category}`, 'yarn'); } function getCacheDirectory(): string { if (process.platform === 'darwin') { - return path.join(userHome, 'Library', 'Caches', 'Yarn'); + return path.join(userHomeDir(), 'Library', 'Caches', 'Yarn'); } return getDirectory('cache'); diff --git a/src/registries/npm-registry.js b/src/registries/npm-registry.js index 176a469918..95c9c3c6c8 100644 --- a/src/registries/npm-registry.js +++ b/src/registries/npm-registry.js @@ -10,8 +10,8 @@ import envReplace from '../util/env-replace.js'; import Registry from './base-registry.js'; import {addSuffix, removePrefix} from '../util/misc'; import isRequestToRegistry from './is-request-to-registry.js'; +import {userHomeDir} from '../util/user-home-dir'; -const userHome = require('../util/user-home-dir').default; const path = require('path'); const url = require('url'); const ini = require('ini'); @@ -97,7 +97,7 @@ export default class NpmRegistry extends Registry { async getPossibleConfigLocations(filename: string): Promise> { const possibles = [ [false, path.join(this.cwd, filename)], - [true, this.config.userconfig || path.join(process.env.YARN_HOME_FOLDER || userHome, filename)], + [true, this.config.userconfig || path.join(userHomeDir(), filename)], [false, path.join(getGlobalPrefix(), filename)], ]; diff --git a/src/registries/yarn-registry.js b/src/registries/yarn-registry.js index d000c0ead6..7934a3c4eb 100644 --- a/src/registries/yarn-registry.js +++ b/src/registries/yarn-registry.js @@ -7,8 +7,8 @@ import NpmRegistry from './npm-registry.js'; import stringify from '../lockfile/stringify.js'; import parse from '../lockfile/parse.js'; import * as fs from '../util/fs.js'; +import {userHomeDir} from '../util/user-home-dir'; -const userHome = require('../util/user-home-dir').default; const path = require('path'); const pkg: { version: string } = require('../../package.json'); @@ -46,7 +46,7 @@ export default class YarnRegistry extends NpmRegistry { constructor(cwd: string, registries: ConfigRegistries, requestManager: RequestManager) { super(cwd, registries, requestManager); - this.homeConfigLoc = path.join(userHome, '.yarnrc'); + this.homeConfigLoc = path.join(userHomeDir(), '.yarnrc'); this.homeConfig = {}; } diff --git a/src/util/user-home-dir.js b/src/util/user-home-dir.js index 8ee2490ce6..e10333b669 100644 --- a/src/util/user-home-dir.js +++ b/src/util/user-home-dir.js @@ -2,8 +2,14 @@ const path = require('path'); const {ROOT_USER} = require('../constants'); -const userHomeDir = (process.platform === 'linux' && ROOT_USER) ? - path.resolve('/usr/local/share') : - require('os').homedir(); +let userHome = (process.platform === 'linux' && ROOT_USER) ? +path.resolve('/usr/local/share') : +require('os').homedir(); -export default userHomeDir; +export function userHomeDir(): string { + return userHome; +} + +export function setUserHomeDir(injectedUserHome: string) { + userHome = injectedUserHome; +}