run method

  1. @override
void run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
void run() {
  final prefix = argResults?['prefix'] ?? '';
  final suffix = argResults?['suffix'] ?? '';
  final globPattern = argResults?['glob-pattern'] ?? '*';
  final workingDirectory =
      argResults?.rest.isNotEmpty ?? false ? argResults!.rest.first : '.';

  final items =
      find(globPattern, workingDirectory: workingDirectory).toList();

  for (var item in items) {
    final path = item.split(separator);
    final filenameWithExtension = path.removeLast().split('.');

    String filename = filenameWithExtension.first;
    String extesion =
        filenameWithExtension.length > 1 ? filenameWithExtension.last : '';

    if (!filename.startsWith(prefix)) {
      filename = '${prefix}_$filename';
    }

    if (!filename.endsWith(suffix)) {
      filename = '${filename}_$suffix';
    }

    filename = filename.snakeCase;

    path.add('$filename.$extesion');

    final finalPath = path.join(separator);
    move(item, finalPath);

    StatusHelper.generated('rename from $item to $finalPath');
  }

  StatusHelper.success('morpheme rename');
}