@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/src/detectors/shared.mjs
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/detectors/shared.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 { dirname, join, resolve } from "node:path";
|
|
15
|
-
import { pathExists } from "../utils/fs.mjs";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @fileoverview Shared detector helpers for nearest marker search.
|
|
19
|
-
* @module fix-headers/detectors/shared
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Finds the closest marker file by walking up parent directories.
|
|
24
|
-
* @param {string} startPath - Starting directory or file path.
|
|
25
|
-
* @param {string[]} markers - Marker filenames to search.
|
|
26
|
-
* @returns {Promise<{root: string, marker: string} | null>} Closest located marker.
|
|
27
|
-
*/
|
|
28
|
-
export async function findNearestMarker(startPath, markers) {
|
|
29
|
-
let currentDir = resolve(startPath);
|
|
30
|
-
|
|
31
|
-
while (true) {
|
|
32
|
-
for (const marker of markers) {
|
|
33
|
-
const markerPath = join(currentDir, marker);
|
|
34
|
-
if (await pathExists(markerPath)) {
|
|
35
|
-
return { root: currentDir, marker };
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const parent = dirname(currentDir);
|
|
40
|
-
if (parent === currentDir) {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
currentDir = parent;
|
|
44
|
-
}
|
|
45
|
-
}
|
package/src/detectors/yaml.mjs
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/detectors/yaml.mjs
|
|
4
|
-
* @Date: 2026-03-01 18:28:31 -08:00 (1772418511)
|
|
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 19:32:27 -08:00 (1772422347)
|
|
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 YAML detector implementation.
|
|
19
|
-
* @module fix-headers/detectors/yaml
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
const markers = ["package.json", ".git"];
|
|
23
|
-
const extensions = [".yaml", ".yml"];
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Parses YAML project name from nearest marker content when available.
|
|
27
|
-
* @param {string} marker - Marker filename.
|
|
28
|
-
* @param {string} markerContent - Marker content.
|
|
29
|
-
* @param {string} rootDirName - Fallback root directory name.
|
|
30
|
-
* @returns {string} Project name.
|
|
31
|
-
*/
|
|
32
|
-
function parseYamlProjectName(marker, markerContent, rootDirName) {
|
|
33
|
-
if (marker !== "package.json") {
|
|
34
|
-
return rootDirName;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
const parsed = JSON.parse(markerContent);
|
|
39
|
-
if (typeof parsed.name === "string" && parsed.name.trim().length > 0) {
|
|
40
|
-
return parsed.name.trim();
|
|
41
|
-
}
|
|
42
|
-
return rootDirName;
|
|
43
|
-
} catch {
|
|
44
|
-
return rootDirName;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Resolves comment syntax for YAML file extensions.
|
|
50
|
-
* @param {string} filePath - File path.
|
|
51
|
-
* @returns {{kind: "line", linePrefix: string} | null} Syntax descriptor.
|
|
52
|
-
*/
|
|
53
|
-
function resolveYamlCommentSyntax(filePath) {
|
|
54
|
-
const extension = extname(filePath).toLowerCase();
|
|
55
|
-
if (extensions.includes(extension)) {
|
|
56
|
-
return {
|
|
57
|
-
kind: "line",
|
|
58
|
-
linePrefix: "#"
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export const detector = {
|
|
65
|
-
id: "yaml",
|
|
66
|
-
priority: 60,
|
|
67
|
-
markers,
|
|
68
|
-
extensions,
|
|
69
|
-
enabledByDefault: true,
|
|
70
|
-
findNearestConfig(startPath) {
|
|
71
|
-
return findNearestMarker(startPath, markers);
|
|
72
|
-
},
|
|
73
|
-
parseProjectName(marker, markerContent, rootDirName) {
|
|
74
|
-
return parseYamlProjectName(marker, markerContent, rootDirName);
|
|
75
|
-
},
|
|
76
|
-
resolveCommentSyntax(filePath) {
|
|
77
|
-
return resolveYamlCommentSyntax(filePath);
|
|
78
|
-
}
|
|
79
|
-
};
|
package/src/fix-header.mjs
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/fix-header.mjs
|
|
4
|
-
* @Date: 2026-03-01 13:34:00 -08:00 (1772400840)
|
|
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 { fixHeaders as fixHeadersCore } from "./core/fix-headers.mjs";
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @fileoverview Public module API exposing a single function for header updates.
|
|
18
|
-
* @module fix-headers
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Runs header fix-up using automatic metadata detection and optional overrides.
|
|
23
|
-
* @param {Parameters<typeof fixHeadersCore>[0]} [options] - Runtime options.
|
|
24
|
-
* @returns {ReturnType<typeof fixHeadersCore>} Update report.
|
|
25
|
-
*/
|
|
26
|
-
export function fixHeaders(options) {
|
|
27
|
-
return fixHeadersCore(options);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export default fixHeaders;
|
package/src/header/parser.mjs
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/header/parser.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 { DEFAULT_MAX_HEADER_SCAN_LINES } from "../constants.mjs";
|
|
15
|
-
import { getPreservedPrefixForFile } from "../detectors/index.mjs";
|
|
16
|
-
import { getHeaderSyntaxForFile } from "./syntax.mjs";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @fileoverview Header parser utilities for extracting and replacing top-of-file headers.
|
|
20
|
-
* @module fix-headers/header/parser
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Escapes special regex characters in literal text.
|
|
25
|
-
* @param {string} text - Literal text.
|
|
26
|
-
* @returns {string} Regex-safe text.
|
|
27
|
-
*/
|
|
28
|
-
function escapeRegex(text) {
|
|
29
|
-
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Splits detector-defined preserved prefix from file content when present.
|
|
34
|
-
* @param {string} filePath - File path used for detector selection.
|
|
35
|
-
* @param {string} content - File content.
|
|
36
|
-
* @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> }} [syntaxOptions={}] - Syntax/detector options.
|
|
37
|
-
* @returns {{prefix: string, body: string}} Shebang prefix and remaining body.
|
|
38
|
-
*/
|
|
39
|
-
function splitPreservedPrefix(filePath, content, syntaxOptions = {}) {
|
|
40
|
-
const prefix = getPreservedPrefixForFile(filePath, content, syntaxOptions);
|
|
41
|
-
if (prefix.length === 0) {
|
|
42
|
-
return { prefix: "", body: content };
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
prefix,
|
|
47
|
-
body: content.slice(prefix.length)
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Matches a top-of-content header block for a given syntax.
|
|
53
|
-
* @param {string} content - Content slice to match from start.
|
|
54
|
-
* @param {{kind: "block" | "line" | "html", linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string}} syntax - Header syntax descriptor.
|
|
55
|
-
* @returns {RegExpMatchArray | null} Matched header segment.
|
|
56
|
-
*/
|
|
57
|
-
function matchHeaderSegment(content, syntax) {
|
|
58
|
-
if (syntax.kind === "html") {
|
|
59
|
-
const blockStart = escapeRegex(syntax.blockStart || "<!--");
|
|
60
|
-
const blockEnd = escapeRegex(syntax.blockEnd || "-->");
|
|
61
|
-
return content.match(new RegExp(`^${blockStart}[\\s\\S]*?${blockEnd}\\n*`));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (syntax.kind === "line") {
|
|
65
|
-
const linePrefix = escapeRegex(syntax.linePrefix || "#");
|
|
66
|
-
return content.match(new RegExp(`^(?:${linePrefix}.*\\n)+\\n*`));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Block comments (/* … */ family). A block comment opens with `/*` and is
|
|
70
|
-
// terminated by the FIRST `*/` after the opener (block comments do not nest in
|
|
71
|
-
// JS/JSONC/CSS/PHP). Match that real grammar rather than the detector's *styled*
|
|
72
|
-
// tokens: `blockStart` may be `/*` or `/**` (so anchor on `/*`, which prefixes
|
|
73
|
-
// both), and `blockEnd` carries a leading space (" */") that exists for GENERATION
|
|
74
|
-
// only. Using the styled " */" to bound an EXISTING header is the data-loss bug —
|
|
75
|
-
// any real closer that isn't byte-for-byte " */" (e.g. `**/`, a tab-indented `*/`,
|
|
76
|
-
// or `*/` at column 0) is skipped, so the lazy match runs on to the next `*/`
|
|
77
|
-
// downstream (inside a `//` line comment, a string literal, or another block) and
|
|
78
|
-
// everything in between is deleted on replace. Trim the styled space off so we stop
|
|
79
|
-
// at the header's own terminator.
|
|
80
|
-
const blockEnd = escapeRegex((syntax.blockEnd || " */").trim());
|
|
81
|
-
return content.match(new RegExp(`^/\\*[\\s\\S]*?${blockEnd}\\n*`));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Finds the first top-level project header block in a file.
|
|
86
|
-
* @param {string} content - File content.
|
|
87
|
-
* @param {string} [filePath=""] - File path used for syntax selection.
|
|
88
|
-
* @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> }} [syntaxOptions={}] - Syntax resolution options.
|
|
89
|
-
* @returns {{start: number, end: number} | null} Header location.
|
|
90
|
-
*/
|
|
91
|
-
export function findProjectHeader(content, filePath = "", syntaxOptions = {}) {
|
|
92
|
-
const { prefix, body } = splitPreservedPrefix(filePath, content, syntaxOptions);
|
|
93
|
-
const scanLines = body.split("\n").slice(0, DEFAULT_MAX_HEADER_SCAN_LINES).join("\n");
|
|
94
|
-
const syntax = getHeaderSyntaxForFile(filePath, syntaxOptions);
|
|
95
|
-
const match = matchHeaderSegment(scanLines, syntax);
|
|
96
|
-
|
|
97
|
-
if (!match) {
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
let offset = 0;
|
|
102
|
-
let end = 0;
|
|
103
|
-
while (true) {
|
|
104
|
-
const remaining = scanLines.slice(offset);
|
|
105
|
-
const segment = matchHeaderSegment(remaining, syntax);
|
|
106
|
-
|
|
107
|
-
if (!segment || !segment[0].includes("@Project:")) {
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
offset += segment[0].length;
|
|
112
|
-
end = offset;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (end === 0) {
|
|
116
|
-
return null;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return {
|
|
120
|
-
start: prefix.length,
|
|
121
|
-
end: prefix.length + end
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Replaces or inserts a project header block.
|
|
127
|
-
* @param {string} content - Original file content.
|
|
128
|
-
* @param {string} newHeader - Generated header text.
|
|
129
|
-
* @param {string} [filePath=""] - File path used for syntax selection.
|
|
130
|
-
* @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> }} [syntaxOptions={}] - Syntax resolution options.
|
|
131
|
-
* @returns {{nextContent: string, changed: boolean}} Updated content result.
|
|
132
|
-
*/
|
|
133
|
-
export function replaceOrInsertHeader(content, newHeader, filePath = "", syntaxOptions = {}) {
|
|
134
|
-
const existing = findProjectHeader(content, filePath, syntaxOptions);
|
|
135
|
-
if (!existing) {
|
|
136
|
-
const { prefix, body } = splitPreservedPrefix(filePath, content, syntaxOptions);
|
|
137
|
-
const nextContent = `${prefix}${newHeader}\n\n${body.replace(/^\n+/, "")}`;
|
|
138
|
-
return { nextContent, changed: nextContent !== content };
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const before = content.slice(0, existing.start);
|
|
142
|
-
const after = content.slice(existing.end).replace(/^\n+/, "");
|
|
143
|
-
const nextContent = `${before}${newHeader}\n\n${after}`;
|
|
144
|
-
return { nextContent, changed: nextContent !== content };
|
|
145
|
-
}
|
package/src/header/syntax.mjs
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/header/syntax.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 { getCommentSyntaxForFile } from "../detectors/index.mjs";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @fileoverview Header comment syntax helpers for mapping file extensions to comment styles.
|
|
19
|
-
* @module fix-headers/header/syntax
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @typedef {{
|
|
24
|
-
* kind: "block" | "line" | "html",
|
|
25
|
-
* linePrefix?: string,
|
|
26
|
-
* lineSeparator?: string,
|
|
27
|
-
* blockStart?: string,
|
|
28
|
-
* blockLinePrefix?: string,
|
|
29
|
-
* blockEnd?: string
|
|
30
|
-
* }} HeaderSyntax
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Resolves comment syntax for a file path based on extension.
|
|
35
|
-
* @param {string} filePath - Absolute or relative file path.
|
|
36
|
-
* @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> }} [options={}] - Syntax resolution options.
|
|
37
|
-
* @returns {HeaderSyntax} Header syntax descriptor.
|
|
38
|
-
*/
|
|
39
|
-
export function getHeaderSyntaxForFile(filePath, options = {}) {
|
|
40
|
-
const extension = extname(filePath).toLowerCase();
|
|
41
|
-
if (extension.length === 0) {
|
|
42
|
-
return { kind: "block", blockStart: "/**", blockLinePrefix: " *\t", blockEnd: " */" };
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return getCommentSyntaxForFile(filePath, options);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Renders header body lines using a chosen syntax.
|
|
50
|
-
* @param {HeaderSyntax} syntax - Header syntax descriptor.
|
|
51
|
-
* @param {string[]} lines - Header lines without comment wrappers.
|
|
52
|
-
* @returns {string} Formatted header block.
|
|
53
|
-
*/
|
|
54
|
-
export function renderHeaderLines(syntax, lines) {
|
|
55
|
-
if (syntax.kind === "line") {
|
|
56
|
-
const lineSeparator = typeof syntax.lineSeparator === "string" ? syntax.lineSeparator : "\t";
|
|
57
|
-
return lines.map((line) => `${syntax.linePrefix || "#"}${lineSeparator}${line}`).join("\n");
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const blockStart = syntax.blockStart || (syntax.kind === "html" ? "<!--" : "/**");
|
|
61
|
-
const blockLinePrefix = syntax.blockLinePrefix || (syntax.kind === "html" ? "\t" : " *\t");
|
|
62
|
-
const blockEnd = syntax.blockEnd || (syntax.kind === "html" ? "-->" : " */");
|
|
63
|
-
|
|
64
|
-
if (syntax.kind === "html") {
|
|
65
|
-
return `${blockStart}\n${lines.map((line) => `${blockLinePrefix}${line}`).join("\n")}\n${blockEnd}`;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return `${blockStart}\n${lines.map((line) => `${blockLinePrefix}${line}`).join("\n")}\n${blockEnd}`;
|
|
69
|
-
}
|
package/src/header/template.mjs
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/header/template.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 { relative } from "node:path";
|
|
15
|
-
import { getHeaderSyntaxForFile, renderHeaderLines } from "./syntax.mjs";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @fileoverview Header template builder used to generate normalized file headers.
|
|
19
|
-
* @module fix-headers/header/template
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Builds a normalized header block for a source file.
|
|
24
|
-
* @param {{
|
|
25
|
-
* absoluteFilePath: string,
|
|
26
|
-
* language?: string,
|
|
27
|
-
* syntaxOptions?: { language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> },
|
|
28
|
-
* projectRoot: string,
|
|
29
|
-
* createdByName?: string,
|
|
30
|
-
* createdByEmail?: string,
|
|
31
|
-
* lastModifiedByName?: string,
|
|
32
|
-
* lastModifiedByEmail?: string,
|
|
33
|
-
* projectName: string,
|
|
34
|
-
* authorName: string,
|
|
35
|
-
* authorEmail: string,
|
|
36
|
-
* createdAt: {date: string, timestamp: number},
|
|
37
|
-
* lastModifiedAt: {date: string, timestamp: number},
|
|
38
|
-
* copyrightStartYear: number,
|
|
39
|
-
* companyName: string,
|
|
40
|
-
* currentYear: number
|
|
41
|
-
* }} data - Header data.
|
|
42
|
-
* @returns {string} Header block text.
|
|
43
|
-
*/
|
|
44
|
-
export function buildHeader(data) {
|
|
45
|
-
const relativePath = `/${relative(data.projectRoot, data.absoluteFilePath).replace(/\\/g, "/")}`;
|
|
46
|
-
const syntax = getHeaderSyntaxForFile(data.absoluteFilePath, data.syntaxOptions || { language: data.language });
|
|
47
|
-
const createdByName = data.createdByName || data.authorName;
|
|
48
|
-
const createdByEmail = data.createdByEmail || data.authorEmail;
|
|
49
|
-
const lastModifiedByName = data.lastModifiedByName || data.authorName;
|
|
50
|
-
const lastModifiedByEmail = data.lastModifiedByEmail || data.authorEmail;
|
|
51
|
-
const headerLines = [
|
|
52
|
-
`@Project: ${data.projectName}`,
|
|
53
|
-
`@Filename: ${relativePath}`,
|
|
54
|
-
`@Date: ${data.createdAt.date} (${data.createdAt.timestamp})`,
|
|
55
|
-
`@Author: ${createdByName}`,
|
|
56
|
-
`@Email: <${createdByEmail}>`,
|
|
57
|
-
"-----",
|
|
58
|
-
`@Last modified by: ${lastModifiedByName} (${lastModifiedByEmail})`,
|
|
59
|
-
`@Last modified time: ${data.lastModifiedAt.date} (${data.lastModifiedAt.timestamp})`,
|
|
60
|
-
"-----",
|
|
61
|
-
`@Copyright: Copyright (c) ${data.copyrightStartYear}-${data.currentYear} ${data.companyName} All rights reserved.`
|
|
62
|
-
];
|
|
63
|
-
|
|
64
|
-
return renderHeaderLines(syntax, headerLines);
|
|
65
|
-
}
|
package/src/utils/fs.mjs
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @Project: @cldmv/fix-headers
|
|
3
|
-
* @Filename: /src/utils/fs.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 { access, readdir, readFile, stat } from "node:fs/promises";
|
|
15
|
-
import { constants as fsConstants } from "node:fs";
|
|
16
|
-
import { dirname, join, resolve } from "node:path";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* @fileoverview Filesystem helpers for project discovery and recursive scanning.
|
|
20
|
-
* @module fix-headers/utils/fs
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Checks whether a file exists and is readable.
|
|
25
|
-
* @param {string} filePath - Absolute or relative path.
|
|
26
|
-
* @returns {Promise<boolean>} True when the path exists.
|
|
27
|
-
*/
|
|
28
|
-
export async function pathExists(filePath) {
|
|
29
|
-
try {
|
|
30
|
-
await access(filePath, fsConstants.F_OK);
|
|
31
|
-
return true;
|
|
32
|
-
} catch {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Finds a marker file by traversing upward from a directory.
|
|
39
|
-
* @param {string} startDir - Starting directory.
|
|
40
|
-
* @param {string[]} markerFiles - Candidate marker file names.
|
|
41
|
-
* @returns {Promise<{root: string, marker: string} | null>} First match with root path.
|
|
42
|
-
*/
|
|
43
|
-
export async function findProjectRoot(startDir, markerFiles) {
|
|
44
|
-
let currentDir = resolve(startDir);
|
|
45
|
-
|
|
46
|
-
while (true) {
|
|
47
|
-
for (const marker of markerFiles) {
|
|
48
|
-
const markerPath = join(currentDir, marker);
|
|
49
|
-
if (await pathExists(markerPath)) {
|
|
50
|
-
return { root: currentDir, marker };
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const parent = dirname(currentDir);
|
|
55
|
-
if (parent === currentDir) {
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
currentDir = parent;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Reads UTF-8 file content if it exists.
|
|
65
|
-
* @param {string} filePath - File path.
|
|
66
|
-
* @returns {Promise<string | null>} File content or null when missing.
|
|
67
|
-
*/
|
|
68
|
-
export async function readTextIfExists(filePath) {
|
|
69
|
-
if (!(await pathExists(filePath))) {
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return readFile(filePath, "utf8");
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Recursively discovers files matching allowed extensions.
|
|
78
|
-
* @param {string} dirPath - Root directory to scan.
|
|
79
|
-
* @param {{
|
|
80
|
-
* allowedExtensions: Set<string>,
|
|
81
|
-
* ignoreFolders: Set<string>,
|
|
82
|
-
* shouldSkipDirectory?: (directoryPath: string, directoryName: string) => boolean
|
|
83
|
-
* }} options - Scan options.
|
|
84
|
-
* @returns {Promise<string[]>} Matching file paths.
|
|
85
|
-
*/
|
|
86
|
-
export async function walkFiles(dirPath, options) {
|
|
87
|
-
const files = [];
|
|
88
|
-
const entries = await readdir(dirPath, { withFileTypes: true });
|
|
89
|
-
|
|
90
|
-
for (const entry of entries) {
|
|
91
|
-
const fullPath = join(dirPath, entry.name);
|
|
92
|
-
|
|
93
|
-
if (entry.isDirectory() && options.ignoreFolders.has(entry.name)) {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (entry.isDirectory() && options.shouldSkipDirectory?.(fullPath, entry.name) === true) {
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (entry.isDirectory()) {
|
|
102
|
-
const nested = await walkFiles(fullPath, options);
|
|
103
|
-
files.push(...nested);
|
|
104
|
-
continue;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (!entry.isFile()) {
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const extensionStart = entry.name.lastIndexOf(".");
|
|
112
|
-
if (extensionStart === -1) {
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const extension = entry.name.slice(extensionStart).toLowerCase();
|
|
117
|
-
if (options.allowedExtensions.has(extension)) {
|
|
118
|
-
files.push(fullPath);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return files;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Gets creation-like and modified timestamps from filesystem stats.
|
|
127
|
-
* @param {string} filePath - Absolute file path.
|
|
128
|
-
* @returns {Promise<{createdAt: Date, updatedAt: Date}>} Date pair.
|
|
129
|
-
*/
|
|
130
|
-
export async function readFileDates(filePath) {
|
|
131
|
-
const fileStats = await stat(filePath);
|
|
132
|
-
const createdAt = fileStats.birthtimeMs > 0 ? fileStats.birthtime : fileStats.mtime;
|
|
133
|
-
|
|
134
|
-
return {
|
|
135
|
-
createdAt,
|
|
136
|
-
updatedAt: fileStats.mtime
|
|
137
|
-
};
|
|
138
|
-
}
|