# JS API

> The version() function and the Interrupt error of the bump package, every option, the log callback, the errors, and the types.

The `bump` package exports the function that the CLI runs. Use it to release from a script, with the same rules and the same result.

```ts
import { version, Interrupt } from 'bump';
```

The package runs on Bun 1.4 or later. See [Install](/install/).

## Example

```ts
import { version } from 'bump';

let result = await version('minor', {
  cwd: 'path/to/repo',
  dryrun: true,
  log: (line, kind) => console.log(kind, line),
});

console.log(result.units.map(unit => unit.tag));
```

For the repository of the [JSON output](/json/#example) example, this prints:

```
plan BUMP @acme/core 1.2.0 → 1.3.0  (changed)
plan BUMP @acme/cli 0.4.1 → 0.4.2  (dependent)
plan EDIT app devDependencies["@acme/core"] ^1.2.0 → ^1.3.0
plan EDIT @acme/cli dependencies["@acme/core"] ^1.2.0 → ^1.3.0
done DONE Dry run; no files were written
[ "@acme/core@1.3.0", "@acme/cli@0.4.2" ]
```

## version(input, options?)

Returns: `Promise<Result>`

Finds the units that changed, computes their new versions, and writes, commits, and tags the release. It resolves with the [Result](/json/) object, the same object that `--json` prints.

### input

Type: `Type | string`

A type (`major`, `minor`, `patch`, `premajor`, `preminor`, `prepatch`, `prerelease`) or an exact version, such as `2.0.0`. It is the first argument of the CLI. See [Type or version](/cli/#type-or-version).

### options

Type: `Options`

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `cwd` | `string` | `'.'` | The directory to start from. The same as `--cwd`. |
| `targets` | `string[]` | `[]` | Package or group names to release. Independent mode only. See [Targets](/cli/#targets). |
| `all` | `boolean` | `false` | Release every unit, changed or not. The same as `--all`. |
| `preid` | `string` | | The preid of a new prerelease line. The same as `--preid`. |
| `adopt` | `boolean` | `false` | Release the units that have no tag, as changed. The same as `--adopt`. |
| `dryrun` | `boolean` | `false` | Do every check, and write nothing. The same as `--dryrun`. |
| `log` | `function` | | Gets each log line and its kind. See [Log](#log). |
| `color` | `boolean` | `false` | Add ANSI colors to the log lines. |
| `independent` | `boolean` | | Replaces [`independent`](/config/#independent) of the config file. |
| `packages` | `string \| string[]` | | Replaces [`packages`](/config/#packages) of the config file. |
| `groups` | `Record<string, string[]>` | | Replaces [`groups`](/config/#groups) of the config file. |
| `tag` | `string` | | Replaces [`tag`](/config/#tag) of the config file. |
| `message` | `string` | | Replaces [`message`](/config/#message) of the config file. |

bump still reads the config file. An option replaces the same key of the file, and the other keys of the file stay. The same rules apply to an option as to its key in the file.

`options.preid` is the `--preid` flag, not the `preid` key of the config. It wins over the config, and it also replaces the preid of a prerelease line that continues. To set the default preid, use the [config file](/config/#preid).

There is no `json` or `quiet` option. The function always returns the result, and prints nothing without `log`.

## Log

With `log`, bump calls it once for each line, with the line and its kind:

| Kind | Lines |
| --- | --- |
| `'plan'` | The `BUMP` and `EDIT` lines, and the member lines below a `BUMP` line. |
| `'done'` | The `DONE` line, and the `Tagged` line below it. |
| `'warn'` | Each line of a `WARN` block. |

The CLI prints `plan` and `done` lines on stdout and `warn` lines on stderr. To do the same:

```ts
await version('patch', {
  log: (line, kind) => {
    if (kind === 'warn') console.error(line);
    else console.log(line);
  },
});
```

The lines have no color by default. With `color: true`, they have the ANSI colors of the CLI. See [Color](/cli/#color).

Errors do not go to `log`. bump throws them.

## Errors

bump throws an `Error` with the same message that the CLI prints after `FAIL`. See [Errors](/errors/). The message can have more than one line:

```ts
try {
  await version('minor', { cwd: 'path/to/repo' });
} catch (err) {
  console.error((err as Error).message);
  process.exitCode = 1;
}
```

Some messages name a CLI flag or `bump.json`, such as `Cannot use --preid with "minor"` for `version('minor', { preid: 'rc' })`. The flag is the option of the same name.

An error in an option that replaces a config key ends with `in the options`:

```ts
await version('minor', { independent: true, tag: 'v{version}' });
// Error: Missing "{name}" placeholder in "tag" in the options
```

The [no-tag error](/errors/#no-tag-found) ends with a `bump` command to run again with `--adopt`. The API builds that command from the options:

```
To use the manifest version as the base, run again with --adopt:
  bump minor --dryrun --cwd /tmp/acme --adopt   →   1.3.0
```

When a step of the release fails, bump undoes the release before it throws. The message then ends with the line `The release was undone: no files, commits, or tags changed.` See [Undo](/errors/#undo).

## Interrupt

`Interrupt` is the error of a release that a signal stopped. It extends `Error`, and its `signal` is `'SIGINT'` or `'SIGTERM'`.

While bump writes the files, commits, and tags, it listens for `SIGINT` and `SIGTERM`. A signal does not stop the process then. bump finishes the current step, undoes the release, and throws an `Interrupt`. Your code decides what to do next:

```ts
import { version, Interrupt } from 'bump';

try {
  await version('patch');
} catch (err) {
  if (err instanceof Interrupt) {
    console.error(err.message);
    process.exit(err.signal === 'SIGINT' ? 130 : 143);
  }
  throw err;
}
```

The message is `Stopped by SIGINT` or `Stopped by SIGTERM`, then the undo line. The CLI exits with `130` or `143`. See [Exit codes](/cli/#exit-codes).

## Types

```ts
export type Type = 'major' | 'minor' | 'patch' | 'premajor' | 'preminor' | 'prepatch' | 'prerelease';

export type Ecosystem = 'npm' | 'cargo';

export type Kind = 'plan' | 'done' | 'warn';

export type Reason = 'target' | 'first' | 'adopted' | 'changed' | 'all' | 'dependent';

export type Config = {
  $schema?: string;
  independent?: boolean;
  packages?: string | [string, ...string[]];
  groups?: Record<string, [string, ...string[]]>;
  preid?: string;
  tag?: string;
  message?: string;
};

export type Options = Omit<Config, '$schema' | 'preid'> & {
  cwd?: string;
  targets?: string[];
  all?: boolean;
  preid?: string;
  adopt?: boolean;
  dryrun?: boolean;
  log?: (line: string, kind: Kind) => void;
  color?: boolean;
};

export type Result = {
  dryrun: boolean;
  type: Type | 'exact';
  commit: string | null;
  message: string | null;
  units: Result.Unit[];
  ranges: Result.Range[];
  files: string[];
};

export namespace Result {
  type Unit = {
    name: string | null;
    type: Type | 'exact';
    tag: string;
    from: string;
    to: string;
    reason: Reason;
    packages: Package[];
  };

  type Package = {
    name: string;
    ecosystem: Ecosystem;
    dir: string;
    publish: boolean;
  };

  type Range = {
    file: string;
    package: string | null;
    dependency: string;
    field: string;
    from: string;
    to: string;
  };
}

export class Interrupt extends Error {
  signal: 'SIGINT' | 'SIGTERM';
  constructor(signal: 'SIGINT' | 'SIGTERM');
}

export function version(input: Type | string, options?: Options): Promise<Result>;
```

`Config` is the shape of the [config file](/config/). Use it to type a config that you build in code:

```ts
import type { Config } from 'bump';

let config: Config = { independent: true, tag: '{name}@{version}' };
```

See [JSON output](/json/#fields) for each field of `Result`.

## Package exports

| Export | File |
| --- | --- |
| `bump` | `index.js`, with the types in `index.d.ts` |
| `bump/schema.json` | The JSON schema of the config file. See [Schema file](/config/#schema-file). |
| `bump/package.json` | The package manifest. |
