@cldmv/fix-headers 1.3.12 → 2.1.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 +354 -25
- package/bin/fix-headers.mjs +64 -0
- package/dist/index.cjs +28 -0
- package/dist/index.mjs +15 -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 +15 -23
- package/types/src/core/file-discovery.d.mts +17 -5
- package/types/src/core/fix-headers.d.mts +61 -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 +31 -8
- 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/parser.d.mts +6 -2
- package/types/src/header/syntax.d.mts +8 -3
- package/types/src/header/template.d.mts +7 -4
- 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
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/core/file-discovery.mjs
|
|
4
|
-
* @Date: 2026-03-01T17:59:32-08:00 (1772416772)
|
|
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-04 20:59:30 -08:00 (1772686770)
|
|
10
|
-
* -----
|
|
11
|
-
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { join, relative, resolve } from "node:path";
|
|
15
|
-
import { readFile, stat } from "node:fs/promises";
|
|
16
|
-
import ignore from "ignore";
|
|
17
|
-
import { ALWAYS_IGNORE_FOLDERS, ROOT_IGNORE_FOLDERS } from "../constants.mjs";
|
|
18
|
-
import { getAllowedExtensions } from "../detectors/index.mjs";
|
|
19
|
-
import { walkFiles } from "../utils/fs.mjs";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @fileoverview Resolves candidate source files for header updates based on language and options.
|
|
23
|
-
* @module fix-headers/core/file-discovery
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Resolves extension set from enabled detectors and optional override.
|
|
28
|
-
* @param {{
|
|
29
|
-
* includeExtensions?: string[],
|
|
30
|
-
* enabledDetectors?: string[],
|
|
31
|
-
* disabledDetectors?: string[]
|
|
32
|
-
* }} options - Extension options.
|
|
33
|
-
* @returns {Set<string>} Effective extension set.
|
|
34
|
-
*/
|
|
35
|
-
function resolveExtensions(options) {
|
|
36
|
-
return getAllowedExtensions(options);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const ALWAYS_IGNORED_FOLDERS = new Set(ALWAYS_IGNORE_FOLDERS);
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Normalizes a user-provided folder path to a project-relative value.
|
|
43
|
-
* @param {string} folderPath - Folder path string.
|
|
44
|
-
* @returns {string} Normalized path.
|
|
45
|
-
*/
|
|
46
|
-
function normalizeFolderPath(folderPath) {
|
|
47
|
-
return folderPath.replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "").replace(/\/+$/, "");
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Determines include roots from explicit include folders.
|
|
52
|
-
* @param {string[] | undefined} includeFolders - Include folder option.
|
|
53
|
-
* @returns {string[]} Effective include roots.
|
|
54
|
-
*/
|
|
55
|
-
function resolveIncludeFolders(includeFolders) {
|
|
56
|
-
if (Array.isArray(includeFolders) && includeFolders.length > 0) {
|
|
57
|
-
return includeFolders;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return [];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Warns when an include folder cannot be scanned and is skipped.
|
|
65
|
-
* @param {string} includeFolder - Project-relative include folder.
|
|
66
|
-
* @param {unknown} error - Original filesystem error.
|
|
67
|
-
* @returns {void}
|
|
68
|
-
*/
|
|
69
|
-
function reportSkippedIncludeFolder(includeFolder, error) {
|
|
70
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
71
|
-
console.warn(`fix-headers: skipped include folder \"${includeFolder}\" (${message})`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Builds a directory exclusion matcher from path and folder-name lists.
|
|
76
|
-
* @param {string} projectRoot - Project root path.
|
|
77
|
-
* @param {string[] | undefined} excludeFolders - Folder exclusions.
|
|
78
|
-
* @returns {(targetPath: string, targetName: string) => boolean} Exclusion predicate.
|
|
79
|
-
*/
|
|
80
|
-
function buildExclusionMatcher(projectRoot, excludeFolders) {
|
|
81
|
-
if (!Array.isArray(excludeFolders) || excludeFolders.length === 0) {
|
|
82
|
-
return () => false;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const absoluteRoot = resolve(projectRoot);
|
|
86
|
-
const pathExclusions = [];
|
|
87
|
-
const nameExclusions = new Set();
|
|
88
|
-
|
|
89
|
-
for (const entry of excludeFolders) {
|
|
90
|
-
if (typeof entry !== "string") {
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const normalized = normalizeFolderPath(entry);
|
|
95
|
-
if (normalized.length === 0) {
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (normalized.includes("/")) {
|
|
100
|
-
pathExclusions.push(resolve(absoluteRoot, normalized));
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
nameExclusions.add(normalized);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return (targetPath, targetName) => {
|
|
108
|
-
if (nameExclusions.has(targetName)) {
|
|
109
|
-
return true;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
for (const excludedPath of pathExclusions) {
|
|
113
|
-
if (targetPath === excludedPath || targetPath.startsWith(`${excludedPath}/`)) {
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const relPath = normalizeFolderPath(relative(absoluteRoot, targetPath));
|
|
119
|
-
return pathExclusions.some((excludedPath) => {
|
|
120
|
-
const relExcluded = normalizeFolderPath(relative(absoluteRoot, excludedPath));
|
|
121
|
-
return relPath === relExcluded || relPath.startsWith(`${relExcluded}/`);
|
|
122
|
-
});
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Discovers source files for processing.
|
|
128
|
-
* @param {{
|
|
129
|
-
* projectRoot: string,
|
|
130
|
-
* language?: string,
|
|
131
|
-
* includeExtensions?: string[],
|
|
132
|
-
* enabledDetectors?: string[],
|
|
133
|
-
* disabledDetectors?: string[],
|
|
134
|
-
* includeFolders?: string[],
|
|
135
|
-
* excludeFolders?: string[],
|
|
136
|
-
* gitignore?: boolean | string | string[]
|
|
137
|
-
* }} options - File discovery options. `gitignore`: `false` disables; a path or array of
|
|
138
|
-
* paths loads those ignore files; anything else / omitted auto-detects `<projectRoot>/.gitignore`.
|
|
139
|
-
* @returns {Promise<string[]>} Absolute file paths.
|
|
140
|
-
*/
|
|
141
|
-
export async function discoverFiles(options) {
|
|
142
|
-
const allowedExtensions = resolveExtensions(options);
|
|
143
|
-
const includeFolders = resolveIncludeFolders(options.includeFolders);
|
|
144
|
-
const excludeFolders = Array.isArray(options.excludeFolders) ? options.excludeFolders : [];
|
|
145
|
-
const exclusionMatcher = buildExclusionMatcher(options.projectRoot, excludeFolders);
|
|
146
|
-
// Build/cache dirs (ROOT_IGNORE_FOLDERS) are ignored only at the PROJECT ROOT, so a nested
|
|
147
|
-
// source directory with the same name (e.g. tools/build) is still processed. node_modules /
|
|
148
|
-
// .git stay any-depth via walkFiles' `ignoreFolders` below.
|
|
149
|
-
const absoluteRoot = resolve(options.projectRoot);
|
|
150
|
-
const rootIgnorePaths = new Set(Array.from(ROOT_IGNORE_FOLDERS, (name) => resolve(absoluteRoot, name)));
|
|
151
|
-
// Optional .gitignore support: skip anything the project's .gitignore matches, so generated /
|
|
152
|
-
// output paths are excluded by the project's own rules without hard-coding names. `options.gitignore`
|
|
153
|
-
// may be `false` (disabled), a file path or array of paths, or anything else / undefined
|
|
154
|
-
// (auto-detect `<projectRoot>/.gitignore`).
|
|
155
|
-
let gitignoreMatcher = null;
|
|
156
|
-
if (options.gitignore !== false) {
|
|
157
|
-
const gitignoreFiles =
|
|
158
|
-
typeof options.gitignore === "string"
|
|
159
|
-
? [options.gitignore]
|
|
160
|
-
: Array.isArray(options.gitignore)
|
|
161
|
-
? options.gitignore.filter((entry) => typeof entry === "string")
|
|
162
|
-
: [".gitignore"];
|
|
163
|
-
const ig = ignore();
|
|
164
|
-
let loaded = false;
|
|
165
|
-
for (const rel of gitignoreFiles) {
|
|
166
|
-
try {
|
|
167
|
-
ig.add(await readFile(resolve(absoluteRoot, rel), "utf8"));
|
|
168
|
-
loaded = true;
|
|
169
|
-
} catch {
|
|
170
|
-
/* missing .gitignore — nothing to add */
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
gitignoreMatcher = loaded ? ig : null;
|
|
174
|
-
}
|
|
175
|
-
const isGitignored = (targetPath) => {
|
|
176
|
-
if (!gitignoreMatcher) return false;
|
|
177
|
-
const rel = relative(absoluteRoot, resolve(targetPath)).replace(/\\/g, "/");
|
|
178
|
-
return rel.length > 0 && !rel.startsWith("..") && gitignoreMatcher.ignores(rel);
|
|
179
|
-
};
|
|
180
|
-
const shouldSkipDirectory = (targetPath, targetName) =>
|
|
181
|
-
rootIgnorePaths.has(resolve(targetPath)) || exclusionMatcher(targetPath, targetName) || isGitignored(targetPath);
|
|
182
|
-
const roots =
|
|
183
|
-
includeFolders.length > 0
|
|
184
|
-
? includeFolders.map((includeFolder) => ({ includeFolder, rootPath: join(options.projectRoot, includeFolder) }))
|
|
185
|
-
: [{ includeFolder: ".", rootPath: options.projectRoot }];
|
|
186
|
-
|
|
187
|
-
const files = [];
|
|
188
|
-
for (const root of roots) {
|
|
189
|
-
const rootStats = await stat(root.rootPath).catch((error) => error);
|
|
190
|
-
if (rootStats instanceof Error) {
|
|
191
|
-
if (/** @type {{ code?: string }} */ (rootStats).code === "ENOENT") {
|
|
192
|
-
reportSkippedIncludeFolder(root.includeFolder, rootStats);
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
throw rootStats;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (!rootStats.isDirectory()) {
|
|
200
|
-
reportSkippedIncludeFolder(root.includeFolder, `path is not a directory: ${root.rootPath}`);
|
|
201
|
-
continue;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const discovered = await walkFiles(root.rootPath, {
|
|
205
|
-
allowedExtensions,
|
|
206
|
-
ignoreFolders: ALWAYS_IGNORED_FOLDERS,
|
|
207
|
-
shouldSkipDirectory
|
|
208
|
-
});
|
|
209
|
-
files.push(...discovered);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return files.filter((filePath) => !exclusionMatcher(filePath, filePath.split("/").at(-1) || "") && !isGitignored(filePath));
|
|
213
|
-
}
|
package/src/core/fix-headers.mjs
DELETED
|
@@ -1,413 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/core/fix-headers.mjs
|
|
4
|
-
* @Date: 2026-03-01T17:59:32-08:00 (1772416772)
|
|
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
|
-
import { readFile, stat, writeFile } from "node:fs/promises";
|
|
15
|
-
import { relative, resolve } from "node:path";
|
|
16
|
-
import { discoverFiles } from "./file-discovery.mjs";
|
|
17
|
-
import { resolveProjectMetadata } from "../detect/project.mjs";
|
|
18
|
-
import { buildHeader } from "../header/template.mjs";
|
|
19
|
-
import { findProjectHeader, replaceOrInsertHeader } from "../header/parser.mjs";
|
|
20
|
-
import { readFileDates } from "../utils/fs.mjs";
|
|
21
|
-
import { getGitCreationDate, getGitLastModifiedDate } from "../utils/git.mjs";
|
|
22
|
-
import { toDatePayload } from "../utils/time.mjs";
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @typedef {{
|
|
26
|
-
* cwd?: string,
|
|
27
|
-
* input?: string,
|
|
28
|
-
* dryRun?: boolean,
|
|
29
|
-
* configFile?: string,
|
|
30
|
-
* sampleOutput?: boolean,
|
|
31
|
-
* forceAuthorUpdate?: boolean,
|
|
32
|
-
* forceLastModifiedAuthorUpdate?: boolean,
|
|
33
|
-
* useGpgSignerAuthor?: boolean,
|
|
34
|
-
* enabledDetectors?: string[],
|
|
35
|
-
* disabledDetectors?: string[],
|
|
36
|
-
* detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }>,
|
|
37
|
-
* includeFolders?: string[],
|
|
38
|
-
* excludeFolders?: string[],
|
|
39
|
-
* includeExtensions?: string[],
|
|
40
|
-
* gitignore?: boolean | string | string[],
|
|
41
|
-
* projectName?: string,
|
|
42
|
-
* language?: string,
|
|
43
|
-
* projectRoot?: string,
|
|
44
|
-
* marker?: string | null,
|
|
45
|
-
* authorName?: string,
|
|
46
|
-
* authorEmail?: string,
|
|
47
|
-
* company?: string,
|
|
48
|
-
* companyName?: string,
|
|
49
|
-
* copyrightStartYear?: number
|
|
50
|
-
* }} FixHeadersOptions
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @typedef {{
|
|
55
|
-
* metadata: {
|
|
56
|
-
* projectName: string,
|
|
57
|
-
* language: string,
|
|
58
|
-
* projectRoot: string,
|
|
59
|
-
* marker: string | null,
|
|
60
|
-
* authorName: string,
|
|
61
|
-
* authorEmail: string,
|
|
62
|
-
* companyName: string,
|
|
63
|
-
* copyrightStartYear: number
|
|
64
|
-
* },
|
|
65
|
-
* detectedProjects: string[],
|
|
66
|
-
* filesScanned: number,
|
|
67
|
-
* filesUpdated: number,
|
|
68
|
-
* dryRun: boolean,
|
|
69
|
-
* changes: Array<{file: string, changed: boolean, sample?: { previousValue: string | null, newValue: string, detectedValues?: {
|
|
70
|
-
* projectName: string,
|
|
71
|
-
* language: string,
|
|
72
|
-
* projectRoot: string,
|
|
73
|
-
* marker: string | null,
|
|
74
|
-
* authorName: string,
|
|
75
|
-
* authorEmail: string,
|
|
76
|
-
* companyName: string,
|
|
77
|
-
* copyrightStartYear: number,
|
|
78
|
-
* createdAtSource: string,
|
|
79
|
-
* lastModifiedAtSource: string,
|
|
80
|
-
* createdAt: {date: string, timestamp: number},
|
|
81
|
-
* lastModifiedAt: {date: string, timestamp: number}
|
|
82
|
-
* } }}>
|
|
83
|
-
* }} FixHeadersResult
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Resolves runtime options including optional JSON config file loading.
|
|
88
|
-
* @param {FixHeadersOptions} options - Runtime options.
|
|
89
|
-
* @returns {Promise<FixHeadersOptions>} Effective runtime options.
|
|
90
|
-
*/
|
|
91
|
-
async function resolveRuntimeOptions(options) {
|
|
92
|
-
const configFile = typeof options.configFile === "string" && options.configFile.trim().length > 0 ? options.configFile.trim() : null;
|
|
93
|
-
|
|
94
|
-
if (!configFile) {
|
|
95
|
-
return options;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const baseCwd = typeof options.cwd === "string" && options.cwd.length > 0 ? options.cwd : process.cwd();
|
|
99
|
-
const absoluteConfigPath = resolve(baseCwd, configFile);
|
|
100
|
-
const raw = await readFile(absoluteConfigPath, "utf8");
|
|
101
|
-
const parsed = JSON.parse(raw);
|
|
102
|
-
|
|
103
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
104
|
-
throw new Error(`Config file must contain a JSON object: ${configFile}`);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const merged = {
|
|
108
|
-
...parsed,
|
|
109
|
-
...options
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
delete merged.configFile;
|
|
113
|
-
/** @type {FixHeadersOptions} */
|
|
114
|
-
const output = merged;
|
|
115
|
-
return output;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Extracts original author identity from an existing header block.
|
|
120
|
-
* @param {string} headerText - Existing header content.
|
|
121
|
-
* @returns {{ authorName?: string, authorEmail?: string }} Parsed identity values.
|
|
122
|
-
*/
|
|
123
|
-
function extractHeaderAuthorIdentity(headerText) {
|
|
124
|
-
const authorMatch = headerText.match(/@Author:\s*(.+)$/m);
|
|
125
|
-
const emailMatch = headerText.match(/@Email:\s*<([^>\n]+)>/m);
|
|
126
|
-
|
|
127
|
-
return {
|
|
128
|
-
authorName: authorMatch?.[1]?.trim(),
|
|
129
|
-
authorEmail: emailMatch?.[1]?.trim()
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Extracts original last-modified-by identity from an existing header block.
|
|
135
|
-
* @param {string} headerText - Existing header content.
|
|
136
|
-
* @returns {{ authorName?: string, authorEmail?: string }} Parsed identity values.
|
|
137
|
-
*/
|
|
138
|
-
function extractHeaderLastModifiedIdentity(headerText) {
|
|
139
|
-
const match = headerText.match(/@Last modified by:\s*(.+?)\s*\(([^)\n]+)\)\s*$/m);
|
|
140
|
-
|
|
141
|
-
return {
|
|
142
|
-
authorName: match?.[1]?.trim(),
|
|
143
|
-
authorEmail: match?.[2]?.trim()
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Extracts original created-at payload from an existing header block.
|
|
149
|
-
* @param {string} headerText - Existing header content.
|
|
150
|
-
* @returns {{date: string, timestamp: number} | null} Parsed created-at value.
|
|
151
|
-
*/
|
|
152
|
-
function extractHeaderCreatedAt(headerText) {
|
|
153
|
-
const dateMatch = headerText.match(/@Date:\s*(.+?)\s*\((\d+)\)$/m);
|
|
154
|
-
if (!dateMatch || !dateMatch[1] || !dateMatch[2]) {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const timestamp = Number.parseInt(dateMatch[2], 10);
|
|
159
|
-
if (Number.isNaN(timestamp)) {
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return {
|
|
164
|
-
date: dateMatch[1].trim(),
|
|
165
|
-
timestamp
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Extracts original last-modified payload from an existing header block.
|
|
171
|
-
* @param {string} headerText - Existing header content.
|
|
172
|
-
* @returns {{date: string, timestamp: number} | null} Parsed last-modified value.
|
|
173
|
-
*/
|
|
174
|
-
function extractHeaderLastModifiedAt(headerText) {
|
|
175
|
-
const modifiedMatch = headerText.match(/@Last modified time:\s*(.+?)\s*\((\d+)\)$/m);
|
|
176
|
-
if (!modifiedMatch || !modifiedMatch[1] || !modifiedMatch[2]) {
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const timestamp = Number.parseInt(modifiedMatch[2], 10);
|
|
181
|
-
if (Number.isNaN(timestamp)) {
|
|
182
|
-
return null;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return {
|
|
186
|
-
date: modifiedMatch[1].trim(),
|
|
187
|
-
timestamp
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* @fileoverview Main header-fixing engine with auto-detection and override support.
|
|
193
|
-
* @module fix-headers/core/fix-headers
|
|
194
|
-
*/
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Fixes headers in a project using auto-detected metadata unless overridden.
|
|
198
|
-
* @param {FixHeadersOptions} [options={}] - Runtime options.
|
|
199
|
-
* @returns {Promise<FixHeadersResult>} Process report.
|
|
200
|
-
*/
|
|
201
|
-
export async function fixHeaders(options = {}) {
|
|
202
|
-
const effectiveOptions = await resolveRuntimeOptions(options);
|
|
203
|
-
const scanRoot = resolve(effectiveOptions.projectRoot || effectiveOptions.cwd || process.cwd());
|
|
204
|
-
const metadata = await resolveProjectMetadata({
|
|
205
|
-
...effectiveOptions,
|
|
206
|
-
cwd: scanRoot
|
|
207
|
-
});
|
|
208
|
-
const dryRun = effectiveOptions.dryRun === true;
|
|
209
|
-
|
|
210
|
-
let files = [];
|
|
211
|
-
if (typeof effectiveOptions.input === "string" && effectiveOptions.input.trim().length > 0) {
|
|
212
|
-
const inputPath = resolve(scanRoot, effectiveOptions.input);
|
|
213
|
-
const targetStats = await stat(inputPath).catch(() => null);
|
|
214
|
-
if (!targetStats) {
|
|
215
|
-
throw new Error(`Input path does not exist: ${effectiveOptions.input}`);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (targetStats.isFile()) {
|
|
219
|
-
files = [inputPath];
|
|
220
|
-
} else if (targetStats.isDirectory()) {
|
|
221
|
-
files = await discoverFiles({
|
|
222
|
-
projectRoot: inputPath,
|
|
223
|
-
language: metadata.language,
|
|
224
|
-
enabledDetectors: effectiveOptions.enabledDetectors,
|
|
225
|
-
disabledDetectors: effectiveOptions.disabledDetectors,
|
|
226
|
-
includeFolders: effectiveOptions.includeFolders,
|
|
227
|
-
excludeFolders: effectiveOptions.excludeFolders,
|
|
228
|
-
includeExtensions: effectiveOptions.includeExtensions,
|
|
229
|
-
gitignore: effectiveOptions.gitignore
|
|
230
|
-
});
|
|
231
|
-
} else {
|
|
232
|
-
throw new Error(`Input path must be a file or directory: ${effectiveOptions.input}`);
|
|
233
|
-
}
|
|
234
|
-
} else {
|
|
235
|
-
files = await discoverFiles({
|
|
236
|
-
projectRoot: scanRoot,
|
|
237
|
-
language: metadata.language,
|
|
238
|
-
enabledDetectors: effectiveOptions.enabledDetectors,
|
|
239
|
-
disabledDetectors: effectiveOptions.disabledDetectors,
|
|
240
|
-
includeFolders: effectiveOptions.includeFolders,
|
|
241
|
-
excludeFolders: effectiveOptions.excludeFolders,
|
|
242
|
-
includeExtensions: effectiveOptions.includeExtensions,
|
|
243
|
-
gitignore: effectiveOptions.gitignore
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const currentYear = new Date().getFullYear();
|
|
248
|
-
/** @type {FixHeadersResult["changes"]} */
|
|
249
|
-
const changes = [];
|
|
250
|
-
const detectedProjects = new Set();
|
|
251
|
-
let filesUpdated = 0;
|
|
252
|
-
|
|
253
|
-
for (const filePath of files) {
|
|
254
|
-
const fileMetadata = await resolveProjectMetadata({
|
|
255
|
-
...effectiveOptions,
|
|
256
|
-
cwd: scanRoot,
|
|
257
|
-
targetFilePath: filePath
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
detectedProjects.add(`${fileMetadata.language}:${fileMetadata.projectRoot}`);
|
|
261
|
-
const relativePath = relative(scanRoot, filePath);
|
|
262
|
-
const original = await readFile(filePath, "utf8");
|
|
263
|
-
const existingHeader = findProjectHeader(original, filePath, {
|
|
264
|
-
language: fileMetadata.language,
|
|
265
|
-
enabledDetectors: effectiveOptions.enabledDetectors,
|
|
266
|
-
disabledDetectors: effectiveOptions.disabledDetectors,
|
|
267
|
-
detectorSyntaxOverrides: effectiveOptions.detectorSyntaxOverrides
|
|
268
|
-
});
|
|
269
|
-
const existingHeaderText = existingHeader ? original.slice(existingHeader.start, existingHeader.end) : "";
|
|
270
|
-
const existingIdentity = existingHeaderText.length > 0 ? extractHeaderAuthorIdentity(existingHeaderText) : {};
|
|
271
|
-
const existingLastModifiedIdentity = existingHeaderText.length > 0 ? extractHeaderLastModifiedIdentity(existingHeaderText) : {};
|
|
272
|
-
const existingCreatedAt = existingHeaderText.length > 0 ? extractHeaderCreatedAt(existingHeaderText) : null;
|
|
273
|
-
const existingLastModifiedAt = existingHeaderText.length > 0 ? extractHeaderLastModifiedAt(existingHeaderText) : null;
|
|
274
|
-
const filesystemDates = await readFileDates(filePath);
|
|
275
|
-
|
|
276
|
-
const metadataRelativePath = relative(fileMetadata.projectRoot, filePath);
|
|
277
|
-
const gitCreated = await getGitCreationDate(fileMetadata.projectRoot, metadataRelativePath);
|
|
278
|
-
const gitLastUpdated = await getGitLastModifiedDate(fileMetadata.projectRoot, metadataRelativePath);
|
|
279
|
-
|
|
280
|
-
const createdAtSource = existingCreatedAt ? "existing-header" : gitCreated ? "git-created" : "filesystem-created";
|
|
281
|
-
const comparisonLastModifiedAtSource = existingLastModifiedAt
|
|
282
|
-
? "existing-header"
|
|
283
|
-
: gitLastUpdated
|
|
284
|
-
? "git-last-modified"
|
|
285
|
-
: "filesystem-updated";
|
|
286
|
-
const createdAt = existingCreatedAt || gitCreated || toDatePayload(filesystemDates.createdAt);
|
|
287
|
-
const comparisonLastModifiedAt = existingLastModifiedAt || gitLastUpdated || toDatePayload(filesystemDates.updatedAt);
|
|
288
|
-
const shouldForceAuthorUpdate = effectiveOptions.forceAuthorUpdate === true;
|
|
289
|
-
const shouldForceLastModifiedAuthorUpdate = effectiveOptions.forceLastModifiedAuthorUpdate === true;
|
|
290
|
-
|
|
291
|
-
const comparisonHeader = buildHeader({
|
|
292
|
-
absoluteFilePath: filePath,
|
|
293
|
-
language: fileMetadata.language,
|
|
294
|
-
syntaxOptions: {
|
|
295
|
-
language: fileMetadata.language,
|
|
296
|
-
enabledDetectors: effectiveOptions.enabledDetectors,
|
|
297
|
-
disabledDetectors: effectiveOptions.disabledDetectors,
|
|
298
|
-
detectorSyntaxOverrides: effectiveOptions.detectorSyntaxOverrides
|
|
299
|
-
},
|
|
300
|
-
projectRoot: fileMetadata.projectRoot,
|
|
301
|
-
projectName: fileMetadata.projectName,
|
|
302
|
-
createdByName: shouldForceAuthorUpdate ? fileMetadata.authorName : existingIdentity.authorName || fileMetadata.authorName,
|
|
303
|
-
createdByEmail: shouldForceAuthorUpdate ? fileMetadata.authorEmail : existingIdentity.authorEmail || fileMetadata.authorEmail,
|
|
304
|
-
lastModifiedByName: shouldForceLastModifiedAuthorUpdate
|
|
305
|
-
? fileMetadata.authorName
|
|
306
|
-
: existingLastModifiedIdentity.authorName || fileMetadata.authorName,
|
|
307
|
-
lastModifiedByEmail: shouldForceLastModifiedAuthorUpdate
|
|
308
|
-
? fileMetadata.authorEmail
|
|
309
|
-
: existingLastModifiedIdentity.authorEmail || fileMetadata.authorEmail,
|
|
310
|
-
authorName: fileMetadata.authorName,
|
|
311
|
-
authorEmail: fileMetadata.authorEmail,
|
|
312
|
-
createdAt,
|
|
313
|
-
lastModifiedAt: comparisonLastModifiedAt,
|
|
314
|
-
copyrightStartYear: fileMetadata.copyrightStartYear,
|
|
315
|
-
companyName: fileMetadata.companyName,
|
|
316
|
-
currentYear
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
const comparisonReplacement = replaceOrInsertHeader(original, comparisonHeader, filePath, {
|
|
320
|
-
language: fileMetadata.language,
|
|
321
|
-
enabledDetectors: effectiveOptions.enabledDetectors,
|
|
322
|
-
disabledDetectors: effectiveOptions.disabledDetectors,
|
|
323
|
-
detectorSyntaxOverrides: effectiveOptions.detectorSyntaxOverrides
|
|
324
|
-
});
|
|
325
|
-
const needsUpdate = comparisonReplacement.changed;
|
|
326
|
-
const finalLastModifiedAt = needsUpdate ? toDatePayload(new Date()) : comparisonLastModifiedAt;
|
|
327
|
-
const lastModifiedAtSource = needsUpdate ? "current-time-on-change" : comparisonLastModifiedAtSource;
|
|
328
|
-
|
|
329
|
-
const header = needsUpdate
|
|
330
|
-
? buildHeader({
|
|
331
|
-
absoluteFilePath: filePath,
|
|
332
|
-
language: fileMetadata.language,
|
|
333
|
-
syntaxOptions: {
|
|
334
|
-
language: fileMetadata.language,
|
|
335
|
-
enabledDetectors: effectiveOptions.enabledDetectors,
|
|
336
|
-
disabledDetectors: effectiveOptions.disabledDetectors,
|
|
337
|
-
detectorSyntaxOverrides: effectiveOptions.detectorSyntaxOverrides
|
|
338
|
-
},
|
|
339
|
-
projectRoot: fileMetadata.projectRoot,
|
|
340
|
-
projectName: fileMetadata.projectName,
|
|
341
|
-
createdByName: shouldForceAuthorUpdate ? fileMetadata.authorName : existingIdentity.authorName || fileMetadata.authorName,
|
|
342
|
-
createdByEmail: shouldForceAuthorUpdate ? fileMetadata.authorEmail : existingIdentity.authorEmail || fileMetadata.authorEmail,
|
|
343
|
-
lastModifiedByName: shouldForceLastModifiedAuthorUpdate
|
|
344
|
-
? fileMetadata.authorName
|
|
345
|
-
: existingLastModifiedIdentity.authorName || fileMetadata.authorName,
|
|
346
|
-
lastModifiedByEmail: shouldForceLastModifiedAuthorUpdate
|
|
347
|
-
? fileMetadata.authorEmail
|
|
348
|
-
: existingLastModifiedIdentity.authorEmail || fileMetadata.authorEmail,
|
|
349
|
-
authorName: fileMetadata.authorName,
|
|
350
|
-
authorEmail: fileMetadata.authorEmail,
|
|
351
|
-
createdAt,
|
|
352
|
-
lastModifiedAt: finalLastModifiedAt,
|
|
353
|
-
copyrightStartYear: fileMetadata.copyrightStartYear,
|
|
354
|
-
companyName: fileMetadata.companyName,
|
|
355
|
-
currentYear
|
|
356
|
-
})
|
|
357
|
-
: comparisonHeader;
|
|
358
|
-
|
|
359
|
-
const replacement = needsUpdate
|
|
360
|
-
? replaceOrInsertHeader(original, header, filePath, {
|
|
361
|
-
language: fileMetadata.language,
|
|
362
|
-
enabledDetectors: effectiveOptions.enabledDetectors,
|
|
363
|
-
disabledDetectors: effectiveOptions.disabledDetectors,
|
|
364
|
-
detectorSyntaxOverrides: effectiveOptions.detectorSyntaxOverrides
|
|
365
|
-
})
|
|
366
|
-
: comparisonReplacement;
|
|
367
|
-
const changeEntry = {
|
|
368
|
-
file: relativePath,
|
|
369
|
-
changed: replacement.changed
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
if (effectiveOptions.sampleOutput === true && replacement.changed) {
|
|
373
|
-
changeEntry.sample = {
|
|
374
|
-
previousValue: existingHeaderText.length > 0 ? existingHeaderText.trimEnd() : null,
|
|
375
|
-
newValue: header,
|
|
376
|
-
detectedValues: {
|
|
377
|
-
projectName: fileMetadata.projectName,
|
|
378
|
-
language: fileMetadata.language,
|
|
379
|
-
projectRoot: fileMetadata.projectRoot,
|
|
380
|
-
marker: fileMetadata.marker,
|
|
381
|
-
authorName: fileMetadata.authorName,
|
|
382
|
-
authorEmail: fileMetadata.authorEmail,
|
|
383
|
-
companyName: fileMetadata.companyName,
|
|
384
|
-
copyrightStartYear: fileMetadata.copyrightStartYear,
|
|
385
|
-
createdAtSource,
|
|
386
|
-
lastModifiedAtSource,
|
|
387
|
-
createdAt,
|
|
388
|
-
lastModifiedAt: finalLastModifiedAt
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
changes.push(changeEntry);
|
|
394
|
-
|
|
395
|
-
if (!replacement.changed) {
|
|
396
|
-
continue;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
filesUpdated += 1;
|
|
400
|
-
if (!dryRun) {
|
|
401
|
-
await writeFile(filePath, replacement.nextContent, "utf8");
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
return {
|
|
406
|
-
metadata,
|
|
407
|
-
detectedProjects: Array.from(detectedProjects),
|
|
408
|
-
filesScanned: files.length,
|
|
409
|
-
filesUpdated,
|
|
410
|
-
dryRun,
|
|
411
|
-
changes
|
|
412
|
-
};
|
|
413
|
-
}
|