diff --git a/packages/cli-kit/src/public/node/system.test.ts b/packages/cli-kit/src/public/node/system.test.ts index ed12516ba40..9457582a93d 100644 --- a/packages/cli-kit/src/public/node/system.test.ts +++ b/packages/cli-kit/src/public/node/system.test.ts @@ -393,3 +393,32 @@ describe('readStdinString', () => { await expect(got).rejects.toThrow('Stdin input exceeded the maximum allowed size.') }) }) + +describe('isWsl', () => { + test('memoizes the promise', async () => { + // Given + system._resetIsWsl() + + // When + const promise1 = system.isWsl() + const promise2 = system.isWsl() + + // Then + expect(promise1).toBe(promise2) + await promise1 + }) + + test('resets the memoized promise', async () => { + // Given + system._resetIsWsl() + const promise1 = system.isWsl() + + // When + system._resetIsWsl() + const promise2 = system.isWsl() + + // Then + expect(promise1).not.toBe(promise2) + await Promise.all([promise1, promise2]) + }) +}) diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index 3f449f8ff34..b74bbda8a3a 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -354,14 +354,28 @@ export function isCI(): boolean { return isTruthy(process.env.CI) } +/** + * Memoized promise for the WSL check. + */ +let memoizedIsWsl: Promise | undefined + /** * Check if the current environment is a WSL environment. * * @returns True if the current environment is a WSL environment. */ -export async function isWsl(): Promise { - const wsl = await import('is-wsl') - return wsl.default +export function isWsl(): Promise { + return (memoizedIsWsl ??= (async () => { + const wsl = await import('is-wsl') + return wsl.default + })()) +} + +/** + * Resets the memoized WSL check. + */ +export function _resetIsWsl(): void { + memoizedIsWsl = undefined } /**