find method

String? find(
  1. String name, {
  2. List<String> extraDirectories = const [],
})

The path of the program name, or null when it isn't installed.

Looks on the PATH, then in extraDirectories.

Implementation

String? find(String name, {List<String> extraDirectories = const []}) {
  final bool windows = Platform.isWindows;
  final String separator = windows ? ';' : ':';
  final List<String> directories = [
    ...(environment['PATH'] ?? '').split(separator),
    ...extraDirectories,
  ];
  for (final String directory in directories) {
    if (directory.isEmpty) continue;
    for (final String candidate in [
      '$directory${Platform.pathSeparator}$name',
      if (windows) '$directory${Platform.pathSeparator}$name.exe',
    ]) {
      if (File(candidate).existsSync()) return candidate;
    }
  }
  return null;
}