Class PackageJsonHelper
java.lang.Object
org.openrewrite.javascript.internal.PackageJsonHelper
Shared utilities for npm dependency recipes operating on package.json files
and their associated lock files.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.openrewrite.json.tree.Json.DocumentaddDependency(org.openrewrite.json.tree.Json.Document doc, String name, String version, String scope) Addname: versionto the given scope object insidedoc.static org.openrewrite.json.tree.Json.DocumentchangeDependency(org.openrewrite.json.tree.Json.Document doc, String oldName, String newName, @Nullable String newVersion, @Nullable String scope) static PatterncompileGlobPattern(String glob) static PathcorrespondingPackageJsonPath(Path lockFilePath) Map a lock file path to the siblingpackage.json.editAndRegenerate(org.openrewrite.SourceFile packageJson, Function<org.openrewrite.json.tree.Json.Document, org.openrewrite.json.tree.Json.Document> editFn, @Nullable String capturedLockContent, @Nullable Map<String, String> configFiles) Apply a recipe-specific edit to a package.json, refresh its declared-deps marker, and (when the marker carries aand a lock was captured at scan time) regenerate the lock file content viainvalid reference
package managerLockFileRegeneration.static @Nullable org.openrewrite.SourceFilegetLiveTree(org.openrewrite.ExecutionContext ctx, Path packageJsonPath) static booleanisLockFile(String basename) True when the basename matches one of the recognised npm-ecosystem lock files.static org.openrewrite.SourceFileoverlayResolvedDeps(org.openrewrite.SourceFile pkg, String lockContent, NodeResolutionResult.PackageManager pm) Re-deriveresolvedDependencieson the marker by parsing the given lock-file content.static voidputLiveTree(org.openrewrite.ExecutionContext ctx, Path packageJsonPath, org.openrewrite.SourceFile tree) static org.openrewrite.SourceFilerefreshMarker(org.openrewrite.SourceFile packageJson) Re-derive thedependencies/devDependencies/etc.static @Nullable LockFileRegeneration.ResultregenerateLockContent(org.openrewrite.SourceFile packageJson, @Nullable String capturedLockContent, @Nullable Map<String, String> configFiles) static org.openrewrite.json.tree.Json.DocumentremoveDependency(org.openrewrite.json.tree.Json.Document doc, String name, Set<String> scopes) Remove the named dependency from each given scope indoc.static org.openrewrite.json.tree.Json.DocumentreparseJson(org.openrewrite.json.tree.Json.Document original, String newContent) static org.openrewrite.SourceFilereparseLock(org.openrewrite.SourceFile original, String newContent) Reparse a regenerated lock file's content, dispatching by the runtime type of the original SourceFile.static org.openrewrite.text.PlainTextreparsePlainText(org.openrewrite.text.PlainText original, String newContent) static org.openrewrite.yaml.tree.Yaml.DocumentsreparseYaml(org.openrewrite.yaml.tree.Yaml.Documents original, String newContent) Serialize the marker's npmrc configs into aMap<filename, content>suitable to seed a temp directory before running the package manager.static org.openrewrite.json.tree.Json.DocumentupgradeTransitive(org.openrewrite.json.tree.Json.Document doc, NodeResolutionResult.PackageManager pm, String name, String newVersion, @Nullable List<DependencyPathSegment> path) Apply a transitive-dependency override todocfor the given package manager.static org.openrewrite.json.tree.Json.DocumentupgradeVersion(org.openrewrite.json.tree.Json.Document doc, List<MatchedDependency> matched, String newVersion)
-
Constructor Details
-
PackageJsonHelper
public PackageJsonHelper()
-
-
Method Details
-
isLockFile
True when the basename matches one of the recognised npm-ecosystem lock files. -
correspondingPackageJsonPath
Map a lock file path to the siblingpackage.json. -
getLiveTree
public static @Nullable org.openrewrite.SourceFile getLiveTree(org.openrewrite.ExecutionContext ctx, Path packageJsonPath) -
putLiveTree
public static void putLiveTree(org.openrewrite.ExecutionContext ctx, Path packageJsonPath, org.openrewrite.SourceFile tree) -
serializeConfigFiles
Serialize the marker's npmrc configs into aMap<filename, content>suitable to seed a temp directory before running the package manager. Returnsnullif there are no configs. -
reparseJson
public static org.openrewrite.json.tree.Json.Document reparseJson(org.openrewrite.json.tree.Json.Document original, String newContent) -
reparseYaml
public static org.openrewrite.yaml.tree.Yaml.Documents reparseYaml(org.openrewrite.yaml.tree.Yaml.Documents original, String newContent) -
reparsePlainText
public static org.openrewrite.text.PlainText reparsePlainText(org.openrewrite.text.PlainText original, String newContent) -
reparseLock
public static org.openrewrite.SourceFile reparseLock(org.openrewrite.SourceFile original, String newContent) Reparse a regenerated lock file's content, dispatching by the runtime type of the original SourceFile. -
refreshMarker
public static org.openrewrite.SourceFile refreshMarker(org.openrewrite.SourceFile packageJson) Re-derive thedependencies/devDependencies/etc. lists on the marker by walking the modified document.resolvedDependenciesand other fields carry over unchanged from the existing marker. Returns the source file unchanged when no marker is present. -
overlayResolvedDeps
public static org.openrewrite.SourceFile overlayResolvedDeps(org.openrewrite.SourceFile pkg, String lockContent, NodeResolutionResult.PackageManager pm) Re-deriveresolvedDependencieson the marker by parsing the given lock-file content. Supports npm, Bun, yarn classic, yarn berry, and pnpm. For other PMs, returns the input unchanged. If parsing fails, throws — the caller (typicallyeditAndRegenerate(org.openrewrite.SourceFile, java.util.function.Function<org.openrewrite.json.tree.Json.Document, org.openrewrite.json.tree.Json.Document>, java.lang.String, java.util.Map<java.lang.String, java.lang.String>)) is expected to catch and surface the failure viaMarkup.warn(T, java.lang.Throwable). -
addDependency
public static org.openrewrite.json.tree.Json.Document addDependency(org.openrewrite.json.tree.Json.Document doc, String name, String version, String scope) Addname: versionto the given scope object insidedoc. If the scope does not exist it is created. Ifnameis already present the document is returned unchanged. -
removeDependency
public static org.openrewrite.json.tree.Json.Document removeDependency(org.openrewrite.json.tree.Json.Document doc, String name, Set<String> scopes) Remove the named dependency from each given scope indoc. If a scope ends up empty after removal, the scope member itself is dropped. Returns the document unchanged if no matching member was found in any scope. -
upgradeVersion
public static org.openrewrite.json.tree.Json.Document upgradeVersion(org.openrewrite.json.tree.Json.Document doc, List<MatchedDependency> matched, String newVersion) -
changeDependency
-
upgradeTransitive
public static org.openrewrite.json.tree.Json.Document upgradeTransitive(org.openrewrite.json.tree.Json.Document doc, NodeResolutionResult.PackageManager pm, String name, String newVersion, @Nullable List<DependencyPathSegment> path) Apply a transitive-dependency override todocfor the given package manager. Delegates toPackageJsonOverrides.applyOverride(org.openrewrite.json.tree.Json.Document, org.openrewrite.javascript.marker.NodeResolutionResult.PackageManager, java.lang.String, java.lang.String, java.util.List<org.openrewrite.javascript.internal.DependencyPathSegment>). -
compileGlobPattern
Compile a glob pattern (using*and?wildcards) into aPattern. Special regex characters in the glob are escaped before compiling. -
editAndRegenerate
public static PackageJsonHelper.EditAndRegenerateResult editAndRegenerate(org.openrewrite.SourceFile packageJson, Function<org.openrewrite.json.tree.Json.Document, org.openrewrite.json.tree.Json.Document> editFn, @Nullable String capturedLockContent, @Nullable Map<String, String> configFiles) Apply a recipe-specific edit to a package.json, refresh its declared-deps marker, and (when the marker carries aand a lock was captured at scan time) regenerate the lock file content viainvalid reference
package managerLockFileRegeneration. -
regenerateLockContent
public static @Nullable LockFileRegeneration.Result regenerateLockContent(org.openrewrite.SourceFile packageJson, @Nullable String capturedLockContent, @Nullable Map<String, String> configFiles)
-