diff --git a/scripts/check-browser-file-storage-boundaries.ts b/scripts/check-browser-file-storage-boundaries.ts index 1d23ab2f..3a1b981f 100644 --- a/scripts/check-browser-file-storage-boundaries.ts +++ b/scripts/check-browser-file-storage-boundaries.ts @@ -1,50 +1,38 @@ -import { spawnSync } from "node:child_process"; import { readFile, readdir } from "node:fs/promises"; import path from "node:path"; -function requiredPnpmCli(): string { - const executable = process.env.npm_execpath; - if (!executable) { - throw new Error( - "check:browser-file-storage-boundaries must run through pnpm", - ); +import { ESLint } from "eslint"; + +/** A lint run's verdict as `eslint --no-ignore --max-warnings=0` would exit, and its report. */ +type LintOutcome = Readonly<{ status: number; output: string }>; + +// One ESLint instance lints every fixture. The check used to start +// `pnpm exec eslint` once per fixture (about 30 processes), and loading the +// flat config each time was most of FE-GATE-010's minute. The options match +// that command line: --no-ignore is `ignore: false`, --max-warnings=0 makes a +// warning fail like an error. +const eslint = new ESLint({ ignore: false }); +const formatter = await eslint.loadFormatter("stylish"); + +async function lintOutcome(results: ESLint.LintResult[]): Promise { + const failed = results.some((result) => result.errorCount > 0 || result.warningCount > 0); + return { status: failed ? 1 : 0, output: await formatter.format(results) }; +} + +async function runEslint(target: string): Promise { + try { + return await lintOutcome(await eslint.lintFiles([target])); + } catch (error) { + // The CLI exits 2 when nothing matches or the config fails to load. + return { status: 2, output: `${error instanceof Error ? error.message : String(error)}\n` }; } - return executable; } -function runEslint(path: string) { - return spawnSync( - process.execPath, - [ - requiredPnpmCli(), - "exec", - "eslint", - path, - "--no-ignore", - "--max-warnings=0", - ], - { encoding: "utf8" }, - ); +async function runEslintSource(source: string, virtualPath: string): Promise { + return lintOutcome(await eslint.lintText(source, { filePath: virtualPath, warnIgnored: true })); } -function runEslintSource(source: string, virtualPath: string) { - return spawnSync( - process.execPath, - [ - requiredPnpmCli(), - "exec", - "eslint", - "--stdin", - "--stdin-filename", - virtualPath, - "--no-ignore", - "--max-warnings=0", - ], - { encoding: "utf8", input: source }, - ); -} - -const allowed = runEslint( +const allowed = await runEslint( "tests/fixtures/browser-file-storage-boundaries/allowed", ); const forbiddenRoot = @@ -54,10 +42,10 @@ const forbiddenFiles = ( ) .filter((entry) => entry.isFile() && /\.tsx?$/u.test(entry.name)) .map((entry) => path.join(forbiddenRoot, entry.name)); -const forbiddenResults = forbiddenFiles.map((file) => ({ - file, - result: runEslint(file), -})); +const forbiddenResults: Array<{ file: string; result: LintOutcome }> = []; +for (const file of forbiddenFiles) { + forbiddenResults.push({ file, result: await runEslint(file) }); +} const acceptedForbidden = forbiddenResults.filter( ({ result }) => result.status === 0, ); @@ -102,28 +90,28 @@ const protectedSourceFixtures = await Promise.all( const approvedAdapterResults = [ { file: "src/adapters/storage/indexeddb/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( indexedDbBypassSource, "src/adapters/storage/indexeddb/boundary-fixture.ts", ), }, { file: "src/adapters/cross-context-invalidation/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( crossContextAdapterSource, "src/adapters/cross-context-invalidation/boundary-fixture.ts", ), }, { file: "src/adapters/storage/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( browserStorageAdapterSource, "src/adapters/storage/boundary-fixture.ts", ), }, { file: "src/adapters/browser-transfer/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( blobBypassSource, "src/adapters/browser-transfer/boundary-fixture.ts", ), @@ -135,40 +123,40 @@ const rejectedApprovedAdapters = approvedAdapterResults.filter( const misplacedAdapterResults = [ { file: "src/adapters/http/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( indexedDbBypassSource, "src/adapters/http/boundary-fixture.ts", ), }, { file: "src/features/fixture-feature/adapters/boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( objectUrlBypassSource, "src/features/fixture-feature/adapters/boundary-fixture.ts", ), }, { file: "src/adapters/http/cross-context-boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( crossContextAdapterSource, "src/adapters/http/cross-context-boundary-fixture.ts", ), }, { file: "src/adapters/http/browser-storage-boundary-fixture.ts", - result: runEslintSource( + result: await runEslintSource( browserStorageAdapterSource, "src/adapters/http/browser-storage-boundary-fixture.ts", ), }, - ...protectedSourceFixtures.map(({ file, source }) => { - const virtualPath = `src/presentation/${file}`; - return { - file: virtualPath, - result: runEslintSource(source, virtualPath), - }; - }), ]; +for (const { file, source } of protectedSourceFixtures) { + const virtualPath = `src/presentation/${file}`; + misplacedAdapterResults.push({ + file: virtualPath, + result: await runEslintSource(source, virtualPath), + }); +} const acceptedMisplacedAdapters = misplacedAdapterResults.filter( ({ result }) => result.status === 0, ); @@ -180,24 +168,24 @@ if ( acceptedForbidden.length > 0 || acceptedMisplacedAdapters.length > 0 ) { - process.stderr.write(allowed.stderr || allowed.stdout); + process.stderr.write(allowed.output); for (const { file, result } of rejectedApprovedAdapters) { process.stderr.write( `${file}: owned browser capability adapter was rejected\n`, ); - process.stderr.write(result.stderr || result.stdout); + process.stderr.write(result.output); } for (const { file, result } of acceptedForbidden) { process.stderr.write( `${file}: forbidden browser API fixture was accepted\n`, ); - process.stderr.write(result.stderr || result.stdout); + process.stderr.write(result.output); } for (const { file, result } of acceptedMisplacedAdapters) { process.stderr.write( `${file}: native browser storage access outside its owned adapter was accepted\n`, ); - process.stderr.write(result.stderr || result.stdout); + process.stderr.write(result.output); } process.exit(1); }