UNPKG

changelog-guru

Version:
143 lines (142 loc) 7.33 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var _State_authors, _State_changes, _State_commits, _State_sections; import { Dependencies, Restriction } from 'package-json-helper/types/package'; import TaskTree from 'tasktree-cli'; import { findSame, isSame, unify } from '../utils/text.js'; import { Exclusion } from './Config.js'; import Author from './entities/Author.js'; import Commit from './entities/Commit.js'; import { ChangeLevel } from './entities/Entity.js'; import Section, { SectionOrder, SectionPosition } from './entities/Section.js'; class State { constructor(currentLicense, previousLicense) { _State_authors.set(this, new Map()); _State_changes.set(this, new Map()); _State_commits.set(this, new Map()); _State_sections.set(this, []); this.currentLicense = currentLicense; this.previousLicense = previousLicense; this.hasChangedLicense = !previousLicense || !!currentLicense.localeCompare(previousLicense); } get sections() { return __classPrivateFieldGet(this, _State_sections, "f"); } get authors() { return [...__classPrivateFieldGet(this, _State_authors, "f").values()].filter(Author.filter).sort(Author.compare); } get commits() { return [...__classPrivateFieldGet(this, _State_commits, "f").values()].filter(Commit.filter).sort(Commit.compare); } get changesLevels() { const engines = __classPrivateFieldGet(this, _State_changes, "f").get(Dependencies.Engines); const os = __classPrivateFieldGet(this, _State_changes, "f").get(Restriction.OS); const cpu = __classPrivateFieldGet(this, _State_changes, "f").get(Restriction.CPU); let major = 0; let minor = 0; let patch = 0; if (engines?.length || os?.length || cpu?.length) { major++; } else { __classPrivateFieldGet(this, _State_commits, "f").forEach(commit => { switch (commit.level) { case ChangeLevel.Major: major++; break; case ChangeLevel.Minor: minor++; break; case ChangeLevel.Patch: patch++; break; default: TaskTree.fail(`Incompatible ChangeLevel - {bold ${commit.level}}`); } }); } return [major, minor, patch]; } addChanges(type, changes) { __classPrivateFieldGet(this, _State_changes, "f").set(type, changes); } addCommit(commit) { if (!__classPrivateFieldGet(this, _State_commits, "f").has(commit.name)) { const { author } = commit; __classPrivateFieldGet(this, _State_commits, "f").set(commit.name, commit); if (__classPrivateFieldGet(this, _State_authors, "f").has(author.name)) { author.contribute(); } else { __classPrivateFieldGet(this, _State_authors, "f").set(author.name, author); } } } addSection(options) { const { name, position = SectionPosition.Group, order = SectionOrder.Default, emoji } = options; let section = this.findSection(name); if (!section && unify(name)) { __classPrivateFieldGet(this, _State_sections, "f").push((section = new Section({ name, position, order, emoji }))); } return section; } findSection(name) { return __classPrivateFieldGet(this, _State_sections, "f").find((section) => isSame(section.name, name)); } getChanges(type) { return [...(__classPrivateFieldGet(this, _State_changes, "f").get(type) ?? [])]; } ignore(exclusions) { const callbacks = { [Exclusion.AuthorLogin]: (rules) => __classPrivateFieldGet(this, _State_authors, "f").forEach(author => (author.isIgnored = rules.includes(author.login))), [Exclusion.CommitType]: (rules) => __classPrivateFieldGet(this, _State_commits, "f").forEach(commit => (commit.isIgnored = !!findSame(commit.type, rules))), [Exclusion.CommitScope]: (rules) => __classPrivateFieldGet(this, _State_commits, "f").forEach(commit => (commit.isIgnored = !!(commit.scope && findSame(commit.scope, rules)))), [Exclusion.CommitSubject]: (rules) => __classPrivateFieldGet(this, _State_commits, "f").forEach(commit => (commit.isIgnored = rules.some(item => commit.subject.includes(item)))), }; exclusions.forEach(([type, rules]) => { if (callbacks[type]) { callbacks[type](rules); } else { TaskTree.fail(`Unacceptable entity exclusion type - {bold ${type}}`); } }); } modify(rules) { const task = TaskTree.add('Formate changelog structure...'); rules.forEach(rule => rule.prepare && rule.prepare({ context: this })); rules.forEach(rule => { if (rule.parse) this.commits.forEach(commit => rule.parse && rule.parse({ commit, context: this })); if (rule.modify) rule.modify({ task, context: this }); }); const subtask = task.add('Bringing the section tree to a consistent state...'); const sections = this.sections.sort(Section.compare); const relations = new Map(); sections.forEach(section => { section.assign(relations, section.isGroup ? SectionPosition.Subsection : undefined); }); __classPrivateFieldSet(this, _State_sections, sections.filter(section => Section.filter(section) && !section.isSubsection).sort(Section.compare), "f"); subtask.complete(); task.complete('Changelog structure is formate!', true); } updateCommitsChangeLevel(types) { __classPrivateFieldGet(this, _State_commits, "f").forEach(commit => { const [, level] = types.find(([name]) => isSame(commit.type, name)) ?? []; if (level) commit.level = level; }); } } _State_authors = new WeakMap(), _State_changes = new WeakMap(), _State_commits = new WeakMap(), _State_sections = new WeakMap(); export default State;