detectProjectInfo function

Future<ProjectInfo> detectProjectInfo(
  1. String projectDir
)

Implementation

Future<ProjectInfo> detectProjectInfo(String projectDir) async {
  final dir = Directory(projectDir);
  if (!dir.existsSync()) {
    return ProjectInfo(projectDir: projectDir);
  }

  // Git detection
  String? gitRoot;
  String? gitBranch;
  String? gitRemoteUrl;
  bool isGitRepo = false;

  try {
    final revParse = await Process.run('git', [
      'rev-parse',
      '--show-toplevel',
    ], workingDirectory: projectDir);
    if (revParse.exitCode == 0) {
      gitRoot = revParse.stdout.toString().trim();
      isGitRepo = true;
    }
  } catch (_) {}

  if (isGitRepo) {
    try {
      final branch = await Process.run('git', [
        'branch',
        '--show-current',
      ], workingDirectory: projectDir);
      if (branch.exitCode == 0) {
        gitBranch = branch.stdout.toString().trim();
      }
    } catch (_) {}

    try {
      final remote = await Process.run('git', [
        'config',
        '--get',
        'remote.origin.url',
      ], workingDirectory: projectDir);
      if (remote.exitCode == 0) {
        gitRemoteUrl = remote.stdout.toString().trim();
      }
    } catch (_) {}
  }

  // File detection
  final hasPackageJson = File('$projectDir/package.json').existsSync();
  final hasPubspecYaml = File('$projectDir/pubspec.yaml').existsSync();
  final hasCargoToml = File('$projectDir/Cargo.toml').existsSync();
  final hasGoMod = File('$projectDir/go.mod').existsSync();
  final hasPyprojectToml = File('$projectDir/pyproject.toml').existsSync();
  final hasRequirementsTxt = File('$projectDir/requirements.txt').existsSync();

  // Language detection
  final languages = <String>[];
  if (hasPubspecYaml) languages.add('dart');
  if (hasPackageJson) languages.add('javascript');
  if (hasCargoToml) languages.add('rust');
  if (hasGoMod) languages.add('go');
  if (hasPyprojectToml || hasRequirementsTxt) languages.add('python');

  // Check for TypeScript
  if (hasPackageJson) {
    if (File('$projectDir/tsconfig.json').existsSync()) {
      languages.add('typescript');
    }
  }

  // Framework detection
  String? framework;
  if (hasPubspecYaml) {
    try {
      final pubspec = File('$projectDir/pubspec.yaml').readAsStringSync();
      if (pubspec.contains('flutter:')) {
        framework = 'flutter';
      }
    } catch (_) {}
  }
  if (hasPackageJson) {
    try {
      final pkg =
          jsonDecode(File('$projectDir/package.json').readAsStringSync())
              as Map<String, dynamic>;
      final deps = <String>[
        ...((pkg['dependencies'] as Map<String, dynamic>?) ?? {}).keys,
        ...((pkg['devDependencies'] as Map<String, dynamic>?) ?? {}).keys,
      ];
      if (deps.contains('next')) {
        framework = 'next.js';
      } else if (deps.contains('react')) {
        framework = 'react';
      } else if (deps.contains('vue')) {
        framework = 'vue';
      } else if (deps.contains('@angular/core')) {
        framework = 'angular';
      } else if (deps.contains('svelte')) {
        framework = 'svelte';
      } else if (deps.contains('express')) {
        framework = 'express';
      }
    } catch (_) {}
  }

  // Package manager detection
  String? packageManager;
  if (File('$projectDir/pnpm-lock.yaml').existsSync()) {
    packageManager = 'pnpm';
  } else if (File('$projectDir/yarn.lock').existsSync()) {
    packageManager = 'yarn';
  } else if (File('$projectDir/bun.lockb').existsSync()) {
    packageManager = 'bun';
  } else if (File('$projectDir/package-lock.json').existsSync()) {
    packageManager = 'npm';
  } else if (hasPubspecYaml) {
    packageManager = 'pub';
  } else if (hasCargoToml) {
    packageManager = 'cargo';
  } else if (hasGoMod) {
    packageManager = 'go';
  } else if (File('$projectDir/Pipfile').existsSync()) {
    packageManager = 'pipenv';
  } else if (File('$projectDir/poetry.lock').existsSync()) {
    packageManager = 'poetry';
  } else if (hasRequirementsTxt) {
    packageManager = 'pip';
  }

  return ProjectInfo(
    gitRoot: gitRoot,
    gitBranch: gitBranch,
    gitRemoteUrl: gitRemoteUrl,
    projectDir: projectDir,
    languages: languages,
    framework: framework,
    packageManager: packageManager,
    isGitRepo: isGitRepo,
    hasPackageJson: hasPackageJson,
    hasPubspecYaml: hasPubspecYaml,
    hasCargoToml: hasCargoToml,
    hasGoMod: hasGoMod,
    hasPyprojectToml: hasPyprojectToml,
    hasRequirementsTxt: hasRequirementsTxt,
  );
}