Skip to content

Commit

Permalink
Merge pull request #1181 from Taiters/scoped-packages-fix
Browse files Browse the repository at this point in the history
Handle scoped packages when removing version
  • Loading branch information
matteofigus authored Mar 1, 2021
2 parents 0ad6151 + fada576 commit 1975adf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const path = require('path');
const fs = require('fs-extra');
const getMissingDependencies = require('./get-missing-dependencies');
const strings = require('../../../resources/index');

const getModuleName = dependency => dependency.split('@')[0];
const stripVersion = require('../../../utils/strip-version');

module.exports = (options, callback) => {
const { componentPath, dependencies, logger } = options;
Expand All @@ -29,7 +28,7 @@ module.exports = (options, callback) => {
const symLinkType = 'dir';
let symLinkError = false;
_.each(missingDependencies, dependency => {
const moduleName = getModuleName(dependency);
const moduleName = stripVersion(dependency);
const pathToComponentModule = path.resolve(
componentPath,
'node_modules',
Expand Down
5 changes: 2 additions & 3 deletions src/utils/npm-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const spawn = require('cross-spawn');
const stripVersion = require('./strip-version');

const buildInstallCommand = options => {
const args = ['install', '--prefix', options.installPath];
Expand All @@ -26,10 +27,8 @@ const executeCommand = (options, callback) => {
cmd.on('close', code => callback(code !== 0 ? code : null));
};

const moduleName = dependency => dependency.split('@')[0];

const getFullPath = ({ installPath, dependency }) =>
path.join(installPath, 'node_modules', moduleName(dependency));
path.join(installPath, 'node_modules', stripVersion(dependency));

module.exports = {
init: (options, callback) => {
Expand Down
6 changes: 6 additions & 0 deletions src/utils/strip-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const path = require('path');

module.exports = dependency => {
const parts = path.parse(dependency);
return path.join(parts.dir, parts.base.split('@')[0]);
};
47 changes: 47 additions & 0 deletions test/unit/utils-strip-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const expect = require('chai').expect;

describe('utils : stripVersion', () => {
const stripVersion = require('../../src/utils/strip-version');

describe('when a non scoped dependency is provided', () => {
const dependency = '/path/to/dependency';

describe('when a version is included', () => {
const name = stripVersion(dependency + '@1.0.0');

it('should return the dependency without the version', () => {
expect(name).to.equal('/path/to/dependency');
});
});

describe('when a version is not included', () => {
const name = stripVersion(dependency);

it('should return the unmodified dependency', () => {
expect(name).to.equal('/path/to/dependency');
});
});
});

describe('when a scoped dependency is provided', () => {
const dependency = '/path/to/@the-scoped/package';

describe('when a version is included', () => {
const name = stripVersion(dependency + '@1.2.3');

it('should return the dependency without the version', () => {
expect(name).to.equal('/path/to/@the-scoped/package');
});
});

describe('when a version is not included', () => {
const name = stripVersion(dependency);

it('should return the unmodified dependency', () => {
expect(name).to.equal('/path/to/@the-scoped/package');
});
});
});
});

0 comments on commit 1975adf

Please sign in to comment.