[healthcheck] Compile sources
Run the compiler on the globbed soruces. The logger is used to capture the success and failure compilation cases at the component level. (If we were to compile the entire file directly, we wouldn't get this granularity) For now, we just log the number of success and failures. In the future, we can provide a better report building on this. ghstack-source-id: 6d2d918190b6ed5d42b795491bbce29a950b9741 Pull Request resolved: https://github.com/facebook/react-forget/pull/2888
Sathya Gunsasekaran committed
Apr 23, 2024 at 12:28 UTC
dff05237c582d379c730bf1132c0fc8e7e0b627a
2 files changed
+51
-1
compiler/packages/babel-plugin-react-forget/src/index.ts
+1
@@ -5,6 +5,7 @@
5
* LICENSE file in the root directory of this source tree.
6
*/
7
8
+export { runReactForgetBabelPlugin } from "./Babel/RunReactForgetBabelPlugin";
9
export {
10
CompilerError,
11
CompilerErrorDetail,
compiler/packages/health-check/src/index.ts
+50
-1
@@ -5,9 +5,54 @@
5
* LICENSE file in the root directory of this source tree.
6
*/
7
8
+import {
9
+ runReactForgetBabelPlugin,
10
+ type PluginOptions,
11
+} from "babel-plugin-react-forget/src";
12
+import { LoggerEvent } from "babel-plugin-react-forget/src/Entrypoint";
13
import { glob } from "fast-glob";
14
+import * as fs from "fs/promises";
15
import yargs from "yargs/yargs";
16
17
+const SUCCESS: Array<LoggerEvent> = [];
18
+const FAILURES: Array<LoggerEvent> = [];
19
+
20
+const logger = {
21
+ logEvent(_: string | null, event: LoggerEvent) {
22
+ switch (event.kind) {
23
+ case "CompileSuccess": {
24
+ SUCCESS.push(event);
25
+ return;
26
+ }
27
+ case "CompileError": {
28
+ FAILURES.push(event);
29
+ return;
30
+ }
31
+ case "CompileDiagnostic":
32
+ case "PipelineError":
33
+ // TODO(gsn): Silenty fail?
34
+ }
35
+ },
36
+};
37
+
38
+const COMPILER_OPTIONS: Partial<PluginOptions> = {
39
+ noEmit: true,
40
+ compilationMode: "infer",
41
+ panicThreshold: "critical_errors",
42
+ logger,
43
+};
44
+
45
+function compile(sourceCode: string, filename: string) {
46
+ try {
47
+ runReactForgetBabelPlugin(
48
+ sourceCode,
49
+ filename,
50
+ "typescript",
51
+ COMPILER_OPTIONS
52
+ );
53
+ } catch {}
54
+}
55
+
56
async function main() {
57
const argv = yargs(process.argv.slice(2))
58
.scriptName("healthcheck")
@@ -41,8 +86,12 @@ async function main() {
86
};
87
88
for (const path of await glob(src, globOptions)) {
44
- console.log(path);
89
+ const source = await fs.readFile(path, "utf-8");
90
+ compile(source, path);
91
}
92
+
93
+ console.log(`Successful compilation: ${SUCCESS.length}`);
94
+ console.log(`Failed compilation: ${FAILURES.length}`);
95
}
96
97
main();