-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Get package version from manifest #121
Conversation
if (error.code === 'ENOENT') { | ||
exitWithError( | ||
`Package manifest not found at path: '${manifestPath}'\nRun this script from the project root directory, or set the project directory using the '--root' flag.`, | ||
); | ||
return; | ||
} else if (error.code === 'EACCES') { | ||
exitWithError( | ||
`Access to package manifest is forbidden by file access permissions: '${manifestPath}'`, | ||
); | ||
return; | ||
} else if (error.name === 'SyntaxError') { | ||
exitWithError( | ||
`Package manifest cannot be parsed as JSON: '${manifestPath}'`, | ||
); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (error.code === 'ENOENT') { | |
exitWithError( | |
`Package manifest not found at path: '${manifestPath}'\nRun this script from the project root directory, or set the project directory using the '--root' flag.`, | |
); | |
return; | |
} else if (error.code === 'EACCES') { | |
exitWithError( | |
`Access to package manifest is forbidden by file access permissions: '${manifestPath}'`, | |
); | |
return; | |
} else if (error.name === 'SyntaxError') { | |
exitWithError( | |
`Package manifest cannot be parsed as JSON: '${manifestPath}'`, | |
); | |
return; | |
} | |
if (error.code === 'ENOENT') { | |
return exitWithError( | |
`Package manifest not found at path: '${manifestPath}'\nRun this script from the project root directory, or set the project directory using the '--root' flag.`, | |
); | |
} | |
if (error.code === 'EACCES') { | |
return exitWithError( | |
`Access to package manifest is forbidden by file access permissions: '${manifestPath}'`, | |
); | |
} | |
if (error.name === 'SyntaxError') { | |
return exitWithError( | |
`Package manifest cannot be parsed as JSON: '${manifestPath}'`, | |
); | |
} | |
throw err; |
exitWithError( | ||
`Version not found. Please set the --currentVersion flag, or run this as an npm script from a project with the 'version' field set.`, | ||
); | ||
return; | ||
} else if (currentVersion && semver.valid(currentVersion) === null) { | ||
exitWithError(`Current version is not valid SemVer: '${currentVersion}'`); | ||
return; | ||
} else if (!repoUrl) { | ||
exitWithError( | ||
`npm package repository URL not found. Please set the '--repo' flag, or run this as an npm script from a project with the 'repository' field set.`, | ||
); | ||
return; | ||
} else if (!isValidUrl(repoUrl)) { | ||
exitWithError(`Invalid repo URL: '${repoUrl}'`); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exitWithError( | |
`Version not found. Please set the --currentVersion flag, or run this as an npm script from a project with the 'version' field set.`, | |
); | |
return; | |
} else if (currentVersion && semver.valid(currentVersion) === null) { | |
exitWithError(`Current version is not valid SemVer: '${currentVersion}'`); | |
return; | |
} else if (!repoUrl) { | |
exitWithError( | |
`npm package repository URL not found. Please set the '--repo' flag, or run this as an npm script from a project with the 'repository' field set.`, | |
); | |
return; | |
} else if (!isValidUrl(repoUrl)) { | |
exitWithError(`Invalid repo URL: '${repoUrl}'`); | |
return; | |
} | |
return exitWithError( | |
`Version not found. Please set the --currentVersion flag, or run this as an npm script from a project with the 'version' field set.`, | |
); | |
} | |
if (semver.valid(currentVersion) === null) { | |
return exitWithError(`Current version is not valid SemVer: '${currentVersion}'`); | |
} | |
if (!repoUrl) { | |
return exitWithError( | |
`npm package repository URL not found. Please set the '--repo' flag, or run this as an npm script from a project with the 'repository' field set.`, | |
); | |
} | |
if (!isValidUrl(repoUrl)) { | |
return exitWithError(`Invalid repo URL: '${repoUrl}'`); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern of placing the return on the following line was to make complying with the ESLint "consistent-return" rule easier.
Now that you're pointing it out, I think I agree that this style is harder to read and more error prone. I can adjust that in a follow-up PR and find a better solution for complying with that lint rule. I'd rather avoid doing it here because the same pattern is used in more places, and I'd like to change them all at once. It's distinct from the main purpose of this PR as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #122
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note in particular rethrowing errors that aren't explicitly caught.
The package version is now obtained from the package manifest rather than from the `npm_package_version` environment variable. This strategy works better with the `--root` flag, which might sometimes specify a root project directory that is different from the current working directory. The current version should always match the version in the manifest at the project root.
b63a99a
to
41cb1e6
Compare
Oh darn, great find! Not re-throwing the error is a bad one. I've just fixed that in 41cb1e6 The requested style changes I can address in a follow-up PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm!
The package version is now obtained from the package manifest rather than from the
npm_package_version
environment variable. This strategy works better with the--root
flag, which might sometimes specify a root project directory that is different from the current working directory. The current version should always match the version in the manifest at the project root.