All files / src/detectors project-detector.js

73.46% Statements 72/98
43.75% Branches 28/64
93.75% Functions 15/16
73.19% Lines 71/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 3343x 3x             1x               1x                       1x 8x   2x 2x     1x                     1x 1x 1x   1x                                                 1x             1x 1x 1x 1x     1x                               1x 1x 1x 1x 1x                                             1x 1x 1x                                           1x             1x 1x 1x                                           1x             1x 1x 1x                               1x 1x 1x 1x   1x 1x   1x 1x                                         1x 1x 1x 1x 1x   1x 1x 1x 1x   1x                               1x             1x 1x 1x 1x 1x   1x 1x   1x                               1x             4x 4x 108x                   1x   1x 2x 1x                 3x  
const fs = require('fs-extra');
const path = require('path');
 
/**
 * Phát hiện loại dự án dựa trên các file cấu hình
 */
class ProjectDetector {
  constructor(projectPath) {
    this.projectPath = projectPath;
  }
 
  /**
   * Phát hiện tất cả loại dự án có thể
   * @returns {Object} Object chứa thông tin các loại dự án được phát hiện
   */
  async detectAll() {
    const results = {
      angular: await this.detectAngular(),
      springBoot: await this.detectSpringBoot(),
      react: await this.detectReact(),
      vue: await this.detectVue(),
      nodejs: await this.detectNodeJS(),
      java: await this.detectJava(),
      python: await this.detectPython(),
      dotnet: await this.detectDotNet()
    };
 
    // Lọc ra các loại dự án được phát hiện
    const detected = Object.entries(results)
      .filter(([_, info]) => info.detected)
      .reduce((acc, [type, info]) => {
        acc[type] = info;
        return acc;
      }, {});
 
    return {
      detected,
      primary: this.determinePrimaryType(detected),
      all: results
    };
  }
 
  /**
   * Phát hiện Angular project
   */
  async detectAngular() {
    try {
      const packageJsonPath = path.join(this.projectPath, 'package.json');
      const angularJsonPath = path.join(this.projectPath, 'angular.json');
      
      Iif (await fs.pathExists(packageJsonPath)) {
        const packageJson = await fs.readJson(packageJsonPath);
        const deps = { ...packageJson.dependencies, ...packageJson.devDependencies };
        
        const hasAngularCore = deps['@angular/core'];
        const hasAngularCli = deps['@angular/cli'];
        const hasAngularJson = await fs.pathExists(angularJsonPath);
        
        if (hasAngularCore || hasAngularCli || hasAngularJson) {
          return {
            detected: true,
            confidence: hasAngularCore && hasAngularJson ? 'high' : 'medium',
            version: hasAngularCore || 'unknown',
            indicators: {
              packageJson: !!hasAngularCore || !!hasAngularCli,
              angularJson: hasAngularJson,
              dependencies: Object.keys(deps).filter(dep => dep.startsWith('@angular/'))
            }
          };
        }
      }
    } catch (error) {
      // Ignore errors
    }
    
    return { detected: false };
  }
 
  /**
   * Phát hiện Spring Boot project
   */
  async detectSpringBoot() {
    try {
      const pomPath = path.join(this.projectPath, 'pom.xml');
      const gradlePath = path.join(this.projectPath, 'build.gradle');
      const gradleKtsPath = path.join(this.projectPath, 'build.gradle.kts');
      
      // Kiểm tra Maven (pom.xml)
      Iif (await fs.pathExists(pomPath)) {
        const pomContent = await fs.readFile(pomPath, 'utf8');
        if (pomContent.includes('spring-boot-starter') || pomContent.includes('org.springframework.boot')) {
          return {
            detected: true,
            confidence: 'high',
            buildTool: 'maven',
            indicators: {
              pomXml: true,
              springBootStarter: pomContent.includes('spring-boot-starter')
            }
          };
        }
      }
      
      // Kiểm tra Gradle
      for (const gradleFile of [gradlePath, gradleKtsPath]) {
        Eif (await fs.pathExists(gradleFile)) {
          const gradleContent = await fs.readFile(gradleFile, 'utf8');
          Eif (gradleContent.includes('spring-boot') || gradleContent.includes('org.springframework.boot')) {
            return {
              detected: true,
              confidence: 'high',
              buildTool: gradleFile.endsWith('.kts') ? 'gradle-kotlin' : 'gradle',
              indicators: {
                buildGradle: true,
                springBootPlugin: gradleContent.includes('org.springframework.boot')
              }
            };
          }
        }
      }
    } catch (error) {
      // Ignore errors
    }
    
    return { detected: false };
  }
 
  /**
   * Phát hiện React project
   */
  async detectReact() {
    try {
      const packageJsonPath = path.join(this.projectPath, 'package.json');
      Iif (await fs.pathExists(packageJsonPath)) {
        const packageJson = await fs.readJson(packageJsonPath);
        const deps = { ...packageJson.dependencies, ...packageJson.devDependencies };
        
        if (deps.react) {
          return {
            detected: true,
            confidence: 'high',
            version: deps.react,
            indicators: {
              react: true,
              reactDom: !!deps['react-dom'],
              nextJs: !!deps.next,
              createReactApp: !!deps['react-scripts']
            }
          };
        }
      }
    } catch (error) {
      // Ignore errors
    }
    
    return { detected: false };
  }
 
  /**
   * Phát hiện Vue project
   */
  async detectVue() {
    try {
      const packageJsonPath = path.join(this.projectPath, 'package.json');
      Iif (await fs.pathExists(packageJsonPath)) {
        const packageJson = await fs.readJson(packageJsonPath);
        const deps = { ...packageJson.dependencies, ...packageJson.devDependencies };
        
        if (deps.vue) {
          return {
            detected: true,
            confidence: 'high',
            version: deps.vue,
            indicators: {
              vue: true,
              vueRouter: !!deps['vue-router'],
              vuex: !!deps.vuex,
              nuxt: !!deps.nuxt
            }
          };
        }
      }
    } catch (error) {
      // Ignore errors
    }
    
    return { detected: false };
  }
 
  /**
   * Phát hiện Node.js project
   */
  async detectNodeJS() {
    try {
      const packageJsonPath = path.join(this.projectPath, 'package.json');
      return {
        detected: await fs.pathExists(packageJsonPath),
        confidence: 'high',
        indicators: {
          packageJson: await fs.pathExists(packageJsonPath)
        }
      };
    } catch (error) {
      return { detected: false };
    }
  }
 
  /**
   * Phát hiện Java project
   */
  async detectJava() {
    try {
      const pomPath = path.join(this.projectPath, 'pom.xml');
      const gradlePath = path.join(this.projectPath, 'build.gradle');
      const gradleKtsPath = path.join(this.projectPath, 'build.gradle.kts');
      
      const hasPom = await fs.pathExists(pomPath);
      const hasGradle = await fs.pathExists(gradlePath) || await fs.pathExists(gradleKtsPath);
      
      Eif (hasPom || hasGradle) {
        return {
          detected: true,
          confidence: 'high',
          buildTool: hasPom ? 'maven' : 'gradle',
          indicators: {
            maven: hasPom,
            gradle: hasGradle
          }
        };
      }
    } catch (error) {
      // Ignore errors
    }
    
    return { detected: false };
  }
 
  /**
   * Phát hiện Python project
   */
  async detectPython() {
    try {
      const requirementsPath = path.join(this.projectPath, 'requirements.txt');
      const pipfilePath = path.join(this.projectPath, 'Pipfile');
      const pyprojectPath = path.join(this.projectPath, 'pyproject.toml');
      const setupPyPath = path.join(this.projectPath, 'setup.py');
      
      const hasRequirements = await fs.pathExists(requirementsPath);
      const hasPipfile = await fs.pathExists(pipfilePath);
      const hasPyproject = await fs.pathExists(pyprojectPath);
      const hasSetupPy = await fs.pathExists(setupPyPath);
      
      Iif (hasRequirements || hasPipfile || hasPyproject || hasSetupPy) {
        return {
          detected: true,
          confidence: 'high',
          indicators: {
            requirements: hasRequirements,
            pipfile: hasPipfile,
            pyproject: hasPyproject,
            setupPy: hasSetupPy
          }
        };
      }
    } catch (error) {
      // Ignore errors
    }
    
    return { detected: false };
  }
 
  /**
   * Phát hiện .NET project
   */
  async detectDotNet() {
    try {
      const csprojFiles = await this.findFilesByExtension('.csproj');
      const vbprojFiles = await this.findFilesByExtension('.vbproj');
      const fsprojFiles = await this.findFilesByExtension('.fsproj');
      const slnFiles = await this.findFilesByExtension('.sln');
      
      const hasProjectFiles = csprojFiles.length > 0 || vbprojFiles.length > 0 || fsprojFiles.length > 0;
      const hasSolution = slnFiles.length > 0;
      
      Iif (hasProjectFiles || hasSolution) {
        return {
          detected: true,
          confidence: 'high',
          indicators: {
            csproj: csprojFiles.length,
            vbproj: vbprojFiles.length,
            fsproj: fsprojFiles.length,
            solution: slnFiles.length
          }
        };
      }
    } catch (error) {
      // Ignore errors
    }
    
    return { detected: false };
  }
 
  /**
   * Tìm files theo extension
   */
  async findFilesByExtension(extension) {
    try {
      const files = await fs.readdir(this.projectPath);
      return files.filter(file => file.endsWith(extension));
    } catch (error) {
      return [];
    }
  }
 
  /**
   * Xác định loại dự án chính
   */
  determinePrimaryType(detected) {
    const priorities = ['angular', 'springBoot', 'react', 'vue', 'nodejs', 'java', 'python', 'dotnet'];
    
    for (const type of priorities) {
      if (detected[type] && detected[type].confidence === 'high') {
        return type;
      }
    }
    
    // Fallback to first detected
    return Object.keys(detected)[0] || 'unknown';
  }
}
 
module.exports = ProjectDetector;