missingSubCommands function

Future<(List<String>, String?)> missingSubCommands({
  1. required Directory directory,
  2. required Command command,
  3. List<String> additionalSubCommands = const [],
})

Returns a list of missing sub commands in directory

Implementation

Future<(List<String> commandList, String? errorMessage)> missingSubCommands({
  required Directory directory,
  required Command<dynamic> command,
  List<String> additionalSubCommands = const [],
}) async {
  // Iterate all files in lib/src/commands
  // and check if they are added to the command runner
  // and if they are added to the help message
  final subCommands =
      directory
          .listSync(recursive: false)
          .where((file) => file.path.endsWith('.dart'))
          .map(
            (e) =>
                basename(e.path)
                    .replaceAll('.dart', '')
                    .replaceAll('_', '-')
                    .replaceAll('gg-', ''),
          )
          .toList()
        ..addAll(additionalSubCommands);

  final runner = CommandRunner<void>('runner', '');
  runner.addCommand(command);

  final messages = <String>[];

  await capturePrint(
    ggLog: messages.add,
    code: () => runner.run([command.name, '--help']),
  );

  final commandList = subCommands
      .where((subCommand) => !hasLog(messages, subCommand))
      .toList();

  final errorMessage = commandList.isNotEmpty
      ? 'The following sub commands needed to be added to '
            'class ${command.name.pascalCase}:\n'
            '- ${commandList.join(', ')}'
      : null;

  return (commandList, errorMessage);
}