Skip to content

Configuration Hooks

All of the following hooks in some way deal with detecting, modifying and validating auto's configuration.

beforeRun

Happens before any command is run. This is a great place to check for platform specific secrets such as a npm token.

auto.hooks.beforeRun.tapPromise("NPM", async (config) => {
  if (!process.env.NPM_TOKEN) {
    auto.logger.log.warn("NPM Token is needed for the NPM plugin!");
  }
});
auto.hooks.beforeRun.tapPromise("NPM", async (config) => {
  if (!process.env.NPM_TOKEN) {
    auto.logger.log.warn("NPM Token is needed for the NPM plugin!");
  }
});

modifyConfig

Modify what is in the config. You must return the config in this hook. If you plugins requires some option to be set this is the place to do it.

auto.hooks.modifyConfig.tap("test", (config) => {
  config.labels.released = {
    name: "released",
    description: "This issue/pull request has been released",
  };

  return config;
});
auto.hooks.modifyConfig.tap("test", (config) => {
  config.labels.released = {
    name: "released",
    description: "This issue/pull request has been released",
  };

  return config;
});

Other examples:

  • cocoapods - Sets noVersionPrefix to true for independent monorepos
  • npm - Sets noVersionPrefix to true for independent monorepos
  • release - Adds released label

getAuthor

Get git author to commit with. Typically from a package distribution description file.

auto.hooks.getAuthor.tapPromise("NPM", async () => {
  const { author } = JSON.parse(await readFile("package.json", "utf-8"));

  if (author) {
    return author;
  }
});
auto.hooks.getAuthor.tapPromise("NPM", async () => {
  const { author } = JSON.parse(await readFile("package.json", "utf-8"));

  if (author) {
    return author;
  }
});

getPreviousVersion

Get the previous version. Typically from a package distribution description file. This hooks is required for plugin that facilitate publishing.

auto.hooks.getPreviousVersion.tapPromise("NPM", async () => {
  const { version } = JSON.parse(await readFile("package.json", "utf-8"));

  if (version) {
    return auto.prefixRelease(
      JSON.parse(await readFile("package.json", "utf-8")).version
    );
  }
});
auto.hooks.getPreviousVersion.tapPromise("NPM", async () => {
  const { version } = JSON.parse(await readFile("package.json", "utf-8"));

  if (version) {
    return auto.prefixRelease(
      JSON.parse(await readFile("package.json", "utf-8")).version
    );
  }
});

getRepository

Get owner and repository for the project to automate releases for. Typically from a package distribution description file. Falls back to global git config author.

auto.hooks.getRepository.tapPromise('NPM', async () => {
  const owner = // get the owner from package.json
  const repo = // get the repo from package.json

  return {
    owner,
    repo
  }
});
auto.hooks.getRepository.tapPromise('NPM', async () => {
  const owner = // get the owner from package.json
  const repo = // get the repo from package.json

  return {
    owner,
    repo
  }
});

validateConfig

Validate how your plugin is configured. Make sure to account for the different ways you plugin can be included in an .autorc.

auto.hooks.validateConfig.tapPromise("test", (name, options) => {
  if (name === this.name || name === `auto-plugin-${this.name}`) {
    return; // your validation error. Can either be strings for { path, expectedType, value }
  }
});
auto.hooks.validateConfig.tapPromise("test", (name, options) => {
  if (name === this.name || name === `auto-plugin-${this.name}`) {
    return; // your validation error. Can either be strings for { path, expectedType, value }
  }
});

auto and it's plugins use io-ts to validate the options for a plugin. If you're using typescript this is a great way to define the options for your plugin.

// Types in TypeScript
interface Options {
  level?: string;
  user?: string;
}

// The equivalent io-ts code
import * as t from "io-ts";

const pluginOptions = t.partial({
  level: t.string,
  user: t.string,
});

export type Options = t.TypeOf<typeof pluginOptions>;
// Types in TypeScript
interface Options {
  level?: string;
  user?: string;
}

// The equivalent io-ts code
import * as t from "io-ts";

const pluginOptions = t.partial({
  level: t.string,
  user: t.string,
});

export type Options = t.TypeOf<typeof pluginOptions>;

Since your type information will now be available at runtime (in pluginOptions) you can use this to validate the configuration! To do this auto exposes a helper function to validate you plugins with the io-ts types.

import { validatePluginConfiguration } from "@auto-it/core";

auto.hooks.validateConfig.tapPromise("test", (name, options) => {
  if (name === this.name || name === `auto-plugin-${this.name}`) {
    return validatePluginConfiguration(this.name, pluginOptions, options);
  }
});
import { validatePluginConfiguration } from "@auto-it/core";

auto.hooks.validateConfig.tapPromise("test", (name, options) => {
  if (name === this.name || name === `auto-plugin-${this.name}`) {
    return validatePluginConfiguration(this.name, pluginOptions, options);
  }
});