summaryrefslogtreecommitdiff
path: root/tools/copyright_checker.js
diff options
context:
space:
mode:
authorYiyu Lin <linyiyu1992@gmail.com>2023-01-13 15:51:32 +0800
committerGitHub <noreply@github.com>2023-01-13 16:51:32 +0900
commita00e432297d2ae119c8e1097aec74badc886f912 (patch)
tree7380b37245e889e97a8c7b1535a1e31fddb74738 /tools/copyright_checker.js
parent5707a958ac2b44d573d50260b1d0d40887662011 (diff)
chore: add `copyright_checker` tool and add the missing copyright (#17285)
Diffstat (limited to 'tools/copyright_checker.js')
-rw-r--r--tools/copyright_checker.js84
1 files changed, 84 insertions, 0 deletions
diff --git a/tools/copyright_checker.js b/tools/copyright_checker.js
new file mode 100644
index 000000000..706f59dbd
--- /dev/null
+++ b/tools/copyright_checker.js
@@ -0,0 +1,84 @@
+#!/usr/bin/env -S deno run --unstable --allow-read --allow-run
+// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
+
+import { getSources, ROOT_PATH } from "./util.js";
+
+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 checkCopyright() {
+ const sourceFiles = await getSources(ROOT_PATH, [
+ // js and ts
+ "*.js",
+ "*.ts",
+ ":!:.github/mtime_cache/action.js",
+ ":!:cli/tests/testdata/**",
+ ":!:cli/bench/testdata/**",
+ ":!:cli/tsc/dts/**",
+ ":!:cli/tsc/*typescript.js",
+ ":!:cli/tsc/compiler.d.ts",
+ ":!:test_util/wpt/**",
+ ":!:tools/**", // these files are starts with `#!/usr/bin/env`
+ ":!:cli/tools/init/templates/**",
+
+ // rust
+ "*.rs",
+ ":!:ops/optimizer_tests/**",
+
+ // toml
+ "*Cargo.toml",
+ ]);
+
+ let totalCount = 0;
+ const sourceFilesSet = new Set(sourceFiles);
+
+ for (const file of sourceFilesSet) {
+ const ERROR_MSG = "Copyright header is missing: ";
+
+ const fileText = await readFirstPartOfFile(file);
+ if (file.endsWith("Cargo.toml")) {
+ if (
+ !fileText.startsWith(
+ "# Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.",
+ )
+ ) {
+ console.log(ERROR_MSG + file);
+ totalCount += 1;
+ }
+ continue;
+ }
+
+ if (
+ !fileText.startsWith(
+ "// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.",
+ )
+ ) {
+ console.log(ERROR_MSG + file);
+ totalCount += 1;
+ }
+ }
+
+ console.log("\nTotal errors: " + totalCount);
+
+ if (totalCount > 0) {
+ Deno.exit(1);
+ }
+}
+
+async function main() {
+ await Deno.chdir(ROOT_PATH);
+
+ await checkCopyright();
+}
+
+await main();