-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
taojiahang.tjh
committed
Mar 21, 2024
1 parent
2e1ec79
commit 3ada245
Showing
6 changed files
with
147 additions
and
99 deletions.
There are no files selected for viewing
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
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
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
18 changes: 18 additions & 0 deletions
18
packages/bundler-okam/plugins/fork-ts-checker/child_process_fork.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// type-check-worker.js | ||
const { TypeChecker } = require('./ts-checker'); // 确保这里的路径正确 | ||
|
||
const projectRoot = process.argv[2]; // 从命令行参数获取项目根目录 | ||
|
||
async function runTypeCheck() { | ||
const typeChecker = new TypeChecker(projectRoot); | ||
await typeChecker.check(); | ||
} | ||
|
||
runTypeCheck() | ||
.then(() => { | ||
process.exit(0); // 成功完成 | ||
}) | ||
.catch((error) => { | ||
console.error(error); | ||
process.exit(1); // 发生错误 | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,30 @@ | ||
const ts = require('typescript'); | ||
const { fork } = require('child_process'); | ||
const path = require('path'); | ||
const fs = require('fs').promises; | ||
const { TypeChecker } = require('./ts-checker'); // 确保这里的路径正确 | ||
|
||
class TypeChecker { | ||
class ForkTsChecker { | ||
constructor(projectRoot) { | ||
this.projectRoot = projectRoot; | ||
} | ||
async runTypeCheck() { | ||
const typeChecker = new TypeChecker(projectRoot); | ||
await typeChecker.check(); | ||
} | ||
|
||
async check() { | ||
try { | ||
const configPath = ts.findConfigFile( | ||
this.projectRoot, | ||
ts.sys.fileExists, | ||
'tsconfig.json', | ||
); | ||
if (!configPath) { | ||
console.error( | ||
'Could not find a valid "tsconfig.json" file in the project root:', | ||
this.projectRoot, | ||
); | ||
return; | ||
} | ||
let configFileText = ''; | ||
try { | ||
configFileText = await fs.readFile(configPath, 'utf8'); | ||
} catch (readError) { | ||
console.error( | ||
`Error reading the "tsconfig.json" file at ${configPath}:`, | ||
readError, | ||
); | ||
return; | ||
} | ||
const configFile = ts.parseConfigFileTextToJson( | ||
configPath, | ||
configFileText, | ||
); | ||
if (configFile.error) { | ||
console.error('Error parsing "tsconfig.json" file:', configFile.error); | ||
return; | ||
} | ||
let parsedCommandLine; | ||
try { | ||
parsedCommandLine = ts.parseJsonConfigFileContent( | ||
configFile.config, | ||
ts.sys, | ||
path.dirname(configPath), | ||
); | ||
} catch (parseError) { | ||
console.error( | ||
'Error parsing the configuration from "tsconfig.json":', | ||
parseError, | ||
); | ||
return; | ||
} | ||
let program; | ||
try { | ||
program = ts.createProgram({ | ||
rootNames: parsedCommandLine.fileNames, | ||
options: { ...parsedCommandLine.options, noEmit: true }, | ||
}); | ||
} catch (programError) { | ||
console.error('Error creating the TypeScript program:', programError); | ||
return; | ||
} | ||
const diagnostics = ts.getPreEmitDiagnostics(program); | ||
if (diagnostics.length > 0) { | ||
diagnostics.forEach((diagnostic) => { | ||
const message = ts.flattenDiagnosticMessageText( | ||
diagnostic.messageText, | ||
'\n', | ||
); | ||
if (diagnostic.file && typeof diagnostic.start === 'number') { | ||
const { line, character } = | ||
diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); | ||
console.error( | ||
`${diagnostic.file.fileName} (${line + 1}, ${ | ||
character + 1 | ||
}): ${message}`, | ||
); | ||
} else { | ||
console.error(message); | ||
} | ||
}); | ||
runTypeCheckInChildProcess() { | ||
const workerScript = path.join(__dirname, 'child_process_fork.js'); // 确保这里的路径正确 | ||
const child = fork(workerScript, [projectRoot], { | ||
stdio: 'inherit', // 继承标准输入输出,以便在主进程中看到输出 | ||
}); | ||
|
||
child.on('exit', (code) => { | ||
if (code === 0) { | ||
console.log('Type checking completed successfully.'); | ||
} else { | ||
console.log('No type errors found.'); | ||
console.error('Type checking failed.'); | ||
} | ||
} catch (error) { | ||
console.error( | ||
'An unexpected error occurred during type checking:', | ||
error, | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { TypeChecker }; | ||
module.exports = { ForkTsChecker }; |
97 changes: 97 additions & 0 deletions
97
packages/bundler-okam/plugins/fork-ts-checker/ts-checker.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const ts = require('typescript'); | ||
const path = require('path'); | ||
const fs = require('fs').promises; | ||
|
||
class TypeChecker { | ||
constructor(projectRoot) { | ||
this.projectRoot = projectRoot; | ||
} | ||
|
||
async check() { | ||
try { | ||
const configPath = ts.findConfigFile( | ||
this.projectRoot, | ||
ts.sys.fileExists, | ||
'tsconfig.json', | ||
); | ||
if (!configPath) { | ||
console.error( | ||
'Could not find a valid "tsconfig.json" file in the project root:', | ||
this.projectRoot, | ||
); | ||
return; | ||
} | ||
let configFileText = ''; | ||
try { | ||
configFileText = await fs.readFile(configPath, 'utf8'); | ||
} catch (readError) { | ||
console.error( | ||
`Error reading the "tsconfig.json" file at ${configPath}:`, | ||
readError, | ||
); | ||
return; | ||
} | ||
const configFile = ts.parseConfigFileTextToJson( | ||
configPath, | ||
configFileText, | ||
); | ||
if (configFile.error) { | ||
console.error('Error parsing "tsconfig.json" file:', configFile.error); | ||
return; | ||
} | ||
let parsedCommandLine; | ||
try { | ||
parsedCommandLine = ts.parseJsonConfigFileContent( | ||
configFile.config, | ||
ts.sys, | ||
path.dirname(configPath), | ||
); | ||
} catch (parseError) { | ||
console.error( | ||
'Error parsing the configuration from "tsconfig.json":', | ||
parseError, | ||
); | ||
return; | ||
} | ||
let program; | ||
try { | ||
program = ts.createProgram({ | ||
rootNames: parsedCommandLine.fileNames, | ||
options: { ...parsedCommandLine.options, noEmit: true }, | ||
}); | ||
} catch (programError) { | ||
console.error('Error creating the TypeScript program:', programError); | ||
return; | ||
} | ||
const diagnostics = ts.getPreEmitDiagnostics(program); | ||
if (diagnostics.length > 0) { | ||
diagnostics.forEach((diagnostic) => { | ||
const message = ts.flattenDiagnosticMessageText( | ||
diagnostic.messageText, | ||
'\n', | ||
); | ||
if (diagnostic.file && typeof diagnostic.start === 'number') { | ||
const { line, character } = | ||
diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); | ||
console.error( | ||
`${diagnostic.file.fileName} (${line + 1}, ${ | ||
character + 1 | ||
}): ${message}`, | ||
); | ||
} else { | ||
console.error(message); | ||
} | ||
}); | ||
} else { | ||
console.log('No type errors found.'); | ||
} | ||
} catch (error) { | ||
console.error( | ||
'An unexpected error occurred during type checking:', | ||
error, | ||
); | ||
} | ||
} | ||
} | ||
|
||
module.exports = { TypeChecker }; |