Add Scripts
In a monorepo, you may have multiple packages that share common scripts. Managing these scripts individually for each package can be time-consuming and error-prone. To streamline this process, you can use the monopkg script add
command to add a script to all packages in the monorepo at once. This ensures consistency across your packages and saves you from the repetitive task of manually adding the same script to each package.
INFO
The script add
command also can be used to update existing scripts, as it will overwrite the script if it already exists in a package.
By using the monopkg script add
command, you can easily maintain and update scripts across your entire monorepo. Whether you need to add build scripts, development scripts, or any other custom scripts, this command provides a convenient and efficient way to manage them centrally. This approach not only improves productivity but also reduces the risk of discrepancies and errors in your scripts.
Usage
This command allows you to add a script to all packages in the monorepo at once.
monopkg script add <name="script"...>
bun x monopkg script add <name="script"...>
npx monopkg script add <name="script"...>
yarn dlx monopkg script add <name="script"...>
Options
-f
,--filter
<packages...>
- Include specific packages.-e
,--exclude
<packages...>
- Exclude specific packages.-w
,--workspace
<workspaces...>
- Root workspaces of the packages.--delimiter
- Delimiter to separate the script name and script value (default:=
).
Examples
Basic Usage
Add build
script that run rimraf dist && tsup && publint
to all packages in the monorepo.
monopkg script add build="rimraf dist && tsup && publint"
bun x monopkg script add build="rimraf dist && tsup && publint"
npx monopkg script add build="rimraf dist && tsup && publint"
yarn dlx monopkg script add build="rimraf dist && tsup && publint"
Output
{
"scripts": {
"dev": "tsup --watch",
"build": "rimraf dist && tsup && publint"
}
}
Multiple Scripts
If you want to add multiple scripts at once, you can specify them as follows:
monopkg script add build="rimraf dist && tsup" test="jest" deploy="npm publish"
bun x monopkg script add build="rimraf dist && tsup" test="jest" deploy="npm publish"
npx monopkg script add build="rimraf dist && tsup" test="jest" deploy="npm publish"
yarn dlx monopkg script add build="rimraf dist && tsup" test="jest" deploy="npm publish"
Output
{
"scripts": {
"dev": "tsup --watch",
"build": "rimraf dist && tsup",
"test": "jest",
"deploy": "npm publish"
}
}
With Target Workspaces
Add dev
script that run tsup --watch
to all packages in the apps
and utils
workspace.
monopkg script add dev="tsup --watch" -w apps utils
bun x monopkg script add dev="tsup --watch" -w apps utils
npx monopkg script add dev="tsup --watch" -w apps utils
yarn dlx monopkg script add dev="tsup --watch" -w apps utils
With Exclusion Filters
Add test
script that run jest
to all packages in the monorepo, except package-a
.
monopkg script add test="jest" -e package-a
bun x monopkg script add test="jest" -e package-a
npx monopkg script add test="jest" -e package-a
yarn dlx monopkg script add test="jest" -e package-a