runStreaming method

Future<int> runStreaming(
  1. String scriptName
)

Run a script with live output streaming

Implementation

Future<int> runStreaming(String scriptName) async {
  final pubspec = findPubspec();
  if (pubspec == null) {
    error('No pubspec.yaml found in current directory or parents');
    return 1;
  }

  final scripts = getScripts(pubspec.path);
  if (scripts.isEmpty) {
    error('No scripts defined in pubspec.yaml');
    return 1;
  }

  final matchedName = findScript(scriptName, scripts);
  if (matchedName == null) {
    final matches = findMatchingScripts(scriptName, scripts);
    if (matches.isEmpty) {
      error('Script "$scriptName" not found');
      info('Available scripts: ${scripts.keys.join(', ')}');
    } else {
      error('Ambiguous script name "$scriptName"');
      info('Did you mean: ${matches.join(', ')}');
    }
    return 1;
  }

  final command = scripts[matchedName]!;
  final workingDir = pubspec.parent.path;

  info('Running: $matchedName');
  verbose('Command: $command');
  print('');

  final process = await Process.start(
    Platform.isWindows ? 'cmd' : 'sh',
    Platform.isWindows ? ['/c', command] : ['-c', command],
    workingDirectory: workingDir,
    runInShell: true,
  );

  // Stream output in real-time
  process.stdout.listen((data) => stdout.add(data));
  process.stderr.listen((data) => stderr.add(data));

  final exitCode = await process.exitCode;

  print('');
  if (exitCode == 0) {
    success('Script "$matchedName" completed successfully');
  } else {
    error('Script "$matchedName" failed with exit code $exitCode');
  }

  return exitCode;
}