run method

void run(
  1. List<String> arguments, {
  2. String? workingDirectory,
})

Implementation

void run(List<String> arguments, {String? workingDirectory}) async {
  final parser = ArgParser()
    ..addOption('package',
        abbr: 'p', help: 'Package name', defaultsTo: 'twafok')
    ..addFlag('help', abbr: 'h', help: 'Show help', negatable: false);

  try {
    final results = parser.parse(arguments);

    if (results['help'] == true) {
      _printHelp();
      return;
    }

    final args = results.rest;
    final workDir = workingDirectory ?? Directory.current.path;
    final packageName = results['package'] as String;

    if (args.isEmpty) {
      print('❌ Feature name is required');
      print('👉 Usage: twafok create_feature <feature_name>');
      exit(1);
    }

    final featureName = args[0];
    final basePath = 'lib/features';
    final featurePath = path.join(workDir, basePath, featureName);

    // Check if feature already exists
    if (await Directory(featurePath).exists()) {
      print('⚠️ Feature already exists: $featurePath');
      exit(1);
    }

    print('');
    print('🚀 Creating feature: $featureName');
    print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');

    // Create feature structure
    await _createFeatureStructure(featurePath);

    // Generate all files
    await _generateEntityModel(featurePath, featureName, packageName);
    await _createUseCase(featurePath, featureName);
    await _createBaseRepository(featurePath, featureName);
    await _createRepository(featurePath, featureName);
    await _createRemoteDataSource(featurePath, featureName);
    await _generateCubit(featurePath, featureName, packageName);
    await _createScreen(featurePath, featureName, packageName);

    // Generate DI and barrel files
    print('');
    print('🔄 Generating DI and barrel files...');
    final diCommand = GenerateDiCommand();
    diCommand.run([featurePath], workingDirectory: workDir);

    final pathsCommand = GeneratePathsCommand();
    pathsCommand.run([featurePath], workingDirectory: workDir);

    print('');

    // Format the project
    await _formatProject(workDir);
    print('');

    // Run dart fix
    await _runDartFix(workDir);

    print('');
    print('🎉 Feature \'$featureName\' created successfully!');
    print('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
    print('');
    print('📁 Feature location: $featurePath');
    print('');
    print('📝 Next steps:');
    print('   1. Add your business logic to the cubit');
    print('   2. Update the API endpoints in remote_data_source');
    print(
        '   3. Add more use cases using: twafok add_usecase $featurePath <action>');
  } catch (e) {
    print('❌ Error: $e');
    exit(1);
  }
}