From c464445b91d0d19ed00ecb81d993666495a5191f Mon Sep 17 00:00:00 2001 From: Sathya Gunsasekaran Date: Wed, 1 May 2024 15:34:40 +0100 Subject: [PATCH] [healthcheck] Check for incompatible libraries Add a configurable list of known incompatible libraries. Check all package.jsons for any uses of known incompatible libraries and warn if found. ghstack-source-id: 7329e3792b57458e681780cba3140a14a9b1a60d Pull Request resolved: https://github.com/facebook/react-forget/pull/2923 --- compiler/packages/healthcheck/package.json | 1 + compiler/packages/healthcheck/src/config.ts | 3 ++ compiler/packages/healthcheck/src/index.ts | 56 +++++++++++++++++---- 3 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 compiler/packages/healthcheck/src/config.ts diff --git a/compiler/packages/healthcheck/package.json b/compiler/packages/healthcheck/package.json index 0589e9317c..2fa96920d6 100644 --- a/compiler/packages/healthcheck/package.json +++ b/compiler/packages/healthcheck/package.json @@ -7,6 +7,7 @@ "test": "echo 'no tests'" }, "dependencies": { + "chalk": "4", "fast-glob": "^3.3.2", "ora": "5.4.1", "yargs": "^17.7.2" diff --git a/compiler/packages/healthcheck/src/config.ts b/compiler/packages/healthcheck/src/config.ts new file mode 100644 index 0000000000..eb1d7a8068 --- /dev/null +++ b/compiler/packages/healthcheck/src/config.ts @@ -0,0 +1,3 @@ +export const config = { + knownIncompatibleLibraries: ["mobx"], +}; diff --git a/compiler/packages/healthcheck/src/index.ts b/compiler/packages/healthcheck/src/index.ts index c3628be9f5..c5de001d33 100644 --- a/compiler/packages/healthcheck/src/index.ts +++ b/compiler/packages/healthcheck/src/index.ts @@ -12,10 +12,12 @@ import { type PluginOptions, } from "babel-plugin-react-forget/src"; import { LoggerEvent } from "babel-plugin-react-forget/src/Entrypoint"; +import chalk from "chalk"; import { glob } from "fast-glob"; import * as fs from "fs/promises"; import ora from "ora"; import yargs from "yargs/yargs"; +import { config } from "./config"; const SUCCESS: Array = []; const ACTIONABLE_FAILURES: Array = []; @@ -41,7 +43,8 @@ const logger = { } case "CompileDiagnostic": case "PipelineError": - // TODO(gsn): Silenty fail? + OTHER_FAILURES.push(event); + return; } }, }; @@ -86,7 +89,7 @@ async function main() { .option("src", { description: "glob expression matching src files to compile", type: "string", - default: "**/*.{js,ts,jsx,tsx,mjs}", + default: "**/*.*", }) .parseSync(); @@ -95,7 +98,7 @@ async function main() { // no file extension specified if (!src.includes(".")) { - src = src + ".{js,ts,jsx,tsx,mjs}"; + src = src; } const globOptions = { @@ -112,20 +115,53 @@ async function main() { ], }; + const jsFileExtensionRE = /(js|ts|jsx|tsx|mjs)$/; + const packageJsonRE = /package\.json$/; + const knownIncompatibleLibrariesUsage = new Set(); + for (const path of await glob(src, globOptions)) { const source = await fs.readFile(path, "utf-8"); - spinner.text = `Compiling ${path}`; - compile(source, path); + if (jsFileExtensionRE.exec(path) !== null) { + spinner.text = `Compiling ${path}`; + compile(source, path); - if (!STRICT_MODE_USAGE) { - STRICT_MODE_USAGE = StrictModeRE.exec(source) !== null; + if (!STRICT_MODE_USAGE) { + STRICT_MODE_USAGE = StrictModeRE.exec(source) !== null; + } + } else if (packageJsonRE.exec(path) !== null) { + const contents = JSON.parse(source); + const deps = contents.dependencies; + for (const library of config.knownIncompatibleLibraries) { + if (Object.hasOwn(deps, library)) { + knownIncompatibleLibrariesUsage.add(library); + } + } } } spinner.stop(); - console.log(`Successful compilation: ${SUCCESS.length}`); - console.log(`Failed compilation: ${ACTIONABLE_FAILURES.length}`); - console.log(`StrictMode usage: ${STRICT_MODE_USAGE}`); + const totalComponents = + SUCCESS.length + OTHER_FAILURES.length + ACTIONABLE_FAILURES.length; + console.log( + chalk.green( + `Successfully compiled ${SUCCESS.length} out of ${totalComponents} components.` + ) + ); + + if (STRICT_MODE_USAGE) { + console.log(chalk.green("StrictMode usage found.")); + } else { + console.log(chalk.red("StrictMode usage not found.")); + } + + if (knownIncompatibleLibrariesUsage.size > 0) { + console.log(chalk.red(`Found the following incompatible libraries:`)); + for (const library of knownIncompatibleLibrariesUsage) { + console.log(library); + } + } else { + console.log(chalk.green(`Found no usage of incompatible libraries.`)); + } } main();