perf(hooks): memoize isWSL detection at module load

Avoids reading /proc/version twice (once in run(), once in findPowerShell())
by computing the result once when the module loads.
This commit is contained in:
QWsin
2026-03-30 14:09:59 +08:00
parent 4ea72dec99
commit 3d5ae70c74

View File

@@ -22,14 +22,14 @@ const TITLE = 'Claude Code';
const MAX_BODY_LENGTH = 100; const MAX_BODY_LENGTH = 100;
/** /**
* Check if running on WSL (Windows Subsystem for Linux). * Memoized WSL detection at module load (avoids repeated /proc/version reads).
*/ */
function isWSL() { let isWSL = false;
if (process.platform !== 'linux') return false; if (process.platform === 'linux') {
try { try {
return require('fs').readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft'); isWSL = require('fs').readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft');
} catch { } catch {
return false; isWSL = false;
} }
} }
@@ -39,7 +39,7 @@ function isWSL() {
* Returns { path, version } or null if none available. * Returns { path, version } or null if none available.
*/ */
function findPowerShell() { function findPowerShell() {
if (!isWSL()) return null; if (!isWSL) return null;
const candidates = [ const candidates = [
'pwsh.exe', // WSL interop resolves from Windows PATH 'pwsh.exe', // WSL interop resolves from Windows PATH
@@ -135,7 +135,7 @@ function run(raw) {
if (isMacOS) { if (isMacOS) {
notifyMacOS(TITLE, summary); notifyMacOS(TITLE, summary);
} else if (isWSL()) { } else if (isWSL) {
// WSL: try PowerShell 7 first, then Windows PowerShell // WSL: try PowerShell 7 first, then Windows PowerShell
const ps = findPowerShell(); const ps = findPowerShell();
if (ps && isBurntToastAvailable(ps.path)) { if (ps && isBurntToastAvailable(ps.path)) {