From 41fe065ca55cc11a286a00469a966c71fd5acced Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 3 Nov 2016 16:31:53 +0000 Subject: [PATCH] Add option to change the prefix of the global bin folder - fixes #630 (#1654) --- src/cli/commands/global.js | 33 ++++++++++++++++++++++----------- src/config.js | 2 +- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/cli/commands/global.js b/src/cli/commands/global.js index dffafe8600..6f9a08a196 100644 --- a/src/cli/commands/global.js +++ b/src/cli/commands/global.js @@ -58,8 +58,12 @@ async function getBins(config: Config): Promise> { return paths; } -function getGlobalPrefix(): string { - if (process.env.PREFIX) { +function getGlobalPrefix(config: Config, flags: Object): string { + if (flags.prefix) { + return flags.prefix; + } else if (config.getOption('prefix')) { + return config.getOption('prefix'); + } else if (process.env.PREFIX) { return process.env.PREFIX; } else if (process.platform === 'win32') { // c:\node\node.exe --> prefix=c:\node\ @@ -77,8 +81,8 @@ function getGlobalPrefix(): string { } } -function getBinFolder(): string { - const prefix = getGlobalPrefix(); +function getBinFolder(config: Config, flags: Object): string { + const prefix = getGlobalPrefix(config, flags); if (process.platform === 'win32') { return prefix; } else { @@ -86,9 +90,9 @@ function getBinFolder(): string { } } -async function initUpdateBins(config: Config, reporter: Reporter): Promise<() => Promise> { +async function initUpdateBins(config: Config, reporter: Reporter, flags: Object): Promise<() => Promise> { const beforeBins = await getBins(config); - const binFolder = getBinFolder(); + const binFolder = getBinFolder(config, flags); function throwPermError(err: Error & { [code: string]: string }, dest: string) { if (err.code === 'EACCES') { @@ -155,7 +159,7 @@ export function hasWrapper(flags: Object, args: Array): boolean { return args[0] !== 'bin'; } -export const {run, setFlags} = buildSubCommands('global', { +const {run, setFlags: _setFlags} = buildSubCommands('global', { async add( config: Config, reporter: Reporter, @@ -164,7 +168,7 @@ export const {run, setFlags} = buildSubCommands('global', { ): Promise { await updateCwd(config); - const updateBins = await initUpdateBins(config, reporter); + const updateBins = await initUpdateBins(config, reporter, flags); // install module const lockfile = await Lockfile.fromDirectory(config.cwd); @@ -212,7 +216,7 @@ export const {run, setFlags} = buildSubCommands('global', { ): Promise { await updateCwd(config); - const updateBins = await initUpdateBins(config, reporter); + const updateBins = await initUpdateBins(config, reporter, flags); // remove module await runRemove(config, reporter, flags, args); @@ -229,8 +233,8 @@ export const {run, setFlags} = buildSubCommands('global', { ): Promise { await updateCwd(config); - const updateBins = await initUpdateBins(config, reporter); - + const updateBins = await initUpdateBins(config, reporter, flags); + // upgrade module await runUpgrade(config, reporter, flags, args); @@ -238,3 +242,10 @@ export const {run, setFlags} = buildSubCommands('global', { await updateBins(); }, }); + +export {run}; + +export function setFlags(commander: Object) { + _setFlags(commander); + commander.option('--prefix ', 'bin prefix to use to install binaries'); +} diff --git a/src/config.js b/src/config.js index f1819e6e66..c9126829d3 100644 --- a/src/config.js +++ b/src/config.js @@ -243,7 +243,7 @@ export default class Config { generateHardModulePath(pkg: ?PackageReference, ignoreLocation?: ?boolean): string { invariant(this.cacheFolder, 'No package root'); invariant(pkg, 'Undefined package'); - + if (pkg.location && !ignoreLocation) { return pkg.location; }