包文件→ .shtukbase/packages/std-keep-a-changelog/checks/kaclib.ts
checks/kaclib.ts
78 行, 3.3 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // Keep a Changelog 1.1.0 (https://keepachangelog.com/en/1.1.0/): reading CHANGELOG.md into releases and sections.
- import { existsSync } from 'node:fs';
- import { join } from 'node:path';
- import { list, readTextFile, ROOT, settings } from './lib/std.ts';
- export const PKG = 'std-keep-a-changelog';
- export const DEFAULTS = { files: ['CHANGELOG.md'], types: ['Added', 'Changed', 'Deprecated', 'Removed', 'Fixed', 'Security'], allowEmptySections: false };
- export type Config = typeof DEFAULTS;
- export function config(): Config {
- const c = settings('keep-a-changelog', DEFAULTS);
- return { files: list(c.files), types: list(c.types), allowEmptySections: c.allowEmptySections === true };
- }
- export type Section = { name: string; line: number; items: number };
- export type Release = { raw: string; line: number; version: string | null; date: string | null; yanked: boolean; unreleased: boolean; sections: Section[] };
- export type Log = { file: string; text: string; title: number; releases: Release[]; links: Set<string>; bad: { line: number; raw: string }[] };
- // `## [1.2.0] - 2026-11-28`, `## [1.2.0] - 2026-11-28 [YANKED]`, `## [Unreleased]`. The brackets only serve the
- // link references, and a typographic dash is the same separator: `## 1.2.0 — 2026-11-28` reads too.
- const REL = /^## (?:\[(?<v1>[^\]]+)\]|(?<v2>v?\d[^\s\[\]]*|[Uu]nreleased))(?:\s+[-–—]\s+(?<d>\S+))?(?<y>\s+\[YANKED\])?\s*$/;
- export function read(file: string): Log | null {
- if (!existsSync(join(ROOT, file))) return null;
- const text = readTextFile(file, 8 * 1024 * 1024) ?? '';
- const log: Log = { file, text, title: 0, releases: [], links: new Set(), bad: [] };
- let fence = false;
- let cur: Release | null = null;
- let sec: Section | null = null;
- text.split('\n').forEach((l, i) => {
- const n = i + 1;
- if (/^\s*(```|~~~)/.test(l)) fence = !fence;
- if (fence) return;
- if (/^# /.test(l) && !log.title) log.title = n;
- const link = /^\[([^\]]+)\]:\s*\S+/.exec(l);
- if (link) log.links.add(link[1].trim().toLowerCase());
- if (/^## /.test(l)) {
- sec = null;
- const m = REL.exec(l.trimEnd());
- if (!m?.groups) {
- log.bad.push({ line: n, raw: l.trim() });
- cur = null;
- return;
- }
- const v = (m.groups.v1 ?? m.groups.v2 ?? '').trim();
- const unreleased = v.toLowerCase() === 'unreleased';
- cur = { raw: l.trim(), line: n, version: unreleased ? null : v, date: m.groups.d ?? null, yanked: !!m.groups.y, unreleased, sections: [] };
- log.releases.push(cur);
- return;
- }
- if (/^### /.test(l) && cur) {
- sec = { name: l.slice(4).trim(), line: n, items: 0 };
- (cur as Release).sections.push(sec);
- return;
- }
- if (sec && l.trim() && !/^#/.test(l)) (sec as Section).items++;
- });
- return log;
- }
- /** A real calendar day written YYYY-MM-DD (ISO 8601). */
- export function isoDay(d: string): boolean {
- if (!/^\d{4}-\d{2}-\d{2}$/.test(d)) return false;
- const t = new Date(`${d}T00:00:00Z`);
- return !Number.isNaN(t.getTime()) && t.toISOString().slice(0, 10) === d;
- }
- /** Each changelog of the settings, or a finding line when it is missing. */
- export function logs(cfg: Config): { logs: Log[]; missing: string[] } {
- const out: Log[] = [];
- const missing: string[] = [];
- for (const f of cfg.files) {
- const l = read(f);
- if (l) out.push(l);
- else missing.push(f);
- }
- return { logs: out, missing };
- }