Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/fifty-plants-ask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme': patch
---

Abort theme command when an invalid environment is passed in so it doesn't fall back to cached store data
12 changes: 12 additions & 0 deletions packages/theme/src/cli/utilities/theme-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ describe('ThemeCommand', () => {
})
})

test('single environment provided but not found in TOML - throws AbortError', async () => {
// Given
vi.mocked(loadEnvironment).mockResolvedValue(undefined)

await CommandConfig.load()
const command = new TestThemeCommand(['--environment', 'notreal'], CommandConfig)

// When/Then
await expect(command.run()).rejects.toThrow(AbortError)
await expect(command.run()).rejects.toThrow('Please provide a valid environment.')
})

test('multiple environments provided - uses renderConcurrent for parallel execution', async () => {
// Given
vi.mocked(loadEnvironment)
Expand Down
4 changes: 4 additions & 0 deletions packages/theme/src/cli/utilities/theme-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export default abstract class ThemeCommand extends Command {

// Single environment or no environment
if (environments.length <= 1) {
if (environments[0] && !flags.store) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've re-read this a few times and I don't think I understand how this works. Wouldn't environments[0] imply that there is an environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! So when you pass in an environment like -e my-store or --environment my-store it's extracted in the base command as the environment before it checks if it's valid or not. Even after it checks and is found to not be a valid environment, the flag.environment still has the value.

So in our case we check if an environment was actually set (no running a regular command like theme list still works without), and if the store flag is missing from the env. If it is, we know the command would not work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Because we'd populate flags.store based on the environment. I gotcha now!

throw new AbortError(`Please provide a valid environment.`)
}

const session = commandRequiresAuth ? await this.createSession(flags) : undefined
const commandName = this.constructor.name.toLowerCase()

Expand Down