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

Get package version from manifest #121

Merged
merged 3 commits into from Nov 4, 2022
Merged

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Nov 2, 2022

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.

@Gudahtt Gudahtt requested a review from a team as a code owner November 2, 2022 22:54
Comment on lines +319 to +350
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;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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;

Comment on lines +339 to +371
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;
}
Copy link
Contributor

@legobeat legobeat Nov 3, 2022

Choose a reason for hiding this comment

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

Suggested change
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}'`);
}

Copy link
Member Author

@Gudahtt Gudahtt Nov 3, 2022

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in #122

Copy link
Contributor

@legobeat legobeat left a 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.
@Gudahtt Gudahtt force-pushed the get-package-version-from-manifest branch from b63a99a to 41cb1e6 Compare November 3, 2022 15:56
@Gudahtt
Copy link
Member Author

Gudahtt commented Nov 3, 2022

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

Copy link
Contributor

@legobeat legobeat left a comment

Choose a reason for hiding this comment

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

lgtm!

@Gudahtt Gudahtt merged commit f5c527c into main Nov 4, 2022
@Gudahtt Gudahtt deleted the get-package-version-from-manifest branch November 4, 2022 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants