packageJson

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: MIT Imports: 9 Imported by: 1

README

packageJson

This is a go module to deal with package.json files, converting them into a struct. It also provides a bunch of methods to deal with such files such as retrieving dependencies information, or testing a given package.json satisfy a workspace dependency.

Check API doc at https://pkgo.dev/github.com/software-t-rex/packageJson

Fundings

If you want, you can sponsors my work on this project here: https://github.com/sponsors/malko

Documentation

Overview

Package to read a package.json file into a struct with some utilities functions around package.json

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidJson = errors.New("invalid json")
View Source
var ErrMismatchName error = fmt.Errorf("%wpackage name mismatch", ErrSatisfaction)
View Source
var ErrMismatchPath error = fmt.Errorf("%wpackage path mismatch dependency path", ErrSatisfaction)
View Source
var ErrMissingWorkspaceInfo error = fmt.Errorf("%wcannot satisfy workspace protocol dependency without a workspace directory", ErrSatisfaction)
View Source
var ErrOutsideWokspace error = fmt.Errorf("%wcan't satisfy a dependency being outside workspace", ErrSatisfaction)
View Source
var ErrParseVersion error = fmt.Errorf("%wpackage version check failed", ErrSatisfaction)
View Source
var ErrRemoteProtocol = fmt.Errorf("%wcannot satisfy remote protocol", ErrSatisfaction)
View Source
var ErrSatisfaction error = errors.New("SatisfyWorskpaceDep failed: ")

Functions

This section is empty.

Types

type PackageJSON

type PackageJSON struct {
	Name                 string                                   `json:"name,omitempty"`
	Version              string                                   `json:"version,omitempty"`
	Description          string                                   `json:"description,omitempty"`
	Keywords             []string                                 `json:"keywords,omitempty"`
	Homepage             string                                   `json:"homepage,omitempty"`
	License              string                                   `json:"license,omitempty"`
	Files                []string                                 `json:"files,omitempty"`
	Main                 string                                   `json:"main,omitempty"`
	Browser              string                                   `json:"browser,omitempty"`
	Directories          map[string]string                        `json:"directories,omitempty"`
	Scripts              map[string]string                        `json:"scripts,omitempty"`
	Dependencies         map[string]string                        `json:"dependencies,omitempty"`
	DevDependencies      map[string]string                        `json:"devDependencies,omitempty"`
	PeerDependencies     map[string]string                        `json:"peerDependencies,omitempty"`
	PeerDependenciesMeta map[string]packageJSONPeerDependencyMeta `json:"peerDependenciesMeta,omitempty"`
	OptionalDependencies map[string]string                        `json:"optionalDependencies,omitempty"`
	Engines              map[string]string                        `json:"engines,omitempty"`
	Os                   []string                                 `json:"os,omitempty"`
	Cpu                  []string                                 `json:"cpu,omitempty"`
	Private              bool                                     `json:"private,omitempty"`
	Workspaces           []string                                 `json:"workspaces,omitempty"`
	PackageManager       string                                   `json:"packageManager,omitempty"`

	// following properties can have multiple types or I was to lazy to dig in at the moment PR welcomes
	Author             any `json:"author,omitempty"`
	Bugs               any `json:"bugs,omitempty"`
	Contributors       any `json:"contributors,omitempty"`
	Funding            any `json:"funding,omitempty"`
	Bin                any `json:"bin,omitempty"`
	Man                any `json:"man,omitempty"`
	Repository         any `json:"repository,omitempty"`
	Config             any `json:"config,omitempty"`
	BundleDependencies any `json:"bundleDependencies,omitempty"`
	Overrides          any `json:"overrides,omitempty"`
	Resolutions        any `json:"resolutions,omitempty"` // resolutions is yarn overrides
	PublishConfig      any `json:"publishConfig,omitempty"`
	Pnpm               any `json:"pnpm,omitempty"` // specific to pnpm

	Dir string     `json:"-"` // this is the absolute path of the package.json directory
	Mu  sync.Mutex `json:"-"`
	// contains filtered or unexported fields
}

Struct that represents a package.json file content properties that can have multiple forms will have any type, you should type assert before working with them It add some specific properties: Dir is the absoulte path to directory where the package.json file is located Mu is a sync.Mutex for the package.json file in case you need manipulating it in a concurrency way

func Read

func Read(path string) (p *PackageJSON, err error)

func (*PackageJSON) FilterWorkspaceDirs

func (p *PackageJSON) FilterWorkspaceDirs(dirList []string, returnAbsPath bool) []string

filter given dirList if they match a workspace defined in this package.json dirs in dirList should be either relative path to this package.json or absolute path

func (*PackageJSON) GetAvailableTasks

func (p *PackageJSON) GetAvailableTasks() []string

return tasks names in scipts

func (*PackageJSON) GetDepencyInfoFor

func (p *PackageJSON) GetDepencyInfoFor(moduleName string) (PackageJsonDepInfo, bool)

lookup for a given module name is the package dependencies

func (*PackageJSON) GetMergedDependencies

func (p *PackageJSON) GetMergedDependencies() map[string]string

return dependencies from dependencies, devDependencies, optionalDependecies

func (*PackageJSON) HasTask

func (p *PackageJSON) HasTask(taskName string) bool

check a script task is set for that taskName

func (*PackageJSON) SatisfyWorskpaceDep

func (p *PackageJSON) SatisfyWorskpaceDep(dep PackageJsonDepInfo, workspacesRootDir string) (bool, error)

workspacesRootDir is optional and is used to determine that the package is within the root of the workspace directory. If not explicitly specified it will default to the directory of dep.FromFile. If false you should also get an error explaining the reason for the failure. You can also receive error while assuming to return true in certain cases You can test with !errors.Is(err, packageJson.ErrSatisfaction) to test for errors that may be unintended.

type PackageJsonDepInfo

type PackageJsonDepInfo struct {
	Name string
	// can be a version range or tarball, or git url as defined in package.json
	VersionRange string
	// one of dependecies, devDependecies, peerDependecies, optionalDependecies
	Key string
	// contains protocol if defined
	Protocol string
	// keep info on the package that require this
	FromName    string
	FromVersion string
	FromFile    string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳