forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyright_checker.js
111 lines (97 loc) · 3.03 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
#!/usr/bin/env -S deno run --allow-read=. --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 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();
}
}
export async function checkCopyright() {
const sourceFiles = await getSources(ROOT_PATH, [
// js and ts
"*.js",
"*.ts",
":!:.github/mtime_cache/action.js",
":!:tests/specs/**",
":!:tests/testdata/**",
":!:cli/bench/testdata/**",
":!:cli/tsc/dts/**",
":!:cli/tsc/*typescript.js",
":!:cli/tsc/compiler.d.ts",
":!:tests/wpt/suite/**",
":!:cli/tools/init/templates/**",
":!:tests/unit_node/testdata/**",
":!:tests/node_compat/test/**",
":!:cli/tools/bench/mitata.rs",
// rust
"*.rs",
":!:ops/optimizer_tests/**",
// toml
"*Cargo.toml",
]);
const errors = [];
const sourceFilesSet = new Set(sourceFiles);
const ERROR_MSG = "Copyright header is missing: ";
// Acceptable content before the copyright line
const ACCEPTABLE_LINES =
/^(\/\/ deno-lint-.*|\/\/ Copyright.*|\/\/ Ported.*|\s*|#!\/.*)\n/;
const COPYRIGHT_LINE =
`Copyright 2018-${copyrightYear} the Deno authors. All rights reserved. MIT license.`;
const TOML_COPYRIGHT_LINE = "# " + COPYRIGHT_LINE;
const C_STYLE_COPYRIGHT_LINE = "// " + COPYRIGHT_LINE;
for (const file of sourceFilesSet) {
const fileText = await readFirstPartOfFile(file);
if (file.endsWith("Cargo.toml")) {
if (
!fileText.startsWith(TOML_COPYRIGHT_LINE)
) {
errors.push(ERROR_MSG + file);
}
continue;
}
if (
!fileText.startsWith(C_STYLE_COPYRIGHT_LINE)
) {
let trimmedText = fileText;
// Attempt to trim accceptable lines
while (
ACCEPTABLE_LINES.test(trimmedText) &&
!trimmedText.startsWith(C_STYLE_COPYRIGHT_LINE)
) {
trimmedText = trimmedText.split("\n").slice(1).join("\n");
}
if (
!trimmedText.startsWith(C_STYLE_COPYRIGHT_LINE)
) {
errors.push(
`${ERROR_MSG}${file} (incorrect line is '${
trimmedText.split("\n", 1)
}')`,
);
}
}
}
// 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.`);
}
}
if (import.meta.main) {
await checkCopyright();
}