runAgentSdkCommand function

Future<int> runAgentSdkCommand(
  1. String executable,
  2. List<String> arguments, {
  3. required String workingDirectory,
  4. required Map<String, String> environment,
  5. required void onOutput(
    1. String line
    ),
})

The production AgentSdkCommandRunner: Process.start, no shell.

No shell, so no argument is ever a shell word — except for a Windows npm.cmd fallback, which CreateProcess cannot start without one. That fallback is reached only when the npm that ships with Node could not be found beside it, and its arguments are constants.

Implementation

Future<int> runAgentSdkCommand(
  String executable,
  List<String> arguments, {
  required String workingDirectory,
  required Map<String, String> environment,
  required void Function(String line) onOutput,
}) async {
  final process = await Process.start(
    executable,
    arguments,
    workingDirectory: workingDirectory,
    environment: environment,
    includeParentEnvironment: true,
    runInShell: Platform.isWindows && executable.toLowerCase().endsWith('.cmd'),
  );
  // stdin is never written; closing it keeps npm from waiting on a prompt.
  unawaited(process.stdin.close());
  Future<void> drain(Stream<List<int>> stream) => stream
      .transform(const Utf8Decoder(allowMalformed: true))
      .transform(const LineSplitter())
      .forEach(onOutput);
  await Future.wait([drain(process.stdout), drain(process.stderr)]);
  return process.exitCode;
}