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