Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle scoped packages when removing version #1181

Merged
merged 4 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
});
});