@@ -5,12 +5,14 @@ import {Config, Flags} from '@oclif/core'
55import { AdminSession , ensureAuthenticatedThemes } from '@shopify/cli-kit/node/session'
66import { loadEnvironment } from '@shopify/cli-kit/node/environments'
77import { renderConcurrent , renderConfirmationPrompt , renderError } from '@shopify/cli-kit/node/ui'
8+ import { fileExistsSync } from '@shopify/cli-kit/node/fs'
89import type { Writable } from 'stream'
910
1011vi . mock ( '@shopify/cli-kit/node/session' )
1112vi . mock ( '@shopify/cli-kit/node/environments' )
1213vi . mock ( '@shopify/cli-kit/node/ui' )
1314vi . mock ( './theme-store.js' )
15+ vi . mock ( '@shopify/cli-kit/node/fs' )
1416
1517const CommandConfig = new Config ( { root : __dirname } )
1618
@@ -27,6 +29,14 @@ class TestThemeCommand extends ThemeCommand {
2729 password : Flags . string ( {
2830 env : 'SHOPIFY_FLAG_PASSWORD' ,
2931 } ) ,
32+ path : Flags . string ( {
33+ env : 'SHOPIFY_FLAG_PATH' ,
34+ default : 'current/working/directory' ,
35+ } ) ,
36+ 'no-color' : Flags . boolean ( {
37+ env : 'SHOPIFY_FLAG_NO_COLOR' ,
38+ default : false ,
39+ } ) ,
3040 }
3141
3242 static multiEnvironmentsFlags : RequiredFlags = [ 'store' ]
@@ -119,7 +129,9 @@ describe('ThemeCommand', () => {
119129 await command . run ( )
120130
121131 // Then
122- expect ( loadEnvironment ) . toHaveBeenCalledWith ( 'development' , 'shopify.theme.toml' , { from : undefined } )
132+ expect ( loadEnvironment ) . toHaveBeenCalledWith ( 'development' , 'shopify.theme.toml' , {
133+ from : 'current/working/directory' ,
134+ } )
123135 expect ( ensureAuthenticatedThemes ) . toHaveBeenCalledTimes ( 1 )
124136 expect ( renderConcurrent ) . not . toHaveBeenCalled ( )
125137 expect ( command . commandCalls ) . toHaveLength ( 1 )
@@ -149,8 +161,14 @@ describe('ThemeCommand', () => {
149161 await command . run ( )
150162
151163 // Then
152- expect ( loadEnvironment ) . toHaveBeenCalledWith ( 'development' , 'shopify.theme.toml' , { from : undefined , silent : true } )
153- expect ( loadEnvironment ) . toHaveBeenCalledWith ( 'staging' , 'shopify.theme.toml' , { from : undefined , silent : true } )
164+ expect ( loadEnvironment ) . toHaveBeenCalledWith ( 'development' , 'shopify.theme.toml' , {
165+ from : 'current/working/directory' ,
166+ silent : true ,
167+ } )
168+ expect ( loadEnvironment ) . toHaveBeenCalledWith ( 'staging' , 'shopify.theme.toml' , {
169+ from : 'current/working/directory' ,
170+ silent : true ,
171+ } )
154172
155173 expect ( renderConcurrent ) . toHaveBeenCalledOnce ( )
156174 expect ( renderConcurrent ) . toHaveBeenCalledWith (
@@ -433,5 +451,106 @@ describe('ThemeCommand', () => {
433451 } ) ,
434452 )
435453 } )
454+
455+ test ( 'commands should display an error if the --path flag is used' , async ( ) => {
456+ // Given
457+ const environmentConfig = { store : 'store.myshopify.com' }
458+ vi . mocked ( loadEnvironment ) . mockResolvedValue ( environmentConfig )
459+ vi . mocked ( renderConfirmationPrompt ) . mockResolvedValue ( true )
460+ vi . mocked ( fileExistsSync ) . mockReturnValue ( true )
461+
462+ await CommandConfig . load ( )
463+ const command = new TestThemeCommand (
464+ [ '--environment' , 'command-error' , '--environment' , 'development' , '--path' , 'path' ] ,
465+ CommandConfig ,
466+ )
467+
468+ // When
469+ await command . run ( )
470+
471+ // Then
472+ expect ( renderError ) . toHaveBeenCalledWith (
473+ expect . objectContaining ( {
474+ body : [
475+ "Can't use `--path` flag with multiple environments." ,
476+ "Configure each environment's theme path in your shopify.theme.toml file instead." ,
477+ ] ,
478+ } ) ,
479+ )
480+ } )
481+
482+ test ( 'commands should display an error if the --path flag is used and no shopify.theme.toml is found' , async ( ) => {
483+ // Given
484+ const environmentConfig = { store : 'store.myshopify.com' }
485+ vi . mocked ( loadEnvironment ) . mockResolvedValue ( environmentConfig )
486+ vi . mocked ( renderConfirmationPrompt ) . mockResolvedValue ( true )
487+ vi . mocked ( fileExistsSync ) . mockReturnValue ( false )
488+
489+ await CommandConfig . load ( )
490+ const command = new TestThemeCommand (
491+ [ '--environment' , 'command-error' , '--environment' , 'development' , '--path' , 'path' ] ,
492+ CommandConfig ,
493+ )
494+
495+ // When
496+ await command . run ( )
497+
498+ // Then
499+ expect ( renderError ) . toHaveBeenCalledWith (
500+ expect . objectContaining ( {
501+ body : [
502+ "Can't use `--path` flag with multiple environments." ,
503+ 'Run this command from the directory containing shopify.theme.toml.' ,
504+ 'No shopify.theme.toml found in current directory.' ,
505+ ] ,
506+ } ) ,
507+ )
508+ } )
509+
510+ test ( 'CLI and shopify.theme.toml flag values take precedence over defaults' , async ( ) => {
511+ // Given
512+ vi . mocked ( loadEnvironment )
513+ . mockResolvedValueOnce ( { store : 'store1.myshopify.com' , theme : 'theme1.myshopify.com' , path : 'theme/path' } )
514+ . mockResolvedValueOnce ( { store : 'store2.myshopify.com' , development : true , path : 'development/path' } )
515+ . mockResolvedValueOnce ( { store : 'store3.myshopify.com' , live : true , 'no-color' : false } )
516+
517+ vi . mocked ( renderConcurrent ) . mockImplementation ( async ( { processes} ) => {
518+ for ( const process of processes ) {
519+ // eslint-disable-next-line no-await-in-loop
520+ await process . action ( { } as Writable , { } as Writable , { } as any )
521+ }
522+ } )
523+
524+ await CommandConfig . load ( )
525+ const command = new TestThemeCommand (
526+ [ '--environment' , 'theme' , '--environment' , 'development' , '--environment' , 'live' , '--no-color' ] ,
527+ CommandConfig ,
528+ )
529+
530+ // When
531+ await command . run ( )
532+
533+ // Then
534+ const commandCalls = command . commandCalls
535+ expect ( commandCalls ) . toHaveLength ( 3 )
536+
537+ const themeEnvFlags = commandCalls [ 0 ] ?. flags
538+ expect ( themeEnvFlags ?. path ) . toEqual ( 'theme/path' )
539+ expect ( themeEnvFlags ?. store ) . toEqual ( 'store1.myshopify.com' )
540+ expect ( themeEnvFlags ?. theme ) . toEqual ( 'theme1.myshopify.com' )
541+ expect ( themeEnvFlags ?. [ 'no-color' ] ) . toEqual ( true )
542+
543+ const developmentEnvFlags = commandCalls [ 1 ] ?. flags
544+ expect ( developmentEnvFlags ?. path ) . toEqual ( 'development/path' )
545+ expect ( developmentEnvFlags ?. store ) . toEqual ( 'store2.myshopify.com' )
546+ expect ( developmentEnvFlags ?. development ) . toEqual ( true )
547+ expect ( developmentEnvFlags ?. [ 'no-color' ] ) . toEqual ( true )
548+
549+ const liveEnvFlags = commandCalls [ 2 ] ?. flags
550+ expect ( liveEnvFlags ?. path ) . toEqual ( 'current/working/directory' )
551+ expect ( liveEnvFlags ?. store ) . toEqual ( 'store3.myshopify.com' )
552+ expect ( liveEnvFlags ?. live ) . toEqual ( true )
553+ expect ( liveEnvFlags ?. [ 'no-color' ] ) . toEqual ( true )
554+ } )
436555 } )
437556} )
0 commit comments