@cldmv/fix-headers 1.3.11 → 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 +42 -22
- 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
package/index.mjs
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /index.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
|
-
/**
|
|
15
|
-
* @fileoverview ESM shim for the fix-headers package API.
|
|
16
|
-
* @module fix-headers/esm-shim
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
export * from "./src/fix-header.mjs";
|
|
20
|
-
export { default } from "./src/fix-header.mjs";
|
package/src/cli.mjs
DELETED
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* @Project: @cldmv/fix-headers
|
|
4
|
-
* @Filename: /src/cli.mjs
|
|
5
|
-
* @Date: 2026-03-01T17:59:32-08:00 (1772416772)
|
|
6
|
-
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
-
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
-
* -----
|
|
9
|
-
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
-
* @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
|
|
11
|
-
* -----
|
|
12
|
-
* @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { readFile } from "node:fs/promises";
|
|
16
|
-
import { realpathSync } from "node:fs";
|
|
17
|
-
import { isAbsolute, join } from "node:path";
|
|
18
|
-
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
19
|
-
import fixHeaders from "./fix-header.mjs";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @fileoverview CLI entry point for running fix-headers from terminal commands.
|
|
23
|
-
* @module fix-headers/cli
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
const HELP_TEXT = `fix-headers CLI\n\nUsage:\n fix-headers [options]\n\nOptions:\n -h, --help Show help\n --dry-run Compute changes without writing files\n --json Print JSON output\n --verbose Print updated file paths in summary mode\n --sample-output Show previous/new header sample for changed files\n --force-author-update Always update @Author/@Email to detected/current values\n --force-last-modified-author-update Always update @Last modified by to detected/current values\n --use-gpg-signer-author Use signed-commit UID (%GS) for detected @Author\n --cwd <path> Working directory for project detection\n --input <path> Single file or folder input\n --include-folder <path> Include folder (repeatable)\n --exclude-folder <path> Exclude folder name/path (repeatable)\n --include-extension <ext> Include extension (repeatable)\n --enable-detector <id> Enable only specific detector (repeatable)\n --disable-detector <id> Disable detector by id (repeatable)\n --project-name <name> Override project name\n --language <id> Override language id\n --project-root <path> Override project root\n --marker <name|null> Override marker filename\n --author-name <name> Override author name\n --author-email <email> Override author email\n --company <name> Append company suffix to @Author (Name <Company>)\n --company-name <name> Override company name\n --copyright-start-year <year> Override copyright start year\n --config <path> Load JSON options file\n\nExamples:\n fix-headers --dry-run --include-folder src\n fix-headers --project-name @scope/pkg --company-name "Catalyzed Motivation Inc."\n`;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Converts CLI flag token to camelCase key.
|
|
30
|
-
* @param {string} token - CLI token without leading dashes.
|
|
31
|
-
* @returns {string} CamelCase key.
|
|
32
|
-
*/
|
|
33
|
-
function toCamelCase(token) {
|
|
34
|
-
return token.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Parses CLI arguments into fixHeaders options and control flags.
|
|
39
|
-
* @param {string[]} argv - Process argument vector without node/script items.
|
|
40
|
-
* @returns {{
|
|
41
|
-
* options: Record<string, unknown>,
|
|
42
|
-
* help: boolean,
|
|
43
|
-
* json: boolean
|
|
44
|
-
* }} Parsed CLI payload.
|
|
45
|
-
*/
|
|
46
|
-
export function parseCliArgs(argv) {
|
|
47
|
-
/** @type {Record<string, unknown>} */
|
|
48
|
-
const options = {};
|
|
49
|
-
const control = { help: false, json: false };
|
|
50
|
-
const multiMap = {
|
|
51
|
-
"include-folder": "includeFolders",
|
|
52
|
-
"exclude-folder": "excludeFolders",
|
|
53
|
-
"include-extension": "includeExtensions",
|
|
54
|
-
"enable-detector": "enabledDetectors",
|
|
55
|
-
"disable-detector": "disabledDetectors"
|
|
56
|
-
};
|
|
57
|
-
const scalarMap = {
|
|
58
|
-
cwd: "cwd",
|
|
59
|
-
input: "input",
|
|
60
|
-
"project-name": "projectName",
|
|
61
|
-
language: "language",
|
|
62
|
-
"project-root": "projectRoot",
|
|
63
|
-
marker: "marker",
|
|
64
|
-
"author-name": "authorName",
|
|
65
|
-
"author-email": "authorEmail",
|
|
66
|
-
company: "company",
|
|
67
|
-
"company-name": "companyName",
|
|
68
|
-
"copyright-start-year": "copyrightStartYear",
|
|
69
|
-
config: "config"
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
for (let index = 0; index < argv.length; index += 1) {
|
|
73
|
-
const arg = argv[index];
|
|
74
|
-
if (arg === "-h" || arg === "--help") {
|
|
75
|
-
control.help = true;
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
if (arg === "--json") {
|
|
79
|
-
control.json = true;
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
if (arg === "--verbose") {
|
|
83
|
-
options.verbose = true;
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
if (arg === "--dry-run") {
|
|
87
|
-
options.dryRun = true;
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
if (arg === "--sample-output") {
|
|
91
|
-
options.sampleOutput = true;
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
if (arg === "--force-author-update") {
|
|
95
|
-
options.forceAuthorUpdate = true;
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (arg === "--force-last-modified-author-update") {
|
|
99
|
-
options.forceLastModifiedAuthorUpdate = true;
|
|
100
|
-
continue;
|
|
101
|
-
}
|
|
102
|
-
if (arg === "--use-gpg-signer-author") {
|
|
103
|
-
options.useGpgSignerAuthor = true;
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
if (!arg.startsWith("--")) {
|
|
107
|
-
throw new Error(`Unexpected argument: ${arg}`);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const flag = arg.slice(2);
|
|
111
|
-
if (multiMap[flag]) {
|
|
112
|
-
const value = argv[index + 1];
|
|
113
|
-
if (!value || value.startsWith("--")) {
|
|
114
|
-
throw new Error(`Missing value for --${flag}`);
|
|
115
|
-
}
|
|
116
|
-
index += 1;
|
|
117
|
-
const key = multiMap[flag];
|
|
118
|
-
const list = Array.isArray(options[key]) ? options[key] : [];
|
|
119
|
-
options[key] = Array.from(new Set([...list, value]));
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (scalarMap[flag]) {
|
|
124
|
-
const value = argv[index + 1];
|
|
125
|
-
if (!value || value.startsWith("--")) {
|
|
126
|
-
throw new Error(`Missing value for --${flag}`);
|
|
127
|
-
}
|
|
128
|
-
index += 1;
|
|
129
|
-
const key = scalarMap[flag];
|
|
130
|
-
if (key === "copyrightStartYear") {
|
|
131
|
-
options[key] = Number.parseInt(value, 10);
|
|
132
|
-
if (Number.isNaN(options[key])) {
|
|
133
|
-
throw new Error(`Invalid number for --${flag}: ${value}`);
|
|
134
|
-
}
|
|
135
|
-
} else if (key === "marker" && value === "null") {
|
|
136
|
-
options[key] = null;
|
|
137
|
-
} else {
|
|
138
|
-
options[key] = value;
|
|
139
|
-
}
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const camelKey = toCamelCase(flag);
|
|
144
|
-
const value = argv[index + 1];
|
|
145
|
-
if (!value || value.startsWith("--")) {
|
|
146
|
-
throw new Error(`Unknown or malformed flag: --${flag}`);
|
|
147
|
-
}
|
|
148
|
-
index += 1;
|
|
149
|
-
options[camelKey] = value;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return {
|
|
153
|
-
options,
|
|
154
|
-
help: control.help,
|
|
155
|
-
json: control.json
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Loads extra options from a JSON config file.
|
|
161
|
-
* @param {Record<string, unknown>} options - Current options object.
|
|
162
|
-
* @returns {Promise<Record<string, unknown>>} Merged options object.
|
|
163
|
-
*/
|
|
164
|
-
export async function applyConfigFile(options) {
|
|
165
|
-
if (typeof options.config !== "string" || options.config.trim().length === 0) {
|
|
166
|
-
return options;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const baseDir = typeof options.cwd === "string" && options.cwd.length > 0 ? options.cwd : process.cwd();
|
|
170
|
-
const configPath = isAbsolute(options.config) ? options.config : join(baseDir, options.config);
|
|
171
|
-
const raw = await readFile(configPath, "utf8");
|
|
172
|
-
const parsed = JSON.parse(raw);
|
|
173
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
174
|
-
throw new Error("Config file must contain a JSON object");
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const merged = { ...parsed, ...options };
|
|
178
|
-
delete merged.config;
|
|
179
|
-
/** @type {Record<string, unknown>} */
|
|
180
|
-
const output = merged;
|
|
181
|
-
return output;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Prints per-file previous/new header samples when available.
|
|
186
|
-
* @param {(message: string) => void} stdout - Standard output writer.
|
|
187
|
-
* @param {unknown} result - Runner result object.
|
|
188
|
-
* @returns {void}
|
|
189
|
-
*/
|
|
190
|
-
function printSampleOutput(stdout, result) {
|
|
191
|
-
if (!result || typeof result !== "object") {
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const report =
|
|
196
|
-
/** @type {{changes?: Array<{file?: string, changed?: boolean, sample?: { previousValue?: string | null, newValue?: string, detectedValues?: Record<string, unknown> }}>}} */ (
|
|
197
|
-
result
|
|
198
|
-
);
|
|
199
|
-
const changes = Array.isArray(report.changes) ? report.changes : [];
|
|
200
|
-
for (const change of changes) {
|
|
201
|
-
if (!change || change.changed !== true || !change.sample || typeof change.sample.newValue !== "string") {
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
stdout(`sample: ${change.file || "<unknown-file>"}`);
|
|
206
|
-
stdout("previous:");
|
|
207
|
-
stdout(change.sample.previousValue === null ? "(none)" : String(change.sample.previousValue));
|
|
208
|
-
stdout("new:");
|
|
209
|
-
stdout(change.sample.newValue);
|
|
210
|
-
if (change.sample.detectedValues && typeof change.sample.detectedValues === "object") {
|
|
211
|
-
stdout("detected-values:");
|
|
212
|
-
stdout(JSON.stringify(change.sample.detectedValues, null, 2));
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Prints changed file paths from result payload.
|
|
219
|
-
* @param {(message: string) => void} stdout - Standard output writer.
|
|
220
|
-
* @param {unknown} result - Runner result object.
|
|
221
|
-
* @returns {void}
|
|
222
|
-
*/
|
|
223
|
-
function printChangedFiles(stdout, result) {
|
|
224
|
-
if (!result || typeof result !== "object") {
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const report = /** @type {{changes?: Array<{file?: string, changed?: boolean}>}} */ (result);
|
|
229
|
-
const changes = Array.isArray(report.changes) ? report.changes : [];
|
|
230
|
-
const updatedFiles = changes.filter((change) => change && change.changed === true).map((change) => change.file || "<unknown-file>");
|
|
231
|
-
for (const file of updatedFiles) {
|
|
232
|
-
stdout(`updated: ${file}`);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Executes CLI flow and returns process-like exit code.
|
|
238
|
-
* @param {string[]} argv - CLI arguments.
|
|
239
|
-
* @param {{
|
|
240
|
-
* runner?: (options: Record<string, unknown>) => Promise<unknown>,
|
|
241
|
-
* stdout?: (message: string) => void,
|
|
242
|
-
* stderr?: (message: string) => void
|
|
243
|
-
* }} [deps={}] - Dependency overrides for tests.
|
|
244
|
-
* @returns {Promise<number>} Exit code.
|
|
245
|
-
*/
|
|
246
|
-
export async function runCli(argv, deps = {}) {
|
|
247
|
-
const runner = deps.runner || fixHeaders;
|
|
248
|
-
const stdout = deps.stdout || console.log;
|
|
249
|
-
const stderr = deps.stderr || console.error;
|
|
250
|
-
|
|
251
|
-
try {
|
|
252
|
-
const parsed = parseCliArgs(argv);
|
|
253
|
-
if (parsed.help) {
|
|
254
|
-
stdout(HELP_TEXT);
|
|
255
|
-
return 0;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const finalOptions = await applyConfigFile(parsed.options);
|
|
259
|
-
const result = await runner(finalOptions);
|
|
260
|
-
|
|
261
|
-
if (parsed.json) {
|
|
262
|
-
stdout(JSON.stringify(result, null, 2));
|
|
263
|
-
return 0;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (result && typeof result === "object") {
|
|
267
|
-
const report = /** @type {{filesScanned?: number, filesUpdated?: number, dryRun?: boolean}} */ (result);
|
|
268
|
-
stdout(
|
|
269
|
-
`fix-headers complete: scanned=${report.filesScanned ?? 0}, updated=${report.filesUpdated ?? 0}, dryRun=${report.dryRun === true}`
|
|
270
|
-
);
|
|
271
|
-
if (finalOptions.verbose === true) {
|
|
272
|
-
printChangedFiles(stdout, result);
|
|
273
|
-
}
|
|
274
|
-
if (finalOptions.sampleOutput === true) {
|
|
275
|
-
printSampleOutput(stdout, result);
|
|
276
|
-
}
|
|
277
|
-
} else {
|
|
278
|
-
if (finalOptions.verbose === true) {
|
|
279
|
-
printChangedFiles(stdout, result);
|
|
280
|
-
}
|
|
281
|
-
if (finalOptions.sampleOutput === true) {
|
|
282
|
-
printSampleOutput(stdout, result);
|
|
283
|
-
}
|
|
284
|
-
stdout("fix-headers complete");
|
|
285
|
-
}
|
|
286
|
-
return 0;
|
|
287
|
-
} catch (error) {
|
|
288
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
289
|
-
stderr(`fix-headers failed: ${message}`);
|
|
290
|
-
return 1;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* Executes CLI flow when the module is the process entrypoint.
|
|
296
|
-
* @param {string[]} [argv=process.argv] - Process argument vector.
|
|
297
|
-
* @param {string} [moduleUrl=import.meta.url] - Current module URL.
|
|
298
|
-
* @param {(args: string[]) => Promise<number>} [executor=runCli] - CLI executor.
|
|
299
|
-
* @returns {boolean} Whether the entrypoint branch was executed.
|
|
300
|
-
*/
|
|
301
|
-
export function runCliAsMain(argv = process.argv, moduleUrl = import.meta.url, executor = runCli) {
|
|
302
|
-
let isMain = false;
|
|
303
|
-
if (argv[1]) {
|
|
304
|
-
try {
|
|
305
|
-
const argvRealPath = realpathSync(argv[1]);
|
|
306
|
-
const moduleRealPath = realpathSync(fileURLToPath(moduleUrl));
|
|
307
|
-
isMain = argvRealPath === moduleRealPath;
|
|
308
|
-
} catch {
|
|
309
|
-
isMain = moduleUrl === pathToFileURL(argv[1]).href;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
if (!isMain) {
|
|
313
|
-
return false;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
executor(argv.slice(2)).then((code) => {
|
|
317
|
-
process.exitCode = code;
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
return true;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
runCliAsMain();
|
package/src/constants.mjs
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/constants.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
|
-
/**
|
|
15
|
-
* @fileoverview Shared constants for project/language detection and header defaults.
|
|
16
|
-
* @module fix-headers/constants
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
export { DETECTOR_PROFILES, getAllowedExtensions, getEnabledDetectors } from "./detectors/index.mjs";
|
|
20
|
-
|
|
21
|
-
/** @type {string} */
|
|
22
|
-
export const DEFAULT_COMPANY_NAME = "Catalyzed Motivation Inc.";
|
|
23
|
-
|
|
24
|
-
/** Header must sit near the top of the file, but a metadata block can legitimately
|
|
25
|
-
* run long; cap the scan generously so a long block's closing `*/` is still seen.
|
|
26
|
-
* @type {number} */
|
|
27
|
-
export const DEFAULT_MAX_HEADER_SCAN_LINES = 200;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Folders skipped at ANY depth — vendored / VCS directories that are never source and can
|
|
31
|
-
* legitimately nest (hoisted `node_modules`, submodule `.git`).
|
|
32
|
-
* @type {Set<string>}
|
|
33
|
-
*/
|
|
34
|
-
export const ALWAYS_IGNORE_FOLDERS = new Set([".git", "node_modules"]);
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Folders skipped ONLY at the project root — build / cache output directories. Anchored to
|
|
38
|
-
* the root so a nested SOURCE directory that happens to share the name (e.g. `tools/build`,
|
|
39
|
-
* `packages/x/dist`-style source) is still processed; only the top-level `/build`, `/dist`,
|
|
40
|
-
* `/coverage`, … are ignored.
|
|
41
|
-
* @type {Set<string>}
|
|
42
|
-
*/
|
|
43
|
-
export const ROOT_IGNORE_FOLDERS = new Set(["dist", "build", "coverage", "tmp", ".next", ".turbo"]);
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Backward-compatible union of {@link ALWAYS_IGNORE_FOLDERS} and {@link ROOT_IGNORE_FOLDERS}.
|
|
47
|
-
* Discovery applies the two sets with different scoping; prefer the specific sets.
|
|
48
|
-
* @type {Set<string>}
|
|
49
|
-
*/
|
|
50
|
-
export const DEFAULT_IGNORE_FOLDERS = new Set([...ALWAYS_IGNORE_FOLDERS, ...ROOT_IGNORE_FOLDERS]);
|
|
@@ -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
|
-
}
|