openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 5.39 kB
JavaScript
import{n as normalizeLowercaseStringOrEmpty}from"./string-coerce-BBqv4GXj.mjs";import{n as resolveDiagnosticProcessEnv}from"./process-env-DPUWNvkW.mjs";import{t as escapeRegExp}from"./regexp-DvzVyjZw.mjs";import{n as canAccessSync}from"./path-existence-4c3xWldM.mjs";import{t as assertAppContainerSpawnSupported}from"./windows-appcontainer-spawn-BQRF-Xno.mjs";import{execFileSync}from"node:child_process";import fsSync from"node:fs";import path from"node:path";
//!src/infra/windows-install-roots.ts
const DEFAULT_WINDOWS_SYSTEM_ROOT=`C:\\Windows`;const DEFAULT_PROGRAM_FILES=`C:\\Program Files`;const DEFAULT_PROGRAM_FILES_X86=`C:\\Program Files (x86)`;const WINDOWS_NT_CURRENT_VERSION_KEY=`HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion`;const WINDOWS_CURRENT_VERSION_KEY=`HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion`;const REG_QUERY_TIMEOUT_MS=5e3;let cachedProcessRoots=null;function locateWindowsRegExe(){const e=path.win32.join(DEFAULT_WINDOWS_SYSTEM_ROOT,`System32`,`reg.exe`);return canAccessSync(e,fsSync.constants.R_OK)?e:null}function trimTrailingSeparators(e){const t=path.win32.parse(e);let n=e;while(n.length>t.root.length&&/[\\/]/.test(n.at(-1)??``))n=n.slice(0,-1);return n}function normalizeWindowsInstallRoot(e){if(typeof e!==`string`)return null;const t=e.trim();if(!t||t.includes(`\0`)||t.includes(`\r`)||t.includes(`
`)||t.includes(`;`))return null;const n=trimTrailingSeparators(path.win32.normalize(t));if(!path.win32.isAbsolute(n)||n.startsWith(`\\\\`))return null;const r=path.win32.parse(n);if(!/^[A-Za-z]:\\$/.test(r.root))return null;if(n.length<=r.root.length)return null;return n}function getEnvValueCaseInsensitive(e,t){const n=e[t];if(n!==void 0)return n;const r=t.toUpperCase();const i=Object.keys(e).find(e=>e.toUpperCase()===r);return i?e[i]:void 0}function queryWindowsRegistryValue(e,t,n){const r=locateWindowsRegExe();if(!r)return null;for(const i of[[`/reg:64`],[]]){const a=n===void 0?REG_QUERY_TIMEOUT_MS:Math.min(REG_QUERY_TIMEOUT_MS,Math.ceil(n-performance.now()));if(a<=0)return null;assertAppContainerSpawnSupported();try{const n=execFileSync(r,[`query`,e,`/v`,t,...i],{env:resolveDiagnosticProcessEnv(),encoding:`utf8`,stdio:[`ignore`,`pipe`,`ignore`],timeout:a,windowsHide:true});const o=new RegExp(`^\\s*${escapeRegExp(t)}\\s+REG_[A-Z0-9_]+\\s+(.+)$`,`im`);const s=n.match(o)?.[1]?.trim();if(s)return s}catch{}}return null}function getRegistryProgramFilesRoots(){const readRoot=e=>normalizeWindowsInstallRoot(queryWindowsRegistryValue(WINDOWS_CURRENT_VERSION_KEY,e)??void 0)??void 0;return{programFiles:readRoot(`ProgramFilesDir`),programFilesX86:readRoot(`ProgramFilesDir (x86)`),programW6432:readRoot(`ProgramW6432Dir`)}}function buildWindowsInstallRoots(e,t={}){const n=normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e,`ProgramW6432`));const r=t.programW6432??n??null;return{systemRoot:t.systemRoot??resolveSystemRootFromEnv(e),programFiles:t.programFiles??normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e,`ProgramFiles`))??r??DEFAULT_PROGRAM_FILES,programFilesX86:t.programFilesX86??normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e,`ProgramFiles(x86)`))??DEFAULT_PROGRAM_FILES_X86,programW6432:r}}function resolveSystemRootFromEnv(e){return normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e,`SystemRoot`))??normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e,`WINDIR`))??DEFAULT_WINDOWS_SYSTEM_ROOT}function getProcessRoots(e){if(!cachedProcessRoots){const t={};for(const e of[`SystemRoot`,`WINDIR`,`ProgramFiles`,`ProgramFiles(x86)`,`ProgramW6432`])t[e]=getEnvValueCaseInsensitive(process.env,e);const n={env:t,systemRoot:normalizeWindowsInstallRoot(queryWindowsRegistryValue(WINDOWS_NT_CURRENT_VERSION_KEY,`SystemRoot`,e)??void 0)??resolveSystemRootFromEnv(t)};if(e!==void 0&&performance.now()>=e)return n;cachedProcessRoots=n}return cachedProcessRoots}function getWindowsSystemRoot(e,t){if(e!==process.env)return resolveSystemRootFromEnv(e);const n=getProcessRoots(t);return n.installRoots?.systemRoot??n.systemRoot}function getWindowsInstallRoots(e=process.env){if(e===process.env){const e=getProcessRoots();e.installRoots??=buildWindowsInstallRoots(e.env,{...getRegistryProgramFilesRoots(),systemRoot:e.systemRoot});return e.installRoots}return buildWindowsInstallRoots(e)}function getWindowsProgramFilesRoots(e=process.env){const t=getWindowsInstallRoots(e);const n=/* @__PURE__ */ new Set;const r=[];for(const e of[t.programW6432,t.programFiles,t.programFilesX86]){if(!e)continue;const t=normalizeLowercaseStringOrEmpty(e);if(n.has(t))continue;n.add(t);r.push(e)}return r}function getWindowsCmdExePath(e=process.env){return getWindowsSystem32ExePath(`cmd.exe`,e)}function getWindowsSystem32ExePath(e,t=process.env){if(path.win32.basename(e)!==e||!/^[A-Za-z0-9_.-]+\.exe$/u.test(e))throw new Error(`Invalid Windows System32 executable name: ${e}`);return path.win32.join(getWindowsSystemRoot(t),`System32`,e)}function getWindowsPowerShellExePath(e=process.env,t){return path.win32.join(getWindowsSystemRoot(e,t),`System32`,`WindowsPowerShell`,`v1.0`,`powershell.exe`)}function getWindowsWmicExePath(e=process.env,t){return path.win32.join(getWindowsSystemRoot(e,t),`System32`,`wbem`,`wmic.exe`)}export{getWindowsSystem32ExePath as a,getWindowsProgramFilesRoots as i,getWindowsInstallRoots as n,getWindowsWmicExePath as o,getWindowsPowerShellExePath as r,getWindowsCmdExePath as t};