Skip to content

Commit

Permalink
chore: simplify license checker tool
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua committed Feb 23, 2024
1 parent 241bcfa commit 49c481f
Showing 1 changed file with 31 additions and 40 deletions.
71 changes: 31 additions & 40 deletions tasks/check_license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,55 @@
// Copied from std/_tools/check_license.ts

import { walk } from "std/fs/walk.ts";
import { globToRegExp } from "std/path/glob_to_regexp.ts";

const EXTENSIONS = [".ts", ".tsx"];
const EXCLUDED_DIRS = [
"data",
"static",
];

const ROOT = new URL("../", import.meta.url);
const CHECK = Deno.args.includes("--check");
const CHECK = Deno.args[0] === "--check";
const CURRENT_YEAR = new Date().getFullYear();
const RX_COPYRIGHT = new RegExp(
`// Copyright 2023-([0-9]{4}) the Deno authors\\. All rights reserved\\. MIT license\\.\\n`,
"m",
);
const COPYRIGHT =
`// Copyright 2023-${CURRENT_YEAR} the Deno authors. All rights reserved. MIT license.`;
`// Copyright 2023-${CURRENT_YEAR} the Deno authors. All rights reserved. MIT license.\n`;

let failed = false;
let filesChecked = 0;
let filesFixed = 0;
const invalidFilePaths: string[] = [];

for await (
const { path } of walk(ROOT, {
exts: EXTENSIONS,
exts: [".ts", ".tsx"],
skip: [
...EXCLUDED_DIRS.map((path) => globToRegExp(ROOT.pathname + path)),
new RegExp("fresh.gen.ts"),
/$fresh\.gen\.ts/,
/$data/,
/$static/,
],
includeDirs: false,
})
) {
filesChecked++;

const content = await Deno.readTextFile(path);
const match = content.match(RX_COPYRIGHT);
if (!content.startsWith(COPYRIGHT)) continue;

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 (parseInt(match[1]) !== 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 (CHECK) {
invalidFilePaths.push(path);
} else {
const contentWithCopyright = COPYRIGHT + content;
await Deno.writeTextFile(path, contentWithCopyright);
filesFixed++;
}
}

if (failed) {
console.log(`Checked ${filesChecked} files`);

if (filesFixed > 0) {
console.info(`Fixed ${filesFixed} files`);
}

if (invalidFilePaths.length > 0) {
console.info(
`The following files have incorrect or missing copyright headers:`,
);
for (const path of invalidFilePaths) {
console.info(path);
}
console.info(`Copyright header should be "${COPYRIGHT}"`);
Deno.exit(1);
}

0 comments on commit 49c481f

Please sign in to comment.