forked from denoland/deno_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyright_checker.js
executable file
·170 lines (146 loc) · 4.5 KB
/
copyright_checker.js
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
169
170
#!/usr/bin/env -S deno run --allow-read=. --allow-write=. --allow-run=git
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { getSources, ROOT_PATH } from "./util.js";
const copyrightYear = 2024;
const SOURCES = [
// js and ts
"*.js",
"*.ts",
// rust
"*.rs",
// toml
"*Cargo.toml",
];
const COPYRIGHT_REGEX =
/^(#|\/\/) Copyright \d+-\d+ the Deno authors. All rights reserved. MIT license./;
const COPYRIGHT_LINE =
`Copyright 2018-${copyrightYear} the Deno authors. All rights reserved. MIT license.`;
// Acceptable content before the copyright line
const ACCEPTABLE_LINES =
/^(\/\/ deno-lint-.*|\/\/ Copyright.*|\/\/ Ported.*|\s*|#!\/.*)$/;
const MISSING_ERROR_MSG = "Copyright header is missing";
const BAD_LINE_ERROR_MSG =
"Unacceptable line appeared before copyright message";
const DATE_ERROR_MSG = "Copyright header is out-of-date";
const buffer = new Uint8Array(1024);
const textDecoder = new TextDecoder();
async function readFirstPartOfFile(filePath) {
const file = await Deno.open(filePath, { read: true });
try {
const byteCount = await file.read(buffer);
return textDecoder.decode(buffer.slice(0, byteCount ?? 0));
} finally {
file.close();
}
}
async function fixFile(commentPrefix, file) {
let fileText = await Deno.readTextFile(file);
if (fileText.includes("\r\n")) {
throw new Error("Cowardly refusing to fix CRLF line endings in " + file);
}
const newContents = [];
let found = false;
let nonBlank = false;
for (const line of fileText.split("\n")) {
if (line.length == 0) {
if (!nonBlank) {
console.error("Trimming empty space before copyright");
continue;
}
} else {
nonBlank = true;
}
if (COPYRIGHT_REGEX.test(line)) {
if (found) {
console.error(`Removing duplicate copyright line in ${file}`);
} else {
if (line != `${commentPrefix} ${COPYRIGHT_LINE}`) {
console.error(`Freshening copyright in ${file}`);
}
found = true;
newContents.push(`${commentPrefix} ${COPYRIGHT_LINE}`);
}
continue;
}
if (ACCEPTABLE_LINES.test(line)) {
newContents.push(line);
continue;
}
if (!found) {
console.error(`Reordered copyright header in ${file}`);
newContents.push(`${commentPrefix} ${COPYRIGHT_LINE}`);
found = true;
}
newContents.push(line);
}
fileText = newContents.join("\n");
await Deno.writeTextFile(file, fileText);
return await checkFile(commentPrefix, file);
}
async function checkFile(commentPrefix, file) {
const fileLines = (await readFirstPartOfFile(file)).split("\n");
for (const line of fileLines) {
// Exact match :+1:
if (line == `${commentPrefix} ${COPYRIGHT_LINE}`) {
return undefined;
}
// Wrong date, but matches regexp
if (COPYRIGHT_REGEX.test(line)) {
return DATE_ERROR_MSG + " (" + line + ")";
}
// Not a match, but acceptable
if (ACCEPTABLE_LINES.test(line)) {
continue;
}
if (COPYRIGHT_REGEX.test(await readFirstPartOfFile(file))) {
return BAD_LINE_ERROR_MSG + " (" + line + ")";
} else {
return DATE_ERROR_MSG + " (" + line + ")";
}
}
return MISSING_ERROR_MSG + " (" + fileLines[0] + ")";
}
export async function checkCopyright(fix = false) {
const sourceFiles = await getSources(ROOT_PATH, SOURCES);
const errors = [];
const sourceFilesSet = new Set(sourceFiles);
for (const file of sourceFilesSet) {
if (file.endsWith("Cargo.toml")) {
const error = await checkFile("#", file);
if (error) {
if (fix) {
await fixFile("#", file);
} else {
errors.push(error + ": " + file);
}
}
continue;
}
const error = await checkFile("//", file);
if (error) {
if (fix) {
await fixFile("//", file);
} else {
errors.push(error + ": " + file);
}
}
}
// check the main license file
const licenseText = Deno.readTextFileSync(ROOT_PATH + "/LICENSE.md");
if (
!licenseText.includes(`Copyright 2018-${copyrightYear} the Deno authors`)
) {
errors.push(`LICENSE.md has old copyright year`);
}
if (errors.length > 0) {
// show all the errors at the same time to prevent overlap with
// other running scripts that may be outputting
console.error(errors.join("\n"));
throw new Error(
`Copyright checker had ${errors.length} errors. Use --fix to repair.`,
);
}
}
if (import.meta.main) {
await checkCopyright(Deno.args[0] == "--fix");
}