npm.star¶
Find package.json files and edit them as data, keeping key order and indentation.
These helpers edit package.json only. To refresh package-lock.json, follow
the Starlark step with a run step such as
npm install --package-lock-only --ignore-scripts.
load("@fleet//npm.star", "packages", "read_package", "write_package", "set_dependency", "set_package_manager", "set_npmrc")
| Function | What it does |
|---|---|
packages |
Lists the repo's package.json files. |
read_package |
Reads a package.json as a dict whose keys keep the file's order. |
write_package |
Writes a dict as a package.json, keeping the file's indentation. |
set_dependency |
Sets a dependency's version range in every package.json that already lists it. |
set_package_manager |
Sets the packageManager field Corepack uses, such as pnpm@9.12.0, where a package already has one. |
set_npmrc |
Sets a key=value setting in .npmrc, adding it, or the file, when missing. |
packages¶
Lists the repo's package.json files.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
Returns: The sorted paths of every package.json outside node_modules.
Example
Used in: node-engines
read_package¶
Reads a package.json as a dict whose keys keep the file's order.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
path |
The package.json to read. |
Returns: The decoded package.json.
Example
Used in: node-engines, node-pnpm-9
write_package¶
Writes a dict as a package.json, keeping the file's indentation.
The indentation the file already uses is detected; a new file gets two spaces. Key order is kept, so only what you changed shows in the diff.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
path |
The package.json to write. |
pkg |
The package, usually from read_package. |
Returns: True when the file changed.
Example
pkg = read_package(repo)
pkg["private"] = True
write_package(repo, "package.json", pkg)
# returns True
Used in: node-engines
set_dependency¶
Sets a dependency's version range in every package.json that already lists it.
A newer version already there is kept, so a campaign never lowers one.
Packages that do not depend on name are left alone, so this never adds
a dependency. package-lock.json is not touched; follow with a run step
such as npm install --package-lock-only --ignore-scripts.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
name |
The package name. |
version |
The new range, such as ^1.7.9. |
sections |
The dependency sections to look in. |
Returns: The list of paths that changed.
Example
Used in: changelog-entry, npm-package-bump
set_package_manager¶
Sets the packageManager field Corepack uses, such as pnpm@9.12.0, where a package already has one.
A newer version of the same package manager is kept.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
spec |
The package manager and version, such as pnpm@9.12.0 or yarn@4.5.0. |
path |
The package.json to edit. |
Returns: True when the file changed.
Example
Used in: node-pnpm-9
set_npmrc¶
Sets a key=value setting in .npmrc, adding it, or the file, when missing.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
key |
The setting, such as engine-strict or save-exact. |
value |
The value, such as true. |
path |
The .npmrc to edit. |
Returns: True when the file changed.
Example
Used in: node-pnpm-9