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
19 changes: 18 additions & 1 deletion packages/cli-kit/src/public/node/context/local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
macAddress,
getThemeKitAccessDomain,
opentelemetryDomain,
_resetIsShopify,
} from './local.js'
import {fileExists} from '../fs.js'
import {exec} from '../system.js'

import {afterEach, expect, describe, vi, test} from 'vitest'
import {afterEach, beforeEach, expect, describe, vi, test} from 'vitest'

vi.mock('../fs.js')
vi.mock('../system.js')
Expand Down Expand Up @@ -97,6 +98,10 @@ describe('isDevelopment', () => {
})

describe('isShopify', () => {
beforeEach(() => {
_resetIsShopify()
})

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
20 changes: 20 additions & 0 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,6 +82,14 @@ 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 (env === process.env) {
// Memoize the result to avoid repeated disk checks for the 'dev' executable.
return (memoizedIsShopify ??= checkIsShopify(env))
}
return checkIsShopify(env)
}

async function checkIsShopify(env: NodeJS.ProcessEnv): Promise<boolean> {
if (Object.prototype.hasOwnProperty.call(env, environmentVariables.runAsUser)) {
return !isTruthy(env[environmentVariables.runAsUser])
}
Expand Down Expand Up @@ -325,4 +338,11 @@ export function opentelemetryDomain(env = process.env): string {
return isSet(domain) ? domain : 'https://otlp-http-production-cli.shopifysvc.com'
}

/**
* Resets the memoized value for the shopify check.
*/
export function _resetIsShopify(): void {
memoizedIsShopify = undefined
}

export type CIMetadata = Metadata
Loading