UNPKG

changelog-guru

Version:
128 lines (127 loc) 6.84 kB
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 __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 _Config_branch, _Config_exclusions, _Config_filePath, _Config_isInitialized, _Config_options, _Config_provider, _Config_rules, _Config_types; import { cosmiconfig } from 'cosmiconfig'; import deepmerge from 'deepmerge'; import path from 'path'; import TaskTree from 'tasktree-cli'; import { fileURLToPath } from 'url'; import { arrayMerge } from '../utils/merge.js'; import { ChangeLevel } from './entities/Entity.js'; import { Rule } from './rules/BaseRule.js'; import HighlightRule from './rules/HighlightRule.js'; import MarkRule from './rules/MarkRule.js'; import PackageStatisticRenderRule from './rules/PackageStatisticRenderRule.js'; import ScopeRenameRule from './rules/ScopeRenameRule.js'; import SectionGroupRule from './rules/SectionGroupRule.js'; export var Exclusion; (function (Exclusion) { Exclusion["AuthorLogin"] = "authorLogin"; Exclusion["CommitType"] = "commitType"; Exclusion["CommitScope"] = "commitScope"; Exclusion["CommitSubject"] = "commitSubject"; })(Exclusion || (Exclusion = {})); export var GitServiceProvider; (function (GitServiceProvider) { GitServiceProvider["GitHub"] = "github"; GitServiceProvider["GitLab"] = "gitlab"; })(GitServiceProvider || (GitServiceProvider = {})); export class Config { constructor(options) { _Config_branch.set(this, ''); _Config_exclusions.set(this, []); _Config_filePath.set(this, 'CHANGELOG.md'); _Config_isInitialized.set(this, false); _Config_options.set(this, void 0); _Config_provider.set(this, GitServiceProvider.GitHub); _Config_rules.set(this, []); _Config_types.set(this, []); if (options?.provider && !Object.values(GitServiceProvider).includes(options.provider)) { TaskTree.fail('Service provider not supported'); } __classPrivateFieldSet(this, _Config_options, options ?? {}, "f"); } get provider() { return __classPrivateFieldGet(this, _Config_provider, "f"); } get branch() { return __classPrivateFieldGet(this, _Config_branch, "f"); } get filePath() { return __classPrivateFieldGet(this, _Config_filePath, "f"); } get exclusions() { return __classPrivateFieldGet(this, _Config_exclusions, "f"); } get types() { return __classPrivateFieldGet(this, _Config_types, "f"); } get rules() { return __classPrivateFieldGet(this, _Config_rules, "f"); } get bump() { return !!__classPrivateFieldGet(this, _Config_options, "f").bump; } async init() { if (!__classPrivateFieldGet(this, _Config_isInitialized, "f")) { const dirname = path.dirname(fileURLToPath(import.meta.url)); const task = TaskTree.add('Reading configuration file...'); const explorer = cosmiconfig('changelog'); const baseConf = await explorer.load(path.join(dirname, '../../.changelogrc.default.yml')); const userConf = await explorer.search(); if (baseConf?.config && !baseConf.isEmpty) { const config = deepmerge(baseConf.config, userConf?.config ?? {}, { arrayMerge }); const filePath = path.relative(process.cwd(), userConf?.filepath ?? baseConf.filepath); __classPrivateFieldSet(this, _Config_types, this.getTypes(config.changes), "f"); __classPrivateFieldSet(this, _Config_rules, this.getRules(config.rules), "f"); __classPrivateFieldSet(this, _Config_provider, __classPrivateFieldGet(this, _Config_options, "f")?.provider ?? config.provider, "f"); __classPrivateFieldSet(this, _Config_branch, __classPrivateFieldGet(this, _Config_options, "f")?.branch ?? config.branch, "f"); __classPrivateFieldSet(this, _Config_filePath, __classPrivateFieldGet(this, _Config_options, "f")?.output ?? config.output.filePath, "f"); __classPrivateFieldSet(this, _Config_exclusions, Object.entries(config.output.exclude ?? {}).map(([name, rules]) => { if (!Object.values(Exclusion).includes(name)) { task.fail(`Unexpected exclusion name: {bold ${name}}`); } return [name, [...new Set(rules)]]; }), "f"); task.complete(`Configuration initialized: {bold ${filePath}}`); } else { task.fail('Default configuration file not found'); } } else { __classPrivateFieldSet(this, _Config_isInitialized, true, "f"); } } getRules(configs) { const map = { [Rule.Highlight]: HighlightRule, [Rule.Mark]: MarkRule, [Rule.PackageStatisticRender]: PackageStatisticRenderRule, [Rule.ScopeRename]: ScopeRenameRule, [Rule.SectionGroup]: SectionGroupRule, }; return [...Object.values(Rule)].map(rule => new map[rule](configs[rule])); } getTypes(changes) { const levels = Object.values(ChangeLevel); return Object.entries(changes).reduce((acc, [level, names]) => { if (!Array.isArray(names)) TaskTree.fail(`Names of change level "${level}" must be array`); if (!levels.includes(level)) TaskTree.fail(`Unexpected level "${level}" of changes`); names.forEach(name => acc.push([name, level])); return acc; }, []); } } _Config_branch = new WeakMap(), _Config_exclusions = new WeakMap(), _Config_filePath = new WeakMap(), _Config_isInitialized = new WeakMap(), _Config_options = new WeakMap(), _Config_provider = new WeakMap(), _Config_rules = new WeakMap(), _Config_types = new WeakMap();