All files / lib rtk.js

100% Statements 100/100
100% Branches 14/14
100% Functions 7/7
100% Lines 100/100

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1011x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 2x 2x 3x 2x 2x 3x 2x 2x 3x 3x 3x 1x 1x 1x  
"use strict";
/**
 * @module rtk
 */
import async from "async";
import rtkReleaseScheme from "./release-schemes/rtk.js";
import hclResourceType from "./resource-types/hcl.js";
import jsonResourceType from "./resource-types/json.js";
import keepAChangelogResourceType from "./resource-types/keep-a-changelog.js";
import makefileResourceType from "./resource-types/makefile.js";
import textResourceType from "./resource-types/text.js";
import tomlResourceType from "./resource-types/toml.js";
import yamlResourceType from "./resource-types/yaml.js";
 
/**
 * RTK is the enchilada, Release The Kraken!
 */
class RTK {
  /**
   * Construct RTK object which will manage a set of resources.
   *
   * @param {Array<Object>} resources - resources which contain version value,
   *   the first resource is used as the source of truth of current version
   * @param {Object} [opts] - optional settings
   * @param {Boolean} [opts.dryRun] - when true, no change will be made
   * @param {String} [opts.releaseIncrementType] - major, minor, or patch
   * @param {String} [opts.postReleaseIncrementType] - major, minor, or patch
   * @param {String} [opts.tagFormat] - SCM tag format, e.g. v{version}
   */
  constructor(resources, opts) {
    this.resources = resources || {};
    this.opts = opts || {};
    this.releaseSchemes = {
      rtk: rtkReleaseScheme,
    };
    this.resourceTypes = {
      hcl: hclResourceType,
      json: jsonResourceType,
      makefile: makefileResourceType,
      text: textResourceType,
      toml: tomlResourceType,
      yaml: yamlResourceType,
    };
    this.resourceTypes["keep-a-changelog"] = keepAChangelogResourceType;
  }
 
  /**
   * Execute release steps for the specified release scheme.
   *
   * @param {String} [releaseSchemeName=rtk] - release scheme name that
   *   defines the steps involved in a release
   * @param {String} [versionSchemeName=semver] - version scheme name that
   *   defines the release and post-release version values
   * @param {String} [scmSchemeName=git] - name of SCM used by the repository
   *   to be released
   * @param {Function} cb - standard cb(err, result) callback
   */
  release(releaseSchemeName, versionSchemeName, scmSchemeName, cb) {
    const self = this;
 
    releaseSchemeName = releaseSchemeName || "rtk";
    versionSchemeName = versionSchemeName || "semver";
    scmSchemeName = scmSchemeName || "git";
 
    let releaseScheme;
 
    function versionTask(cb) {
      // uses first resource as the source of truth and the correct pre-release version
      const preReleaseResource = self.resources[0];
 
      function versionCb(err, result) {
        if (!err) {
          const ReleaseSchemeClass = self.releaseSchemes[releaseSchemeName];
          releaseScheme = new ReleaseSchemeClass(
            versionSchemeName,
            scmSchemeName,
            result,
            self.opts,
          );
        }
        cb(err, result);
      }
      const resourceType = self.resourceTypes[preReleaseResource.type];
      resourceType.getVersion(preReleaseResource, versionCb);
    }
    function preTask(cb) {
      releaseScheme.pre(self.resources, self.opts, cb);
    }
    function releaseTask(cb) {
      releaseScheme.release(self.resources, self.opts, cb);
    }
    function postTask(cb) {
      releaseScheme.post(self.resources, self.opts, cb);
    }
 
    async.series([versionTask, preTask, releaseTask, postTask], cb);
  }
}
 
export { RTK as default };