createModelsPackage method

Future<bool> createModelsPackage()

Create a models package

Implementation

Future<bool> createModelsPackage() async {
  if (!config.createModels) {
    return false;
  }

  final String projectPath = p.join(config.outputDir, config.modelsPackageName);

  info('Creating models package: ${config.modelsPackageName}');

  // Use flutter create -t package for models
  final List<String> args = <String>[
    'create',
    '-t',
    'package',
    '--project-name',
    config.modelsPackageName,
    projectPath,
  ];

  final result = await _runner.runWithRetry(
    'flutter',
    args,
    operationName: 'Create models package',
  );

  if (result == null || !result.success) {
    error('Failed to create models package');
    return false;
  }

  // Delete the generated lib and example folders to replace with template
  final libDir = Directory(p.join(projectPath, 'lib'));
  if (libDir.existsSync()) {
    await libDir.delete(recursive: true);
  }

  final exampleDir = Directory(p.join(projectPath, 'example'));
  if (exampleDir.existsSync()) {
    await exampleDir.delete(recursive: true);
  }

  success('Models package created at: $projectPath');
  return true;
}