From 6daafd831849f7ff56e7c4b58543cce1dab20ac7 Mon Sep 17 00:00:00 2001 From: James Meng Date: Thu, 3 Oct 2024 19:57:04 -0700 Subject: [PATCH 1/6] Add CLI environment configuration to exported pull, push, and theme selection operations --- packages/theme/src/cli/services/pull.ts | 2 + packages/theme/src/cli/services/push.ts | 7 ++ .../src/cli/utilities/cli-config.test.ts | 73 +++++++++++++++++++ .../theme/src/cli/utilities/cli-config.ts | 20 +++++ .../theme/src/cli/utilities/theme-selector.ts | 11 +++ 5 files changed, 113 insertions(+) create mode 100644 packages/theme/src/cli/utilities/cli-config.test.ts create mode 100644 packages/theme/src/cli/utilities/cli-config.ts 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..8f0cb5cea6 --- /dev/null +++ b/packages/theme/src/cli/utilities/cli-config.test.ts @@ -0,0 +1,73 @@ +import {configureCLIEnvironment} from './cli-config.js' +import {describe, expect, beforeEach, afterAll, test} from 'vitest' + +describe('configureCLIEnvironment', () => { + const originalEnv = process.env + + beforeEach(() => { + process.env = {...originalEnv} + }) + + afterAll(() => { + process.env = originalEnv + }) + + describe('verbose', () => { + test('sets DEBUG environment variable to * when verbose is true', () => { + // Given + delete process.env.DEBUG + + // When + configureCLIEnvironment({verbose: true}) + + // Then + expect(process.env.DEBUG).toBe('*') + }) + + test('does not overwrite existing DEBUG value when verbose is true', () => { + // Given + process.env.DEBUG = 'existing-value' + + // When + configureCLIEnvironment({verbose: true}) + + // Then + expect(process.env.DEBUG).toBe('existing-value') + }) + + test('does not set DEBUG environment variable when verbose is false', () => { + // Given + delete process.env.DEBUG + + // When + configureCLIEnvironment({verbose: false}) + + // Then + expect(process.env.DEBUG).toBeUndefined() + }) + }) + + describe('noColor', () => { + test('sets FORCE_COLOR to 0 when noColor is true', () => { + // Given + delete process.env.FORCE_COLOR + + // When + configureCLIEnvironment({noColor: true}) + + // Then + expect(process.env.FORCE_COLOR).toBe('0') + }) + + test('does not set FORCE_COLOR when noColor is false', () => { + // Given + delete process.env.FORCE_COLOR + + // When + configureCLIEnvironment({noColor: false}) + + // Then + expect(process.env.FORCE_COLOR).toBeUndefined() + }) + }) +}) 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..f742d849dc --- /dev/null +++ b/packages/theme/src/cli/utilities/cli-config.ts @@ -0,0 +1,20 @@ +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.DEBUG = process.env.DEBUG ?? '*' + } + + if (options.noColor) { + process.env.FORCE_COLOR = '0' + } +} diff --git a/packages/theme/src/cli/utilities/theme-selector.ts b/packages/theme/src/cli/utilities/theme-selector.ts index 42f0bab00b..56c914a0f8 100644 --- a/packages/theme/src/cli/utilities/theme-selector.ts +++ b/packages/theme/src/cli/utilities/theme-selector.ts @@ -1,5 +1,6 @@ import {fetchStoreThemes} from './theme-selector/fetch.js' import {Filter, FilterProps, filterThemes} from './theme-selector/filter.js' +import {configureCLIEnvironment} from './cli-config.js' import {getDevelopmentTheme} from '../services/local-storage.js' import {renderAutocompletePrompt} from '@shopify/cli-kit/node/ui' import {AdminSession} from '@shopify/cli-kit/node/session' @@ -25,6 +26,15 @@ interface FindOrSelectOptions { * The filter applied in the list of themes in the store. */ filter: FilterProps + /** + * Disable color output. + */ + noColor?: boolean + + /** + * Increase the verbosity of the output. + */ + verbose?: boolean } /** @@ -35,6 +45,7 @@ interface FindOrSelectOptions { * @returns the selected {@link Theme} */ export async function findOrSelectTheme(session: AdminSession, options: FindOrSelectOptions) { + configureCLIEnvironment({verbose: options.verbose, noColor: options.noColor}) const themes = await fetchStoreThemes(session) const filter = new Filter(options.filter) const store = session.storeFqdn From dd2691f3ee22c0e66eb3ca0b2cba9e70ffd4f01e Mon Sep 17 00:00:00 2001 From: James Meng Date: Fri, 11 Oct 2024 09:18:01 -0700 Subject: [PATCH 2/6] Fix how environment variable setters --- packages/theme/src/cli/utilities/cli-config.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/theme/src/cli/utilities/cli-config.ts b/packages/theme/src/cli/utilities/cli-config.ts index f742d849dc..613758bfd8 100644 --- a/packages/theme/src/cli/utilities/cli-config.ts +++ b/packages/theme/src/cli/utilities/cli-config.ts @@ -1,3 +1,5 @@ +import {globalFlags} from '@shopify/cli-kit/node/cli' + interface CLIConfigOptions { verbose?: boolean noColor?: boolean @@ -11,10 +13,10 @@ interface CLIConfigOptions { */ export function configureCLIEnvironment(options: CLIConfigOptions): void { if (options.verbose) { - process.env.DEBUG = process.env.DEBUG ?? '*' + process.env[globalFlags.verbose.env!] = 'true' } if (options.noColor) { - process.env.FORCE_COLOR = '0' + process.env[globalFlags['no-color'].env!] = 'true' } } From bf08b23523cdb98f37757d36bd180253f0f28b47 Mon Sep 17 00:00:00 2001 From: James Meng Date: Fri, 11 Oct 2024 09:19:31 -0700 Subject: [PATCH 3/6] Remove verbose and noColor support for findAndSelectThemes --- packages/theme/src/cli/utilities/theme-selector.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/theme/src/cli/utilities/theme-selector.ts b/packages/theme/src/cli/utilities/theme-selector.ts index 56c914a0f8..42f0bab00b 100644 --- a/packages/theme/src/cli/utilities/theme-selector.ts +++ b/packages/theme/src/cli/utilities/theme-selector.ts @@ -1,6 +1,5 @@ import {fetchStoreThemes} from './theme-selector/fetch.js' import {Filter, FilterProps, filterThemes} from './theme-selector/filter.js' -import {configureCLIEnvironment} from './cli-config.js' import {getDevelopmentTheme} from '../services/local-storage.js' import {renderAutocompletePrompt} from '@shopify/cli-kit/node/ui' import {AdminSession} from '@shopify/cli-kit/node/session' @@ -26,15 +25,6 @@ interface FindOrSelectOptions { * The filter applied in the list of themes in the store. */ filter: FilterProps - /** - * Disable color output. - */ - noColor?: boolean - - /** - * Increase the verbosity of the output. - */ - verbose?: boolean } /** @@ -45,7 +35,6 @@ interface FindOrSelectOptions { * @returns the selected {@link Theme} */ export async function findOrSelectTheme(session: AdminSession, options: FindOrSelectOptions) { - configureCLIEnvironment({verbose: options.verbose, noColor: options.noColor}) const themes = await fetchStoreThemes(session) const filter = new Filter(options.filter) const store = session.storeFqdn From fd0b49627839c71885af9b7d7d2d54beb41f7d7e Mon Sep 17 00:00:00 2001 From: James Meng Date: Fri, 11 Oct 2024 09:31:20 -0700 Subject: [PATCH 4/6] Update tests --- .../src/cli/utilities/cli-config.test.ts | 36 +++++++------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/packages/theme/src/cli/utilities/cli-config.test.ts b/packages/theme/src/cli/utilities/cli-config.test.ts index 8f0cb5cea6..0857a47a38 100644 --- a/packages/theme/src/cli/utilities/cli-config.test.ts +++ b/packages/theme/src/cli/utilities/cli-config.test.ts @@ -1,4 +1,5 @@ import {configureCLIEnvironment} from './cli-config.js' +import {globalFlags} from '@shopify/cli-kit/node/cli' import {describe, expect, beforeEach, afterAll, test} from 'vitest' describe('configureCLIEnvironment', () => { @@ -13,61 +14,50 @@ describe('configureCLIEnvironment', () => { }) describe('verbose', () => { - test('sets DEBUG environment variable to * when verbose is true', () => { + test('sets verbose environment variable when verbose is true', () => { // Given - delete process.env.DEBUG + delete process.env[globalFlags.verbose.env!] // When configureCLIEnvironment({verbose: true}) // Then - expect(process.env.DEBUG).toBe('*') + expect(process.env[globalFlags.verbose.env!]).toBe('true') }) - test('does not overwrite existing DEBUG value when verbose is true', () => { + test('does not set verbose environment variable when verbose is false', () => { // Given - process.env.DEBUG = 'existing-value' - - // When - configureCLIEnvironment({verbose: true}) - - // Then - expect(process.env.DEBUG).toBe('existing-value') - }) - - test('does not set DEBUG environment variable when verbose is false', () => { - // Given - delete process.env.DEBUG + delete process.env[globalFlags.verbose.env!] // When configureCLIEnvironment({verbose: false}) // Then - expect(process.env.DEBUG).toBeUndefined() + expect(process.env[globalFlags.verbose.env!]).toBeUndefined() }) }) describe('noColor', () => { - test('sets FORCE_COLOR to 0 when noColor is true', () => { + test('sets no-color environment variable when noColor is true', () => { // Given - delete process.env.FORCE_COLOR + delete process.env[globalFlags['no-color'].env!] // When configureCLIEnvironment({noColor: true}) // Then - expect(process.env.FORCE_COLOR).toBe('0') + expect(process.env[globalFlags['no-color'].env!]).toBe('true') }) - test('does not set FORCE_COLOR when noColor is false', () => { + test('does not set no-color environment variable when noColor is false', () => { // Given - delete process.env.FORCE_COLOR + delete process.env[globalFlags['no-color'].env!] // When configureCLIEnvironment({noColor: false}) // Then - expect(process.env.FORCE_COLOR).toBeUndefined() + expect(process.env[globalFlags['no-color'].env!]).toBeUndefined() }) }) }) From a54dfaad4cd9993e8dd8326946a0604380537b92 Mon Sep 17 00:00:00 2001 From: James Meng Date: Fri, 11 Oct 2024 09:32:57 -0700 Subject: [PATCH 5/6] Connect pull command layer --- packages/theme/src/cli/commands/theme/pull.ts | 2 ++ 1 file changed, 2 insertions(+) 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) From 4bbdc4ff801870d7fc370248acc053ddcdd2dfd3 Mon Sep 17 00:00:00 2001 From: James Meng Date: Fri, 11 Oct 2024 09:43:47 -0700 Subject: [PATCH 6/6] Fix noColor --- packages/theme/src/cli/utilities/cli-config.test.ts | 8 ++++++-- packages/theme/src/cli/utilities/cli-config.ts | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/theme/src/cli/utilities/cli-config.test.ts b/packages/theme/src/cli/utilities/cli-config.test.ts index 0857a47a38..cc08f0d1c3 100644 --- a/packages/theme/src/cli/utilities/cli-config.test.ts +++ b/packages/theme/src/cli/utilities/cli-config.test.ts @@ -1,5 +1,6 @@ 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', () => { @@ -7,6 +8,7 @@ describe('configureCLIEnvironment', () => { beforeEach(() => { process.env = {...originalEnv} + colors.level = 1 }) afterAll(() => { @@ -46,7 +48,8 @@ describe('configureCLIEnvironment', () => { configureCLIEnvironment({noColor: true}) // Then - expect(process.env[globalFlags['no-color'].env!]).toBe('true') + expect(colors.level).toBe(0) + expect(process.env.FORCE_COLOR).toBe('0') }) test('does not set no-color environment variable when noColor is false', () => { @@ -57,7 +60,8 @@ describe('configureCLIEnvironment', () => { configureCLIEnvironment({noColor: false}) // Then - expect(process.env[globalFlags['no-color'].env!]).toBeUndefined() + 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 index 613758bfd8..f644ae4a2d 100644 --- a/packages/theme/src/cli/utilities/cli-config.ts +++ b/packages/theme/src/cli/utilities/cli-config.ts @@ -1,4 +1,5 @@ import {globalFlags} from '@shopify/cli-kit/node/cli' +import colors from '@shopify/cli-kit/node/colors' interface CLIConfigOptions { verbose?: boolean @@ -17,6 +18,7 @@ export function configureCLIEnvironment(options: CLIConfigOptions): void { } if (options.noColor) { - process.env[globalFlags['no-color'].env!] = 'true' + colors.level = 0 + process.env.FORCE_COLOR = '0' } }