diff --git a/packages/cli-kit/src/public/node/system.test.ts b/packages/cli-kit/src/public/node/system.test.ts index ed12516ba4..9796e0a3b7 100644 --- a/packages/cli-kit/src/public/node/system.test.ts +++ b/packages/cli-kit/src/public/node/system.test.ts @@ -353,6 +353,32 @@ describe('isStdinPiped', () => { }) }) +describe('isWsl', () => { + test('memoizes the result', async () => { + // When + const result1 = system.isWsl() + const result2 = system.isWsl() + + // Then + expect(result1).toBe(result2) + await expect(result1).resolves.toBeTypeOf('boolean') + }) + + test('clears the cache when reset', async () => { + // Given + const result1 = system.isWsl() + system._resetIsWsl() + + // When + const result2 = system.isWsl() + + // Then + expect(result1).not.toBe(result2) + await expect(result1).resolves.toBeTypeOf('boolean') + await expect(result2).resolves.toBeTypeOf('boolean') + }) +}) + describe('readStdinString', () => { test('returns undefined when stdin is not piped', async () => { // Given diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index 3f449f8ff3..c8f1824d3e 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -354,14 +354,26 @@ export function isCI(): boolean { return isTruthy(process.env.CI) } +/** + * Memoized value 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 ??= import('is-wsl').then((module) => module.default)) +} + +/** + * Resets the memoized value for the WSL check. + * This is used for testing purposes. + */ +export function _resetIsWsl() { + memoizedIsWsl = undefined } /**