Skip to Content
harness-kit

format-on-edit

The built-in Policy format-on-edit, against edited files left unformatted: its defaults, its options and a configuration that writes it.

To learn what it guards against and see it at work, read the guide: Formatting.

Defaults

DefaultValue
On by defaultno: an entry turns it on
Fail modeopen
Timeout13000 ms
Eventsset by its options
Toolsevery tool, or as its options set
Agent typesevery agent, or as its options set

Options

OptionTypeDefaultDescription
deadlineMillisinteger—How long the formatter may run, in milliseconds, before it is stopped (10000 when absent).
formatters(object | object)[]requiredThe formatters in the order they are tried, each a preset or a command; the first whose glob takes the edited file and whose whenFile the folder holds runs, and no other.

Example

A configuration that writes every option, a comment above each:

import { defineConfig } from '@microeinhundert/harness-kit';

export default defineConfig({
  // The Policy entries, in the order their Decisions merge in.
  policies: [
    {
      // The Policy's name, unique in the configuration; messages and logs name the Policy by it.
      id: 'format-on-edit',
      // The constructor's options, which it decodes.
      options: {
        // How long the formatter may run, in milliseconds, before it is stopped (10000 when
        // absent).
        deadlineMillis: 1,
        // The formatters in the order they are tried, each a `preset` or a command; the first whose
        // glob takes the edited file and whose `whenFile` the folder holds runs, and no other.
        formatters: [
          {
            // The edited files the formatter takes, one glob or several, relative to the folder the
            // Policy works in.
            glob: 'src/**/*.ts',
            // What the note says to do when the formatter's program is not installed; `{install}`
            // stands for the project's install command.
            missingHint: 'run {install}; a fresh worktree has no node_modules',
            // The formatter command, a program and its arguments, with `{file}` for the edited
            // file's absolute path; a program with a `/` is a path relative to the folder, any
            // other is found on `PATH`.
            run: ['npx', 'prettier', '--write', '{file}'],
            // A file of the folder the Policy works in, without which the formatter is skipped.
            whenFile: '.prettierrc',
            // A prefix put before the command where a file is in the folder, such as `bundle exec`
            // with a `Gemfile.lock`.
            wrapIf: {
              // A file whose presence in the folder the Policy works in puts the prefix before the
              // command.
              file: 'Gemfile.lock',
              // The words put before the formatter command, a program and its arguments.
              prefix: ['bundle'],
            },
          },
        ],
      },
      // The name of the kit's Policy constructor that builds the Policy.
      use: 'format-on-edit',
    },
  ],
});

On this page