Last active
January 10, 2025 19:19
-
-
Save felskov/84fa477e87fb18f3ff7bee4ee2ce1c57 to your computer and use it in GitHub Desktop.
simple script to run TypeScript in --incremental --noEmit mode, while ignoring errors in any files outside the project folder (useful in mono repos with different and incompatible tsconfig.json's per package)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
import path from "path"; | |
import ts from "typescript"; | |
// | |
// parse tsconfig.json | |
// | |
const configFilePath = ts.findConfigFile( | |
"./", | |
ts.sys.fileExists, | |
"tsconfig.json" | |
); | |
const projectPath = path.resolve(path.dirname(configFilePath)); | |
const configFile = ts.readConfigFile(configFilePath, ts.sys.readFile); | |
const compilerOptions = ts.parseJsonConfigFileContent( | |
configFile.config, | |
ts.sys, | |
"./", | |
{ | |
incremental: true, | |
tsBuildInfoFile: path.join(projectPath, "type-check.tsbuildinfo"), | |
// required to force emission of tsbuildinfo file(?) | |
outDir: "tmp", | |
noEmit: true, | |
} | |
); | |
// | |
// setup program | |
// | |
const program = ts.createIncrementalProgram({ | |
rootNames: compilerOptions.fileNames, | |
options: compilerOptions.options, | |
}); | |
// | |
// run diagnostics and emit a tsbuildinfo for incremental type-checks | |
// | |
const preEmitDiagnostics = ts.getPreEmitDiagnostics(program); | |
const emitResult = program.emit(); | |
const buildInfoEmitResult = program.emitBuildInfo(); | |
const diagnostics = [ | |
...preEmitDiagnostics, | |
...emitResult.diagnostics, | |
...buildInfoEmitResult.diagnostics, | |
].filter( | |
(it) => | |
it.file.fileName.startsWith(projectPath + path.sep) && | |
!it.file.fileName.includes("node_modules") | |
); | |
// | |
// setup reporter utilities | |
// | |
const errorCount = ts.getErrorCountForSummary(diagnostics); | |
const filesInError = ts.getFilesInErrorForSummary(diagnostics); | |
const diagnosticsReporter = ts.createDiagnosticReporter(ts.sys, true); | |
const reportDiagnostics = (diagnostics) => { | |
for (const diagnostic of diagnostics) { | |
diagnosticsReporter(diagnostic); | |
} | |
}; | |
const reportSummary = (errorCount, filesInError) => { | |
console.log( | |
ts.getErrorSummaryText(errorCount, filesInError, ts.sys.newLine, ts.sys) | |
); | |
}; | |
// | |
// flush output to console | |
// | |
reportDiagnostics(diagnostics); | |
reportSummary(errorCount, filesInError); | |
process.exit(errorCount === 0 ? 0 : 1); |
Thank you for writing this, works like a charm :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found that some error fileName don't match the filter rule, and my
projectPath
is/Users/luchengjie/Documents/earth-ama