2024-09-13 05:43:13 +00:00
|
|
|
// deno-lint-ignore-file no-console
|
2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Running this script provides a list of suggested files that might be browser-compatible.
|
|
|
|
* It skips test code, benchmark code, internal code and `version.ts` at the root.
|
|
|
|
*
|
|
|
|
* Run using: deno run --allow-read --allow-run _tools/check_browser_compat.ts
|
|
|
|
*/
|
2024-02-05 01:53:06 +00:00
|
|
|
|
|
|
|
import { walk } from "../fs/walk.ts";
|
|
|
|
import { COPYRIGHT } from "./check_licence.ts";
|
2023-03-18 12:36:00 +00:00
|
|
|
|
|
|
|
const ROOT = new URL("../", import.meta.url);
|
|
|
|
const SKIP = [/(test|bench|\/_|\\_|testdata|version.ts)/];
|
|
|
|
const DECLARATION = "// This module is browser compatible.";
|
2024-02-05 01:53:06 +00:00
|
|
|
const CHECK = Deno.args.includes("--check");
|
2023-03-18 12:36:00 +00:00
|
|
|
|
|
|
|
function isBrowserCompatible(filePath: string): boolean {
|
|
|
|
const command = new Deno.Command(Deno.execPath(), {
|
|
|
|
args: [
|
|
|
|
"check",
|
2024-01-12 04:38:41 +00:00
|
|
|
"--no-lock",
|
2023-03-18 12:36:00 +00:00
|
|
|
"--config",
|
|
|
|
"browser-compat.tsconfig.json",
|
|
|
|
filePath,
|
|
|
|
],
|
|
|
|
});
|
|
|
|
const { success } = command.outputSync();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2024-02-05 01:53:06 +00:00
|
|
|
function hasBrowserCompatibleComment(source: string): boolean {
|
|
|
|
return source.includes(DECLARATION);
|
2023-03-18 12:36:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 01:53:06 +00:00
|
|
|
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),
|
|
|
|
);
|
|
|
|
}
|
2023-03-18 12:36:00 +00:00
|
|
|
}
|
|
|
|
}
|