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

Feature support NPM package aliases #45

Closed
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
30 changes: 27 additions & 3 deletions lib/check-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ const checkDependenciesHelper = (syncOrAsync, config, callback) => {
return finish();
};

const supportsPackageAliases = packageManager =>
packageManager === 'npm' || packageManager === 'yarn';
Comment on lines +107 to +108
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this check? I imagine some other package managers like pnpm may also support aliases. We might want to exclude Bower here as that's completely different but in the rest I wouldn't assume lack of support.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgol Yes, keeping a whitelist is secure, keeping a blacklist will eventually cause issues. All it takes is one package manager that interprets the colon differently to have huge discrepancies rendering the package unusable. Either way, when support for these other package managers is added code like this can be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, not having sufficient checks like this is what prompted this PR in the first place, you take aliased version structure and pretend to know what it is, causing incorrect behaviour. Care should be taken to keep publicly used code forwards compatible

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pnpm already supports package aliases:
https://pnpm.js.org/en/aliases
and the syntax looks the same as the Yarn/npm one. It looks like the Yarn-originated syntax has become a de-facto standard that all package managers will soon follow. It's not likely that other package managers will implement it differently IMO.


options.packageDir = options.packageDir || findup(packageJsonName);
if (!options.packageDir) {
return missingPackageJson();
Expand Down Expand Up @@ -152,9 +155,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 +205,28 @@ const checkDependenciesHelper = (syncOrAsync, config, callback) => {
return;
}

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

// Support package aliases
if (
supportsPackageAliases(options.packageManager) &&
/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