@tinymce/beehive-flow
Version:
A CLI tool implementing the beehive flow git branching process
103 lines • 5.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkVersionFromFile = exports.checkVersion = exports.updateFromFile = exports.update = exports.parseFromFile = exports.parse = void 0;
const tslib_1 = require("tslib");
const function_1 = require("fp-ts/function");
const Arr = tslib_1.__importStar(require("fp-ts/Array"));
const E = tslib_1.__importStar(require("fp-ts/Either"));
const O = tslib_1.__importStar(require("fp-ts/Option"));
const KeepAChangelog = tslib_1.__importStar(require("keep-a-changelog"));
const luxon_1 = require("luxon");
const Version = tslib_1.__importStar(require("../core/Version"));
const Files = tslib_1.__importStar(require("../utils/Files"));
const PromiseUtils = tslib_1.__importStar(require("../utils/PromiseUtils"));
class CustomRelease extends KeepAChangelog.Release {
constructor(version, date, description) {
super(version, date, description);
// Add in "improved" and ensure the ordering
const changeTypes = [
['added', []],
['improved', []],
['changed', []],
['fixed', []],
['security', []],
['deprecated', []],
['removed', []]
];
this.changes = new Map(changeTypes);
}
improved(change) {
return this.addChange('improved', change);
}
toString(changelog) {
// Ensure new lines between the version header and section header
return super.toString(changelog).replace(/## (.+)\n###/g, '## $1\n\n###');
}
}
const hasAnySectionType = (release, types) => (0, function_1.pipe)(types, Arr.some((type) => release.changes.has(type) && release.changes.get(type).length > 0));
// Attempt to find the specific release if not fallback to finding the unreleased section
const findRelease = (changelog, version) => { var _a; return O.fromNullable((_a = changelog.findRelease(version)) !== null && _a !== void 0 ? _a : changelog.findRelease()); };
const parse = (content) => KeepAChangelog.parser(content, {
releaseCreator: (version, date, description) => new CustomRelease(version, date, description)
});
exports.parse = parse;
const parseFromFile = async (changelogFile) => {
const content = await Files.readFileAsString(changelogFile);
return (0, exports.parse)(content);
};
exports.parseFromFile = parseFromFile;
const update = (content, version) => {
const versionString = Version.versionToString(version);
const date = luxon_1.DateTime.now();
const dateString = date.toFormat('yyyy-MM-dd');
const changelog = (0, exports.parse)(content);
// Find the current version, if not find unreleased and update the version
const release = (0, function_1.pipe)(findRelease(changelog, versionString), O.filter((current) => !current.isEmpty()), O.map((current) => {
const isUnreleased = current.version === undefined;
if (isUnreleased) {
console.log(`Changing unreleased changelog header to ${versionString} - ${dateString}`);
current.setVersion(versionString);
changelog.addRelease(new CustomRelease());
}
else {
console.log(`Updating existing changelog header to ${versionString} - ${dateString}`);
}
current.setDate(date.toJSDate());
return current;
}));
return O.isSome(release) ? changelog.toString() : content;
};
exports.update = update;
const updateFromFile = async (changelogFile, version) => {
const content = await Files.readFileAsString(changelogFile);
const newContent = (0, exports.update)(content, version);
console.log(`Saving changes to ${changelogFile}`);
await Files.writeFile(changelogFile, newContent);
};
exports.updateFromFile = updateFromFile;
const checkVersion = (content, version) => {
const isPatch = version.patch !== 0;
const isMinor = version.minor !== 0;
const changelog = (0, exports.parse)(content);
return (0, function_1.pipe)(findRelease(changelog, Version.versionToString(version)), O.map((release) => {
if (isPatch && hasAnySectionType(release, ['added', 'improved'])) {
return E.left('Changelog contains an Added or Improved section for a patch release. This should be at least a minor.');
}
else if (isPatch && hasAnySectionType(release, ['removed'])) {
return E.left('Changelog contains a Removed section for a patch release. This should be at least a major.');
}
else if (isMinor && hasAnySectionType(release, ['removed'])) {
return E.left('Changelog contains a Removed section for a minor release. This should be at least a major.');
}
else {
return E.right(true);
}
}), O.getOrElse(() => E.right(false)));
};
exports.checkVersion = checkVersion;
const checkVersionFromFile = async (changelogFile, version) => {
const content = await Files.readFileAsString(changelogFile);
return PromiseUtils.eitherToPromise((0, exports.checkVersion)(content, version));
};
exports.checkVersionFromFile = checkVersionFromFile;
//# sourceMappingURL=Changelog.js.map