runCmd static method

Future<Process> runCmd({
  1. String? cmd,
  2. ProcessRunner? runner,
  3. String? workingDirectory,
  4. bool runInShell = true,
  5. ProcessStartMode mode = ProcessStartMode.inheritStdio,
  6. bool waitForExit = true,
  7. Map<String, String>? environment,
})

执行指令

cmd 需要执行的指令 workingDirectory 工作目录

Implementation

static Future<Process> runCmd({
  String? cmd,
  ProcessRunner? runner,
  String? workingDirectory,
  bool runInShell = true,
  ProcessStartMode mode = ProcessStartMode.inheritStdio,
  bool waitForExit = true,
  Map<String, String>? environment,
}) async {
  try {
    final mergedRunner = _parseRunner(cmd: cmd, runner: runner);

    Logger.info(
      '[Run CMD](${workingDirectory ?? PathUtil.cwd.path}) >>',
      mergedRunner.toStringCmd(),
    );
    final process = await Process.start(
      mergedRunner.executable,
      mergedRunner.startArguments,
      runInShell: runInShell,
      workingDirectory: workingDirectory,
      mode: mode,
      environment: environment,
    );

    if (waitForExit) {
      final exitCode = await process.exitCode;
      if (exitCode != ExitCode.ok) {
        throw Exception('Failed to run cmd: $cmd');
      }
    }
    return process;
  } catch (err, stack) {
    await SentryUtil.reportError(err, stack);
    Logger.log('[runCmd]', err, stack);
    rethrow;
  }
}