@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.
Files changed (63) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +313 -25
  3. package/bin/fix-headers.mjs +70 -0
  4. package/dist/index.cjs +28 -0
  5. package/dist/index.mjs +23 -0
  6. package/package.json +42 -22
  7. package/types/src/cli.d.mts +20 -2
  8. package/types/src/config/load.d.mts +36 -0
  9. package/types/src/constants.d.mts +1 -23
  10. package/types/src/core/file-discovery.d.mts +17 -5
  11. package/types/src/core/fix-headers.d.mts +59 -4
  12. package/types/src/core/ignore-rules.d.mts +38 -0
  13. package/types/src/detect/project.d.mts +64 -15
  14. package/types/src/detectors/css.d.mts +0 -8
  15. package/types/src/detectors/go.d.mts +0 -8
  16. package/types/src/detectors/html.d.mts +0 -8
  17. package/types/src/detectors/index.d.mts +28 -7
  18. package/types/src/detectors/json.d.mts +0 -8
  19. package/types/src/detectors/node.d.mts +0 -8
  20. package/types/src/detectors/php.d.mts +0 -8
  21. package/types/src/detectors/python.d.mts +0 -8
  22. package/types/src/detectors/rust.d.mts +0 -8
  23. package/types/src/detectors/yaml.d.mts +0 -8
  24. package/types/src/drivers/go.d.mts +2 -0
  25. package/types/src/drivers/index.d.mts +154 -0
  26. package/types/src/drivers/node.d.mts +2 -0
  27. package/types/src/drivers/php.d.mts +2 -0
  28. package/types/src/drivers/python.d.mts +2 -0
  29. package/types/src/drivers/rust.d.mts +2 -0
  30. package/types/src/drivers/shared.d.mts +78 -0
  31. package/types/src/header/dates.d.mts +108 -0
  32. package/types/src/header/fields.d.mts +48 -0
  33. package/types/src/header/template.d.mts +4 -3
  34. package/types/src/utils/diff.d.mts +53 -0
  35. package/types/src/utils/fs.d.mts +10 -3
  36. package/types/src/utils/git.d.mts +27 -0
  37. package/types/src/utils/time.d.mts +55 -0
  38. package/index.cjs +0 -39
  39. package/index.mjs +0 -20
  40. package/src/cli.mjs +0 -323
  41. package/src/constants.mjs +0 -50
  42. package/src/core/file-discovery.mjs +0 -213
  43. package/src/core/fix-headers.mjs +0 -413
  44. package/src/detect/project.mjs +0 -190
  45. package/src/detectors/css.mjs +0 -67
  46. package/src/detectors/go.mjs +0 -65
  47. package/src/detectors/html.mjs +0 -67
  48. package/src/detectors/index.mjs +0 -187
  49. package/src/detectors/json.mjs +0 -76
  50. package/src/detectors/node.mjs +0 -90
  51. package/src/detectors/php.mjs +0 -66
  52. package/src/detectors/python.mjs +0 -75
  53. package/src/detectors/rust.mjs +0 -62
  54. package/src/detectors/shared.mjs +0 -45
  55. package/src/detectors/yaml.mjs +0 -79
  56. package/src/fix-header.mjs +0 -30
  57. package/src/header/parser.mjs +0 -145
  58. package/src/header/syntax.mjs +0 -69
  59. package/src/header/template.mjs +0 -65
  60. package/src/utils/fs.mjs +0 -138
  61. package/src/utils/git.mjs +0 -135
  62. package/src/utils/time.mjs +0 -44
  63. package/types/src/detectors/shared.d.mts +0 -14
@@ -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
- }
@@ -1,190 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detect/project.mjs
4
- * @Date: 2026-03-01 13:32:57 -08:00 (1772400777)
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 { dirname, extname, join, resolve } from "node:path";
15
- import { DEFAULT_COMPANY_NAME } from "../constants.mjs";
16
- import { getEnabledDetectors } from "../detectors/index.mjs";
17
- import { readTextIfExists } from "../utils/fs.mjs";
18
- import { detectGitAuthor } from "../utils/git.mjs";
19
-
20
- /**
21
- * @fileoverview Auto-detects project metadata from known language markers and git config.
22
- * @module fix-headers/detect/project
23
- */
24
-
25
- async function findClosestDetectorMatch(startPath, detectors, preferredExtension = "") {
26
- const extension = typeof preferredExtension === "string" ? preferredExtension.trim().toLowerCase() : "";
27
- const extensionMatched =
28
- extension.length > 0
29
- ? detectors.filter((detector) => Array.isArray(detector.extensions) && detector.extensions.includes(extension))
30
- : [];
31
- const detectorsToSearch = extensionMatched.length > 0 ? extensionMatched : detectors;
32
-
33
- const matches = await Promise.all(
34
- detectorsToSearch.map(async (detector) => {
35
- if (typeof detector.findNearestConfig !== "function") {
36
- return null;
37
- }
38
-
39
- const located = await detector.findNearestConfig(startPath);
40
- if (!located) {
41
- return null;
42
- }
43
-
44
- return {
45
- detector,
46
- root: located.root,
47
- marker: located.marker
48
- };
49
- })
50
- );
51
-
52
- const validMatches = matches.filter((item) => item !== null);
53
- if (validMatches.length === 0) {
54
- return null;
55
- }
56
-
57
- let closest = validMatches[0];
58
- for (const candidate of validMatches.slice(1)) {
59
- if (candidate.root.length > closest.root.length) {
60
- closest = candidate;
61
- continue;
62
- }
63
-
64
- if (candidate.root.length === closest.root.length) {
65
- const candidatePriority = Number.isFinite(candidate.detector.priority) ? candidate.detector.priority : 0;
66
- const closestPriority = Number.isFinite(closest.detector.priority) ? closest.detector.priority : 0;
67
- if (candidatePriority > closestPriority) {
68
- closest = candidate;
69
- }
70
- }
71
- }
72
-
73
- return closest;
74
- }
75
-
76
- /**
77
- * Appends company suffix to author display name when requested.
78
- * @param {string} authorName - Base author name.
79
- * @param {string | undefined} company - Optional company suffix value.
80
- * @returns {string} Formatted author name.
81
- */
82
- function formatAuthorNameWithCompany(authorName, company) {
83
- if (typeof company !== "string") {
84
- return authorName;
85
- }
86
-
87
- const trimmedCompany = company.trim();
88
- if (trimmedCompany.length === 0) {
89
- return authorName;
90
- }
91
-
92
- if (/<[^>]+>/.test(authorName)) {
93
- return authorName;
94
- }
95
-
96
- return `${authorName} <${trimmedCompany}>`;
97
- }
98
-
99
- /**
100
- * Detects project root and language by scanning known marker files.
101
- * @param {string} cwd - Starting working directory.
102
- * @param {{ detectors?: { id: string, extensions: string[], priority?: number, findNearestConfig: (path: string) => Promise<{ root: string, marker: string } | null>, parseProjectName: (marker: string, content: string, rootDirName: string) => string }[], enabledDetectors?: string[], disabledDetectors?: string[], preferredExtension?: string }} [options={}] - Detection options.
103
- * @returns {Promise<{
104
- * language: string,
105
- * rootDir: string,
106
- * marker: string | null,
107
- * projectName: string
108
- * }>} Detection result.
109
- */
110
- export async function detectProjectFromMarkers(cwd, options = {}) {
111
- const detectors = Array.isArray(options.detectors) ? options.detectors : getEnabledDetectors(options);
112
- const located = await findClosestDetectorMatch(cwd, detectors, options.preferredExtension);
113
-
114
- if (located) {
115
- const markerPath = join(located.root, located.marker);
116
- const markerContent = (await readTextIfExists(markerPath)) || "";
117
- const rootDirName = located.root.split("/").filter(Boolean).at(-1) || "project";
118
- const projectName = located.detector.parseProjectName(located.marker, markerContent, rootDirName);
119
-
120
- return {
121
- language: located.detector.id,
122
- rootDir: located.root,
123
- marker: located.marker,
124
- projectName
125
- };
126
- }
127
-
128
- const fallbackRoot = resolve(cwd);
129
- const fallbackName = fallbackRoot.split("/").filter(Boolean).at(-1) || "project";
130
- return {
131
- language: "unknown",
132
- rootDir: fallbackRoot,
133
- marker: null,
134
- projectName: fallbackName
135
- };
136
- }
137
-
138
- /**
139
- * Resolves project metadata with override support for every auto-detected field.
140
- * @param {{
141
- * cwd?: string,
142
- * targetFilePath?: string,
143
- * enabledDetectors?: string[],
144
- * disabledDetectors?: string[],
145
- * projectName?: string,
146
- * language?: string,
147
- * projectRoot?: string,
148
- * marker?: string | null,
149
- * useGpgSignerAuthor?: boolean,
150
- * authorName?: string,
151
- * authorEmail?: string,
152
- * company?: string,
153
- * companyName?: string,
154
- * copyrightStartYear?: number
155
- * }} [options={}] - Detection options and overrides.
156
- * @returns {Promise<{
157
- * projectName: string,
158
- * language: string,
159
- * projectRoot: string,
160
- * marker: string | null,
161
- * authorName: string,
162
- * authorEmail: string,
163
- * companyName: string,
164
- * copyrightStartYear: number
165
- * }>} Final metadata.
166
- */
167
- export async function resolveProjectMetadata(options = {}) {
168
- const basePath = options.targetFilePath || options.cwd || process.cwd();
169
- const cwd = resolve(basePath);
170
- const detectors = getEnabledDetectors(options);
171
- const detectFrom = options.targetFilePath ? dirname(cwd) : cwd;
172
- const preferredExtension = options.targetFilePath ? extname(cwd).toLowerCase() : "";
173
- const detected = await detectProjectFromMarkers(detectFrom, { detectors, preferredExtension });
174
- const gitAuthor = await detectGitAuthor(detected.rootDir, {
175
- useGpgSignerAuthor: options.useGpgSignerAuthor === true
176
- });
177
- const currentYear = new Date().getFullYear();
178
- const baseAuthorName = options.authorName || gitAuthor.authorName || "Unknown Author";
179
-
180
- return {
181
- projectName: options.projectName || detected.projectName,
182
- language: options.language || detected.language,
183
- projectRoot: options.projectRoot || detected.rootDir,
184
- marker: options.marker === undefined ? detected.marker : options.marker,
185
- authorName: formatAuthorNameWithCompany(baseAuthorName, options.company),
186
- authorEmail: options.authorEmail || gitAuthor.authorEmail || "unknown@example.com",
187
- companyName: options.companyName || DEFAULT_COMPANY_NAME,
188
- copyrightStartYear: Number.isInteger(options.copyrightStartYear) ? Number(options.copyrightStartYear) : currentYear
189
- };
190
- }
@@ -1,67 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/css.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 { extname } from "node:path";
15
- import { findNearestMarker } from "./shared.mjs";
16
-
17
- /**
18
- * @fileoverview CSS detector implementation.
19
- * @module fix-headers/detectors/css
20
- */
21
-
22
- const markers = ["package.json", "postcss.config.js", "postcss.config.cjs", "postcss.config.mjs"];
23
- const extensions = [".css"];
24
-
25
- /**
26
- * Parses CSS project name from marker context.
27
- * @param {string} rootDirName - Fallback directory name.
28
- * @returns {string} Project name.
29
- */
30
- function parseCssProjectName(rootDirName) {
31
- return rootDirName;
32
- }
33
-
34
- /**
35
- * Resolves block comment syntax for CSS files.
36
- * @param {string} filePath - File path.
37
- * @returns {{kind: "block", blockStart: string, blockLinePrefix: string, blockEnd: string} | null} Syntax descriptor.
38
- */
39
- function resolveCssCommentSyntax(filePath) {
40
- const extension = extname(filePath).toLowerCase();
41
- if (extensions.includes(extension)) {
42
- return {
43
- kind: "block",
44
- blockStart: "/*",
45
- blockLinePrefix: " *\t",
46
- blockEnd: " */"
47
- };
48
- }
49
- return null;
50
- }
51
-
52
- export const detector = {
53
- id: "css",
54
- priority: 70,
55
- markers,
56
- extensions,
57
- enabledByDefault: true,
58
- findNearestConfig(startPath) {
59
- return findNearestMarker(startPath, markers);
60
- },
61
- parseProjectName(_marker, _markerContent, rootDirName) {
62
- return parseCssProjectName(rootDirName);
63
- },
64
- resolveCommentSyntax(filePath) {
65
- return resolveCssCommentSyntax(filePath);
66
- }
67
- };