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 5bc6b66
Show file tree
Hide file tree
Showing 4 changed files with 37 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
38 changes: 33 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';
import validateNPMPackageName from 'validate-npm-package-name';

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

View workflow job for this annotation

GitHub Actions / Build (18.x)

Could not find a declaration file for module 'validate-npm-package-name'. '/home/runner/work/create-release-branch/create-release-branch/node_modules/validate-npm-package-name/lib/index.js' implicitly has an 'any' type.

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

View workflow job for this annotation

GitHub Actions / Build (20.x)

Could not find a declaration file for module 'validate-npm-package-name'. '/home/runner/work/create-release-branch/create-release-branch/node_modules/validate-npm-package-name/lib/index.js' implicitly has an 'any' type.
import { readJsonObjectFile } from './fs.js';
import { isTruthyString } from './misc-utils.js';
import { semver, SemVer } from './semver.js';
Expand Down Expand Up @@ -144,8 +145,10 @@ function isValidPackageManifestVersionField(

/**
* Type guard to ensure that the provided version value is a valid dependency version
* 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,34 @@ 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:^'
) {
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 5bc6b66

Please sign in to comment.