Skip to content

Commit

Permalink
Fixes mgol#45: support npm package aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
IceCreamYou committed Feb 1, 2022
1 parent 9c3dae4 commit 4d2353d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/check-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ const checkDependenciesHelper = (syncOrAsync, config, callback) => {
let versionString = pkg.versionString;

const depDir = `${depsDir}/${name}`;
const depJson = `${depDir}/${depsJsonName}`;
const depJsonPath = `${depDir}/${depsJsonName}`;

if (!fs.existsSync(depDir) || !fs.existsSync(depJson)) {
if (!fs.existsSync(depDir) || !fs.existsSync(depJsonPath)) {
if (pkg.isOptional) {
log(`${name}: ${chalk.red('not installed!')}`);
} else {
Expand Down Expand Up @@ -202,7 +202,28 @@ const checkDependenciesHelper = (syncOrAsync, config, callback) => {
return;
}

const depVersion = require(depJson).version;
const depJson = require(depJsonPath);

// Support package aliases
if (
options.packageManager !== 'bower' &&
/npm:(.+)@(.+)/.test(versionString)
) {
const [, depName, version] = versionString.match(/npm:(.+)@(.+)/);

versionString = version;

if (depJson.name !== depName) {
success = false;
error(
`${name}: installed: ${chalk.red(
depName,
)}, expected: ${chalk.green(depJson.name)}`,
);
}
}

const depVersion = depJson.version;
if (semver.satisfies(depVersion, versionString)) {
log(
`${name}: installed: ${chalk.green(
Expand Down
7 changes: 7 additions & 0 deletions test/npm-fixtures/ok/node_modules/c-alias/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/npm-fixtures/ok/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"a": "1.2.3",
"b": ">=1.0.0",
"c": "<2.0",
"c-alias": "npm:c@<2.0",
"d": "git+ssh://[email protected]:d/d.git#0.5.9",
"@e-f/g-h": "~2.5.7"
}
Expand Down
1 change: 1 addition & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,7 @@ describe('checkDependencies', () => {
'a: installed: 1.2.3, expected: 1.2.3',
'b: installed: 1.2.3, expected: >=1.0.0',
'c: installed: 1.2.3, expected: <2.0',
'c-alias: installed: 1.2.3, expected: <2.0',
'@e-f/g-h: installed: 2.5.9, expected: ~2.5.7',
'',
].join('\n'),
Expand Down

0 comments on commit 4d2353d

Please sign in to comment.