Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nicer permission errors when trying to write global binaries #1592

Merged
merged 1 commit into from
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/cli/commands/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<void> {
const afterBins = await getBins(config);

Expand All @@ -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
Expand All @@ -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);
}
}
};
}
Expand Down
1 change: 1 addition & 0 deletions src/reporters/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'We'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yarn is talking to the user, seems appropriate, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just say "Yarn doesn't…".


tooManyArguments: 'Too many arguments, maximum of $0.',
tooFewArguments: 'Not enough arguments, expected at least $0.',
Expand Down