pkgjson

package
v0.4.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2025 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package pkgjson handles package.json file reading, writing, and manipulation.

It provides a strongly-typed representation of package.json that preserves unknown fields during read/write operations and includes helpers for dependency management and remote package lookups.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatVersion

func FormatVersion(version string, useExactVersion bool) string

FormatVersion formats a version string with optional caret prefix. If useExactVersion is true, returns the version as-is. Otherwise, adds a caret (^) prefix.

func Write

func Write(fs afero.Fs, path string, pkg *PackageJSON) error

Write saves PackageJSON to disk with npm-standard field order, pretty-printed JSON & no HTML escaping. Dependencies are sorted alphabetically.

Types

type Author

type Author struct {
	Name  string `json:"name"`
	Email string `json:"email,omitempty"`
	URL   string `json:"url,omitempty"`
}

Author represents a package.json author or contributor. Can be used for author, contributors, and maintainers fields.

type DependencyMatcher

type DependencyMatcher func(name string) bool

DependencyMatcher is a function that determines whether a dependency should be updated.

type PackageJSON

type PackageJSON struct {
	// Strongly-typed fields for common package.json properties.
	Name             string            `json:"name,omitempty"`
	Description      string            `json:"description,omitempty"`
	License          string            `json:"license,omitempty"`
	Private          any               `json:"private,omitempty"` // Can be string or bool
	Type             string            `json:"type,omitempty"`
	Version          string            `json:"version,omitempty"`
	Scripts          map[string]any    `json:"scripts,omitempty"`
	Dependencies     map[string]string `json:"dependencies,omitempty"`
	DevDependencies  map[string]string `json:"devDependencies,omitempty"`
	PeerDependencies map[string]string `json:"peerDependencies,omitempty"`
	PackageManager   string            `json:"packageManager,omitempty"`
	Engines          map[string]any    `json:"engines,omitempty"`
	Workspaces       any               `json:"workspaces,omitempty"`
	Repository       any               `json:"repository,omitempty"`
	Keywords         []string          `json:"keywords,omitempty"`
	Author           any               `json:"author,omitempty"` // Can be string or object
	Contributors     any               `json:"contributors,omitempty"`
	Maintainers      any               `json:"maintainers,omitempty"`
	Homepage         string            `json:"homepage,omitempty"`
	Bugs             any               `json:"bugs,omitempty"`
	Funding          any               `json:"funding,omitempty"`
	Files            []string          `json:"files,omitempty"`
	Main             string            `json:"main,omitempty"`
	Module           string            `json:"module,omitempty"`
	Browser          any               `json:"browser,omitempty"`
	Bin              any               `json:"bin,omitempty"`
	Man              any               `json:"man,omitempty"`
	Directories      any               `json:"directories,omitempty"`
	Config           any               `json:"config,omitempty"`
	Pnpm             any               `json:"pnpm,omitempty"`
	Overrides        any               `json:"overrides,omitempty"`
	Resolutions      any               `json:"resolutions,omitempty"`
	// contains filtered or unexported fields
}

PackageJSON represents a package.json file structure. This type preserves all fields during read/write operations while providing strongly-typed access to common fields.

Unknown fields are automatically captured by marshmallow during unmarshaling and merged back during marshaling, ensuring complete preservation.

func FetchFromRemote

func FetchFromRemote(ctx context.Context, url string) (*PackageJSON, error)

FetchFromRemote fetches and parses a package.json from a remote URL.

func Read

func Read(fs afero.Fs, path string) (*PackageJSON, error)

Read parses package.json from disk and preserves unknown fields in raw.

func (*PackageJSON) HasAnyDependency

func (pkg *PackageJSON) HasAnyDependency(matcher DependencyMatcher) bool

HasAnyDependency checks if the package has any dependencies matching the matcher.

func (*PackageJSON) HasDependency

func (pkg *PackageJSON) HasDependency(name string) bool

HasDependency checks if the package has a specific dependency in any dependency type.

func (*PackageJSON) IsDevDependency

func (pkg *PackageJSON) IsDevDependency(name string) bool

IsDevDependency checks if a package is in devDependencies.

type PnpmConfig

type PnpmConfig struct {
	OnlyBuiltDependencies []string `json:"onlyBuiltDependencies,omitempty"`
}

PnpmConfig represents pnpm-specific configuration in package.json.

type UpdateResult

type UpdateResult struct {
	Updated     []string
	OldVersions map[string]string
}

UpdateResult contains information about what dependencies were updated.

func UpdateDependencies

func UpdateDependencies(
	pkg *PackageJSON,
	matcher DependencyMatcher,
	versionFormatter func(name, version string) string,
) *UpdateResult

UpdateDependencies updates dependencies that match the provided matcher function. It updates dependencies across all dependency types (dependencies, devDependencies, peerDependencies).

The versionFormatter function determines how to format the version string for each dependency. For example, regular dependencies might use "^1.0.0" while devDependencies use "1.0.0".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL