From a365d4e73ec78d72dd9816759ed0a774ab17381f Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 25 Jan 2024 09:45:03 +0100 Subject: [PATCH 1/2] initial commit --- _tools/check_browser_compat.ts | 39 ++++++++++++++++++---------------- _tools/check_licence.ts | 2 +- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/_tools/check_browser_compat.ts b/_tools/check_browser_compat.ts index de356d7f9696..04b2cbdb380c 100644 --- a/_tools/check_browser_compat.ts +++ b/_tools/check_browser_compat.ts @@ -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 + "\n" + + 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}`) - ); -} diff --git a/_tools/check_licence.ts b/_tools/check_licence.ts index afe618327cba..47b20fe7d55f 100644 --- a/_tools/check_licence.ts +++ b/_tools/check_licence.ts @@ -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; From 5c8c2218b03a2a1e21d1e226e948c2544ee17db0 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 2 Feb 2024 21:35:43 +0100 Subject: [PATCH 2/2] remove newline --- _tools/check_browser_compat.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tools/check_browser_compat.ts b/_tools/check_browser_compat.ts index 04b2cbdb380c..1422e6b3c4dd 100644 --- a/_tools/check_browser_compat.ts +++ b/_tools/check_browser_compat.ts @@ -44,7 +44,7 @@ for await (const { path } of walk(ROOT, { exts: [".ts"], skip: SKIP })) { const index = source.indexOf(COPYRIGHT); await Deno.writeTextFile( path, - source.slice(0, index + COPYRIGHT.length) + "\n" + DECLARATION + "\n" + + source.slice(0, index + COPYRIGHT.length) + "\n" + DECLARATION + source.slice(index + COPYRIGHT.length), ); }