Skip to content

Commit

Permalink
Support increment option
Browse files Browse the repository at this point in the history
  • Loading branch information
casmith committed Dec 21, 2021
1 parent ff3124a commit 2918876
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
33 changes: 27 additions & 6 deletions calver-plugin-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,45 @@ const expect = require("chai").expect;
const CalverPlugin = require("./calver-plugin");

const formatMonth = (date) => "" + (date.getMonth() + 1);
const formatYear = (date) => "" + (date.getFullYear() - 2000);
const formatYear = (date, fullYear = false) => "" + (date.getFullYear() - (fullYear ? 0 : 2000));

function versionFromDate(date, micro = 0, fullYear = false) {
return `${formatYear(date, fullYear)}.${formatMonth(date)}.${micro}`;
}


describe('plugin', function () {
it('should bump calendar when incrementing in a new month', function () {

const before = new Date();
before.setMonth(before.getMonth() - 2);
console.log(before)
const latestVersion = `${formatYear(before)}.${formatMonth(before)}.0`;
console.log(latestVersion)
const latestVersion = versionFromDate(before);
const incrementedVersion = new CalverPlugin().getIncrementedVersion({latestVersion});

const now = new Date();
expect(incrementedVersion).to.equal(`${formatYear(now)}.${formatMonth(now)}.0`);
});
it('should bump micro when incrementing twice in the same month', function () {
const now = new Date();
const incrementedVersion = new CalverPlugin().getIncrementedVersion({latestVersion: `${formatYear(now)}.${formatMonth(now)}.0`});
expect(incrementedVersion).to.equal(`${formatYear(now)}.${formatMonth(now)}.1`);
const incrementedVersion = new CalverPlugin().getIncrementedVersion({latestVersion: versionFromDate(now)});
expect(incrementedVersion).to.equal(versionFromDate(now, 1));
});

it('should acccept an increment option', function () {
const before = new Date();
before.setMonth(before.getMonth() - 2);
const latestVersion = versionFromDate(before);
const plugin = new CalverPlugin();
plugin.setContext({increment: 'micro'});
const incrementedVersion = plugin.getIncrementedVersion({latestVersion});
expect(incrementedVersion).to.equal(versionFromDate(before, 1));
});
it('should accept a format option', function () {
const now = new Date();
const latestVersion = versionFromDate(now, 0, true);
const plugin = new CalverPlugin();
plugin.setContext({format: "YYYY.MM.MICRO"});
const incrementedVersion = plugin.getIncrementedVersion({latestVersion});
expect(incrementedVersion).to.equal(versionFromDate(now, 1, true));
});
});
23 changes: 17 additions & 6 deletions calver-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,40 @@

const Plugin = require('release-it/lib/plugin/Plugin'),
calver = require('calver'),
DEFAULT_FORMAT = 'YY.MM.MICRO';
DEFAULT_FORMAT = 'YY.MM.MICRO',
DEFAULT_INCREMENT = 'calendar',
FALLBACK_INCREMENT = 'micro';

class CalverPlugin extends Plugin {

getFormat() {
return this.getContext().format || DEFAULT_FORMAT;
}

getIncrementedVersion({latestVersion}) {
getInc() {
return this.getContext().increment || DEFAULT_INCREMENT;
}

getFallbackInc() {
return this.getContext().fallbackIncrement || FALLBACK_INCREMENT;
}

getIncrementedVersion(args) {
const {latestVersion} = args || {};
calver.init(this.getFormat());
try {
return calver.inc(this.getFormat(), latestVersion, 'calendar');
return calver.inc(this.getFormat(), latestVersion, this.getInc());
} catch (e) {
return calver.inc(this.getFormat(), latestVersion, 'micro');
return calver.inc(this.getFormat(), latestVersion, this.getFallbackInc());
}
}

getIncrementedVersionCI() {
return this.getIncrementedVersion(...arguments);
return this.getIncrementedVersion(...arguments);
}

getIncrement() {
return this.getIncrementedVersion(...arguments);
return this.getIncrementedVersion(...arguments);
}
}

Expand Down

0 comments on commit 2918876

Please sign in to comment.