@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.
Files changed (65) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +354 -25
  3. package/bin/fix-headers.mjs +64 -0
  4. package/dist/index.cjs +28 -0
  5. package/dist/index.mjs +15 -0
  6. package/package.json +39 -19
  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 +15 -23
  10. package/types/src/core/file-discovery.d.mts +17 -5
  11. package/types/src/core/fix-headers.d.mts +61 -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 +31 -8
  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/parser.d.mts +6 -2
  34. package/types/src/header/syntax.d.mts +8 -3
  35. package/types/src/header/template.d.mts +7 -4
  36. package/types/src/utils/diff.d.mts +53 -0
  37. package/types/src/utils/fs.d.mts +10 -3
  38. package/types/src/utils/git.d.mts +27 -0
  39. package/types/src/utils/time.d.mts +55 -0
  40. package/index.cjs +0 -39
  41. package/index.mjs +0 -20
  42. package/src/cli.mjs +0 -323
  43. package/src/constants.mjs +0 -50
  44. package/src/core/file-discovery.mjs +0 -213
  45. package/src/core/fix-headers.mjs +0 -413
  46. package/src/detect/project.mjs +0 -190
  47. package/src/detectors/css.mjs +0 -67
  48. package/src/detectors/go.mjs +0 -65
  49. package/src/detectors/html.mjs +0 -67
  50. package/src/detectors/index.mjs +0 -187
  51. package/src/detectors/json.mjs +0 -76
  52. package/src/detectors/node.mjs +0 -90
  53. package/src/detectors/php.mjs +0 -66
  54. package/src/detectors/python.mjs +0 -75
  55. package/src/detectors/rust.mjs +0 -62
  56. package/src/detectors/shared.mjs +0 -45
  57. package/src/detectors/yaml.mjs +0 -79
  58. package/src/fix-header.mjs +0 -30
  59. package/src/header/parser.mjs +0 -145
  60. package/src/header/syntax.mjs +0 -69
  61. package/src/header/template.mjs +0 -65
  62. package/src/utils/fs.mjs +0 -138
  63. package/src/utils/git.mjs +0 -135
  64. package/src/utils/time.mjs +0 -44
  65. package/types/src/detectors/shared.d.mts +0 -14
@@ -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
- };
@@ -1,65 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/go.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 Go detector implementation.
19
- * @module fix-headers/detectors/go
20
- */
21
-
22
- const markers = ["go.mod"];
23
- const extensions = [".go"];
24
-
25
- /**
26
- * Resolves Go comment syntax.
27
- * @param {string} filePath - File path.
28
- * @returns {{kind: "block", blockStart: string, blockLinePrefix: string, blockEnd: string} | null} Syntax descriptor.
29
- */
30
- function resolveGoCommentSyntax(filePath) {
31
- const extension = extname(filePath).toLowerCase();
32
- if (extensions.includes(extension)) {
33
- return {
34
- kind: "block",
35
- blockStart: "/**",
36
- blockLinePrefix: " *\t",
37
- blockEnd: " */"
38
- };
39
- }
40
- return null;
41
- }
42
-
43
- export const detector = {
44
- id: "go",
45
- priority: 80,
46
- markers,
47
- extensions,
48
- enabledByDefault: true,
49
- findNearestConfig(startPath) {
50
- return findNearestMarker(startPath, markers);
51
- },
52
- parseProjectName(_marker, markerContent, rootDirName) {
53
- const moduleMatch = markerContent.match(/^module\s+(.+)$/m);
54
- if (moduleMatch?.[1]) {
55
- const value = moduleMatch[1].trim();
56
- if (value.length > 0) {
57
- return value;
58
- }
59
- }
60
- return rootDirName;
61
- },
62
- resolveCommentSyntax(filePath) {
63
- return resolveGoCommentSyntax(filePath);
64
- }
65
- };
@@ -1,67 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/html.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 HTML detector implementation.
19
- * @module fix-headers/detectors/html
20
- */
21
-
22
- const markers = ["index.html", "vite.config.js", "vite.config.mjs", "next.config.js", "next.config.mjs"];
23
- const extensions = [".html", ".htm"];
24
-
25
- /**
26
- * Parses HTML project name from marker context.
27
- * @param {string} rootDirName - Fallback directory name.
28
- * @returns {string} Project name.
29
- */
30
- function parseHtmlProjectName(rootDirName) {
31
- return rootDirName;
32
- }
33
-
34
- /**
35
- * Resolves HTML comment syntax for HTML-like files.
36
- * @param {string} filePath - File path.
37
- * @returns {{kind: "html", blockStart: string, blockLinePrefix: string, blockEnd: string} | null} Syntax descriptor.
38
- */
39
- function resolveHtmlCommentSyntax(filePath) {
40
- const extension = extname(filePath).toLowerCase();
41
- if (extensions.includes(extension)) {
42
- return {
43
- kind: "html",
44
- blockStart: "<!--",
45
- blockLinePrefix: "\t",
46
- blockEnd: "-->"
47
- };
48
- }
49
- return null;
50
- }
51
-
52
- export const detector = {
53
- id: "html",
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 parseHtmlProjectName(rootDirName);
63
- },
64
- resolveCommentSyntax(filePath) {
65
- return resolveHtmlCommentSyntax(filePath);
66
- }
67
- };
@@ -1,187 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/index.mjs
4
- * @Date: 2026-03-01 16:34:41 -08:00 (1772411681)
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 { readdir } from "node:fs/promises";
15
- import { extname } from "node:path";
16
- import { fileURLToPath, pathToFileURL } from "node:url";
17
- import { dirname, join } from "node:path";
18
-
19
- /**
20
- * @fileoverview Detector registry and shared selector helpers.
21
- * @module fix-headers/detectors
22
- */
23
-
24
- /**
25
- * @typedef {{
26
- * id: string,
27
- * markers: string[],
28
- * extensions: string[],
29
- * enabledByDefault: boolean,
30
- * findNearestConfig: (startPath: string) => Promise<{root: string, marker: string} | null>,
31
- * parseProjectName: (marker: string, markerContent: string, rootDirName: string) => string,
32
- * resolveCommentSyntax: (filePath: string) => ({kind: "block" | "line" | "html", linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string} | null),
33
- * resolvePreservedPrefix?: (filePath: string, content: string) => string,
34
- * priority?: number
35
- * }} DetectorProfile
36
- */
37
-
38
- /**
39
- * Loads detector modules from the current directory.
40
- * @returns {Promise<DetectorProfile[]>} Loaded detectors.
41
- */
42
- async function loadDetectorsFromDirectory() {
43
- const directoryPath = dirname(fileURLToPath(import.meta.url));
44
- const files = await readdir(directoryPath);
45
- const detectorFiles = files
46
- .filter((fileName) => fileName.endsWith(".mjs"))
47
- .filter((fileName) => fileName !== "index.mjs" && fileName !== "shared.mjs")
48
- .sort((left, right) => left.localeCompare(right));
49
-
50
- const modules = await Promise.all(
51
- detectorFiles.map((fileName) => {
52
- const fileUrl = pathToFileURL(join(directoryPath, fileName)).href;
53
- return import(fileUrl);
54
- })
55
- );
56
-
57
- return modules.map((moduleExports) => moduleExports.detector).filter((entry) => entry && typeof entry.id === "string");
58
- }
59
-
60
- export const DETECTOR_PROFILES = await loadDetectorsFromDirectory();
61
-
62
- /** @type {Map<string, typeof DETECTOR_PROFILES[number]>} */
63
- const detectorMap = new Map(DETECTOR_PROFILES.map((detector) => [detector.id, detector]));
64
-
65
- /**
66
- * Applies runtime syntax overrides to a detector-provided syntax descriptor.
67
- * @param {{kind: "block" | "line" | "html", linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string}} syntax - Base syntax descriptor.
68
- * @param {{ linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string } | undefined} override - Override descriptor.
69
- * @returns {{kind: "block" | "line" | "html", linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string}} Effective descriptor.
70
- */
71
- function applySyntaxOverride(syntax, override) {
72
- if (!override || typeof override !== "object") {
73
- return syntax;
74
- }
75
-
76
- if (syntax.kind === "line") {
77
- return {
78
- ...syntax,
79
- linePrefix: typeof override.linePrefix === "string" && override.linePrefix.length > 0 ? override.linePrefix : syntax.linePrefix,
80
- lineSeparator: typeof override.lineSeparator === "string" ? override.lineSeparator : syntax.lineSeparator
81
- };
82
- }
83
-
84
- return {
85
- ...syntax,
86
- blockStart: typeof override.blockStart === "string" && override.blockStart.length > 0 ? override.blockStart : syntax.blockStart,
87
- blockLinePrefix:
88
- typeof override.blockLinePrefix === "string" && override.blockLinePrefix.length > 0
89
- ? override.blockLinePrefix
90
- : syntax.blockLinePrefix,
91
- blockEnd: typeof override.blockEnd === "string" && override.blockEnd.length > 0 ? override.blockEnd : syntax.blockEnd
92
- };
93
- }
94
-
95
- /**
96
- * Gets enabled detector profiles based on include/exclude options.
97
- * @param {{ enabledDetectors?: string[], disabledDetectors?: string[] }} [options={}] - Runtime options.
98
- * @returns {typeof DETECTOR_PROFILES} Enabled detector list.
99
- */
100
- export function getEnabledDetectors(options = {}) {
101
- const explicitEnabled = new Set(Array.isArray(options.enabledDetectors) ? options.enabledDetectors : []);
102
- const explicitDisabled = new Set(Array.isArray(options.disabledDetectors) ? options.disabledDetectors : []);
103
-
104
- return DETECTOR_PROFILES.filter((detector) => {
105
- if (explicitEnabled.size > 0) {
106
- return explicitEnabled.has(detector.id);
107
- }
108
-
109
- if (explicitDisabled.has(detector.id)) {
110
- return false;
111
- }
112
-
113
- return detector.enabledByDefault;
114
- });
115
- }
116
-
117
- /**
118
- * Gets allowed file extensions for enabled detectors.
119
- * @param {{ enabledDetectors?: string[], disabledDetectors?: string[], includeExtensions?: string[] }} [options={}] - Runtime options.
120
- * @returns {Set<string>} Allowed extensions.
121
- */
122
- export function getAllowedExtensions(options = {}) {
123
- if (Array.isArray(options.includeExtensions) && options.includeExtensions.length > 0) {
124
- return new Set(options.includeExtensions.map((extension) => extension.toLowerCase()));
125
- }
126
-
127
- const detectors = getEnabledDetectors(options);
128
- return new Set(detectors.flatMap((detector) => detector.extensions));
129
- }
130
-
131
- /**
132
- * Gets a detector by id.
133
- * @param {string} id - Detector id.
134
- * @returns {typeof DETECTOR_PROFILES[number] | undefined} Detector.
135
- */
136
- export function getDetectorById(id) {
137
- return detectorMap.get(id);
138
- }
139
-
140
- /**
141
- * Resolves comment syntax for a file path using detector-specific templates.
142
- * @param {string} filePath - File path.
143
- * @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectors?: DetectorProfile[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> }} [options={}] - Runtime options. `detectors` overrides the enabled-detector set (matching {@link detectProjectFromMarkers}).
144
- * @returns {{kind: "block" | "line" | "html", linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string}} Syntax descriptor.
145
- */
146
- export function getCommentSyntaxForFile(filePath, options = {}) {
147
- const extension = extname(filePath).toLowerCase();
148
- const detectors = Array.isArray(options.detectors) ? options.detectors : getEnabledDetectors(options);
149
- for (const detector of detectors) {
150
- if (!detector.extensions.includes(extension)) {
151
- continue;
152
- }
153
- const resolved = detector.resolveCommentSyntax(filePath);
154
- if (resolved) {
155
- const overrides =
156
- options.detectorSyntaxOverrides && typeof options.detectorSyntaxOverrides === "object" ? options.detectorSyntaxOverrides : {};
157
- return applySyntaxOverride(resolved, overrides[detector.id]);
158
- }
159
- }
160
-
161
- return { kind: "block", blockStart: "/**", blockLinePrefix: " *\t", blockEnd: " */" };
162
- }
163
-
164
- /**
165
- * Resolves detector-specific leading content that must be preserved above inserted headers.
166
- * @param {string} filePath - File path.
167
- * @param {string} content - Full file content.
168
- * @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[] }} [options={}] - Runtime options.
169
- * @returns {string} Preserved prefix (possibly empty).
170
- */
171
- export function getPreservedPrefixForFile(filePath, content, options = {}) {
172
- const extension = extname(filePath).toLowerCase();
173
- const detectors = getEnabledDetectors(options);
174
- for (const detector of detectors) {
175
- if (!detector.extensions.includes(extension)) {
176
- continue;
177
- }
178
-
179
- if (typeof detector.resolvePreservedPrefix === "function") {
180
- return detector.resolvePreservedPrefix(filePath, content);
181
- }
182
-
183
- return "";
184
- }
185
-
186
- return "";
187
- }
@@ -1,76 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/json.mjs
4
- * @Date: 2026-03-01 20:00:00 -08:00 (1772433600)
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-01 20:00:00 -08:00 (1772433600)
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 JSON-family detector implementation.
19
- * @module fix-headers/detectors/json
20
- */
21
-
22
- const markers = ["package.json"];
23
- const extensions = [".jsonv", ".jsonc", ".json5"];
24
-
25
- /**
26
- * Parses project name from package marker content.
27
- * @param {string} markerContent - Marker file content.
28
- * @param {string} rootDirName - Fallback root directory name.
29
- * @returns {string} Project name.
30
- */
31
- function parseJsonProjectName(markerContent, rootDirName) {
32
- try {
33
- const parsed = JSON.parse(markerContent);
34
- if (typeof parsed.name === "string" && parsed.name.trim().length > 0) {
35
- return parsed.name.trim();
36
- }
37
- return rootDirName;
38
- } catch {
39
- return rootDirName;
40
- }
41
- }
42
-
43
- /**
44
- * Resolves comment syntax for JSON-family file extensions.
45
- * @param {string} filePath - File path.
46
- * @returns {{kind: "block", blockStart: string, blockLinePrefix: string, blockEnd: string} | null} Syntax descriptor.
47
- */
48
- function resolveJsonCommentSyntax(filePath) {
49
- const extension = extname(filePath).toLowerCase();
50
- if (extensions.includes(extension)) {
51
- return {
52
- kind: "block",
53
- blockStart: "/**",
54
- blockLinePrefix: " *\t",
55
- blockEnd: " */"
56
- };
57
- }
58
- return null;
59
- }
60
-
61
- export const detector = {
62
- id: "json",
63
- priority: 95,
64
- markers,
65
- extensions,
66
- enabledByDefault: true,
67
- findNearestConfig(startPath) {
68
- return findNearestMarker(startPath, markers);
69
- },
70
- parseProjectName(_marker, markerContent, rootDirName) {
71
- return parseJsonProjectName(markerContent, rootDirName);
72
- },
73
- resolveCommentSyntax(filePath) {
74
- return resolveJsonCommentSyntax(filePath);
75
- }
76
- };