run method

  1. @override
Future<T?> run(
  1. Iterable<String> args
)
override

Parses args and invokes Command.run on the chosen command.

This always returns a Future in case the command is asynchronous. The Future will throw a UsageException if args was invalid.

Implementation

@override
Future<T?> run(Iterable<String> args) async {
  // a new command gets executes, reset whatever exitCode the previous command has set
  exitCode = 0;

  final unmount = mount(debugName: args.join(' '));

  ArgResults? parsedArgs;
  try {
    parsedArgs = parse(args);
    if (parsedArgs['version'] == true) {
      print('${SidekickContext.cliName} is using sidekick version $version');
      return null;
    }

    final result = await super.runCommand(parsedArgs);
    return result;
  } finally {
    // don't print anything additionally when running the hidden tab completion command (runs in the background when pressing tab),
    // otherwise anything that is printed will also be used as suggestion
    bool isRunningTabCompletionCommand() {
      final reservedCommands = [
        HandleCompletionRequestCommand.commandName,
        InstallCompletionFilesCommand.commandName,
      ];
      return reservedCommands.contains(parsedArgs?.command?.name);
    }

    // don't show the install-global suggesting when running the install-global command
    bool isInstallGlobalCommand() =>
        parsedArgs?.command?.name == 'sidekick' &&
        parsedArgs!.arguments.contains('install-global');

    if (!enableAutoInstall &&
        !isRunningTabCompletionCommand() &&
        !isInstallGlobalCommand()) {
      final command =
          yellow('./${SidekickContext.cliName} sidekick install-global');
      printerr(
        '${cyan('💡Tip: Run')} $command ${cyan('to enable tab completion.')}',
      );
    }

    try {
      if (_isUpdateCheckEnabled &&
          !_isSidekickCliUpdateCommand(parsedArgs) &&
          !isRunningTabCompletionCommand()) {
        // print warning if the user didn't fully update their CLI
        _checkCliVersionIntegrity();
        // print warning if CLI update is available
        // TODO start the update check in the background at command start
        // TODO prevent multiple update checks when a command start another command
        await _checkForUpdates();
      }
    } finally {
      unmount();
    }
  }
}