src / platform.ts
import * as os from "os";
import * as path from "path";
import * as fs from "fs";
import { expandPath } from "./securityEnhanced";
import { Platform } from "./types";
// ── Platform Detection ──
export function detectPlatform(): Platform {
const p = os.platform();
return p === "win32" ? "windows" : p === "darwin" ? "macos" : "linux";
}
export function resolvePwshPath(): string {
const platform = detectPlatform();
if (platform === "windows") {
const candidates = [
"C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"C:\\Program Files (x86)\\PowerShell\\7\\pwsh.exe",
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
];
for (const c of candidates) {
try {
const resolved = path.resolve(expandPath(c));
if (fs.existsSync(resolved)) return resolved;
} catch { /* skip */ }
}
return "powershell";
}
const candidates = ["/usr/bin/pwsh", "/usr/bin/powershell", "/opt/microsoft/powershell/7/pwsh"];
for (const c of candidates) {
try {
const resolved = path.resolve(expandPath(c));
fs.accessSync(resolved, fs.constants.X_OK);
return resolved;
} catch { /* skip */ }
}
return "pwsh";
}
src / platform.ts
import * as os from "os";
import * as path from "path";
import * as fs from "fs";
import { expandPath } from "./securityEnhanced";
import { Platform } from "./types";
// ── Platform Detection ──
export function detectPlatform(): Platform {
const p = os.platform();
return p === "win32" ? "windows" : p === "darwin" ? "macos" : "linux";
}
export function resolvePwshPath(): string {
const platform = detectPlatform();
if (platform === "windows") {
const candidates = [
"C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"C:\\Program Files (x86)\\PowerShell\\7\\pwsh.exe",
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
];
for (const c of candidates) {
try {
const resolved = path.resolve(expandPath(c));
if (fs.existsSync(resolved)) return resolved;
} catch { /* skip */ }
}
return "powershell";
}
const candidates = ["/usr/bin/pwsh", "/usr/bin/powershell", "/opt/microsoft/powershell/7/pwsh"];
for (const c of candidates) {
try {
const resolved = path.resolve(expandPath(c));
fs.accessSync(resolved, fs.constants.X_OK);
return resolved;
} catch { /* skip */ }
}
return "pwsh";
}