installModule function

Future<void> installModule(
  1. String projectPath,
  2. String module, {
  3. bool force = false,
})

Implementation

Future<void> installModule(String projectPath, String module,
    {bool force = false}) async {
  final flutterCmd = Platform.isWindows ? 'flutter.bat' : 'flutter';

  // ==========================
  // NETWORK MODULE
  // ==========================

  if (module == 'network') {
    await installNetworkModule(projectPath);

    print('🚀 Module "network" added successfully.');
    return; // ⭐ IMPORTANT
  }

  // ==========================
  // WIDGETS MODULE (All or Specific)
  // ==========================

  if (module == 'common_widgets' || module == 'widgets') {
    await installWidgetsModule(projectPath, force: force);
    print('🚀 Module "common_widgets" added successfully.');
    return;
  }

  if (module == 'common_utils' || module == 'utils') {
    await installUtilsModule(projectPath, force: force);
    print('🚀 Module "common_utils" added successfully.');
    return;
  }

  // Try to install as a specific widget
  bool installedWidget =
      await installSpecificWidget(projectPath, module, force: force);
  if (installedWidget) return;

  final modules = {
    'ui': 'project_setup',
  };

  final package = modules[module];

  if (package == null) {
    print('❌ Unknown module: $module');
    print('');
    print('Available modules:');
    modules.keys.forEach((m) => print('  $m'));
    return;
  }

  print('📦 Installing $package...');

  await Process.run(
    flutterCmd,
    ['pub', 'add', package],
    workingDirectory: projectPath,
    runInShell: true,
  );

  print('✅ $package installed successfully.');

  // ======================================
  // Generate UI starter example
  // ======================================

  if (module == 'ui') {
    final widgetDir = Directory(
      p.join(projectPath, 'lib', 'presentation', 'widgets'),
    );

    widgetDir.createSync(recursive: true);

    final exampleFile = File(
      p.join(widgetDir.path, 'example_button.dart'),
    );

    if (!exampleFile.existsSync()) {
      safeWriteFile(
          exampleFile.path,
          '''
import 'package:flutter/material.dart';
import 'package:project_setup/project_setup.dart';

class ExampleButton extends StatelessWidget {
  const ExampleButton({super.key});

  @override
  Widget build(BuildContext context) {
    return CustomButton(
      text: "Click Me",
      onTap: () {},
    );
  }
}
''',
          projectPath: projectPath);

      print('🎨 Example UI widget generated.');
    }
  }

  print('🚀 Module "$module" added successfully.');
}