-
Notifications
You must be signed in to change notification settings - Fork 117
feat(tools-packages): Add package validation helpers to tools-packages #4110
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
Open
JasonVMo
wants to merge
6
commits into
main
Choose a base branch
from
user/jasonvmo/package-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5414792
add package context and validation types
JasonVMo 65542e8
add default fix mode setting and exposure
JasonVMo 1e2bfc3
update tools-packages with enhanced functionality around validating p…
JasonVMo 06dd282
docs(changeset): Add package context, validation context, and abstrac…
JasonVMo b59a812
fix prototype pollution security risk
JasonVMo 7ff1b97
remove the index signature from package context and put it back on pa…
JasonVMo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@rnx-kit/tools-packages": minor | ||
| --- | ||
|
|
||
| Add package context, validation context, and abstractions that allow working in yarn constraints and directly against files |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { readJSONFileSync } from "@rnx-kit/tools-filesystem"; | ||
| import type { PackageManifest } from "@rnx-kit/types-node"; | ||
| import type { Yarn } from "@yarnpkg/types"; | ||
| import path from "node:path"; | ||
| import { | ||
| createJSONValidator, | ||
| getJSONPathSegments, | ||
| isJSONValidator, | ||
| type JSONValidatorOptions, | ||
| } from "./json.ts"; | ||
| import type { | ||
| JSONValue, | ||
| PackageContext, | ||
| PackageValidationContext, | ||
| JSONValuePath, | ||
| JSONValidationResult, | ||
| } from "./types"; | ||
|
|
||
| /** | ||
| * Create a core package context for a given root directory and optional manifest. | ||
| * @param root root directory of the package | ||
| * @param manifest optional package manifest, if not provided it will be loaded from root/package.json | ||
| * @returns a CorePackageContext with basic properties and file checking capabilities, but no enforce or validate functions | ||
| */ | ||
| export function createPackageContext< | ||
| TManifest extends PackageManifest = PackageManifest, | ||
| >(root: string, loadedManifest?: TManifest): PackageContext<TManifest> { | ||
| root = path.resolve(root); | ||
| const manifest = | ||
| loadedManifest ?? | ||
| readJSONFileSync<TManifest>(path.join(root, "package.json")); | ||
| return { | ||
| root, | ||
| manifest, | ||
| name: manifest.name, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a package validation context from a path to a package root (with an optional manifest). | ||
| * This will wrap the package manifest in a JSON validator so that enforce, error, changed, and finish | ||
| * methods are available for validating and optionally fixing the package.json contents. | ||
| * | ||
| * @param base root path or for the package | ||
| * @param manifest optional package manifest to use instead of loading from the package root | ||
| * @param options optional JSON validator options to configure how the package.json is validated and fixed | ||
| * @returns a PackageValidationContext wrapping the package manifest with JSON validation capabilities | ||
| */ | ||
| export function createPackageValidationContext< | ||
| TManifest extends PackageManifest = PackageManifest, | ||
| >( | ||
| base: string, | ||
| manifest: TManifest | undefined = undefined, | ||
| options: JSONValidatorOptions = {} | ||
| ): PackageValidationContext<TManifest> { | ||
| const context = createPackageContext<TManifest>(base, manifest); | ||
| const jsonFilePath = path.join(context.root, "package.json"); | ||
|
|
||
| return createJSONValidator( | ||
| context.manifest as Record<string, JSONValue>, | ||
| { ...options, jsonFilePath }, | ||
| context | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Adds JSON validator capabilities to an existing package context using the default options for JSON validation. | ||
| * If it is already a JSON validator it will be returned as-is without modification. | ||
| * | ||
| * @param context the package context to enhance with JSON validator capabilities | ||
| * @returns a package validation context wrapping the provided package context with JSON validation capabilities | ||
| */ | ||
| export function asPackageValidationContext< | ||
| TManifest extends PackageManifest = PackageManifest, | ||
| >(context: PackageContext<TManifest>): PackageValidationContext<TManifest> { | ||
| if (isJSONValidator(context)) { | ||
| return context; | ||
| } | ||
| const jsonFilePath = path.join(context.root, "package.json"); | ||
| return createJSONValidator( | ||
| context.manifest as Record<string, JSONValue>, | ||
| { jsonFilePath }, | ||
| context | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Create a package validation context for a yarn workspace provided by the constraints API. This will route the validation context | ||
| * APIs to the provided workspace, which will error or fix depending on whether fix mode is enabled for the constraints execution. | ||
| * This allows the same validation code to be used both in standalone mode and as part of yarn constraints. | ||
| * | ||
| * --- IMPORTANT NOTE --- | ||
| * When running in yarn constraints mode, yarn handles error reporting and tracking internally. As a result, the 'changed' and 'finish' | ||
| * methods will be no-ops and will not reflect changes. Also manual modifications to manifest may have unexpected results. | ||
| * | ||
| * @param workspace The yarn workspace to create the context for | ||
| * @returns A package validation context for the provided yarn workspace | ||
| */ | ||
| export function createYarnWorkspaceContext< | ||
| TManifest extends PackageManifest = PackageManifest, | ||
| >(workspace: Yarn.Constraints.Workspace): PackageValidationContext<TManifest> { | ||
| return { | ||
| ...createPackageContext(workspace.cwd, workspace.manifest as TManifest), | ||
| enforce(path: JSONValuePath, value: JSONValue | undefined): void { | ||
| const safePath = getJSONPathSegments(path); | ||
| if (value === undefined) { | ||
| workspace.unset(safePath); | ||
| } else { | ||
| workspace.set(safePath, value); | ||
| } | ||
| }, | ||
| changed: yarnChangedStub, | ||
| finish: yarnFinishStub, | ||
| error(message: string): void { | ||
| workspace.error(message); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function yarnFinishStub(): JSONValidationResult { | ||
| return { changes: false, errors: false }; | ||
| } | ||
|
|
||
| function yarnChangedStub(): void { | ||
| // no-op as yarn constraints will handle this internally | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,41 @@ | ||
| export { | ||
| createObjectValueAccessors, | ||
| createPackageValueAccessors, | ||
| createPackageValueLoader, | ||
| createValueLoader, | ||
| } from "./accessors.ts"; | ||
|
|
||
| export { | ||
| asPackageValidationContext, | ||
| createPackageContext, | ||
| createPackageValidationContext, | ||
| createYarnWorkspaceContext, | ||
| } from "./context.ts"; | ||
|
|
||
| export type { JSONValidatorOptions } from "./json.ts"; | ||
| export { | ||
| createJSONValidator, | ||
| getJSONPathSegments, | ||
| isJSONValidator, | ||
| compareValues, | ||
| setDefaultValidationOptions, | ||
| } from "./json.ts"; | ||
|
|
||
| export { | ||
| findPackageInfo, | ||
| getPackageInfoFromPath, | ||
| getPackageInfoFromWorkspaces, | ||
| } from "./package.ts"; | ||
|
|
||
| export type { | ||
| JSONValue, | ||
| JSONValidationResult, | ||
| JSONValidator, | ||
| JSONValuePath, | ||
| GetPackageValue, | ||
| ObjectValueAccessors, | ||
| PackageContext, | ||
| PackageValidationContext, | ||
| PackageInfo, | ||
| PackageValueAccessors, | ||
| } from "./types.ts"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
Record<symbol, T>instead ofobjecthere to avoid casting? I've managed to reduce it to 1 casting with this:friendlyName: string, initialize: (pkgInfo: PackageInfo) => T ): GetPackageValue<T> { + return createValueLoader<T, PackageInfo<T>>(friendlyName, initialize); +} + +/** + * Helper function to create a typed accessor function for getting and storing information + * in any object. This can be whatever you want, the key is only created and stored in + * the generated function so there are no collisions. + * + * @param friendlyName name used to create a symbol key for the package info + * @param initialize function used to initialize the value stored in the key + * @returns a function to retrieve the value from the object, if unset the initialize function is called + */ +export function createValueLoader<T, TObj extends Record<symbol, T>>( + friendlyName: string, + initialize: (obj: TObj) => T +): (obj: TObj) => T { const symbolKey = Symbol(friendlyName); - return (pkgInfo: PackageInfo) => { - if (!(symbolKey in pkgInfo)) { - pkgInfo[symbolKey] = initialize(pkgInfo); + return (obj: TObj) => { + if (!Object.hasOwn(obj, symbolKey)) { + (obj as Record<symbol, T>)[symbolKey] = initialize(obj); } - return pkgInfo[symbolKey] as T; + return obj[symbolKey]; }; }And the following changes in
types.ts:The final cast can't be removed because of TS2862 Type 'T' is generic and can only be indexed for reading. Which I guess is a limitation in TypeScript itself.