Skip to content

Latest commit

History

History
75 lines (68 loc) 路 1.79 KB

my-version.md

File metadata and controls

75 lines (68 loc) 路 1.79 KB

Example plugin: my-version

This example reads a VERSION file, bumps it, and publishes to a package repository. It is only enabled if the ./VERSION file actually exists.

const { Plugin } = require('release-it');
const fs = require('fs');
const path = require('path');

const prompts = {
  publish: {
    type: 'confirm',
    message: context => `Publish version ${context.version} of ${context.name}?`
  }
};

class MyVersionPlugin extends Plugin {
  constructor(...args) {
    super(...args);
    this.registerPrompts(prompts);
    this.setContext({ versionFile: path.resolve('./VERSION') });
  }
  static isEnabled() {
    try {
      fs.accessSync('./VERSION');
      return true;
    } catch (err) {}
    return false;
  }
  init() {
    const data = fs.readFileSync(this.versionFile);
    const latestVersion = data.toString().trim();
    this.setContext({ latestVersion });
  }
  getPackageName() {
    return this.config.getContext('name');
  }
  getLatestVersion() {
    return this.getContext('latestVersion');
  }
  bump(version) {
    this.setContext({ version });
    fs.writeFileSync(this.getContext('versionFile'), version);
  }
  async release() {
    await this.step({ task: () => this.publish(), label: 'Publish with pkg-manager', prompt: 'publish' });
  }
  publish() {
    // <insert command to publish>, example: await this.exec('pkg-manager publish');
    this.isReleased = true;
  }
  afterRelease() {
    if (this.isReleased) {
      const name = this.getPackageName();
      const { version } = this.getContext();
      this.log.log(`馃敆 https://registry.example.org/${name}/${version}`);
    }
  }
}

module.exports = MyVersionPlugin;

To add this plugin to a project, use this configuration:

{
  "plugins": {
    "my-version": {
      "unused": "option"
    }
  }
}