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
18 changes: 18 additions & 0 deletions packages/cli-kit/src/public/node/context/local.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ciPlatform,
hasGit,
_resetHasGit,
isDevelopment,
isShopify,
isTerminalInteractive,
Expand Down Expand Up @@ -125,6 +126,7 @@ describe('isShopify', () => {
describe('hasGit', () => {
test('returns false if git --version errors', async () => {
// Given
_resetHasGit()
vi.mocked(exec).mockRejectedValue(new Error('git not found'))

// When
Expand All @@ -136,6 +138,7 @@ describe('hasGit', () => {

test('returns true if git --version succeeds', async () => {
// Given
_resetHasGit()
vi.mocked(exec).mockResolvedValue(undefined)

// When
Expand All @@ -144,6 +147,21 @@ describe('hasGit', () => {
// Then
expect(got).toBeTruthy()
})

test('memoizes the result', async () => {
// Given
_resetHasGit()
vi.mocked(exec).mockResolvedValue(undefined)

// When
await hasGit()
await hasGit()
const got = await hasGit()

// Then
expect(got).toBeTruthy()
expect(vi.mocked(exec)).toHaveBeenCalledTimes(1)
})
})

describe('analitycsDisabled', () => {
Expand Down
35 changes: 28 additions & 7 deletions packages/cli-kit/src/public/node/context/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,40 @@ export function cloudEnvironment(env: NodeJS.ProcessEnv = process.env): {
return {platform: 'localhost', editor: false}
}

/**
* Memoized promise for the hasGit check.
*/
let hasGitPromise: Promise<boolean> | undefined

/**
* Returns whether the environment has Git available.
*
* @returns A promise that resolves with the value.
*/
export async function hasGit(): Promise<boolean> {
try {
await lazyExec('git', ['--version'])
return true
// eslint-disable-next-line no-catch-all/no-catch-all
} catch {
return false
export function hasGit(): Promise<boolean> {
if (hasGitPromise) {
return hasGitPromise
}

hasGitPromise = (async () => {
try {
await lazyExec('git', ['--version'])
return true
// eslint-disable-next-line no-catch-all/no-catch-all
} catch {
return false
}
})()

return hasGitPromise
}

/**
* Resets the memoized value for the hasGit check.
* This is only intended for use in tests.
*/
export function _resetHasGit(): void {
hasGitPromise = undefined
}

/**
Expand Down
Loading