UNPKG

increment-type-check

Version:

用于 TS 项目的增量检查工具,可以帮助你管理存量错误并专注于新代码的质量。

69 lines 3.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.runIncrementalTypeCheck = void 0; const fs_1 = __importDefault(require("fs")); const child_process_1 = require("child_process"); const constants_1 = require("./constants"); const utils_1 = require("./utils"); const runIncrementalTypeCheck = (options) => { const { updateIgnore } = options ?? {}; fs_1.default.writeFileSync(constants_1.tempConfigPath, constants_1.tempTsConfigTip + JSON.stringify((0, utils_1.getTypeCheckTsConfig)({ readArgs: true }), null, 2)); if (updateIgnore) { (0, utils_1.clearTsCheckIgnoreFileContent)(); } // 创建一个子进程 const child = (0, child_process_1.spawn)("npx", ["tsc", "-p", constants_1.tempConfigPath, "--skipLibCheck"], { stdio: "inherit", shell: true, }); let resultStr = ""; // 监听子进程的stdout事件 child.stdout?.on("data", (data) => { const output = Buffer.from(data).toString("utf-8"); resultStr += output; }); // 监听子进程的 exit 事件 child.on("exit", async (code) => { // 删除校验时的临时tsconfig配置 fs_1.default.rmSync(constants_1.tempConfigPath); // 输出的所有行 const errorLines = resultStr.split("\n"); // 保存所有有错误的文件路径以及对应的错误信息 const errorFilesRecords = []; errorLines.forEach((line) => { // 表示该行不是单纯的错误信息,而是包含路径的错误文件信息 if (line && !line.startsWith(" ")) { const filePath = line.match(/(.+)(?=\(.+\): error )/)?.[0] ?? ""; const record = { filePath, error: line }; errorFilesRecords.push(record); } else { // 否则错误行添加到对应的错误信息中 const lastRecord = errorFilesRecords[errorFilesRecords.length - 1]; if (lastRecord) lastRecord.error = lastRecord.error + "\n" + line; } }); // 得到过滤后的有错误的文件路径以及对应的错误信息 const errorFileFilterRecords = errorFilesRecords.filter(({ filePath }) => filePath && !(0, utils_1.isTypeCheckIgnoreFile)(filePath)); // 如果除去被ignore错误的文件还存在错误,则退出进程,否则表示校验通过 if (errorFileFilterRecords.length) { if (updateIgnore) { (0, utils_1.updateTsCheckIgnoreFileContent)(errorFileFilterRecords); } else { console.log(errorFileFilterRecords.map(({ error }) => error).join("\n")); process.exit(code ?? 0); } } else { console.log("typeCheck Success!"); } }); }; exports.runIncrementalTypeCheck = runIncrementalTypeCheck; //# sourceMappingURL=typeCheck.js.map