diff --git a/packages/theme/src/cli/commands/theme/pull.ts b/packages/theme/src/cli/commands/theme/pull.ts index 67fd466ff8..3bb19c0850 100644 --- a/packages/theme/src/cli/commands/theme/pull.ts +++ b/packages/theme/src/cli/commands/theme/pull.ts @@ -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) diff --git a/packages/theme/src/cli/services/pull.ts b/packages/theme/src/cli/services/pull.ts index 059d4fcc10..6b03ba423c 100644 --- a/packages/theme/src/cli/services/pull.ts +++ b/packages/theme/src/cli/services/pull.ts @@ -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' @@ -95,6 +96,7 @@ export interface PullFlags { * @param flags - All flags are optional. */ export async function pull(flags: PullFlags): Promise { + configureCLIEnvironment({verbose: flags.verbose, noColor: flags.noColor}) showEmbeddedCLIWarning() const store = ensureThemeStore({store: flags.store}) diff --git a/packages/theme/src/cli/services/push.ts b/packages/theme/src/cli/services/push.ts index f7fa99f95f..670359240a 100644 --- a/packages/theme/src/cli/services/push.ts +++ b/packages/theme/src/cli/services/push.ts @@ -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' @@ -110,6 +111,12 @@ export interface PushFlags { */ export async function push(flags: PushFlags): Promise { const {path} = flags + + configureCLIEnvironment({ + verbose: flags.verbose, + noColor: flags.noColor, + }) + const force = flags.force ?? false const store = ensureThemeStore({store: flags.store}) diff --git a/packages/theme/src/cli/utilities/cli-config.test.ts b/packages/theme/src/cli/utilities/cli-config.test.ts new file mode 100644 index 0000000000..cc08f0d1c3 --- /dev/null +++ b/packages/theme/src/cli/utilities/cli-config.test.ts @@ -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') + }) + }) +}) diff --git a/packages/theme/src/cli/utilities/cli-config.ts b/packages/theme/src/cli/utilities/cli-config.ts new file mode 100644 index 0000000000..f644ae4a2d --- /dev/null +++ b/packages/theme/src/cli/utilities/cli-config.ts @@ -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' + } +}