Skip to content

Commit

Permalink
Merge pull request #4579 from Shopify/jm/env-var
Browse files Browse the repository at this point in the history
[Themes] Add CLI environment configuration to exported theme pull and push commands
  • Loading branch information
jamesmengo authored Oct 16, 2024
2 parents f4bedd7 + 4bbdc4f commit 7e9fb90
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/theme/src/cli/commands/theme/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ If no theme is specified, then you're prompted to select the theme to pull from
only: flags.only,
ignore: flags.ignore,
force: flags.force,
verbose: flags.verbose,
noColor: flags['no-color'],
}

await pull(pullFlags)
Expand Down
2 changes: 2 additions & 0 deletions packages/theme/src/cli/services/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {showEmbeddedCLIWarning} from '../utilities/embedded-cli-warning.js'
import {ensureThemeStore} from '../utilities/theme-store.js'
import {DevelopmentThemeManager} from '../utilities/development-theme-manager.js'
import {findOrSelectTheme} from '../utilities/theme-selector.js'
import {configureCLIEnvironment} from '../utilities/cli-config.js'
import {Theme} from '@shopify/cli-kit/node/themes/types'
import {AdminSession, ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session'
import {fetchChecksums} from '@shopify/cli-kit/node/themes/api'
Expand Down Expand Up @@ -95,6 +96,7 @@ export interface PullFlags {
* @param flags - All flags are optional.
*/
export async function pull(flags: PullFlags): Promise<void> {
configureCLIEnvironment({verbose: flags.verbose, noColor: flags.noColor})
showEmbeddedCLIWarning()

const store = ensureThemeStore({store: flags.store})
Expand Down
7 changes: 7 additions & 0 deletions packages/theme/src/cli/services/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {ensureThemeStore} from '../utilities/theme-store.js'
import {DevelopmentThemeManager} from '../utilities/development-theme-manager.js'
import {findOrSelectTheme} from '../utilities/theme-selector.js'
import {Role} from '../utilities/theme-selector/fetch.js'
import {configureCLIEnvironment} from '../utilities/cli-config.js'
import {AdminSession, ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session'
import {createTheme, fetchChecksums, publishTheme} from '@shopify/cli-kit/node/themes/api'
import {Result, Theme} from '@shopify/cli-kit/node/themes/types'
Expand Down Expand Up @@ -110,6 +111,12 @@ export interface PushFlags {
*/
export async function push(flags: PushFlags): Promise<void> {
const {path} = flags

configureCLIEnvironment({
verbose: flags.verbose,
noColor: flags.noColor,
})

const force = flags.force ?? false

const store = ensureThemeStore({store: flags.store})
Expand Down
67 changes: 67 additions & 0 deletions packages/theme/src/cli/utilities/cli-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {configureCLIEnvironment} from './cli-config.js'
import {globalFlags} from '@shopify/cli-kit/node/cli'
import colors from '@shopify/cli-kit/node/colors'
import {describe, expect, beforeEach, afterAll, test} from 'vitest'

describe('configureCLIEnvironment', () => {
const originalEnv = process.env

beforeEach(() => {
process.env = {...originalEnv}
colors.level = 1
})

afterAll(() => {
process.env = originalEnv
})

describe('verbose', () => {
test('sets verbose environment variable when verbose is true', () => {
// Given
delete process.env[globalFlags.verbose.env!]

// When
configureCLIEnvironment({verbose: true})

// Then
expect(process.env[globalFlags.verbose.env!]).toBe('true')
})

test('does not set verbose environment variable when verbose is false', () => {
// Given
delete process.env[globalFlags.verbose.env!]

// When
configureCLIEnvironment({verbose: false})

// Then
expect(process.env[globalFlags.verbose.env!]).toBeUndefined()
})
})

describe('noColor', () => {
test('sets no-color environment variable when noColor is true', () => {
// Given
delete process.env[globalFlags['no-color'].env!]

// When
configureCLIEnvironment({noColor: true})

// Then
expect(colors.level).toBe(0)
expect(process.env.FORCE_COLOR).toBe('0')
})

test('does not set no-color environment variable when noColor is false', () => {
// Given
delete process.env[globalFlags['no-color'].env!]

// When
configureCLIEnvironment({noColor: false})

// Then
expect(colors.level).toBe(1)
expect(process.env.FORCE_COLOR).toBe('1')
})
})
})
24 changes: 24 additions & 0 deletions packages/theme/src/cli/utilities/cli-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {globalFlags} from '@shopify/cli-kit/node/cli'
import colors from '@shopify/cli-kit/node/colors'

interface CLIConfigOptions {
verbose?: boolean
noColor?: boolean
}

/**
* Emulates the environment setup that oCLI commands perform when executed theme commands are executed programmatically.
* Source: packages/cli-kit/src/public/node/cli.ts
*
* @param options - Configuration options for the CLI environment
*/
export function configureCLIEnvironment(options: CLIConfigOptions): void {
if (options.verbose) {
process.env[globalFlags.verbose.env!] = 'true'
}

if (options.noColor) {
colors.level = 0
process.env.FORCE_COLOR = '0'
}
}

0 comments on commit 7e9fb90

Please sign in to comment.