Skip to content

Commit

Permalink
feat: allow "npm:name@version" dependency redirections in manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Oct 26, 2024
1 parent 2f7a489 commit 3687bea
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"execa": "^8.0.1",
"pony-cause": "^2.1.9",
"semver": "^7.5.4",
"validate-npm-package-name": "^5.0.0",
"which": "^3.0.0",
"yaml": "^2.2.2",
"yargs": "^17.7.1"
Expand Down
2 changes: 2 additions & 0 deletions src/package-manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('package-manifest', () => {
b: '^2.0.0',
c: '~4.3.0',
d: 'workspace:^',
e: 'npm:a@^2.0.0',
},
};
const validated = {
Expand All @@ -79,6 +80,7 @@ describe('package-manifest', () => {
b: '^2.0.0',
c: '~4.3.0',
d: 'workspace:^',
e: 'npm:a@^2.0.0',
},
peerDependencies: {},
};
Expand Down
27 changes: 22 additions & 5 deletions src/package-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ManifestDependencyFieldNames as PackageManifestDependenciesFieldNames,
} from '@metamask/action-utils';
import { isPlainObject } from '@metamask/utils';

Check failure on line 6 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Expected 1 empty line after import statement not followed by another import

Check failure on line 6 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Expected 1 empty line after import statement not followed by another import
const validateNPMPackageName = require('validate-npm-package-name');

Check failure on line 7 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Expected 1 empty line after require statement not followed by another require

Check failure on line 7 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

A `require()` style import is forbidden

Check failure on line 7 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Require statement not part of import statement

Check failure on line 7 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

`validate-npm-package-name` import should occur after import of `./semver.js`

Check failure on line 7 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Expected 1 empty line after require statement not followed by another require

Check failure on line 7 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

A `require()` style import is forbidden

Check failure on line 7 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Require statement not part of import statement

Check failure on line 7 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

`validate-npm-package-name` import should occur after import of `./semver.js`
import { readJsonObjectFile } from './fs.js';

Check failure on line 8 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Import in body of module; reorder to top

Check failure on line 8 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Import in body of module; reorder to top
import { isTruthyString } from './misc-utils.js';

Check failure on line 9 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Import in body of module; reorder to top

Check failure on line 9 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Import in body of module; reorder to top
import { semver, SemVer } from './semver.js';

Check failure on line 10 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Import in body of module; reorder to top

Check failure on line 10 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Import in body of module; reorder to top
Expand Down Expand Up @@ -144,8 +145,10 @@ function isValidPackageManifestVersionField(

/**
* Type guard to ensure that the provided version value is a valid dependency version

Check failure on line 147 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

JSDoc description does not satisfy the regex pattern

Check failure on line 147 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

JSDoc description does not satisfy the regex pattern
* specifier for a package manifest. This function validates both semantic versioning
* ranges and the special 'workspace:^' notation.
* specifier for a package manifest. This function validates:
* - semantic versioning ranges
* - 'workspace:^' notation
* - 'npm:{packageName}:{semverRange}' redirections
*
* @param version - The value to check.
* @returns `true` if the version is a valid string that either
Expand All @@ -155,9 +158,23 @@ function isValidPackageManifestVersionField(
function isValidPackageManifestDependencyValue(
version: unknown,
): version is string {
return (
isValidPackageManifestVersionField(version) || version === 'workspace:^'
);
if (typeof version !== 'string') {
return false;
}
if (isValidPackageManifestVersionField(version) || version === 'workspace:^') {

Check failure on line 164 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Expected blank line before this statement

Check failure on line 164 in src/package-manifest.ts

View workflow job for this annotation

GitHub Actions / Lint (20.x)

Expected blank line before this statement
return true;
}
const redirectedDependencyRegexp = /^npm:(.*)@(.*?)$/u;
try {
const redirectedDependencyMatch = redirectedDependencyRegexp.exec(version);
if (!redirectedDependencyMatch || redirectedDependencyMatch.length < 3) {
return false;
}
const [_, redirectedName, redirectedVersion] = redirectedDependencyMatch;
return validateNPMPackageName(redirectedName)?.validForOldPackages && isValidPackageManifestVersionField(redirectedVersion);
} catch (e) {
return false;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,7 @@ __metadata:
stdio-mock: ^1.2.0
tsx: ^4.6.1
typescript: ~5.1.6
validate-npm-package-name: ^5.0.0
which: ^3.0.0
yaml: ^2.2.2
yargs: ^17.7.1
Expand Down

0 comments on commit 3687bea

Please sign in to comment.