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

Support 'workspace:^' notation in dependency versions #125

Merged
merged 3 commits into from
Dec 15, 2023
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
4 changes: 4 additions & 0 deletions src/package-manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('package-manifest', () => {
a: '1.0.0',
b: '^2.0.0',
c: '~4.3.0',
d: 'workspace:^',
},
};
const validated = {
Expand All @@ -77,6 +78,7 @@ describe('package-manifest', () => {
a: '1.0.0',
b: '^2.0.0',
c: '~4.3.0',
d: 'workspace:^',
},
peerDependencies: {},
};
Expand All @@ -100,6 +102,7 @@ describe('package-manifest', () => {
a: '1.0.0',
b: '^2.0.0',
c: '~4.3.0',
d: 'workspace:^',
},
};
const validated = {
Expand All @@ -112,6 +115,7 @@ describe('package-manifest', () => {
a: '1.0.0',
b: '^2.0.0',
c: '~4.3.0',
d: 'workspace:^',
},
};
await fs.promises.writeFile(manifestPath, JSON.stringify(unvalidated));
Expand Down
21 changes: 20 additions & 1 deletion src/package-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ function isValidPackageManifestVersionField(
return isTruthyString(version) && semver.validRange(version) !== null;
}

/**
* 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.
*
* @param version - The value to check.
* @returns `true` if the version is a valid string that either
* represents a semantic versioning range or is exactly 'workspace:^'.
* Otherwise, it returns `false`.
*/
function isValidPackageManifestDependencyValue(
version: unknown,
): version is string {
return (
isValidPackageManifestVersionField(version) || version === 'workspace:^'
);
}

/**
* Retrieves and validates the "version" field within the package manifest
* object.
Expand Down Expand Up @@ -285,7 +303,8 @@ function isValidPackageManifestDependenciesField(
(isPlainObject(depsValue) &&
Object.entries(depsValue).every(([pkgName, version]) => {
return (
isTruthyString(pkgName) && isValidPackageManifestVersionField(version)
isTruthyString(pkgName) &&
isValidPackageManifestDependencyValue(version)
);
}))
);
Expand Down