2023-01-13 13:42:15 -05:00
|
|
|
#!/usr/bin/env -S deno run --unstable --allow-read=. --allow-run=git
|
2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
|
|
|
import { getSources, ROOT_PATH } from "./util.js";
|
|
|
|
|
2024-01-06 19:14:38 -05:00
|
|
|
const copyrightYear = 2024;
|
|
|
|
|
2023-01-13 02:51:32 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 13:42:15 -05:00
|
|
|
export async function checkCopyright() {
|
2023-01-13 02:51:32 -05:00
|
|
|
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/**",
|
|
|
|
":!:cli/tools/init/templates/**",
|
2023-02-16 08:30:14 -05:00
|
|
|
":!:cli/tests/unit_node/testdata/**",
|
2023-02-17 09:58:52 -05:00
|
|
|
":!:cli/tests/node_compat/test/**",
|
2023-07-28 11:27:10 -04:00
|
|
|
":!:cli/tools/bench/mitata.rs",
|
2023-01-13 02:51:32 -05:00
|
|
|
|
|
|
|
// rust
|
|
|
|
"*.rs",
|
|
|
|
":!:ops/optimizer_tests/**",
|
|
|
|
|
|
|
|
// toml
|
|
|
|
"*Cargo.toml",
|
|
|
|
]);
|
|
|
|
|
2023-03-15 22:39:40 -04:00
|
|
|
const errors = [];
|
2023-01-13 02:51:32 -05:00
|
|
|
const sourceFilesSet = new Set(sourceFiles);
|
2023-06-26 11:38:55 -04:00
|
|
|
const ERROR_MSG = "Copyright header is missing: ";
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2023-06-26 11:38:55 -04:00
|
|
|
// Acceptable content before the copyright line
|
|
|
|
const ACCEPTABLE_LINES =
|
|
|
|
/^(\/\/ deno-lint-.*|\/\/ Copyright.*|\/\/ Ported.*|\s*|#!\/.*)\n/;
|
|
|
|
const COPYRIGHT_LINE =
|
2024-01-06 19:14:38 -05:00
|
|
|
`Copyright 2018-${copyrightYear} the Deno authors. All rights reserved. MIT license.`;
|
2023-06-26 11:38:55 -04:00
|
|
|
const TOML_COPYRIGHT_LINE = "# " + COPYRIGHT_LINE;
|
|
|
|
const C_STYLE_COPYRIGHT_LINE = "// " + COPYRIGHT_LINE;
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2023-06-26 11:38:55 -04:00
|
|
|
for (const file of sourceFilesSet) {
|
2023-01-13 02:51:32 -05:00
|
|
|
const fileText = await readFirstPartOfFile(file);
|
|
|
|
if (file.endsWith("Cargo.toml")) {
|
|
|
|
if (
|
2023-06-26 11:38:55 -04:00
|
|
|
!fileText.startsWith(TOML_COPYRIGHT_LINE)
|
2023-01-13 02:51:32 -05:00
|
|
|
) {
|
2023-03-15 22:39:40 -04:00
|
|
|
errors.push(ERROR_MSG + file);
|
2023-01-13 02:51:32 -05:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2023-06-26 11:38:55 -04:00
|
|
|
!fileText.startsWith(C_STYLE_COPYRIGHT_LINE)
|
2023-01-13 02:51:32 -05:00
|
|
|
) {
|
2023-06-26 11:38:55 -04:00
|
|
|
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)
|
|
|
|
}')`,
|
|
|
|
);
|
|
|
|
}
|
2023-01-13 02:51:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 19:14:38 -05:00
|
|
|
// 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`);
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:39:40 -04:00
|
|
|
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"));
|
2023-03-21 12:31:34 -04:00
|
|
|
throw new Error(`Copyright checker had ${errors.length} errors.`);
|
2023-01-13 02:51:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 13:42:15 -05:00
|
|
|
if (import.meta.main) {
|
2023-01-13 02:51:32 -05:00
|
|
|
await checkCopyright();
|
|
|
|
}
|