forked from yarnpkg/yarn
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve handling of packages without manifest (yarnpkg#3701)
- Better guessing of package names - Support for hosted git, like github or gitlab - Support for .tar.gz archives
- Loading branch information
Showing
6 changed files
with
108 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* @flow */ | ||
|
||
import guessName from '../../src/util/guess-name'; | ||
|
||
const examples = [ | ||
'http://github.com/foo-bar/awesome-name', | ||
'http://github.com/foo-bar/awesome-name.git', | ||
'http://awesome.com/awesome-name.git', | ||
'http://awesome.com/awesome-name.git?foo/bar#fiz/fuz', | ||
'https://github.com/hashicorp/awesome-name/archive/v0.5.5.tar.gz', | ||
'https://gitlab.com/foo/awesome-name/repository/archive.tar.gz?ref=3.11.0', | ||
'https://gitlab.com/foo/awesome-name/repository/archive.tar.bz2?ref=3.11.0', | ||
'https://gitlab.com/foo/awesome-name/repository/archive.tar?ref=3.11.0', | ||
'https://gitlab.com/foo/awesome-name/repository/archive.zip?ref=3.11.0', | ||
'[email protected]:yolo/awesome-name.git', | ||
'https://gitlab.com/yolo/awesome-name.git', | ||
'https://asesome.com/yolo/awesome-name-0.2.3.tar.gz', | ||
'/foo/bar/awesome-name', | ||
'./foo/bar/awesome-name', | ||
'../foo/bar/awesome-name', | ||
'file:../foo/bar/awesome-name', | ||
'file:../foo/bar/awesome-name.tar.gz', | ||
'awesome-name', | ||
'awesome-name.tar.gz', | ||
]; | ||
|
||
describe('guessName', () => { | ||
for (const source of examples) { | ||
it(`guess name of ${source}`, () => { | ||
expect(guessName(source)).toBe('awesome-name'); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* @flow */ | ||
|
||
import url from 'url'; | ||
|
||
function cleanup(name: string): string { | ||
name = name.replace(/-\d+\.\d+\.\d+/, ''); | ||
return name.split('.')[0]; | ||
} | ||
|
||
function guessNameFallback(source: string): string { | ||
// If cannot parse as url, just return cleaned up last part | ||
const parts = source.split('/'); | ||
return cleanup(parts[parts.length - 1]); | ||
} | ||
|
||
export default function guessName(source: string): string { | ||
try { | ||
const parsed = url.parse(source); | ||
|
||
if (!parsed.pathname) { | ||
return guessNameFallback(source); | ||
} | ||
|
||
const parts = parsed.pathname.split('/'); | ||
|
||
// Priority goes to part that ends with .git | ||
for (const part of parts) { | ||
if (part.match(/\.git$/)) { | ||
return cleanup(part); | ||
} | ||
} | ||
|
||
// Most likely a directory | ||
if (parsed.host == null) { | ||
return cleanup(parts[parts.length - 1]); | ||
} | ||
|
||
// A site like github or gitlab | ||
if (parts.length > 2) { | ||
return cleanup(parts[2]); | ||
} | ||
|
||
// Privately hosted package? | ||
if (parts.length > 1) { | ||
return cleanup(parts[1]); | ||
} | ||
|
||
return guessNameFallback(source); | ||
} catch (e) { | ||
return guessNameFallback(source); | ||
} | ||
} |