-
Notifications
You must be signed in to change notification settings - Fork 576
/
check_licence.ts
executable file
·60 lines (50 loc) · 1.41 KB
/
check_licence.ts
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
#!/usr/bin/env -S deno run --allow-read
// Copyright 2022-2022 the Deno authors. All rights reserved. MIT license.
const EXTENSIONS = [".mjs", ".js", ".ts", ".rs"];
const EXCLUDED_PATHS = [
".git",
"node/_module",
"dotenv/testdata",
"fs/testdata",
"http/testdata",
"node/_module/cjs",
"node/_module/node_modules",
"node/_tools",
"node/testdata",
];
const ROOT = new URL("../", import.meta.url).pathname.slice(0, -1);
const FAIL_FAST = Deno.args.includes("--fail-fast");
const COPYRIGHT_REGEX =
/\/\/ Copyright \d{4}-\d{4} (the )?Deno authors. All rights reserved. MIT license./;
let shouldFail = false;
function walk(dir: string): void {
for (const x of Deno.readDirSync(dir)) {
const filePath = `${dir}/${x.name}`;
if (x.isDirectory) {
walk(filePath);
continue;
}
const isExcluded = EXCLUDED_PATHS
.map((x) => filePath.includes(x))
.some((x) => x);
if (
isExcluded ||
!EXTENSIONS.map((x) => filePath.endsWith(x)).some((x) => x)
) {
continue;
}
const content = Deno.readTextFileSync(filePath);
const hasNotice = content
.split("\n")
.filter((_, i) => i < 10)
.map((x) => COPYRIGHT_REGEX.test(x))
.some((x) => x);
if (!hasNotice) {
console.error(`Missing Copyright Notice: ${filePath}`);
if (FAIL_FAST) Deno.exit(1);
shouldFail = true;
}
}
}
walk(ROOT);
if (shouldFail) Deno.exit(1);