Skip to content

Commit

Permalink
Modify user-home-dir to function to inject user-home
Browse files Browse the repository at this point in the history
  • Loading branch information
TimvdLippe committed Feb 28, 2017
1 parent 599e4df commit 550a74f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
7 changes: 4 additions & 3 deletions __tests__/commands/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -101,8 +102,8 @@ export async function run<T, R>(

// 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'));
Expand Down Expand Up @@ -134,6 +135,6 @@ export async function run<T, R>(
} catch (err) {
throw new Error(`${err && err.stack} \nConsole output:\n ${out}`);
} finally {
process.env.YARN_HOME_FOLDER = homeFolderLocation;
setUserHomeDir(homeFolderLocation);
}
}
7 changes: 4 additions & 3 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions src/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -97,7 +97,7 @@ export default class NpmRegistry extends Registry {
async getPossibleConfigLocations(filename: string): Promise<Array<[boolean, string, string]>> {
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)],
];

Expand Down
4 changes: 2 additions & 2 deletions src/registries/yarn-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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 = {};
}

Expand Down
14 changes: 10 additions & 4 deletions src/util/user-home-dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 550a74f

Please sign in to comment.