Skip to content
Closed
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
34 changes: 34 additions & 0 deletions packages/cli-kit/src/public/node/context/local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
isDevelopment,
isShopify,
isTerminalInteractive,
_resetIsTerminalInteractiveCache,
isUnitTest,
analyticsDisabled,
cloudEnvironment,
Expand All @@ -25,6 +26,7 @@ describe('isTerminalInteractive', () => {
const originalEnv = {...process.env}

afterEach(() => {
_resetIsTerminalInteractiveCache()
process.stdout.isTTY = originalIsTTY
process.env.TERM = originalEnv.TERM
if (originalEnv.CI === undefined) {
Expand Down Expand Up @@ -68,6 +70,38 @@ describe('isTerminalInteractive', () => {
process.env.TERM = 'xterm-256color'
expect(isTerminalInteractive()).toBe(false)
})

test('memoizes the result', () => {
// Given
process.stdout.isTTY = true
delete process.env.CI
process.env.TERM = 'xterm-256color'
const firstResult = isTerminalInteractive()

// When
process.stdout.isTTY = false
const secondResult = isTerminalInteractive()

// Then
expect(firstResult).toBe(true)
expect(secondResult).toBe(true)
})

test('resets the cache', () => {
// Given
process.stdout.isTTY = true
delete process.env.CI
process.env.TERM = 'xterm-256color'
isTerminalInteractive()

// When
_resetIsTerminalInteractiveCache()
process.stdout.isTTY = false
const secondResult = isTerminalInteractive()

// Then
expect(secondResult).toBe(false)
})
})

describe('isUnitTest', () => {
Expand Down
16 changes: 15 additions & 1 deletion packages/cli-kit/src/public/node/context/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@ async function lazyExec(command: string, args: string[]): Promise<void> {
await exec(command, args)
}

/**
* Memoized value for the terminal interactive check.
*/
let memoizedIsTerminalInteractive: boolean | undefined

/**
* It returns true if the terminal is interactive.
*
* @returns True if the terminal is interactive.
*/
export function isTerminalInteractive(): boolean {
return Boolean(process.stdout.isTTY && process.env.TERM !== 'dumb' && !('CI' in process.env))
return (memoizedIsTerminalInteractive ??= Boolean(
process.stdout.isTTY && process.env.TERM !== 'dumb' && !('CI' in process.env),
))
}

/**
* Resets the memoized value for the terminal interactive check.
*/
export function _resetIsTerminalInteractiveCache(): void {
memoizedIsTerminalInteractive = undefined
}

/**
Expand Down
Loading