57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import { existsSync } from 'node:fs';
|
|
import { homedir } from 'node:os';
|
|
import path from 'node:path';
|
|
import { prependPathEntry } from './env-path';
|
|
|
|
type EnvMap = Record<string, string | undefined>;
|
|
|
|
function dotnetExecutableName(): string {
|
|
return process.platform === 'win32' ? 'dotnet.exe' : 'dotnet';
|
|
}
|
|
|
|
export function getDotnetPathCandidates(): string[] {
|
|
if (process.platform === 'win32') {
|
|
return [
|
|
path.join(homedir(), '.dotnet'),
|
|
path.join(process.env.ProgramFiles || 'C:\\Program Files', 'dotnet'),
|
|
path.join(process.env['ProgramFiles(x86)'] || 'C:\\Program Files (x86)', 'dotnet'),
|
|
];
|
|
}
|
|
|
|
return [
|
|
path.join(homedir(), '.dotnet'),
|
|
'/usr/local/share/dotnet',
|
|
'/opt/homebrew/bin',
|
|
'/usr/local/bin',
|
|
];
|
|
}
|
|
|
|
export function resolveDotnetExecutable(): string | null {
|
|
const executable = dotnetExecutableName();
|
|
for (const dir of getDotnetPathCandidates()) {
|
|
const candidate = path.join(dir, executable);
|
|
if (existsSync(candidate)) return candidate;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function buildDotnetEnv(baseEnv: EnvMap): EnvMap {
|
|
let env: EnvMap = { ...baseEnv };
|
|
const existingDirs = getDotnetPathCandidates().filter((entry) => existsSync(entry));
|
|
for (const dir of existingDirs.slice().reverse()) {
|
|
env = prependPathEntry(env, dir).env;
|
|
}
|
|
|
|
const userDotnet = path.join(homedir(), '.dotnet');
|
|
if (existsSync(path.join(userDotnet, dotnetExecutableName()))) {
|
|
env.DOTNET_ROOT = userDotnet;
|
|
} else {
|
|
const systemRoot = process.platform === 'darwin' ? '/usr/local/share/dotnet' : null;
|
|
if (systemRoot && existsSync(path.join(systemRoot, dotnetExecutableName()))) {
|
|
env.DOTNET_ROOT = systemRoot;
|
|
}
|
|
}
|
|
|
|
return env;
|
|
}
|