getCommands method

Iterable<GetCommandsResult> getCommands(
  1. List<String> keys, {
  2. required bool listOut,
})

Implementation

Iterable<GetCommandsResult> getCommands(
  List<String> keys, {
  required bool listOut,
}) sync* {
  final flagStartAt = keys.indexWhere((e) => e.startsWith('-'));
  final scriptKeys = keys.sublist(0, flagStartAt == -1 ? null : flagStartAt);

  if (scriptKeys.isEmpty) {
    logger.err('No script specified');
    yield GetCommandsResult.exit(ExitCode.config);
    return;
  }

  final content = scriptsYaml.scripts();
  if (content == null) {
    logger.err('No ${ScriptsYaml.fileName} file found');
    yield GetCommandsResult.exit(ExitCode.noInput);
    return;
  }

  final scriptConfig = ScriptsConfig.fromJson(content);

  final script = scriptConfig.find(scriptKeys);

  if (script == null) {
    logger.err('No script found for ${scriptKeys.join(' ')}');
    yield GetCommandsResult.exit(ExitCode.config);
    return;
  }

  if (listOut) {
    _listOutScript(script);

    yield GetCommandsResult.exit(ExitCode.success);
    return;
  }

  if (script.commands.isEmpty) {
    logger
      ..warn('There are no commands to run for "${scriptKeys.join(' ')}"')
      ..warn('Here are the available scripts:');

    _listOutScript(script);

    yield GetCommandsResult.exit(ExitCode.config);
    return;
  }

  final resolvedScripts = variables.replace(
    script,
    scriptConfig,
    flags: optionalFlags(keys),
  );

  for (final resolved in resolvedScripts) {
    yield GetCommandsResult(
      resolveScript: resolved,
      script: script,
    );
  }
}