|
| 1 | +import {JavaAnalysis} from "./analysis/java"; |
| 2 | +import {spawnSync} from "node:child_process"; |
| 3 | + |
| 4 | +export class CLDK { |
| 5 | + /** |
| 6 | + * The programming language of choice |
| 7 | + */ |
| 8 | + private language: string; |
| 9 | + |
| 10 | + constructor(language: string) { |
| 11 | + this.language = language; |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * A static for method to create a new instance of the CLDK class |
| 16 | + */ |
| 17 | + public static for(language: string): CLDK { |
| 18 | + return new CLDK(language); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Get the programming language of the CLDK instance |
| 23 | + */ |
| 24 | + public getLanguage(): string { |
| 25 | + return this.language; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Implementation of the analysis method |
| 30 | + */ |
| 31 | + public analysis({ projectPath, analysisLevel }: { projectPath: string, analysisLevel: string }): JavaAnalysis { |
| 32 | + if (this.language === "java") { |
| 33 | + this.makeSureJavaIsInstalled(); |
| 34 | + return new JavaAnalysis({ |
| 35 | + projectDir: projectPath, |
| 36 | + analysisLevel: analysisLevel, |
| 37 | + }); |
| 38 | + } else { |
| 39 | + throw new Error(`Analysis support for ${this.language} is not implemented yet.`); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private makeSureJavaIsInstalled(): Promise<void> { |
| 44 | + try { |
| 45 | + const result = spawnSync("java", ["-version"], {encoding: "utf-8", stdio: "pipe"}); |
| 46 | + if (result.error) { |
| 47 | + throw result.error; |
| 48 | + } |
| 49 | + if (result.status !== 0) { |
| 50 | + throw new Error(result.stderr || "Java is not installed. Please install Java 11+ to be able to analyze java projects."); |
| 51 | + } |
| 52 | + } catch (e: any) { |
| 53 | + throw new Error(e.message || String(e)); |
| 54 | + } |
| 55 | + return Promise.resolve(); |
| 56 | + } |
| 57 | +} |
0 commit comments