preflightAdd function

PreflightResult preflightAdd(
  1. String cwd,
  2. String componentName
)

Implementation

PreflightResult preflightAdd(String cwd, String componentName) {
  final validation = validateFlutterProject(cwd);

  if (!validation.isValid) {
    return PreflightResult(
      success: false,
      error: '✗ Flutter project validation failed',
      details: ['  • ${validation.error}'],
    );
  }

  if (!isAlreadyInitialized(cwd)) {
    return PreflightResult(
      success: false,
      error: '✗ fluttercn is not initialized',
      details: [
        '  Please run fluttercn init first to set up fluttercn.',
      ],
    );
  }

  final component = getComponent(componentName);

  if (component == null) {
    return PreflightResult(
      success: false,
      error: "✗ Component '$componentName' not found",
      details: [
        '  Run fluttercn list to see available components.',
      ],
    );
  }

  if (component.dependencies != null && component.dependencies!.isNotEmpty) {
    final missingDeps = <String>[];

    for (final dep in component.dependencies!) {
      final depPath = path.join(cwd, 'lib', dep.file);
      if (!File(depPath).existsSync()) {
        missingDeps.add(dep.message ?? dep.file);
      }
    }

    if (missingDeps.isNotEmpty) {
      return PreflightResult(
        success: false,
        error: '✗ Missing required dependencies',
        details: missingDeps.map((dep) => '  • $dep').toList(),
      );
    }
  }

  return PreflightResult(
    success: true,
    component: component,
  );
}