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

fix(get-package-name): resolve paths properly before matching #45

Merged

Conversation

smackfu
Copy link
Member

@smackfu smackfu commented Dec 22, 2023

The current logic for local template packages is:

  1. Install the template package,
  2. Look up the template module name via the template package path using the package.json.

This seems to only work properly when the local template package filename is in the form ./some/path/package.tgz. If the filename is an absolute path, it doesn't match and errors out. If the filename is a relative path but uses .. to go up the tree, it doesn't match and errors. I believe it only works in the one case because the "." in the filename is parsed as a regex and matches any character.

This PR changes the logic to fully resolve both the filename in the package.json and the filename passed on the command line, then directly compare them. This should work reliably for all path values.

It also removes the logic that only resolves the path to the tar.gz file when it's a relative path. This isn't needed since the check if the file exists is more reliable and general. This fixes the case where you do not provide a path at all, for instance: npm init using-template some-template-template-1.0.0.tgz

Fixes #44

@10xLaCroixDrinker 10xLaCroixDrinker requested a review from a team January 2, 2024 16:58
.find(([, version]) => version.match(templateName))[0];
.find(([, version]) => {
if (version.startsWith('file:')) {
const dependencyPath = path.resolve(__dirname, '../../', version.slice(5));
Copy link
Member

Choose a reason for hiding this comment

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

curious, since it's hard to visualize, why ../../ and not ../ or ../../../

Copy link
Member Author

Choose a reason for hiding this comment

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

If I remember correctly, __dirname is the current javascript file's dir, so ../../ is reversing out src/utils to get back to the directory where the package.json is. Then the path stored in the package.json with the "file:" prefix is a relative path from that directory.

@smackfu smackfu requested review from giulianok and a team January 5, 2024 16:56
@JAdshead JAdshead enabled auto-merge (squash) January 5, 2024 19:23
@smackfu smackfu force-pushed the fix/local-install-path-matching branch from 9958046 to bc3e49d Compare January 5, 2024 20:02
@JAdshead JAdshead merged commit 28aefa6 into americanexpress:main Jan 5, 2024
5 checks passed
oneamexbot added a commit that referenced this pull request Jan 5, 2024
## [1.11.1](v1.11.0...v1.11.1) (2024-01-05)

### Bug Fixes

* **get-package-name:** resolve paths properly before matching ([#45](#45)) ([28aefa6](28aefa6))
@oneamexbot
Copy link
Contributor

🎉 This PR is included in version 1.11.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Local template packages only work from relative paths
5 participants