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
29 changes: 29 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,32 @@ describe('readStdinString', () => {
await expect(got).rejects.toThrow('Stdin input exceeded the maximum allowed size.')
})
})

describe('isWsl', () => {
test('memoizes the result', async () => {
// Given
// We need to use the real isWsl but since it's already exported from system.js
// we can check if it returns the same promise.

// When
const promise1 = system.isWsl()
const promise2 = system.isWsl()

// Then
expect(promise1).toBe(promise2)
await promise1
})

test('can be reset', async () => {
// Given
const promise1 = system.isWsl()

// When
;(system as any)._resetIsWsl()
const promise2 = system.isWsl()

// Then
expect(promise1).not.toBe(promise2)
await Promise.all([promise1, promise2])
})
})
21 changes: 18 additions & 3 deletions packages/cli-kit/src/public/node/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,29 @@ 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
export function isWsl(): Promise<boolean> {
return (memoizedIsWsl ??= (async () => {
const wsl = await import('is-wsl')
return wsl.default
})())
}

/**
* Resets the memoized value of isWsl.
* This is only used for testing.
*/
export function _resetIsWsl() {
memoizedIsWsl = undefined
}

/**
Expand Down
Loading