Last active
October 29, 2024 06:34
-
-
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 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); |
@childrentime I think that this would ignore errors from node_modules out of the box (see line 56), have you tried it?
yeah! Thanks, great work😍!
@felskov While it's a shame that this is even needed, it really is a lifesaver and the only thing that makes proper typechecking in our monorepo possible right now. Thank you so much 🙏
it ignores node_modules but it also ignores .d.ts in my project source files just like skipLibCheck
here is my tsbuildinfo which is ignored
[
2118,
[
{
"file": "./src/typings/policy-promotion-quantity.d.ts",
"start": 466,
"length": 32,
"messageText": "Cannot find name 'PolicyPromotionQuantityDetailDTO'. Did you mean 'PolicyPromotionQuantityLadder'?",
"category": 1,
"code": 2552
},
{
"file": "./src/typings/policy-promotion-quantity.d.ts",
"start": 2253,
"length": 5,
"messageText": "Cannot find name 'State'.",
"category": 1,
"code": 2304
},
{
"file": "./src/typings/policy-promotion-quantity.d.ts",
"start": 2299,
"length": 11,
"messageText": "Cannot find name 'SupplierDTO'.",
"category": 1,
"code": 2304
}
]
]
I found that some error fileName don't match the filter rule, and my projectPath
is /Users/luchengjie/Documents/earth-ama
fileName: 'src/typings/policy-promotion-quantity.d.ts',
path: '/users/luchengjie/documents/earth-ama/src/typings/policy-promotion-quantity.d.ts',
resolvedPath: '/users/luchengjie/documents/earth-ama/src/typings/policy-promotion-quantity.d.ts',
originalFileName: 'src/typings/policy-promotion-quantity.d.ts',
fileName: '/Users/luchengjie/Documents/earth-ama/src/lib/app/request.tsx',
path: '/users/luchengjie/documents/earth-ama/src/lib/app/request.tsx',
resolvedPath: '/users/luchengjie/documents/earth-ama/src/lib/app/request.tsx',
originalFileName: '/Users/luchengjie/Documents/earth-ama/src/lib/app/request.tsx',
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
how this works to ignore node_modules?