deno.land / std@0.224.0 / _tools / check_licence.ts

check_licence.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { walk } from "../fs/walk.ts";import { globToRegExp } from "../path/glob_to_regexp.ts";
const EXTENSIONS = [".mjs", ".js", ".ts", ".rs"];const EXCLUDED_DIRS = [ "**/dotenv/testdata", "**/fs/testdata", "**/http/testdata", "**/crypto/_wasm/target", "**/crypto/_wasm/lib", "**/.git", "**/docs/**",];
const ROOT = new URL("../", import.meta.url);const CHECK = Deno.args.includes("--check");const FIRST_YEAR = 2018;const CURRENT_YEAR = new Date().getFullYear();const RX_COPYRIGHT = new RegExp( `// Copyright ([0-9]{4})-([0-9]{4}) the Deno authors\\. All rights reserved\\. MIT license\\.\n`,);export const COPYRIGHT = `// Copyright ${FIRST_YEAR}-${CURRENT_YEAR} the Deno authors. All rights reserved. MIT license.`;
let failed = false;
for await ( const { path } of walk(ROOT, { exts: EXTENSIONS, skip: EXCLUDED_DIRS.map((path) => globToRegExp(path)), includeDirs: false, })) { const content = await Deno.readTextFile(path); const match = content.match(RX_COPYRIGHT);
if (!match) { if (CHECK) { console.error(`Missing copyright header: ${path}`); failed = true; } else { const contentWithCopyright = COPYRIGHT + "\n" + content; await Deno.writeTextFile(path, contentWithCopyright); console.log("Copyright header automatically added to " + path); } } else if ( (match[1] && parseInt(match[1]) !== FIRST_YEAR) || (match[2] && parseInt(match[2]) !== CURRENT_YEAR) ) { if (CHECK) { console.error(`Incorrect copyright year: ${path}`); failed = true; } else { const index = match.index ?? 0; const contentWithoutCopyright = content.replace(match[0], ""); const contentWithCopyright = contentWithoutCopyright.substring(0, index) + COPYRIGHT + "\n" + contentWithoutCopyright.substring(index); await Deno.writeTextFile(path, contentWithCopyright); console.log("Copyright header automatically updated in " + path); } }}
if (failed) { console.info(`Copyright header should be "${COPYRIGHT}"`); Deno.exit(1);}
std

Version Info

Tagged at
6 months ago