Skip to content

File Policies

The files policy domain validates files, directories, and symlinks across your workspace.

Each key under files is the path of a file, directory, or symlink. Its value is either a FilePolicy or an array of FilePolicys.

FilePolicy

FilePolicy extends the shared Policy, so it inherits its options.

OptionTypeDefaultDescription
kind"file" | "directory" | "symlink"Expected filesystem item kind. When omitted, any filesystem item kind is accepted.
contentstring | RegExpExpected file contents (exact string or RegExp)
autofixbooleanfalseAutomatically create, remove, or overwrite files with moniq fix (string content only)

Examples

presence

Control whether a file must exist, may exist, or must not exist.

  • "required" — the file must exist.
  • "optional" — the file may exist.
  • "forbidden" — the file must not exist.
ts
export default defineConfig({
  files: {
    "README.md": {
      presence: "required",
    },
  },
});

include

Apply a policy only to selected packages.

ts
export default defineConfig({
  files: {
    "README.md": {
      include: ["packages/*"],
    },
  },
});

exclude

Exclude specific packages after matching include.

ts
export default defineConfig({
  files: {
    "tsconfig.json": {
      include: ["*"],
      exclude: ["packages/legacy"],
    },
  },
});

content (string)

Require file contents to exactly match a string.

ts
export default defineConfig({
  files: {
    ".nvmrc": {
      include: ["."],
      kind: "file",
      content: "24",
    },
  },
});

content (RegExp)

Match file contents using a regular expression.

ts
export default defineConfig({
  files: {
    "README.md": {
      kind: "file",
      content: /^# /,
    },
  },
});

autofix

Autofixes are limited to the following operations:

  • create missing presence: "required" files or directories (only when kind is explicitly set to "file" or "directory")
  • remove presence: "forbidden" items
  • overwrite files whose content is an exact string when their contents differ

TIP

Run moniq fix to apply available autofixes.

ts
export default defineConfig({
  files: {
    ".env": {
      presence: "forbidden",
      autofix: true,
    },
    ".nvmrc": {
      include: ["."],
      kind: "file",
      content: "24",
      autofix: true,
    },
  },
});

severity

Use "warn" to report violations without failing the process.

ts
export default defineConfig({
  files: {
    ".editorconfig": {
      include: ["."],
      presence: "required",
      severity: "warn",
    },
  },
});

description

Displayed alongside diagnostics to explain why the policy exists.

ts
export default defineConfig({
  files: {
    ".editorconfig": {
      include: ["."],
      description:
        "The workspace should use a single shared EditorConfig file.",
    },
  },
});

Released under the MIT License.