From 399323ccea340f636267222c678910abc8c39c89 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 1 Nov 2016 14:00:53 +0000 Subject: [PATCH] Nicer permission errors when trying to write global binaries - fixes #1578 --- src/cli/commands/global.js | 23 ++++++++++++++++++++--- src/reporters/lang/en.js | 1 + 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/cli/commands/global.js b/src/cli/commands/global.js index fdd4f9dac3..f7a7829493 100644 --- a/src/cli/commands/global.js +++ b/src/cli/commands/global.js @@ -3,6 +3,7 @@ import type {Reporter} from '../../reporters/index.js'; import type {Manifest} from '../../types.js'; import type Config from '../../config.js'; +import {MessageError} from '../../errors.js'; import {registries} from '../../registries/index.js'; import NoopReporter from '../../reporters/base-reporter.js'; import buildSubCommands from './_build-sub-commands.js'; @@ -88,6 +89,14 @@ async function initUpdateBins(config: Config, reporter: Reporter): Promise<() => const beforeBins = await getBins(config); const binFolder = getBinFolder(); + function throwPermError(err: Error & { [code: string]: string }, dest: string) { + if (err.code === 'EACCES') { + throw new MessageError(reporter.lang('noFilePermission', dest)); + } else { + throw err; + } + } + return async function(): Promise { const afterBins = await getBins(config); @@ -100,7 +109,11 @@ async function initUpdateBins(config: Config, reporter: Reporter): Promise<() => // remove old bin const dest = path.join(binFolder, path.basename(src)); - await fs.unlink(dest); + try { + await fs.unlink(dest); + } catch (err) { + throwPermError(err, dest); + } } // add new bins @@ -112,8 +125,12 @@ async function initUpdateBins(config: Config, reporter: Reporter): Promise<() => // insert new bin const dest = path.join(binFolder, path.basename(src)); - await fs.unlink(dest); - await linkBin(src, dest); + try { + await fs.unlink(dest); + await linkBin(src, dest); + } catch (err) { + throwPermError(err, dest); + } } }; } diff --git a/src/reporters/lang/en.js b/src/reporters/lang/en.js index 25d175b02c..a950043997 100644 --- a/src/reporters/lang/en.js +++ b/src/reporters/lang/en.js @@ -74,6 +74,7 @@ const messages = { missingWhyDependency: 'Missing package name, folder or path to file to identify why a package has been installed', unexpectedError: 'An unexpected error occurred, please open a bug report with the information provided in $0.', jsonError: 'Error parsing JSON at $0, $1.', + noFilePermission: "We don't have permissions to touch the file $0.", tooManyArguments: 'Too many arguments, maximum of $0.', tooFewArguments: 'Not enough arguments, expected at least $0.',