mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
chore(tools): add check flag to browser_compat.ts
(#4240)
This commit is contained in:
parent
980545f4b8
commit
bcfec95e6f
@ -6,11 +6,14 @@
|
||||
*
|
||||
* Run using: deno run --allow-read --allow-run _tools/check_browser_compat.ts
|
||||
*/
|
||||
import { walkSync } from "../fs/walk.ts";
|
||||
|
||||
import { walk } from "../fs/walk.ts";
|
||||
import { COPYRIGHT } from "./check_licence.ts";
|
||||
|
||||
const ROOT = new URL("../", import.meta.url);
|
||||
const SKIP = [/(test|bench|\/_|\\_|testdata|version.ts)/];
|
||||
const DECLARATION = "// This module is browser compatible.";
|
||||
const CHECK = Deno.args.includes("--check");
|
||||
|
||||
function isBrowserCompatible(filePath: string): boolean {
|
||||
const command = new Deno.Command(Deno.execPath(), {
|
||||
@ -26,24 +29,24 @@ function isBrowserCompatible(filePath: string): boolean {
|
||||
return success;
|
||||
}
|
||||
|
||||
function hasBrowserCompatibleComment(path: string): boolean {
|
||||
const output = Deno.readTextFileSync(path);
|
||||
return output.includes(DECLARATION);
|
||||
function hasBrowserCompatibleComment(source: string): boolean {
|
||||
return source.includes(DECLARATION);
|
||||
}
|
||||
|
||||
const maybeBrowserCompatibleFiles: string[] = [];
|
||||
|
||||
for (const { path } of walkSync(ROOT, { exts: [".ts"], skip: SKIP })) {
|
||||
if (isBrowserCompatible(path) && !hasBrowserCompatibleComment(path)) {
|
||||
maybeBrowserCompatibleFiles.push(path);
|
||||
for await (const { path } of walk(ROOT, { exts: [".ts"], skip: SKIP })) {
|
||||
const source = await Deno.readTextFile(path);
|
||||
if (isBrowserCompatible(path) && !hasBrowserCompatibleComment(source)) {
|
||||
if (CHECK) {
|
||||
console.log(
|
||||
`${path} is likely browser-compatible and can have the "${DECLARATION}" comment added.`,
|
||||
);
|
||||
} else {
|
||||
const index = source.indexOf(COPYRIGHT);
|
||||
await Deno.writeTextFile(
|
||||
path,
|
||||
source.slice(0, index + COPYRIGHT.length) + "\n" + DECLARATION +
|
||||
source.slice(index + COPYRIGHT.length),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (maybeBrowserCompatibleFiles.length) {
|
||||
console.log(
|
||||
`The following files are likely browser-compatible and can have the "${DECLARATION}" comment added:`,
|
||||
);
|
||||
maybeBrowserCompatibleFiles.forEach((path, index) =>
|
||||
console.log(`${index + 1}. ${path}`)
|
||||
);
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ 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`,
|
||||
);
|
||||
const COPYRIGHT =
|
||||
export const COPYRIGHT =
|
||||
`// Copyright ${FIRST_YEAR}-${CURRENT_YEAR} the Deno authors. All rights reserved. MIT license.`;
|
||||
|
||||
let failed = false;
|
||||
|
Loading…
Reference in New Issue
Block a user