Script Policies
The scripts policy domain validates the scripts field of package.json files across your workspace.
Each key under scripts is the name of a package script. Its value is either a ScriptPolicy or an array of ScriptPolicys.
ScriptPolicy
ScriptPolicy extends the shared Policy, so it inherits its options.
| Option | Type | Default | Description |
|---|---|---|---|
command | string | RegExp | function | — | Expected command (exact string, RegExp, or predicate) |
allowCustomCommands | string[] | [] | Workspace packages allowed to use a different command |
autofix | boolean | false | Automatically fix missing or mismatched scripts with moniq fix (exact string commands only) |
Examples
presence
Control whether a script must exist, may exist, or must not exist.
"required"— the script must exist."optional"— the script may exist."forbidden"— the script must not exist.
export default defineConfig({
scripts: {
build: {
presence: "required",
},
},
});include
Apply a policy only to selected packages.
export default defineConfig({
scripts: {
build: {
include: ["packages/*"],
command: "tsup",
},
},
});exclude
Exclude specific packages after matching include.
export default defineConfig({
scripts: {
build: {
include: ["*"],
exclude: ["packages/legacy"],
command: "tsup",
},
},
});command (string)
Require an exact command.
export default defineConfig({
scripts: {
build: {
command: "tsup",
},
},
});command (RegExp)
Match commands using a regular expression.
TIP
Remember to anchor your expression so the binary is matched rather than appearing somewhere later in the command.
export default defineConfig({
scripts: {
lint: {
command: /^eslint\b/,
},
},
});command (bin())
Match only the executable instead of the entire command. Arguments and flags may vary as long as the same binary is used.
import { defineConfig, bin } from "@udohjeremiah/moniq";
export default defineConfig({
scripts: {
lint: {
command: bin("eslint"),
},
},
});For example:
eslint .
eslint src --fix
eslint "src/**/*.ts"would all satisfy:
command: bin("eslint");allowCustomCommands
Allow selected packages to use a different command.
export default defineConfig({
scripts: {
build: {
command: "tsup",
allowCustomCommands: ["packages/legacy"],
},
},
});autofix
Autofixes are only available when command is an exact string.
TIP
Run moniq fix to apply available autofixes.
export default defineConfig({
scripts: {
build: {
command: "tsup",
autofix: true,
},
},
});severity
Use "warn" to report violations without failing the process.
export default defineConfig({
scripts: {
build: {
presence: "required",
severity: "warn",
},
},
});description
Displayed alongside diagnostics to explain why the policy exists.
export default defineConfig({
scripts: {
build: {
command: "tsup",
description: "All packages are built with tsup.",
},
},
});