|
| 1 | +import * as kit from "@volar/kit"; |
| 2 | +import { gitHubScriptLanguagePlugin } from "@yamachu/workflow-script-highlighter-core/src/languagePlugin"; |
| 3 | +import { readdirSync, statSync } from "node:fs"; |
| 4 | +import { isAbsolute, join, resolve } from "node:path"; |
| 5 | +import { ScriptTarget } from "typescript"; |
| 6 | +import { create as createTypeScriptServices } from "volar-service-typescript"; |
| 7 | + |
| 8 | +const maybePaths = process.argv.slice(2); |
| 9 | + |
| 10 | +const abortController = new AbortController(); |
| 11 | + |
| 12 | +process.on("SIGINT", () => { |
| 13 | + abortController.abort(); |
| 14 | + process.exit(1); |
| 15 | +}); |
| 16 | + |
| 17 | +const getScripts = (paths: string[]) => |
| 18 | + paths |
| 19 | + .map((path) => { |
| 20 | + if (isAbsolute(path)) { |
| 21 | + return path; |
| 22 | + } |
| 23 | + |
| 24 | + return resolve(path); |
| 25 | + }) |
| 26 | + .reduce<string[]>((prev, curr) => { |
| 27 | + const stat = statSync(curr); |
| 28 | + if (stat.isFile()) { |
| 29 | + return [...prev, curr]; |
| 30 | + } |
| 31 | + return [...prev, ...findYamlFiles(curr)]; |
| 32 | + }, []); |
| 33 | + |
| 34 | +const findYamlFiles = (dir: string, fileList: string[] = []): string[] => { |
| 35 | + const files = readdirSync(dir); |
| 36 | + files.forEach((file) => { |
| 37 | + const filePath = join(dir, file); |
| 38 | + const stat = statSync(filePath); |
| 39 | + if (stat.isDirectory()) { |
| 40 | + findYamlFiles(filePath, fileList); |
| 41 | + } else if (file.endsWith(".yaml") || file.endsWith(".yml")) { |
| 42 | + fileList.push(filePath); |
| 43 | + } |
| 44 | + }); |
| 45 | + return fileList; |
| 46 | +}; |
| 47 | + |
| 48 | +const targetFiles = getScripts(maybePaths); |
| 49 | + |
| 50 | +const packageRoot = resolve(__dirname, "..", "resources", "node_modules"); |
| 51 | +const tsPath = resolve(packageRoot, "typescript"); |
| 52 | + |
| 53 | +const checker = kit.createTypeScriptInferredChecker( |
| 54 | + [gitHubScriptLanguagePlugin], |
| 55 | + createTypeScriptServices(require(tsPath)), |
| 56 | + () => targetFiles, |
| 57 | + { |
| 58 | + typeRoots: [packageRoot], |
| 59 | + checkJs: true, |
| 60 | + target: ScriptTarget.ES2022, |
| 61 | + lib: ["ES2022"], |
| 62 | + } |
| 63 | +); |
| 64 | + |
| 65 | +Promise.all( |
| 66 | + checker.getRootFileNames().map((fileName) => { |
| 67 | + return Promise.race([ |
| 68 | + new Promise((_, reject) => { |
| 69 | + abortController.signal.addEventListener("abort", () => { |
| 70 | + reject(); |
| 71 | + }); |
| 72 | + }), |
| 73 | + checker.check(fileName).then((diagnostics) => { |
| 74 | + return [fileName, diagnostics] as const; |
| 75 | + }), |
| 76 | + ] as const); |
| 77 | + }) |
| 78 | +) |
| 79 | + .then((results: any[]) => { |
| 80 | + results.forEach(([fileName, diagnostics]: [string, kit.Diagnostic[]]) => { |
| 81 | + console.log(checker.printErrors(fileName, diagnostics)); |
| 82 | + }); |
| 83 | + }) |
| 84 | + .then(() => { |
| 85 | + process.exit(0); |
| 86 | + }) |
| 87 | + .catch((_) => { |
| 88 | + process.exit(1); |
| 89 | + }); |
0 commit comments