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 using pre-release tags with a tarball url in --scripts-version #876

Merged
merged 4 commits into from
Oct 12, 2016
Merged

Fix using pre-release tags with a tarball url in --scripts-version #876

merged 4 commits into from
Oct 12, 2016

Conversation

jihchi
Copy link
Contributor

@jihchi jihchi commented Oct 9, 2016

For example, react-scripts version0.2.0-alpha.1 with tarball url didn't works:

$ create-react-app --scripts-version=https://registry.npmjs.org/react-scripts/-/react-scripts-0.2.0-alpha.1.tgz test-app-tarball-url
Creating a new React app in /Users/archie/Repos/test-app-tarball-url.

Installing packages. This might take a couple minutes.
Installing react-scripts from npm...


> fsevents@1.0.14 install /Users/archie/Repos/test-app-tarball-url/node_modules/fsevents
> node-pre-gyp install --fallback-to-build

...

module.js:457
    throw err;
    ^

Error: Cannot find module '/Users/archie/Repos/test-app-tarball-url/node_modules/react-scripts-0.2.0/package.json'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.Module._load (module.js:403:25)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at checkNodeVersion (/Users/archie/.nvm/versions/node/v6.7.0/lib/node_modules/create-react-app/index.js:170:21)
    at ChildProcess.<anonymous> (/Users/archie/.nvm/versions/node/v6.7.0/lib/node_modules/create-react-app/index.js:127:5)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:877:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)

create-react-app version: 0.5.0

@gaearon
Copy link
Contributor

gaearon commented Oct 11, 2016

Thanks for the PR! Can you please explain why this works and the other one didn't?
Also it would need rebasing.

@jihchi
Copy link
Contributor Author

jihchi commented Oct 11, 2016

create-react-app is expected that getPackageName returns exactly react-scripts string while processing tarball URL, for example:

getPackageName('https://registry.npmjs.org/react-scripts/-/react-scripts-0.2.0-alpha.1.tgz');
// expected: react-scripts
// actual: react-scripts-0.2.0

Current regex didn't handle pre-release tags.

This modification adds support for this case as well as original case.

Just like original regex, first it searches first backslash /^.+\/, then capture the group name (.+), and then the version -[0-9]+\.[0-9]+\.[0-9]+ (major-minor-patch), then a non-capture group (?:[-+].+)? which is optional and searches for pre-release tags, the last is tarball extension \.tgz$.

@@ -153,7 +153,7 @@ function getInstallPackage(version) {
// Extract package name from tarball url or path.
function getPackageName(installPackage) {
if (installPackage.indexOf('.tgz') > -1) {
return installPackage.match(/^.+\/(.+)-.+\.tgz$/)[1];
return installPackage.match(/^.+\/(.+)-[0-9]+\.[0-9]+\.[0-9]+(?:[-+].+)?\.tgz$/)[1];
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a comment showing intended inputs and what should match them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment added.

@@ -153,6 +153,8 @@ function getInstallPackage(version) {
// Extract package name from tarball url or path.
function getPackageName(installPackage) {
if (installPackage.indexOf('.tgz') > -1) {
// The package name could be with or without semver version, e.g. react-scripts-0.2.0-alpha.1.tgz
// However, This function returns package name only wihout semver version.
return installPackage.match(/^.+\/(.+)-[0-9]+\.[0-9]+\.[0-9]+(?:[-+].+)?\.tgz$/)[1];
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make regex cut everything after the first -\d?

Copy link
Contributor Author

@jihchi jihchi Oct 11, 2016

Choose a reason for hiding this comment

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

Code changed.

return installPackage.match(/^.+\/(.+)-.+\.tgz$/)[1];
// The package name could be with or without semver version, e.g. react-scripts-0.2.0-alpha.1.tgz
// However, This function returns package name only wihout semver version.
return installPackage.match(/^.+\/(.+)-\d+.+\.tgz$/)[1];
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make that group optional? So that just react-scripts.tgz will also get parsed correctly.

Copy link
Contributor Author

@jihchi jihchi Oct 12, 2016

Choose a reason for hiding this comment

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

Code changed.

Regex breakdown:

  • ^.+\/ starts with any characters until last slash
    • ( group 1
      • .+? searches for package name (non-greedy for searching optional semver version)
    • )
    • (?: non-capturing group
      • -\d+ searches for version starts with dash & number
      • .+ cut everything after
    • )? it's optional
  • \.tgz$ finally, the tarball file extension

@@ -153,7 +153,9 @@ function getInstallPackage(version) {
// Extract package name from tarball url or path.
function getPackageName(installPackage) {
if (installPackage.indexOf('.tgz') > -1) {
return installPackage.match(/^.+\/(.+)-.+\.tgz$/)[1];
// The package name could be with or without semver version, e.g. react-scripts-0.2.0-alpha.1.tgz
// However, This function returns package name only wihout semver version.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: "This" -> "this".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@gaearon gaearon added this to the 0.7.0 milestone Oct 12, 2016
@gaearon
Copy link
Contributor

gaearon commented Oct 12, 2016

Looks good to me. Thanks!

@gaearon gaearon merged commit c5f5b00 into facebook:master Oct 12, 2016
feiqitian pushed a commit to feiqitian/create-react-app that referenced this pull request Oct 25, 2016
…acebook#876)

* Add supports for prelease tags version

* Add comment to regex

* Cut everything after the first -\d

* Make semver version optional, so just package name get parsed correctly
jarlef pushed a commit to jarlef/create-react-app that referenced this pull request Nov 28, 2016
…acebook#876)

* Add supports for prelease tags version

* Add comment to regex

* Cut everything after the first -\d

* Make semver version optional, so just package name get parsed correctly
@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants