Skip to content

Commit

Permalink
feat(npm): Include directory details from repository objects (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
greysteil authored and Haroenv committed Feb 19, 2019
1 parent 60f08f5 commit ccb1766
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/__tests__/formatPkg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ describe('test getRepositoryInfo', () => {
url: 'git+https://bitbucket.org/2klicdev/2klic-sdk.git',
};

const githubRepoWithDirectory = {
type: 'git',
url: 'https://github.com/facebook/react.git',
directory: './packages/react-dom',
};

const githubRepoWithPathUrlAndDirectory = {
type: 'git',
url: 'https://github.com/facebook/react/tree/master/packages/wrong',
directory: './packages/react-dom',
};

expect(getRepositoryInfo(githubRepo)).toEqual({
host: 'github.com',
user: 'webpack',
Expand All @@ -272,6 +284,20 @@ describe('test getRepositoryInfo', () => {
project: '2klic-sdk',
path: '',
});

expect(getRepositoryInfo(githubRepoWithDirectory)).toEqual({
host: 'github.com',
user: 'facebook',
project: 'react',
path: 'packages/react-dom',
});

expect(getRepositoryInfo(githubRepoWithPathUrlAndDirectory)).toEqual({
host: 'github.com',
user: 'facebook',
project: 'react',
path: 'packages/react-dom',
});
});

it('should return null if it cannot get information', () => {
Expand Down
12 changes: 10 additions & 2 deletions src/formatPkg.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ function getRepositoryInfo(repository) {
}

const url = typeof repository === 'string' ? repository : repository.url;
const path = typeof repository === 'string' ? '' : repository.directory || '';

if (!url) {
return null;
Expand All @@ -354,7 +355,7 @@ function getRepositoryInfo(repository) {
project,
user,
host: domain,
path: '',
path: path.replace(/^[./]+/, ''),
};
}

Expand All @@ -363,7 +364,14 @@ function getRepositoryInfo(repository) {
* https://github.com/babel/babel/tree/master/packages/babel-core
* so we need to do it
*/
return getRepositoryInfoFromHttpUrl(url);
const repositoryInfoFromUrl = getRepositoryInfoFromHttpUrl(url);
if (!repositoryInfoFromUrl) {
return null;
}
return {
...repositoryInfoFromUrl,
path: path.replace(/^[./]+/, '') || repositoryInfoFromUrl.path,
};
}

function formatUser(user) {
Expand Down

0 comments on commit ccb1766

Please sign in to comment.