Skip to content

Commit

Permalink
Add option to change the prefix of the global bin folder - fixes #630 (
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian McKenzie authored Nov 3, 2016
1 parent 9a604a0 commit 41fe065
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
33 changes: 22 additions & 11 deletions src/cli/commands/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ async function getBins(config: Config): Promise<Set<string>> {
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\
Expand All @@ -77,18 +81,18 @@ 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 {
return path.resolve(prefix, 'bin');
}
}

async function initUpdateBins(config: Config, reporter: Reporter): Promise<() => Promise<void>> {
async function initUpdateBins(config: Config, reporter: Reporter, flags: Object): Promise<() => Promise<void>> {
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') {
Expand Down Expand Up @@ -155,7 +159,7 @@ export function hasWrapper(flags: Object, args: Array<string>): boolean {
return args[0] !== 'bin';
}

export const {run, setFlags} = buildSubCommands('global', {
const {run, setFlags: _setFlags} = buildSubCommands('global', {
async add(
config: Config,
reporter: Reporter,
Expand All @@ -164,7 +168,7 @@ export const {run, setFlags} = buildSubCommands('global', {
): Promise<void> {
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);
Expand Down Expand Up @@ -212,7 +216,7 @@ export const {run, setFlags} = buildSubCommands('global', {
): Promise<void> {
await updateCwd(config);

const updateBins = await initUpdateBins(config, reporter);
const updateBins = await initUpdateBins(config, reporter, flags);

// remove module
await runRemove(config, reporter, flags, args);
Expand All @@ -229,12 +233,19 @@ export const {run, setFlags} = buildSubCommands('global', {
): Promise<void> {
await updateCwd(config);

const updateBins = await initUpdateBins(config, reporter);
const updateBins = await initUpdateBins(config, reporter, flags);

// upgrade module
await runUpgrade(config, reporter, flags, args);

// update binaries
await updateBins();
},
});

export {run};

export function setFlags(commander: Object) {
_setFlags(commander);
commander.option('--prefix <prefix>', 'bin prefix to use to install binaries');
}
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 41fe065

Please sign in to comment.