Skip to content

Commit ccb1766

Browse files
greysteilHaroenv
authored andcommitted
feat(npm): Include directory details from repository objects (#320)
1 parent 60f08f5 commit ccb1766

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/__tests__/formatPkg.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,18 @@ describe('test getRepositoryInfo', () => {
252252
url: 'git+https://bitbucket.org/2klicdev/2klic-sdk.git',
253253
};
254254

255+
const githubRepoWithDirectory = {
256+
type: 'git',
257+
url: 'https://github.com/facebook/react.git',
258+
directory: './packages/react-dom',
259+
};
260+
261+
const githubRepoWithPathUrlAndDirectory = {
262+
type: 'git',
263+
url: 'https://github.com/facebook/react/tree/master/packages/wrong',
264+
directory: './packages/react-dom',
265+
};
266+
255267
expect(getRepositoryInfo(githubRepo)).toEqual({
256268
host: 'github.com',
257269
user: 'webpack',
@@ -272,6 +284,20 @@ describe('test getRepositoryInfo', () => {
272284
project: '2klic-sdk',
273285
path: '',
274286
});
287+
288+
expect(getRepositoryInfo(githubRepoWithDirectory)).toEqual({
289+
host: 'github.com',
290+
user: 'facebook',
291+
project: 'react',
292+
path: 'packages/react-dom',
293+
});
294+
295+
expect(getRepositoryInfo(githubRepoWithPathUrlAndDirectory)).toEqual({
296+
host: 'github.com',
297+
user: 'facebook',
298+
project: 'react',
299+
path: 'packages/react-dom',
300+
});
275301
});
276302

277303
it('should return null if it cannot get information', () => {

src/formatPkg.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ function getRepositoryInfo(repository) {
338338
}
339339

340340
const url = typeof repository === 'string' ? repository : repository.url;
341+
const path = typeof repository === 'string' ? '' : repository.directory || '';
341342

342343
if (!url) {
343344
return null;
@@ -354,7 +355,7 @@ function getRepositoryInfo(repository) {
354355
project,
355356
user,
356357
host: domain,
357-
path: '',
358+
path: path.replace(/^[./]+/, ''),
358359
};
359360
}
360361

@@ -363,7 +364,14 @@ function getRepositoryInfo(repository) {
363364
* https://github.com/babel/babel/tree/master/packages/babel-core
364365
* so we need to do it
365366
*/
366-
return getRepositoryInfoFromHttpUrl(url);
367+
const repositoryInfoFromUrl = getRepositoryInfoFromHttpUrl(url);
368+
if (!repositoryInfoFromUrl) {
369+
return null;
370+
}
371+
return {
372+
...repositoryInfoFromUrl,
373+
path: path.replace(/^[./]+/, '') || repositoryInfoFromUrl.path,
374+
};
367375
}
368376

369377
function formatUser(user) {

0 commit comments

Comments
 (0)