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

Warn when man or bin directory not found but don't crash #1663

Merged
merged 4 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/reporters/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const messages = {
manifestStringExpected: '$0 is not a string',
manifestDependencyBuiltin: 'Dependency $0 listed in $1 is the name of a built-in module',
manifestDependencyCollision: '$0 has dependency $1 with range $2 that collides with a dependency in $3 of the same name with version $4',
manifestDirectoryNotFound: 'Unable to read $0 directory of module $1',

configSet: 'Set $0 to $1.',
configDelete: 'Deleted $0.',
Expand Down
29 changes: 22 additions & 7 deletions src/util/normalize-manifest/fix.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ type Dict<T> = {
[key: string]: T;
};

type WarnFunction = (msg: string) => void;

export default async function (
info: Dict<mixed>,
moduleLoc: string,
reporter: Reporter,
warn: WarnFunction,
looseSemver: boolean,
): Promise<void> {
const files = await fs.readdir(moduleLoc);
Expand Down Expand Up @@ -185,11 +188,17 @@ export default async function (
if (!info.bin && binDir && typeof binDir === 'string') {
const bin = info.bin = {};

for (const scriptName of await fs.readdir(path.join(moduleLoc, binDir))) {
if (scriptName[0] === '.') {
continue;
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we do await fs.exists() here instead of the try-catch? Seems much nicer.

Copy link
Author

Choose a reason for hiding this comment

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

fs.exists is deprecated. I'm not sure how long it has been deprecated and the other options of fs.stat() or fs.access() seem like they would have the same problem of having the error thrown.

for (const scriptName of await fs.readdir(path.join(moduleLoc, binDir))) {
if (scriptName[0] === '.') {
continue;
}
bin[scriptName] = path.join('.', binDir, scriptName);
}
} catch (err) {
if (err.code && err.code === 'ENOTENT') {
warn(reporter.lang('manifestDirectoryNotFound', binDir, info.name));
}
bin[scriptName] = path.join('.', binDir, scriptName);
}
}

Expand All @@ -198,9 +207,15 @@ export default async function (
if (!info.man && typeof manDir === 'string') {
const man = info.man = [];

for (const filename of await fs.readdir(path.join(moduleLoc, manDir))) {
if (/^(.*?)\.[0-9]$/.test(filename)) {
man.push(path.join('.', manDir, filename));
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here too please.

for (const filename of await fs.readdir(path.join(moduleLoc, manDir))) {
if (/^(.*?)\.[0-9]$/.test(filename)) {
man.push(path.join('.', manDir, filename));
}
}
} catch (err) {
if (err.code && err.code === 'ENOTENT') {
warn(reporter.lang('manifestDirectoryNotFound', manDir, info.name));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/normalize-manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function (
config: Config,
isRoot: boolean,
): Promise<Manifest> {
await fix(info, moduleLoc, config.reporter, config.looseSemver);
await fix(info, moduleLoc, config.reporter, warn, config.looseSemver);

// create human readable name
const {name, version} = info;
Expand Down