execute method

  1. @override
Future<int> execute(
  1. List<String> arguments
)
override

Implementation

@override
Future<int> execute(List<String> arguments) async {
  final options = _SeederOptions.parse(arguments);
  if (options == null) return ExitCode.usage;

  final relativeSeederPath = _seederPath(options.name);
  final seederFile = File('${workingDirectory.path}/$relativeSeederPath');
  if (seederFile.existsSync() && !options.force) {
    Console.error('Seeder already exists at $relativeSeederPath.');
    return ExitCode.failure;
  }

  final className = options.name.split('/').last.pascalCase;
  var factoryImport = '';
  var body = '    // Add the records this seeder owns.';
  final factoryName = options.factory;
  if (factoryName != null) {
    final relativeFactoryPath =
        'lib/database/factory/${factoryName.snakeCase}.dart';
    final factoryFile = File('${workingDirectory.path}/$relativeFactoryPath');
    if (!factoryFile.existsSync()) {
      await factoryFile.create(recursive: true);
      await factoryFile.writeAsString(
        Stubs.factory.replaceAll('FactoryName', factoryName.pascalCase),
      );
      Console.success('Factory created at $relativeFactoryPath');
    }

    final importPath = path
        .relative(factoryFile.path, from: seederFile.parent.path)
        .replaceAll('\\', '/');
    factoryImport = "\nimport '$importPath';\n";
    body =
        '    final records = ${factoryName.pascalCase}().createMany(500);\n'
        '    // Persist records using the model for this seeder.\n'
        '    print(records.length);';
  }

  final source = Stubs.seeder
      .replaceAll('FactoryImport', factoryImport)
      .replaceAll('SeederName', className)
      .replaceAll('SeederBody', body);
  await seederFile.create(recursive: true);
  await seederFile.writeAsString(source);
  await _register(relativeSeederPath, className);
  Console.success('Seeder created at $relativeSeederPath');
  return ExitCode.success;
}