@cldmv/fix-headers 1.3.12 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +313 -25
- package/bin/fix-headers.mjs +70 -0
- package/dist/index.cjs +28 -0
- package/dist/index.mjs +23 -0
- package/package.json +39 -19
- package/types/src/cli.d.mts +20 -2
- package/types/src/config/load.d.mts +36 -0
- package/types/src/constants.d.mts +1 -23
- package/types/src/core/file-discovery.d.mts +17 -5
- package/types/src/core/fix-headers.d.mts +59 -4
- package/types/src/core/ignore-rules.d.mts +38 -0
- package/types/src/detect/project.d.mts +64 -15
- package/types/src/detectors/css.d.mts +0 -8
- package/types/src/detectors/go.d.mts +0 -8
- package/types/src/detectors/html.d.mts +0 -8
- package/types/src/detectors/index.d.mts +28 -7
- package/types/src/detectors/json.d.mts +0 -8
- package/types/src/detectors/node.d.mts +0 -8
- package/types/src/detectors/php.d.mts +0 -8
- package/types/src/detectors/python.d.mts +0 -8
- package/types/src/detectors/rust.d.mts +0 -8
- package/types/src/detectors/yaml.d.mts +0 -8
- package/types/src/drivers/go.d.mts +2 -0
- package/types/src/drivers/index.d.mts +154 -0
- package/types/src/drivers/node.d.mts +2 -0
- package/types/src/drivers/php.d.mts +2 -0
- package/types/src/drivers/python.d.mts +2 -0
- package/types/src/drivers/rust.d.mts +2 -0
- package/types/src/drivers/shared.d.mts +78 -0
- package/types/src/header/dates.d.mts +108 -0
- package/types/src/header/fields.d.mts +48 -0
- package/types/src/header/template.d.mts +4 -3
- package/types/src/utils/diff.d.mts +53 -0
- package/types/src/utils/fs.d.mts +10 -3
- package/types/src/utils/git.d.mts +27 -0
- package/types/src/utils/time.d.mts +55 -0
- package/index.cjs +0 -39
- package/index.mjs +0 -20
- package/src/cli.mjs +0 -323
- package/src/constants.mjs +0 -50
- package/src/core/file-discovery.mjs +0 -213
- package/src/core/fix-headers.mjs +0 -413
- package/src/detect/project.mjs +0 -190
- package/src/detectors/css.mjs +0 -67
- package/src/detectors/go.mjs +0 -65
- package/src/detectors/html.mjs +0 -67
- package/src/detectors/index.mjs +0 -187
- package/src/detectors/json.mjs +0 -76
- package/src/detectors/node.mjs +0 -90
- package/src/detectors/php.mjs +0 -66
- package/src/detectors/python.mjs +0 -75
- package/src/detectors/rust.mjs +0 -62
- package/src/detectors/shared.mjs +0 -45
- package/src/detectors/yaml.mjs +0 -79
- package/src/fix-header.mjs +0 -30
- package/src/header/parser.mjs +0 -145
- package/src/header/syntax.mjs +0 -69
- package/src/header/template.mjs +0 -65
- package/src/utils/fs.mjs +0 -138
- package/src/utils/git.mjs +0 -135
- package/src/utils/time.mjs +0 -44
- package/types/src/detectors/shared.d.mts +0 -14
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets a driver by id.
|
|
3
|
+
* @param {string} id - Driver id.
|
|
4
|
+
* @returns {ManifestDriver | undefined} Driver.
|
|
5
|
+
*/
|
|
6
|
+
export function getDriverById(id: string): ManifestDriver | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Runs every driver's `detect` on one folder and orders the drivers that claim it: the
|
|
9
|
+
* file's native driver first, then the registry order.
|
|
10
|
+
* @param {string} dirPath - Folder.
|
|
11
|
+
* @param {ManifestDriver[]} drivers - Drivers, in registry order.
|
|
12
|
+
* @param {string | undefined} language - The file's detector id (undefined for no file).
|
|
13
|
+
* @returns {Promise<Array<{ driver: ManifestDriver, detection: DriverDetection }>>} Ordered claims.
|
|
14
|
+
*/
|
|
15
|
+
export function claimFolder(dirPath: string, drivers: ManifestDriver[], language: string | undefined): Promise<Array<{
|
|
16
|
+
driver: ManifestDriver;
|
|
17
|
+
detection: DriverDetection;
|
|
18
|
+
}>>;
|
|
19
|
+
/**
|
|
20
|
+
* Resolves the project a path belongs to from the manifest drivers.
|
|
21
|
+
*
|
|
22
|
+
* 1. Walk up from `startPath`. The nearest folder that at least one driver claims is the
|
|
23
|
+
* project root. A folder holding `.git` ends the walk: with no claimed folder up to and
|
|
24
|
+
* including it, that repository root is the project root and no driver provides values.
|
|
25
|
+
* 2. Each field comes from the first driver in the root's claim order (native driver first,
|
|
26
|
+
* then node, python, php, rust, go) whose manifest provides it.
|
|
27
|
+
* 3. A field no driver at the root provides is looked up the same way in each ancestor folder
|
|
28
|
+
* a driver claims, climbing no higher than `scanRoot` and stopping at a folder that holds
|
|
29
|
+
* `.git`. Without a `scanRoot`, or when the root is not inside it, nothing climbs.
|
|
30
|
+
*
|
|
31
|
+
* Only values climb; the project root stays the nearest claimed folder.
|
|
32
|
+
* @param {string} startPath - Folder to start from (a file's folder, or the scan root).
|
|
33
|
+
* @param {{ drivers?: ManifestDriver[], language?: string, scanRoot?: string }} [options={}] - Resolution options.
|
|
34
|
+
* @returns {Promise<ManifestProject | null>} Resolved project, or null when neither a manifest nor a `.git` exists up to the filesystem root.
|
|
35
|
+
*/
|
|
36
|
+
export function resolveManifestProject(startPath: string, options?: {
|
|
37
|
+
drivers?: ManifestDriver[];
|
|
38
|
+
language?: string;
|
|
39
|
+
scanRoot?: string;
|
|
40
|
+
}): Promise<ManifestProject | null>;
|
|
41
|
+
/**
|
|
42
|
+
* @fileoverview Manifest driver registry and project resolution.
|
|
43
|
+
*
|
|
44
|
+
* A manifest driver knows one ecosystem's manifest files: whether a folder is a project of
|
|
45
|
+
* its kind (`detect`) and what that project's manifest says (`read`). Which project a file
|
|
46
|
+
* belongs to, and the values its header takes from that project, are resolved here from the
|
|
47
|
+
* drivers alone, whatever the file's type; comment syntax stays with the file-type detectors
|
|
48
|
+
* in `src/detectors/`.
|
|
49
|
+
* @module fix-headers/drivers
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* @typedef {import("./shared.mjs").DriverDetection} DriverDetection
|
|
53
|
+
*/
|
|
54
|
+
/**
|
|
55
|
+
* @typedef {{ name?: string, company?: string }} ManifestData
|
|
56
|
+
* Values a manifest provides. A field is left undefined when the manifest doesn't carry it.
|
|
57
|
+
* - `name` - the project name (`@Project`).
|
|
58
|
+
* - `company` - the copyright holder (`@Copyright`), taken from the manifest's author.
|
|
59
|
+
*/
|
|
60
|
+
/**
|
|
61
|
+
* @typedef {{
|
|
62
|
+
* id: string,
|
|
63
|
+
* languages: string[],
|
|
64
|
+
* manifests: string[],
|
|
65
|
+
* detect: (dirPath: string) => Promise<DriverDetection | null>,
|
|
66
|
+
* read: (detection: DriverDetection) => ManifestData
|
|
67
|
+
* }} ManifestDriver
|
|
68
|
+
* - `id` - driver id, reported as the source of a value.
|
|
69
|
+
* - `languages` - file-type detector ids whose files are native to this driver (`node` for
|
|
70
|
+
* `.js`/`.mjs`/`.ts`…); in a folder claimed by several drivers, a file's native driver is
|
|
71
|
+
* read first.
|
|
72
|
+
* - `manifests` - manifest filenames that claim a folder, in the order they are read.
|
|
73
|
+
* - `detect` - the manifests present and readable in a folder, or null when there are none.
|
|
74
|
+
* - `read` - the values those manifests provide.
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* @typedef {{ driver: string, manifest: string, dir: string }} FieldSource
|
|
78
|
+
* Where a value came from: the driver, its manifest (the first one found in the folder) and
|
|
79
|
+
* the folder.
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* @typedef {{
|
|
83
|
+
* root: string,
|
|
84
|
+
* marker: string,
|
|
85
|
+
* drivers: string[],
|
|
86
|
+
* fields: ManifestData,
|
|
87
|
+
* sources: Partial<Record<keyof ManifestData, FieldSource>>
|
|
88
|
+
* }} ManifestProject
|
|
89
|
+
* - `root` - the project root: the nearest folder a driver claims, or the repository root
|
|
90
|
+
* (`marker: ".git"`, no drivers) when no manifest exists below it.
|
|
91
|
+
* - `marker` - the first manifest of the first driver claiming the root, or `.git`.
|
|
92
|
+
* - `drivers` - the drivers claiming the root, in the order they are read.
|
|
93
|
+
* - `fields` / `sources` - the resolved values and where each came from.
|
|
94
|
+
*/
|
|
95
|
+
/**
|
|
96
|
+
* Registered drivers, in the fixed order used when a folder is claimed by several drivers
|
|
97
|
+
* and none of them is native to the file. Add a new driver module here.
|
|
98
|
+
* @type {ManifestDriver[]}
|
|
99
|
+
*/
|
|
100
|
+
export const MANIFEST_DRIVERS: ManifestDriver[];
|
|
101
|
+
/**
|
|
102
|
+
* Fields resolved from manifests. Resolution climbs until each of them is found.
|
|
103
|
+
* @type {Array<keyof ManifestData>}
|
|
104
|
+
*/
|
|
105
|
+
export const MANIFEST_FIELDS: Array<keyof ManifestData>;
|
|
106
|
+
export type DriverDetection = import("./shared.mjs").DriverDetection;
|
|
107
|
+
/**
|
|
108
|
+
* Values a manifest provides. A field is left undefined when the manifest doesn't carry it.
|
|
109
|
+
* - `name` - the project name (`@Project`).
|
|
110
|
+
* - `company` - the copyright holder (`@Copyright`), taken from the manifest's author.
|
|
111
|
+
*/
|
|
112
|
+
export type ManifestData = {
|
|
113
|
+
name?: string;
|
|
114
|
+
company?: string;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* - `id` - driver id, reported as the source of a value.
|
|
118
|
+
* - `languages` - file-type detector ids whose files are native to this driver (`node` for
|
|
119
|
+
* `.js`/`.mjs`/`.ts`…); in a folder claimed by several drivers, a file's native driver is
|
|
120
|
+
* read first.
|
|
121
|
+
* - `manifests` - manifest filenames that claim a folder, in the order they are read.
|
|
122
|
+
* - `detect` - the manifests present and readable in a folder, or null when there are none.
|
|
123
|
+
* - `read` - the values those manifests provide.
|
|
124
|
+
*/
|
|
125
|
+
export type ManifestDriver = {
|
|
126
|
+
id: string;
|
|
127
|
+
languages: string[];
|
|
128
|
+
manifests: string[];
|
|
129
|
+
detect: (dirPath: string) => Promise<DriverDetection | null>;
|
|
130
|
+
read: (detection: DriverDetection) => ManifestData;
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Where a value came from: the driver, its manifest (the first one found in the folder) and
|
|
134
|
+
* the folder.
|
|
135
|
+
*/
|
|
136
|
+
export type FieldSource = {
|
|
137
|
+
driver: string;
|
|
138
|
+
manifest: string;
|
|
139
|
+
dir: string;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* - `root` - the project root: the nearest folder a driver claims, or the repository root
|
|
143
|
+
* (`marker: ".git"`, no drivers) when no manifest exists below it.
|
|
144
|
+
* - `marker` - the first manifest of the first driver claiming the root, or `.git`.
|
|
145
|
+
* - `drivers` - the drivers claiming the root, in the order they are read.
|
|
146
|
+
* - `fields` / `sources` - the resolved values and where each came from.
|
|
147
|
+
*/
|
|
148
|
+
export type ManifestProject = {
|
|
149
|
+
root: string;
|
|
150
|
+
marker: string;
|
|
151
|
+
drivers: string[];
|
|
152
|
+
fields: ManifestData;
|
|
153
|
+
sources: Partial<Record<keyof ManifestData, FieldSource>>;
|
|
154
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detects a driver's manifests in one folder. The folder counts as a project of the driver's
|
|
3
|
+
* kind when at least one of the manifests is present and readable; its content is not
|
|
4
|
+
* validated here, so a malformed manifest still marks the project root.
|
|
5
|
+
* @param {string} dirPath - Folder to check.
|
|
6
|
+
* @param {string[]} manifests - Manifest filenames, in the order the driver reads them.
|
|
7
|
+
* @returns {Promise<DriverDetection | null>} Detection, or null when the folder holds none of them.
|
|
8
|
+
*/
|
|
9
|
+
export function detectManifests(dirPath: string, manifests: string[]): Promise<DriverDetection | null>;
|
|
10
|
+
/**
|
|
11
|
+
* Gets the content of one manifest from a detection.
|
|
12
|
+
* @param {DriverDetection} detection - Driver detection.
|
|
13
|
+
* @param {string} file - Manifest filename.
|
|
14
|
+
* @returns {string | null} Content, or null when that manifest isn't present.
|
|
15
|
+
*/
|
|
16
|
+
export function manifestContent(detection: DriverDetection, file: string): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* Normalizes a candidate name: a trimmed non-empty string, else undefined.
|
|
19
|
+
* @param {unknown} value - Candidate value.
|
|
20
|
+
* @returns {string | undefined} Name.
|
|
21
|
+
*/
|
|
22
|
+
export function cleanName(value: unknown): string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Reads the name part of a person string in the `Name <email> (url)` form used by
|
|
25
|
+
* `package.json` string authors, Poetry and Cargo authors: the text before the first `<` or `(`.
|
|
26
|
+
* @param {unknown} value - Candidate value.
|
|
27
|
+
* @returns {string | undefined} Name, or undefined when the value isn't a string or has no name
|
|
28
|
+
* before its email or url.
|
|
29
|
+
*/
|
|
30
|
+
export function personName(value: unknown): string | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Parses a JSON manifest (`package.json`, `composer.json`).
|
|
33
|
+
* @param {string} content - Manifest content.
|
|
34
|
+
* @returns {any} Parsed value, or undefined when the content isn't valid JSON.
|
|
35
|
+
*/
|
|
36
|
+
export function parseJsonManifest(content: string): any;
|
|
37
|
+
/**
|
|
38
|
+
* Reads a string value from a TOML table: `key = "value"` or `key = 'value'` on its own line.
|
|
39
|
+
* @param {string} content - TOML content.
|
|
40
|
+
* @param {string} table - Table name, dotted for nested tables (`tool.poetry`).
|
|
41
|
+
* @param {string} key - Bare key.
|
|
42
|
+
* @returns {string | undefined} Value, or undefined when the table or key is missing.
|
|
43
|
+
*/
|
|
44
|
+
export function readTomlString(content: string, table: string, key: string): string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Reads a value from an INI section (`key = value` or `key: value`), as in `setup.cfg`.
|
|
47
|
+
* @param {string} content - INI content.
|
|
48
|
+
* @param {string} section - Section name.
|
|
49
|
+
* @param {string} key - Key.
|
|
50
|
+
* @returns {string | undefined} Value, or undefined when the section or key is missing.
|
|
51
|
+
*/
|
|
52
|
+
export function readIniValue(content: string, section: string, key: string): string | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Reads the name of the first author in a TOML `authors` array: the name part of a string
|
|
55
|
+
* entry (`authors = ["Name <email>"]`, as in Poetry and Cargo) or the `name` of an inline-table
|
|
56
|
+
* entry (`authors = [{ name = "Name", email = "..." }]`, as in PEP 621).
|
|
57
|
+
* @param {string} content - TOML content.
|
|
58
|
+
* @param {string} table - Table name, dotted for nested tables (`tool.poetry`).
|
|
59
|
+
* @returns {string | undefined} Name, or undefined when the table has no `authors` array or its
|
|
60
|
+
* first entry carries no name.
|
|
61
|
+
*/
|
|
62
|
+
export function readTomlAuthor(content: string, table: string): string | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* A manifest file found in a folder, with its content.
|
|
65
|
+
*/
|
|
66
|
+
export type ManifestFile = {
|
|
67
|
+
file: string;
|
|
68
|
+
content: string;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* What a driver's `detect` found in a folder: the folder, the first manifest found (in the
|
|
72
|
+
* driver's `manifests` order) and every manifest of the driver that is present and readable.
|
|
73
|
+
*/
|
|
74
|
+
export type DriverDetection = {
|
|
75
|
+
dir: string;
|
|
76
|
+
manifest: string;
|
|
77
|
+
files: ManifestFile[];
|
|
78
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Picks the earliest of several labelled date payloads. Missing payloads are skipped and the
|
|
3
|
+
* earlier entry wins a tie, so callers list their preferred source first.
|
|
4
|
+
* @template {string} S
|
|
5
|
+
* @param {Array<{source: S, payload: DatePayload | null | undefined}>} candidates - Labelled payloads.
|
|
6
|
+
* @returns {{source: S, payload: DatePayload} | null} Earliest candidate, or null when none has a payload.
|
|
7
|
+
*/
|
|
8
|
+
export function pickOldestDate<S extends string>(candidates: Array<{
|
|
9
|
+
source: S;
|
|
10
|
+
payload: DatePayload | null | undefined;
|
|
11
|
+
}>): {
|
|
12
|
+
source: S;
|
|
13
|
+
payload: DatePayload;
|
|
14
|
+
} | null;
|
|
15
|
+
/**
|
|
16
|
+
* Resolves the `@Date` to write ("oldest wins").
|
|
17
|
+
* - No usable existing `@Date` (no header, or no `(epoch)`): the older of the git first-commit
|
|
18
|
+
* date and the filesystem creation time (git on a tie).
|
|
19
|
+
* - Existing `@Date`, default: kept as written (epoch repaired separately by {@link repairDateEpoch}).
|
|
20
|
+
* - Existing `@Date` with `fixCreatedDate`: the oldest of the existing date (when its datetime is
|
|
21
|
+
* recognised), the git first commit, and the filesystem creation time. The existing date wins a
|
|
22
|
+
* tie, so a correction only ever moves `@Date` earlier.
|
|
23
|
+
* @param {{ existing: DatePayload | null, gitCreated: DatePayload | null, filesystemCreated: DatePayload, fixCreatedDate?: boolean }} input - Candidate dates.
|
|
24
|
+
* @returns {{source: CreatedDateSource, payload: DatePayload}} Chosen date and where it came from.
|
|
25
|
+
*/
|
|
26
|
+
export function resolveCreatedDate({ existing, gitCreated, filesystemCreated, fixCreatedDate }: {
|
|
27
|
+
existing: DatePayload | null;
|
|
28
|
+
gitCreated: DatePayload | null;
|
|
29
|
+
filesystemCreated: DatePayload;
|
|
30
|
+
fixCreatedDate?: boolean;
|
|
31
|
+
}): {
|
|
32
|
+
source: CreatedDateSource;
|
|
33
|
+
payload: DatePayload;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Reads the calendar year of a header date, for the `@Copyright` start year. With a time zone the
|
|
37
|
+
* year is the zone's at the date's instant; without one it is the year the datetime text is
|
|
38
|
+
* written in, in its own offset. Text that is not a recognised datetime falls back to the year of
|
|
39
|
+
* its epoch in the local time zone, the zone fix-headers writes dates in by default.
|
|
40
|
+
* @param {DatePayload} payload - Date payload, with an epoch consistent with its text.
|
|
41
|
+
* @param {string | null} [timeZone=null] - IANA zone name, already validated.
|
|
42
|
+
* @returns {number} Calendar year.
|
|
43
|
+
*/
|
|
44
|
+
export function dateYear(payload: DatePayload, timeZone?: string | null): number;
|
|
45
|
+
/**
|
|
46
|
+
* Validates the `@Date` and `@Last modified time` values of an existing header.
|
|
47
|
+
*
|
|
48
|
+
* Every comparison is between instants, so the same moment written with another offset or in
|
|
49
|
+
* the space/`T` form is not drift. The checks are independent of the rendered-header diff, so
|
|
50
|
+
* an author, identity, or other content difference never produces an issue here.
|
|
51
|
+
* - `*-format`: the value is not a recognised `<datetime> (<epoch>)` pair.
|
|
52
|
+
* - `*-epoch`: the parenthesised epoch is not the instant the datetime text describes.
|
|
53
|
+
* - `created-newer-than-source`: `@Date` is later than the older of the git first commit and the
|
|
54
|
+
* filesystem creation time. An earlier `@Date` is fine. Advisory unless `strictCreatedDate`.
|
|
55
|
+
* - `modified-git`: `@Last modified time` is not the file's last-commit date. Always advisory.
|
|
56
|
+
* @param {string} headerText - Existing header content.
|
|
57
|
+
* @param {{ gitCreated?: DatePayload | null, gitLastModified?: DatePayload | null, filesystemCreated?: DatePayload | null }} [sources={}] - Dates the header is compared against.
|
|
58
|
+
* @param {{ strictCreatedDate?: boolean }} [options={}] - Check options.
|
|
59
|
+
* @returns {DateCheckIssue[]} Issues found, `@Date` first.
|
|
60
|
+
*/
|
|
61
|
+
export function checkHeaderDates(headerText: string, sources?: {
|
|
62
|
+
gitCreated?: DatePayload | null;
|
|
63
|
+
gitLastModified?: DatePayload | null;
|
|
64
|
+
filesystemCreated?: DatePayload | null;
|
|
65
|
+
}, options?: {
|
|
66
|
+
strictCreatedDate?: boolean;
|
|
67
|
+
}): DateCheckIssue[];
|
|
68
|
+
/**
|
|
69
|
+
* Repairs a header date payload whose epoch disagrees with its datetime text. The text is kept
|
|
70
|
+
* as written and the epoch is recomputed from it. Unrecognised text is returned unchanged,
|
|
71
|
+
* since there is no instant to recompute from.
|
|
72
|
+
* @param {DatePayload | null} payload - Date payload read from a header.
|
|
73
|
+
* @returns {DatePayload | null} Consistent payload, or null when none was given.
|
|
74
|
+
*/
|
|
75
|
+
export function repairDateEpoch(payload: DatePayload | null): DatePayload | null;
|
|
76
|
+
/**
|
|
77
|
+
* Rewrites a date payload's text in the git `%aI` form (`YYYY-MM-DDTHH:mm:ss±HH:MM`), keeping
|
|
78
|
+
* its offset and instant. Unrecognised text is returned unchanged.
|
|
79
|
+
* @param {DatePayload} payload - Date payload.
|
|
80
|
+
* @returns {DatePayload} Payload with ISO 8601 text.
|
|
81
|
+
*/
|
|
82
|
+
export function normalizeDatePayload(payload: DatePayload): DatePayload;
|
|
83
|
+
/**
|
|
84
|
+
* Rewrites a date payload's text in a time zone, keeping its instant: the wall-clock time and
|
|
85
|
+
* offset become the zone's at that instant (DST included). The text keeps its shape: the T-form
|
|
86
|
+
* stays in the T-form and anything else is written in the space form. The payload is returned
|
|
87
|
+
* unchanged when its text is unrecognised (there is no instant to convert), when it is already
|
|
88
|
+
* in the zone's offset at that instant, or when the zone's offset then is not a whole number of
|
|
89
|
+
* minutes (historical local mean time), which a `±HH:MM` offset cannot express.
|
|
90
|
+
* @param {DatePayload} payload - Date payload.
|
|
91
|
+
* @param {string} timeZone - IANA zone name, already validated.
|
|
92
|
+
* @returns {DatePayload} Payload expressed in the zone.
|
|
93
|
+
*/
|
|
94
|
+
export function convertDatePayload(payload: DatePayload, timeZone: string): DatePayload;
|
|
95
|
+
export type DatePayload = {
|
|
96
|
+
date: string;
|
|
97
|
+
timestamp: number;
|
|
98
|
+
};
|
|
99
|
+
export type CreatedDateSource = "existing-header" | "git-created" | "filesystem-created";
|
|
100
|
+
export type DateCheckIssue = {
|
|
101
|
+
check: "created-format" | "created-epoch" | "created-newer-than-source" | "modified-format" | "modified-epoch" | "modified-git";
|
|
102
|
+
field: "@Date" | "@Last modified time";
|
|
103
|
+
advisory: boolean;
|
|
104
|
+
value: string;
|
|
105
|
+
expected?: string;
|
|
106
|
+
source?: "git-created" | "filesystem-created";
|
|
107
|
+
message: string;
|
|
108
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses every known field out of a header block. Values are the text as written in
|
|
3
|
+
* the header (dates keep their `date (timestamp)` form); a field that is missing or
|
|
4
|
+
* cannot be parsed is `null`.
|
|
5
|
+
* @param {string} headerText - Header block text.
|
|
6
|
+
* @returns {HeaderFields} Parsed field values.
|
|
7
|
+
*/
|
|
8
|
+
export function parseHeaderFields(headerText: string): HeaderFields;
|
|
9
|
+
/**
|
|
10
|
+
* Compares an existing header with the header written by this run, field by field.
|
|
11
|
+
* Both sides are parsed with {@link parseHeaderFields}, so a field only appears when
|
|
12
|
+
* the written value really differs from what the file had. Values fix-headers
|
|
13
|
+
* preserves (the original author and last-modified identity, unless forced) therefore
|
|
14
|
+
* show up only when they actually change.
|
|
15
|
+
* @param {string | null} previousHeader - Existing header text, or null when the file had none.
|
|
16
|
+
* @param {string} nextHeader - Header text written by this run.
|
|
17
|
+
* @returns {HeaderFieldIssue[]} One entry per differing field, in header order.
|
|
18
|
+
*/
|
|
19
|
+
export function compareHeaderFields(previousHeader: string | null, nextHeader: string): HeaderFieldIssue[];
|
|
20
|
+
/**
|
|
21
|
+
* @Project: @cldmv/fix-headers
|
|
22
|
+
* @Filename: /src/header/fields.mjs
|
|
23
|
+
* @Date: 2026-09-28T09:14:18-07:00 (1790612058)
|
|
24
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
25
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
26
|
+
* -----
|
|
27
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
28
|
+
* @Last modified time: 2026-09-28T09:14:18-07:00 (1790612058)
|
|
29
|
+
* -----
|
|
30
|
+
* @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
31
|
+
*/
|
|
32
|
+
/**
|
|
33
|
+
* @fileoverview Parses the individual fields back out of a rendered header block and
|
|
34
|
+
* compares two header blocks field by field.
|
|
35
|
+
* @module fix-headers/header/fields
|
|
36
|
+
*/
|
|
37
|
+
/**
|
|
38
|
+
* Header field identifiers, in the order they appear in a rendered header.
|
|
39
|
+
* @type {ReadonlyArray<HeaderFieldName>}
|
|
40
|
+
*/
|
|
41
|
+
export const HEADER_FIELDS: ReadonlyArray<HeaderFieldName>;
|
|
42
|
+
export type HeaderFieldName = "projectName" | "filename" | "createdAt" | "authorName" | "authorEmail" | "lastModifiedByName" | "lastModifiedByEmail" | "lastModifiedAt" | "copyrightStartYear" | "copyrightEndYear" | "companyName";
|
|
43
|
+
export type HeaderFields = Record<HeaderFieldName, string | null>;
|
|
44
|
+
export type HeaderFieldIssue = {
|
|
45
|
+
field: HeaderFieldName;
|
|
46
|
+
previous: string | null;
|
|
47
|
+
detected: string | null;
|
|
48
|
+
};
|
|
@@ -19,9 +19,10 @@
|
|
|
19
19
|
* createdAt: {date: string, timestamp: number},
|
|
20
20
|
* lastModifiedAt: {date: string, timestamp: number},
|
|
21
21
|
* copyrightStartYear: number,
|
|
22
|
-
* companyName
|
|
22
|
+
* companyName?: string | null,
|
|
23
23
|
* currentYear: number
|
|
24
|
-
* }} data - Header data.
|
|
24
|
+
* }} data - Header data. Without a `companyName` (null, undefined or blank), the `@Copyright`
|
|
25
|
+
* line carries no holder: `Copyright (c) 2019-2026 All rights reserved.`
|
|
25
26
|
* @returns {string} Header block text.
|
|
26
27
|
*/
|
|
27
28
|
export function buildHeader(data: {
|
|
@@ -56,6 +57,6 @@ export function buildHeader(data: {
|
|
|
56
57
|
timestamp: number;
|
|
57
58
|
};
|
|
58
59
|
copyrightStartYear: number;
|
|
59
|
-
companyName
|
|
60
|
+
companyName?: string | null;
|
|
60
61
|
currentYear: number;
|
|
61
62
|
}): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @Project: @cldmv/fix-headers
|
|
3
|
+
* @Filename: /src/utils/diff.mjs
|
|
4
|
+
* @Date: 2026-09-28T09:14:18-07:00 (1790612058)
|
|
5
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
6
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
7
|
+
* -----
|
|
8
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
9
|
+
* @Last modified time: 2026-09-28T09:14:18-07:00 (1790612058)
|
|
10
|
+
* -----
|
|
11
|
+
* @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* @fileoverview Minimal line-level unified diff for header blocks. Header blocks are a
|
|
15
|
+
* handful of lines, so a plain longest-common-subsequence table is fast enough and keeps
|
|
16
|
+
* the package free of a runtime diff dependency.
|
|
17
|
+
* @module fix-headers/utils/diff
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {{ type: " " | "-" | "+", line: string }} DiffOperation
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Computes the line operations that turn `previous` into `next`.
|
|
24
|
+
* @param {string[]} previous - Old lines.
|
|
25
|
+
* @param {string[]} next - New lines.
|
|
26
|
+
* @returns {DiffOperation[]} Ordered keep/remove/add operations.
|
|
27
|
+
*/
|
|
28
|
+
export function diffLines(previous: string[], next: string[]): DiffOperation[];
|
|
29
|
+
/**
|
|
30
|
+
* Builds a unified diff between two texts.
|
|
31
|
+
* @param {string | null} previous - Old text, or null when there was none.
|
|
32
|
+
* @param {string} next - New text.
|
|
33
|
+
* @param {{
|
|
34
|
+
* fromFile?: string,
|
|
35
|
+
* toFile?: string,
|
|
36
|
+
* context?: number,
|
|
37
|
+
* lineOffset?: number
|
|
38
|
+
* }} [options={}] - `fromFile`/`toFile` label the `---`/`+++` lines (a null `previous`
|
|
39
|
+
* labels the old side `/dev/null`); `context` is the number of unchanged lines kept
|
|
40
|
+
* around each change (default 3); `lineOffset` is the number of file lines that precede
|
|
41
|
+
* both texts, so hunk line numbers match the file.
|
|
42
|
+
* @returns {string} Unified diff text, or an empty string when the texts are identical.
|
|
43
|
+
*/
|
|
44
|
+
export function createUnifiedDiff(previous: string | null, next: string, options?: {
|
|
45
|
+
fromFile?: string;
|
|
46
|
+
toFile?: string;
|
|
47
|
+
context?: number;
|
|
48
|
+
lineOffset?: number;
|
|
49
|
+
}): string;
|
|
50
|
+
export type DiffOperation = {
|
|
51
|
+
type: " " | "-" | "+";
|
|
52
|
+
line: string;
|
|
53
|
+
};
|
package/types/src/utils/fs.d.mts
CHANGED
|
@@ -30,17 +30,24 @@ export function readTextIfExists(filePath: string): Promise<string | null>;
|
|
|
30
30
|
* @param {{
|
|
31
31
|
* allowedExtensions: Set<string>,
|
|
32
32
|
* ignoreFolders: Set<string>,
|
|
33
|
-
* shouldSkipDirectory?: (directoryPath: string, directoryName: string) => boolean
|
|
34
|
-
*
|
|
33
|
+
* shouldSkipDirectory?: (directoryPath: string, directoryName: string) => boolean | Promise<boolean>,
|
|
34
|
+
* recursive?: boolean
|
|
35
|
+
* }} options - Scan options. `recursive: false` lists only the directory's own files.
|
|
36
|
+
* `shouldSkipDirectory` may be async. Symlinked directories are never followed.
|
|
35
37
|
* @returns {Promise<string[]>} Matching file paths.
|
|
36
38
|
*/
|
|
37
39
|
export function walkFiles(dirPath: string, options: {
|
|
38
40
|
allowedExtensions: Set<string>;
|
|
39
41
|
ignoreFolders: Set<string>;
|
|
40
|
-
shouldSkipDirectory?: (directoryPath: string, directoryName: string) => boolean
|
|
42
|
+
shouldSkipDirectory?: (directoryPath: string, directoryName: string) => boolean | Promise<boolean>;
|
|
43
|
+
recursive?: boolean;
|
|
41
44
|
}): Promise<string[]>;
|
|
42
45
|
/**
|
|
43
46
|
* Gets creation-like and modified timestamps from filesystem stats.
|
|
47
|
+
* The creation time is the earlier of the birth time and the modification time: content last
|
|
48
|
+
* written at `mtime` existed by then, so `mtime` bounds creation even when the birth time is
|
|
49
|
+
* later (an archive extraction or `cp -p` keeps the source's `mtime` but gets a new birth time).
|
|
50
|
+
* Where the platform reports no birth time (`birthtimeMs` 0), `mtime` is used.
|
|
44
51
|
* @param {string} filePath - Absolute file path.
|
|
45
52
|
* @returns {Promise<{createdAt: Date, updatedAt: Date}>} Date pair.
|
|
46
53
|
*/
|
|
@@ -9,8 +9,35 @@
|
|
|
9
9
|
* @returns {Promise<string | null>} Trimmed stdout or null on failure.
|
|
10
10
|
*/
|
|
11
11
|
export function runGit(cwd: string, args: string[]): Promise<string | null>;
|
|
12
|
+
/**
|
|
13
|
+
* Parses a signer UID string into author name and optional email.
|
|
14
|
+
* The OpenPGP UID comment (`Name (Comment) <email>`) is dropped from the name;
|
|
15
|
+
* when the UID is only a comment, the raw name is kept.
|
|
16
|
+
* @param {string} signerUid - Raw signer UID (for example: "Name (Comment) <email@example.com>").
|
|
17
|
+
* @returns {{authorName: string | null, authorEmail: string | null}} Parsed signer identity.
|
|
18
|
+
*/
|
|
19
|
+
export function parseSignerUid(signerUid: string): {
|
|
20
|
+
authorName: string | null;
|
|
21
|
+
authorEmail: string | null;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Reads the user ID of the OpenPGP key git signs commits with (`user.signingkey`).
|
|
25
|
+
* Returns the first user ID that is not revoked or expired (GnuPG lists the primary
|
|
26
|
+
* one first), or null when git signs with SSH/X.509, no signing key is configured,
|
|
27
|
+
* or GnuPG is unavailable or doesn't know the key.
|
|
28
|
+
* @param {string} cwd - Project directory (git config is read as git sees it there).
|
|
29
|
+
* @returns {Promise<string | null>} The signing key's user ID.
|
|
30
|
+
*/
|
|
31
|
+
export function readSigningKeyUid(cwd: string): Promise<string | null>;
|
|
12
32
|
/**
|
|
13
33
|
* Detects git author name and email from config or commit history.
|
|
34
|
+
*
|
|
35
|
+
* With `useGpgSignerAuthor`, the name comes from the user ID of the OpenPGP key git is
|
|
36
|
+
* configured to sign with (`user.signingkey`), so it describes whoever is running the
|
|
37
|
+
* tool. When no such key can be read, it falls back to the signer of the last commit
|
|
38
|
+
* (`%GS`), and then to `user.name`. The last commit alone is unreliable: on a branch
|
|
39
|
+
* whose tip is a squash merge made by GitHub or a bot, it has no locally verifiable
|
|
40
|
+
* signer.
|
|
14
41
|
* @param {string} cwd - Project directory.
|
|
15
42
|
* @param {{useGpgSignerAuthor?: boolean}} [options={}] - Detection options.
|
|
16
43
|
* @returns {Promise<{authorName: string | null, authorEmail: string | null}>} Author information.
|
|
@@ -20,6 +20,30 @@
|
|
|
20
20
|
* @returns {string} Formatted date string.
|
|
21
21
|
*/
|
|
22
22
|
export function formatDateWithTimezone(date: Date): string;
|
|
23
|
+
/**
|
|
24
|
+
* Renders datetime parts in the space form (`YYYY-MM-DD HH:mm:ss ±HH:MM`), keeping their offset.
|
|
25
|
+
* @param {DateParts} parts - Datetime parts.
|
|
26
|
+
* @returns {string} Formatted date string.
|
|
27
|
+
*/
|
|
28
|
+
export function formatSpaceDate(parts: DateParts): string;
|
|
29
|
+
/**
|
|
30
|
+
* Validates an IANA time zone name with Intl (`new Intl.DateTimeFormat` throws a RangeError for a
|
|
31
|
+
* zone it does not know).
|
|
32
|
+
* @param {unknown} timeZone - Zone name to validate, such as `America/Los_Angeles` or `UTC`.
|
|
33
|
+
* @returns {string} The zone name.
|
|
34
|
+
* @throws {Error} When the value is not a string, or Intl does not know the zone.
|
|
35
|
+
*/
|
|
36
|
+
export function assertTimeZone(timeZone: unknown): string;
|
|
37
|
+
/**
|
|
38
|
+
* Expresses an instant in a zone: its wall-clock parts there and the zone's UTC offset at that
|
|
39
|
+
* instant (DST included), read from Intl's time zone data rather than a fixed table. The offset is
|
|
40
|
+
* the difference between that wall-clock time and the instant, so a historical offset that is not
|
|
41
|
+
* a whole number of minutes (local mean time) comes back fractional.
|
|
42
|
+
* @param {number} timestamp - Unix timestamp in seconds.
|
|
43
|
+
* @param {string} timeZone - IANA zone name.
|
|
44
|
+
* @returns {DateParts} Wall-clock parts and offset in the zone.
|
|
45
|
+
*/
|
|
46
|
+
export function toZonedDateParts(timestamp: number, timeZone: string): DateParts;
|
|
23
47
|
/**
|
|
24
48
|
* Returns a formatted date and unix timestamp.
|
|
25
49
|
* @param {Date} [date=new Date()] - Date value to format.
|
|
@@ -29,3 +53,34 @@ export function toDatePayload(date?: Date): {
|
|
|
29
53
|
date: string;
|
|
30
54
|
timestamp: number;
|
|
31
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Parses a header datetime string into its wall-clock parts, UTC offset, and unix timestamp.
|
|
58
|
+
* Calendar-invalid values (month 13, February 30, hour 24, ...) are rejected rather than rolled over.
|
|
59
|
+
* @param {string} text - Datetime text as written in a header.
|
|
60
|
+
* @returns {{ year: number, month: number, day: number, hour: number, minute: number, second: number, offsetMinutes: number, timestamp: number } | null} Parsed datetime, or null when unrecognised.
|
|
61
|
+
*/
|
|
62
|
+
export function parseHeaderDate(text: string): {
|
|
63
|
+
year: number;
|
|
64
|
+
month: number;
|
|
65
|
+
day: number;
|
|
66
|
+
hour: number;
|
|
67
|
+
minute: number;
|
|
68
|
+
second: number;
|
|
69
|
+
offsetMinutes: number;
|
|
70
|
+
timestamp: number;
|
|
71
|
+
} | null;
|
|
72
|
+
/**
|
|
73
|
+
* Renders parsed datetime parts in the git `%aI` form (`YYYY-MM-DDTHH:mm:ss±HH:MM`), keeping the original offset.
|
|
74
|
+
* @param {DateParts} parts - Parsed datetime parts.
|
|
75
|
+
* @returns {string} ISO 8601 datetime text.
|
|
76
|
+
*/
|
|
77
|
+
export function formatIsoDate(parts: DateParts): string;
|
|
78
|
+
export type DateParts = {
|
|
79
|
+
year: number;
|
|
80
|
+
month: number;
|
|
81
|
+
day: number;
|
|
82
|
+
hour: number;
|
|
83
|
+
minute: number;
|
|
84
|
+
second: number;
|
|
85
|
+
offsetMinutes: number;
|
|
86
|
+
};
|
package/index.cjs
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /index.cjs
|
|
4
|
-
* @Date: 2026-03-01 13:29:45 -08:00 (1772400585)
|
|
5
|
-
* @Author: Nate Corcoran <CLDMV>
|
|
6
|
-
* @Email: <Shinrai@users.noreply.github.com>
|
|
7
|
-
* -----
|
|
8
|
-
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
9
|
-
* @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
|
|
10
|
-
* -----
|
|
11
|
-
* @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @fileoverview CJS shim for consuming the ESM-based fix-headers module.
|
|
16
|
-
* @module fix-headers/cjs-shim
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
"use strict";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Loads the ESM module implementation.
|
|
23
|
-
* @returns {Promise<import("./index.mjs")>} Loaded ESM module.
|
|
24
|
-
*/
|
|
25
|
-
function loadEsmModule() {
|
|
26
|
-
return import("./index.mjs");
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Runs header normalization with auto-detection and override options.
|
|
31
|
-
* @param {Record<string, unknown>} [options] - Runtime options.
|
|
32
|
-
* @returns {Promise<unknown>} Header update report.
|
|
33
|
-
*/
|
|
34
|
-
async function fixHeaders(options) {
|
|
35
|
-
const mod = await loadEsmModule();
|
|
36
|
-
return mod.default(options);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
module.exports = fixHeaders;
|