Skip to content

Commit

Permalink
feat(utils): Resolve paths for scoped node modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Schmidt committed Mar 7, 2018
1 parent a150915 commit d669cfd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 10 additions & 0 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,14 @@ describe('utils', () => {
var chaiJsPath = utils.resolveModulePath(path.join('chai', 'lib', 'chai.js'));
expect(chaiJsPath).to.equal(path.join(chaiPath, 'lib', 'chai.js'));
});

it('should get the path for a scoped module', () => {
expect(utils.resolveModulePath('@some-scope/not-a-real-package')).to.be.undefined;

var modulePath = utils.resolveModulePath('@semantic-release/changelog');
expect(modulePath).to.exist;

var indexPath = utils.resolveModulePath('@semantic-release/changelog/index.js');
expect(indexPath).to.equal(path.join(modulePath, 'index.js'));
});
});
12 changes: 8 additions & 4 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,16 @@ const getPackage = function(packageName) {
*/
const resolveModulePath = function(modulePath, optBasedir) {
try {
var match = modulePath.match(/([^/]+)\/?(.*)/);
if (match && match[1]) {
var basePath = path.dirname(resolve.sync(path.join(match[1], 'package.json'), {
var parts = modulePath.split(path.sep);
if (parts && parts.length) {
// if the package is scoped, use the first two parts of the path. ie, @scope/package.
var packageName = parts[0].startsWith('@') ? path.join(parts.shift(), parts.shift()) : parts.shift();
var basePath = path.dirname(resolve.sync(path.join(packageName, 'package.json'), {
basedir: optBasedir
}));
return path.join(basePath, match[2]);

// join the remaining path to the resource (if any)
return path.join(basePath, parts.join(path.sep));
}
} catch (e) {
}
Expand Down

0 comments on commit d669cfd

Please sign in to comment.