2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-11-05 10:03:44 +00:00
|
|
|
import { walk } from "../fs/walk.ts";
|
2023-11-12 21:05:09 +00:00
|
|
|
import { globToRegExp } from "../path/glob_to_regexp.ts";
|
2022-06-01 11:49:12 +00:00
|
|
|
|
|
|
|
const EXTENSIONS = [".mjs", ".js", ".ts", ".rs"];
|
2022-11-05 10:03:44 +00:00
|
|
|
const EXCLUDED_DIRS = [
|
2024-05-13 00:11:47 +00:00
|
|
|
"**/cli/testdata",
|
2023-01-06 04:10:06 +00:00
|
|
|
"**/dotenv/testdata",
|
|
|
|
"**/fs/testdata",
|
|
|
|
"**/http/testdata",
|
|
|
|
"**/crypto/_wasm/target",
|
2023-05-31 05:16:30 +00:00
|
|
|
"**/crypto/_wasm/lib",
|
2023-12-12 10:37:09 +00:00
|
|
|
"**/.git",
|
2024-01-08 02:47:54 +00:00
|
|
|
"**/docs/**",
|
2022-06-01 11:49:12 +00:00
|
|
|
];
|
|
|
|
|
2022-11-05 10:03:44 +00:00
|
|
|
const ROOT = new URL("../", import.meta.url);
|
2022-12-15 05:58:15 +00:00
|
|
|
const CHECK = Deno.args.includes("--check");
|
2022-11-05 10:03:44 +00:00
|
|
|
const FIRST_YEAR = 2018;
|
|
|
|
const CURRENT_YEAR = new Date().getFullYear();
|
2022-12-15 05:58:15 +00:00
|
|
|
const RX_COPYRIGHT = new RegExp(
|
|
|
|
`// Copyright ([0-9]{4})-([0-9]{4}) the Deno authors\\. All rights reserved\\. MIT license\\.\n`,
|
|
|
|
);
|
2024-02-05 01:53:06 +00:00
|
|
|
export const COPYRIGHT =
|
2022-11-05 10:03:44 +00:00
|
|
|
`// 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,
|
2023-01-06 04:10:06 +00:00
|
|
|
skip: EXCLUDED_DIRS.map((path) => globToRegExp(path)),
|
2022-11-05 10:03:44 +00:00
|
|
|
includeDirs: false,
|
|
|
|
})
|
|
|
|
) {
|
|
|
|
const content = await Deno.readTextFile(path);
|
2022-12-15 05:58:15 +00:00
|
|
|
const match = content.match(RX_COPYRIGHT);
|
2022-12-13 04:07:56 +00:00
|
|
|
|
2022-12-15 05:58:15 +00:00
|
|
|
if (!match) {
|
|
|
|
if (CHECK) {
|
|
|
|
console.error(`Missing copyright header: ${path}`);
|
2022-11-05 10:03:44 +00:00
|
|
|
failed = true;
|
2022-12-13 04:07:56 +00:00
|
|
|
} else {
|
|
|
|
const contentWithCopyright = COPYRIGHT + "\n" + content;
|
|
|
|
await Deno.writeTextFile(path, contentWithCopyright);
|
2022-12-15 05:58:15 +00:00
|
|
|
console.log("Copyright header automatically added to " + path);
|
|
|
|
}
|
|
|
|
} else if (
|
2024-03-06 05:09:13 +00:00
|
|
|
(match[1] && parseInt(match[1]) !== FIRST_YEAR) ||
|
|
|
|
(match[2] && parseInt(match[2]) !== CURRENT_YEAR)
|
2022-12-15 05:58:15 +00:00
|
|
|
) {
|
|
|
|
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);
|
2022-06-01 11:49:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-05 10:03:44 +00:00
|
|
|
if (failed) {
|
|
|
|
console.info(`Copyright header should be "${COPYRIGHT}"`);
|
|
|
|
Deno.exit(1);
|
|
|
|
}
|