getInjectedEnvironment function

Map<String, String> getInjectedEnvironment()

Generates an environment map with professional tool paths injected.

Automatically detects and adds paths for:

  • Node.js & NPM
  • Dart & Pub global binaries
  • Homebrew (on macOS)

This ensures that newly installed tools can be used immediately without requiring a terminal restart.

Implementation

Map<String, String> getInjectedEnvironment() {
  final Map<String, String> env = Map.from(Platform.environment);
  final separator = Platform.isWindows ? ';' : ':';
  String currentPath = env['PATH'] ?? '';

  final pathsToInject = Platform.isWindows
      ? [
          'C:\\Program Files\\nodejs',
          '${env['APPDATA']}\\npm',
          '${env['LOCALAPPDATA']}\\Pub\\Cache\\bin',
        ]
      : [
          '/usr/local/bin',
          '/opt/homebrew/bin',
          '${env['HOME']}/.pub-cache/bin',
          '${env['HOME']}/.npm-global/bin',
        ];

  for (final path in pathsToInject) {
    if (Directory(path).existsSync() && !currentPath.contains(path)) {
      currentPath = '$path$separator$currentPath';
    }
  }
  env['PATH'] = currentPath;

  return env;
}