-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidate-licenses.ts
More file actions
168 lines (146 loc) · 5.03 KB
/
validate-licenses.ts
File metadata and controls
168 lines (146 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* License header validation tool.
*
* Validates that all JavaScript/TypeScript files have the correct copyright header.
* Can be used as a library or as a standalone script.
*
* Library usage:
* ```typescript
* import * as licenses from "@eserstack/codebase/validate-licenses";
*
* // Check licenses
* const result = await licenses.run();
* if (result.issues.length > 0) {
* console.log("Missing headers:", result.issues);
* }
*
* // Fix licenses
* await licenses.run({ fix: true });
* ```
*
* CLI usage:
* deno run --allow-all ./validate-licenses.ts # Check licenses
* deno run --allow-all ./validate-licenses.ts --fix # Auto-fix missing/incorrect headers
*
* @module
*/
import * as standards from "@eserstack/standards";
import { JS_FILE_EXTENSIONS } from "@eserstack/standards/patterns";
import {
createFileTool,
type FileEntry,
type FileTool,
type ToolIssue,
type ToolOptions,
} from "./file-tool.ts";
// =============================================================================
// Constants
// =============================================================================
const BASE_YEAR = "2023";
const RX_COPYRIGHT = new RegExp(
`// Copyright ([0-9]{4})-present Eser Ozvataf and other contributors\\. All rights reserved\\. ([0-9A-Za-z-.]+) license\\.\n`,
);
const COPYRIGHT =
`// Copyright ${BASE_YEAR}-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.`;
/**
* Tool-specific paths that are always skipped, regardless of user `exclude` config.
* These are not user-configurable — they represent files that should never have
* license headers (generated files, doc files, templates).
*
* Note: common directories (node_modules, .git, dist, etc.) are handled by
* DEFAULT_EXCLUDES in file-tools-shared.ts.
*/
const SKIP_PATTERNS = [
/docs\//,
/etc\/templates\//,
/manifest\.gen\.ts$/,
];
const shouldSkip = (path: string): boolean =>
SKIP_PATTERNS.some((p) => p.test(path));
// =============================================================================
// Check function
// =============================================================================
const checkLicenseHeader = (
file: FileEntry,
content: string | undefined,
options: ToolOptions,
): ToolIssue[] => {
if (content === undefined || shouldSkip(file.path)) {
return [];
}
const hasShebang = content.startsWith("#!");
const shebangEnd = hasShebang ? content.indexOf("\n") + 1 : 0;
const afterShebang = content.slice(shebangEnd);
const match = afterShebang.match(RX_COPYRIGHT);
if (match !== null) {
if (match[1] === BASE_YEAR) {
return [];
}
return [{
path: file.path,
message: "incorrect copyright year",
fixed: options.fix,
}];
}
return [{
path: file.path,
message: "missing copyright header",
fixed: options.fix,
}];
};
// =============================================================================
// Fix function
// =============================================================================
const fixLicenseHeader = (
file: FileEntry,
content: string,
_options: ToolOptions,
): { path: string; oldContent: string; newContent: string } | undefined => {
if (shouldSkip(file.path)) {
return undefined;
}
const hasShebang = content.startsWith("#!");
const shebangEnd = hasShebang ? content.indexOf("\n") + 1 : 0;
const afterShebang = content.slice(shebangEnd);
const match = afterShebang.match(RX_COPYRIGHT);
if (match !== null && match[1] === BASE_YEAR) {
return undefined; // Already correct
}
let newContent: string;
if (match !== null) {
// Incorrect year — replace existing header in full content
const shebang = content.slice(0, shebangEnd);
const rest = afterShebang.replace(match[0], "");
newContent = `${shebang}${COPYRIGHT}\n${rest}`;
} else {
// Missing header — insert after shebang if present
newContent = hasShebang
? `${content.slice(0, shebangEnd)}${COPYRIGHT}\n${afterShebang}`
: `${COPYRIGHT}\n${content}`;
}
return { path: file.path, oldContent: content, newContent };
};
// =============================================================================
// Tool
// =============================================================================
export const tool: FileTool = createFileTool({
name: "validate-licenses",
description: "Validate license headers",
canFix: true,
stacks: ["javascript"],
defaults: {},
// Dotted format required — matches path.extname() output in walkSourceFiles git-aware path
extensions: JS_FILE_EXTENSIONS,
checkFile: checkLicenseHeader,
fixFile: fixLicenseHeader,
});
export const run: FileTool["run"] = tool.run;
export const validator: FileTool["validator"] = tool.validator;
export const main: FileTool["main"] = tool.main;
if (import.meta.main) {
const { runCliMain } = await import("./cli-support.ts");
runCliMain(
await main(standards.crossRuntime.runtime.process.args as string[]),
);
}