diff --git a/packages/cli-kit/src/public/node/context/local.test.ts b/packages/cli-kit/src/public/node/context/local.test.ts index abe71bb52b7..77a90202c88 100644 --- a/packages/cli-kit/src/public/node/context/local.test.ts +++ b/packages/cli-kit/src/public/node/context/local.test.ts @@ -10,6 +10,8 @@ import { macAddress, getThemeKitAccessDomain, opentelemetryDomain, + firstPartyDev, + _resetFirstPartyDevCache, } from './local.js' import {fileExists} from '../fs.js' import {exec} from '../system.js' @@ -83,6 +85,62 @@ describe('isUnitTest', () => { }) }) +describe('firstPartyDev', () => { + afterEach(() => { + _resetFirstPartyDevCache() + }) + + test('returns true when SHOPIFY_CLI_1P_DEV is truthy', () => { + // Given + const env = {SHOPIFY_CLI_1P_DEV: '1'} + + // When + const got = firstPartyDev(env) + + // Then + expect(got).toBe(true) + }) + + test('memoizes the result', () => { + // Given + const env = process.env + const originalValue = env.SHOPIFY_CLI_1P_DEV + env.SHOPIFY_CLI_1P_DEV = '1' + + // When + const got1 = firstPartyDev() + env.SHOPIFY_CLI_1P_DEV = '0' + const got2 = firstPartyDev() + + // Then + expect(got1).toBe(true) + expect(got2).toBe(true) + + // Cleanup + env.SHOPIFY_CLI_1P_DEV = originalValue + }) + + test('resets the cache', () => { + // Given + const env = process.env + const originalValue = env.SHOPIFY_CLI_1P_DEV + env.SHOPIFY_CLI_1P_DEV = '1' + + // When + const got1 = firstPartyDev() + _resetFirstPartyDevCache() + env.SHOPIFY_CLI_1P_DEV = '0' + const got2 = firstPartyDev() + + // Then + expect(got1).toBe(true) + expect(got2).toBe(false) + + // Cleanup + env.SHOPIFY_CLI_1P_DEV = originalValue + }) +}) + describe('isDevelopment', () => { test('returns true when SHOPIFY_CLI_ENV is debug', () => { // Given diff --git a/packages/cli-kit/src/public/node/context/local.ts b/packages/cli-kit/src/public/node/context/local.ts index ad816399db9..d776f1a6611 100644 --- a/packages/cli-kit/src/public/node/context/local.ts +++ b/packages/cli-kit/src/public/node/context/local.ts @@ -44,6 +44,11 @@ let memoizedIsVerbose: boolean | undefined */ let memoizedIsUnitTest: boolean | undefined +/** + * Memoized value for the first party dev check. + */ +let memoizedFirstPartyDev: boolean | undefined + /** * Returns true if the CLI is running in debug mode. * @@ -142,9 +147,19 @@ export function alwaysLogMetrics(env = process.env): boolean { * @returns True if SHOPIFY_CLI_1P is truthy. */ export function firstPartyDev(env = process.env): boolean { + if (env === process.env) { + return (memoizedFirstPartyDev ??= isTruthy(env[environmentVariables.firstPartyDev])) + } return isTruthy(env[environmentVariables.firstPartyDev]) } +/** + * Resets the memoized value for the first party dev check. + */ +export function _resetFirstPartyDevCache(): void { + memoizedFirstPartyDev = undefined +} + /** * Returns true if the CLI can run the "doctor-release" command. *