PackageJson

PHP based interface for interacting with package.json files. This allows for modification of deps, devDeps, package name and workspaces.

 class System\Classes\Asset\PackageJson

Properties

protected $data : array

The contents of the package.json being modified

Methods

public __construct ($path = null)

Create a new instance with optional path, loads file if file already exists

Parameters
Property Description
$path
mixed
Returns
mixed

public addDependency (string $package, string $version, boolean $dev = false, boolean $overwrite = false) : static

Adds a dependency, supports adding to dependencies or devDependencies based on $dev and allows moving if $overwrite is set

Parameters
Property Description
$package
string
$version
string
$dev
boolean
$overwrite
boolean
Returns
static

public addIgnoredPackage (string $path) : static

Adds an ignored package, removes from workspaces if present

Parameters
Property Description
$path
string
Returns
static

public addScript (string $name, string $script) : static

Adds a script

Parameters
Property Description
$name
string
$script
string
Returns
static

public addWorkspace (string $path) : static

Adds a new workspace, removes from ignored workspaces if present

Parameters
Property Description
$path
string
Returns
static

public getContents () : array

Returns the package.json contents as an array

Returns
array

public getName () : string | null

Returns the package name if set

Returns
string | null

public getPath () : string | null

Returns the path of the package.json if set

Returns
string | null

public getScript (string $name) : string | null

Returns the value of a script by name

Parameters
Property Description
$name
string
Returns
string | null

public hasDependency (string $package) : bool

Checks if package.json has a dependency

Parameters
Property Description
$package
string
Returns
bool

public hasIgnoredPackage (string $path) : bool

Check if package is ignored

Parameters
Property Description
$path
string
Returns
bool

public hasScript (string $name) : bool

Returns if a script exists

Parameters
Property Description
$name
string
Returns
bool

public hasWorkspace (string $path) : bool

Checks if workspace package is set

Parameters
Property Description
$path
string
Returns
bool

public removeDependency (string $package) : static

Removes a package from both dependencies and devDependencies

Parameters
Property Description
$package
string
Returns
static

public removeIgnoredPackage (string $path) : static

Removes an ignored package

Parameters
Property Description
$path
string
Returns
static

public removeScript (string $name) : static

Removes a script by name

Parameters
Property Description
$name
string
Returns
static

public removeWorkspace (string $path) : static

Removes a workspace

Parameters
Property Description
$path
string
Returns
static

public save ($path = null) : integer

Saves the contents to a file, if the object was init'ed with a path it will save to the path, or can be overwritten with $path.

Parameters
Property Description
$path
mixed
Returns
integer

public setName (string | null $name) : static

Sets the package name, throws InvalidArgumentException on invalid name

Parameters
Property Description
$name
string | null
Returns
static
Copyright © 2024 Winter CMS