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
17 changes: 17 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 @@ -3,6 +3,7 @@ import {
hasGit,
isDevelopment,
isShopify,
_resetIsShopifyCache,
isTerminalInteractive,
isUnitTest,
analyticsDisabled,
Expand Down Expand Up @@ -97,6 +98,10 @@ describe('isDevelopment', () => {
})

describe('isShopify', () => {
afterEach(() => {
_resetIsShopifyCache()
})

test('returns false when the SHOPIFY_RUN_AS_USER env. variable is truthy', async () => {
// Given
const env = {SHOPIFY_RUN_AS_USER: '1'}
Expand All @@ -120,6 +125,18 @@ describe('isShopify', () => {
// When
await expect(isShopify()).resolves.toBe(true)
})

test('memoizes the result', async () => {
// Given
vi.mocked(fileExists).mockResolvedValue(true)

// When
await isShopify()
await isShopify()

// Then
expect(fileExists).toHaveBeenCalledTimes(1)
})
})

describe('hasGit', () => {
Expand Down
32 changes: 28 additions & 4 deletions packages/cli-kit/src/public/node/context/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ let memoizedIsVerbose: boolean | undefined
*/
let memoizedIsUnitTest: boolean | undefined

/**
* Memoized value for the shopify check.
*/
let memoizedIsShopify: Promise<boolean> | undefined

/**
* Returns true if the CLI is running in debug mode.
*
Expand Down Expand Up @@ -77,11 +82,22 @@ export function isVerbose(env = process.env): boolean {
* @returns True if the CLI is used in a Shopify environment.
*/
export async function isShopify(env = process.env): Promise<boolean> {
if (Object.prototype.hasOwnProperty.call(env, environmentVariables.runAsUser)) {
return !isTruthy(env[environmentVariables.runAsUser])
if (env === process.env && memoizedIsShopify !== undefined) {
return memoizedIsShopify
}

const resultPromise = (async () => {
if (Object.prototype.hasOwnProperty.call(env, environmentVariables.runAsUser)) {
return !isTruthy(env[environmentVariables.runAsUser])
} else {
return lazyFileExists(pathConstants.executables.dev)
}
})()

if (env === process.env) {
memoizedIsShopify = resultPromise
}
const devInstalled = await lazyFileExists(pathConstants.executables.dev)
return devInstalled
return resultPromise
}

/**
Expand Down Expand Up @@ -325,4 +341,12 @@ export function opentelemetryDomain(env = process.env): string {
return isSet(domain) ? domain : 'https://otlp-http-production-cli.shopifysvc.com'
}

/**
* This function is only used during UnitTesting.
* It resets the memoized value of the shopify check.
*/
export function _resetIsShopifyCache(): void {
memoizedIsShopify = undefined
}

export type CIMetadata = Metadata
Loading