Class PackageJsonHelper

java.lang.Object
org.openrewrite.javascript.internal.PackageJsonHelper

public class PackageJsonHelper extends Object
Shared utilities for npm dependency recipes operating on package.json files and their associated lock files.
  • Constructor Details

    • PackageJsonHelper

      public PackageJsonHelper()
  • Method Details

    • isLockFile

      public static boolean isLockFile(String basename)
      True when the basename matches one of the recognised npm-ecosystem lock files.
    • correspondingPackageJsonPath

      public static Path correspondingPackageJsonPath(Path lockFilePath)
      Map a lock file path to the sibling package.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

      public static @Nullable Map<String,String> serializeConfigFiles(NodeResolutionResult marker)
      Serialize the marker's npmrc configs into a Map<filename, content> suitable to seed a temp directory before running the package manager. Returns null if 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 the dependencies/devDependencies/etc. lists on the marker by walking the modified document. resolvedDependencies and 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-derive resolvedDependencies on 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 (typically editAndRegenerate(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 via Markup.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)
      Add name: version to the given scope object inside doc. If the scope does not exist it is created. If name is 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 in doc. 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

      public static org.openrewrite.json.tree.Json.Document changeDependency(org.openrewrite.json.tree.Json.Document doc, String oldName, String newName, @Nullable String newVersion, @Nullable String scope)
    • 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)
    • compileGlobPattern

      public static Pattern compileGlobPattern(String glob)
      Compile a glob pattern (using * and ? wildcards) into a Pattern. 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 a
      invalid reference
      package manager
      and a lock was captured at scan time) regenerate the lock file content via LockFileRegeneration.
    • regenerateLockContent

      public static @Nullable LockFileRegeneration.Result regenerateLockContent(org.openrewrite.SourceFile packageJson, @Nullable String capturedLockContent, @Nullable Map<String,String> configFiles)