ShtukbaseStandard
包文件→ .shtukbase/packages/std-keep-a-changelog/checks/kaclib.ts

checks/kaclib.ts

78 行, 3.3 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // Keep a Changelog 1.1.0 (https://keepachangelog.com/en/1.1.0/): reading CHANGELOG.md into releases and sections.
  2. import { existsSync } from 'node:fs';
  3. import { join } from 'node:path';
  4. import { list, readTextFile, ROOT, settings } from './lib/std.ts';
  5. export const PKG = 'std-keep-a-changelog';
  6. export const DEFAULTS = { files: ['CHANGELOG.md'], types: ['Added', 'Changed', 'Deprecated', 'Removed', 'Fixed', 'Security'], allowEmptySections: false };
  7. export type Config = typeof DEFAULTS;
  8. export function config(): Config {
  9. const c = settings('keep-a-changelog', DEFAULTS);
  10. return { files: list(c.files), types: list(c.types), allowEmptySections: c.allowEmptySections === true };
  11. }
  12. export type Section = { name: string; line: number; items: number };
  13. export type Release = { raw: string; line: number; version: string | null; date: string | null; yanked: boolean; unreleased: boolean; sections: Section[] };
  14. export type Log = { file: string; text: string; title: number; releases: Release[]; links: Set<string>; bad: { line: number; raw: string }[] };
  15. // `## [1.2.0] - 2026-11-28`, `## [1.2.0] - 2026-11-28 [YANKED]`, `## [Unreleased]`. The brackets only serve the
  16. // link references, and a typographic dash is the same separator: `## 1.2.0 — 2026-11-28` reads too.
  17. const REL = /^## (?:\[(?<v1>[^\]]+)\]|(?<v2>v?\d[^\s\[\]]*|[Uu]nreleased))(?:\s+[-–—]\s+(?<d>\S+))?(?<y>\s+\[YANKED\])?\s*$/;
  18. export function read(file: string): Log | null {
  19. if (!existsSync(join(ROOT, file))) return null;
  20. const text = readTextFile(file, 8 * 1024 * 1024) ?? '';
  21. const log: Log = { file, text, title: 0, releases: [], links: new Set(), bad: [] };
  22. let fence = false;
  23. let cur: Release | null = null;
  24. let sec: Section | null = null;
  25. text.split('\n').forEach((l, i) => {
  26. const n = i + 1;
  27. if (/^\s*(```|~~~)/.test(l)) fence = !fence;
  28. if (fence) return;
  29. if (/^# /.test(l) && !log.title) log.title = n;
  30. const link = /^\[([^\]]+)\]:\s*\S+/.exec(l);
  31. if (link) log.links.add(link[1].trim().toLowerCase());
  32. if (/^## /.test(l)) {
  33. sec = null;
  34. const m = REL.exec(l.trimEnd());
  35. if (!m?.groups) {
  36. log.bad.push({ line: n, raw: l.trim() });
  37. cur = null;
  38. return;
  39. }
  40. const v = (m.groups.v1 ?? m.groups.v2 ?? '').trim();
  41. const unreleased = v.toLowerCase() === 'unreleased';
  42. cur = { raw: l.trim(), line: n, version: unreleased ? null : v, date: m.groups.d ?? null, yanked: !!m.groups.y, unreleased, sections: [] };
  43. log.releases.push(cur);
  44. return;
  45. }
  46. if (/^### /.test(l) && cur) {
  47. sec = { name: l.slice(4).trim(), line: n, items: 0 };
  48. (cur as Release).sections.push(sec);
  49. return;
  50. }
  51. if (sec && l.trim() && !/^#/.test(l)) (sec as Section).items++;
  52. });
  53. return log;
  54. }
  55. /** A real calendar day written YYYY-MM-DD (ISO 8601). */
  56. export function isoDay(d: string): boolean {
  57. if (!/^\d{4}-\d{2}-\d{2}$/.test(d)) return false;
  58. const t = new Date(`${d}T00:00:00Z`);
  59. return !Number.isNaN(t.getTime()) && t.toISOString().slice(0, 10) === d;
  60. }
  61. /** Each changelog of the settings, or a finding line when it is missing. */
  62. export function logs(cfg: Config): { logs: Log[]; missing: string[] } {
  63. const out: Log[] = [];
  64. const missing: string[] = [];
  65. for (const f of cfg.files) {
  66. const l = read(f);
  67. if (l) out.push(l);
  68. else missing.push(f);
  69. }
  70. return { logs: out, missing };
  71. }