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
58 changes: 58 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 @@ -10,6 +10,8 @@ import {
macAddress,
getThemeKitAccessDomain,
opentelemetryDomain,
firstPartyDev,
_resetFirstPartyDevCache,
} from './local.js'
import {fileExists} from '../fs.js'
import {exec} from '../system.js'
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 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 first party dev check.
*/
let memoizedFirstPartyDev: boolean | undefined

/**
* Returns true if the CLI is running in debug mode.
*
Expand Down Expand Up @@ -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.
*
Expand Down
Loading