Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/cli-kit/src/public/node/system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,26 @@ describe('readStdinString', () => {
await expect(got).rejects.toThrow('Stdin input exceeded the maximum allowed size.')
})
})

describe('isWsl', () => {
test('memoizes the result', async () => {
// Given
vi.doMock('is-wsl', () => ({default: true}))

// When
const firstCall = await system.isWsl()

// Then
expect(firstCall).toBe(true)

// Given
// We change the mock value. If it's memoized, it should still return the first value.
vi.doMock('is-wsl', () => ({default: false}))

// When
const secondCall = await system.isWsl()

// Then
expect(secondCall).toBe(true)
})
})
8 changes: 6 additions & 2 deletions packages/cli-kit/src/public/node/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,18 @@ export function isCI(): boolean {
return isTruthy(process.env.CI)
}

/**
* Memoized value for the WSL check.
*/
let memoizedIsWsl: Promise<boolean> | 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<boolean> {
const wsl = await import('is-wsl')
return wsl.default
return (memoizedIsWsl ??= import('is-wsl').then((wsl) => wsl.default))
}

/**
Expand Down
Loading