[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
This commit is contained in:
Sathya Gunsasekaran
2024-05-01 15:34:40 +01:00
parent 36d775596f
commit c464445b91
3 changed files with 50 additions and 10 deletions
@@ -7,6 +7,7 @@
"test": "echo 'no tests'"
},
"dependencies": {
"chalk": "4",
"fast-glob": "^3.3.2",
"ora": "5.4.1",
"yargs": "^17.7.2"
@@ -0,0 +1,3 @@
export const config = {
knownIncompatibleLibraries: ["mobx"],
};
+46 -10
View File
@@ -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<LoggerEvent> = [];
const ACTIONABLE_FAILURES: Array<LoggerEvent> = [];
@@ -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();