-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcheck-license.js
More file actions
83 lines (69 loc) · 2.06 KB
/
check-license.js
File metadata and controls
83 lines (69 loc) · 2.06 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
#!/usr/bin/env node
// SPDX-License-Identifier: Apache-2.0
const fs = require("fs");
const path = require("path");
const LICENSE_HEADER = "// SPDX-License-Identifier: Apache-2.0";
/**
* Check if a file should be excluded from license checking
* @param {string} filePath - Path to the file to check
* @returns {boolean} - True if file should be excluded
*/
function shouldExcludeFile(filePath) {
// Exclude test files
return (
filePath.includes(".test.ts") ||
filePath.includes(".test.tsx") ||
filePath.includes(".spec.ts") ||
filePath.includes(".spec.tsx") ||
filePath.includes("/test/") ||
filePath.includes("/__tests__/")
);
}
/**
* Check if a file contains the required license header
* @param {string} filePath - Path to the file to check
* @returns {boolean} - True if license header is present
*/
function checkLicenseHeader(filePath) {
const content = fs.readFileSync(filePath, "utf8");
const lines = content.split("\n");
// Check first few lines for the exact license header
for (let i = 0; i < Math.min(5, lines.length); i++) {
const line = lines[i].trim();
if (line === LICENSE_HEADER) {
return true;
}
}
return false;
}
/**
* Main function to check all files passed as arguments
*/
function main() {
const files = process.argv.slice(2);
if (files.length === 0) {
console.log("No files to check");
process.exit(0);
}
const filesWithoutLicense = [];
files.forEach((file) => {
if (shouldExcludeFile(file)) {
return; // Skip test files
}
if (!checkLicenseHeader(file)) {
filesWithoutLicense.push(file);
}
});
if (filesWithoutLicense.length > 0) {
console.error("\n❌ The following files are missing the SPDX license header:");
console.error(" Expected: " + LICENSE_HEADER + "\n");
filesWithoutLicense.forEach((file) => {
console.error(" - " + file);
});
console.error("\nPlease add the license header to these files.\n");
process.exit(1);
}
console.log("✅ All files have the required license header");
process.exit(0);
}
main();