Skip to content

Add default environment functionality to theme commands #5931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2025

Conversation

graygilmore
Copy link
Contributor

@graygilmore graygilmore commented Jun 2, 2025

WHY are these changes introduced?

Allows developers to set a default environment in their environments config file:

[environments.default]
store = "abc.myshopify.com"

This environment will automatically be used for CLI commands even if the developer does not specify the environment with the --environment flag. This gives developers a bit of a safety net when running commands and switching between different storefronts because this default environment will take precedence over any storefront saved to local storage.

How to test your changes?

  1. Install the snapped version:
    npm i -g @shopify/[email protected] --@shopify:registry=https://registry.npmjs.org
    
  2. Navigate to a theme folder
  3. Create a shopify.theme.toml file if one doesn't already exist and populate it with at least a [environments.default] entry that has a store entry
  4. Run a theme command like shopify theme dev
  5. Ensure that it is using the default environment's store
  6. Run the command again but specify a store shopify theme dev --store different-store.myshopify.com
  7. Ensure that it's using the flag you passed in instead of the default store
  8. Add a separate entry to the shopify.theme.toml file
  9. Run the command again but specify your other environment shopify theme dev -e new
  10. Ensure that it's using the new environment's store instead of the default

Post-release steps

Once this is approved and merged we'll want to update the docs: https://shopify.dev/docs/storefronts/themes/tools/cli/environments#configure-environments

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes

Copy link
Contributor

github-actions bot commented Jun 2, 2025

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
78.17% (+0.02% 🔼)
12481/15967
🟡 Branches
72.37% (+0.08% 🔼)
6072/8390
🟡 Functions
78.38% (+0.01% 🔼)
3263/4163
🟡 Lines
78.6% (+0.03% 🔼)
11810/15026

Test suite run success

2878 tests passing in 1253 suites.

Report generated by 🧪jest coverage report action from 4139e95

@graygilmore graygilmore force-pushed the gg-default-environment branch 2 times, most recently from 6951290 to 879bacc Compare June 2, 2025 19:01
@graygilmore
Copy link
Contributor Author

/snapit

Copy link
Contributor

github-actions bot commented Jun 2, 2025

🫰✨ Thanks @graygilmore! Your snapshot has been published to npm.

Test the snapshot by installing your package globally:

pnpm i -g @shopify/[email protected]

Tip

If you get an ETARGET error, install it with NPM and the flag --@shopify:registry=https://registry.npmjs.org

Caution

After installing, validate the version by running just shopify in your terminal.
If the versions don't match, you might have multiple global instances installed.
Use which shopify to find out which one you are running and uninstall it.

@graygilmore graygilmore force-pushed the gg-default-environment branch from 879bacc to 782dbc6 Compare June 2, 2025 23:48
@graygilmore
Copy link
Contributor Author

/snapit

Copy link
Contributor

github-actions bot commented Jun 3, 2025

🫰✨ Thanks @graygilmore! Your snapshot has been published to npm.

Test the snapshot by installing your package globally:

pnpm i -g @shopify/[email protected]

Tip

If you get an ETARGET error, install it with NPM and the flag --@shopify:registry=https://registry.npmjs.org

Caution

After installing, validate the version by running just shopify in your terminal.
If the versions don't match, you might have multiple global instances installed.
Use which shopify to find out which one you are running and uninstall it.

@graygilmore graygilmore force-pushed the gg-default-environment branch from e771005 to 782dbc6 Compare June 3, 2025 16:43
@graygilmore
Copy link
Contributor Author

/snapit

Copy link
Contributor

github-actions bot commented Jun 3, 2025

🫰✨ Thanks @graygilmore! Your snapshot has been published to npm.

Test the snapshot by installing your package globally:

pnpm i -g @shopify/[email protected]

Tip

If you get an ETARGET error, install it with NPM and the flag --@shopify:registry=https://registry.npmjs.org

Caution

After installing, validate the version by running just shopify in your terminal.
If the versions don't match, you might have multiple global instances installed.
Use which shopify to find out which one you are running and uninstall it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The conditionals got a little out of hand in this file but it's a bit of a complicated thing to check for. Let me know if any of this is too complicated to read through and I can think about rearranging things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed up a commit to help with this fbdb994

@graygilmore graygilmore marked this pull request as ready for review June 3, 2025 17:22
@graygilmore graygilmore requested review from a team as code owners June 3, 2025 17:22
Copy link
Contributor

@jamesmengo jamesmengo left a comment

Choose a reason for hiding this comment

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

I do think the code in base-command would benefit from another pass for readability, but my tophat went well!


Aside from that, I wanted to raise the edge case where a user may have created an environment called 'default', which is no longer actually used in their workflow or should only be used when specified.

Not too worried about functional impact, since we output information to the user about the environment being used.

I wonder if that's worth making this a minor change though 🤔

@EvilGenius13
Copy link
Contributor

I do think the code in base-command would benefit from another pass for readability, but my tophat went well!

Aside from that, I wanted to raise the edge case where a user may have created an environment called 'default', which is no longer actually used in their workflow or should only be used when specified.

Not too worried about functional impact, since we output information to the user about the environment being used.

I wonder if that's worth making this a minor change though 🤔

🎩 went great here too. Chiming in that I agree making it a minor change would be nice.

@graygilmore graygilmore force-pushed the gg-default-environment branch from 782dbc6 to 60c1b9d Compare June 4, 2025 22:14
@graygilmore
Copy link
Contributor Author

Force Push Patch Notes

  • Rebased with main

if (!environmentFileExists && !environmentSpecified) return originalResult

// Noop if multiple environments were specified (let commands handle this)
if (environmentSpecified && environments.length > 1) return originalResult
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we skip the check for environmentSpecified?

Copy link
Contributor

@jamesmengo jamesmengo left a comment

Choose a reason for hiding this comment

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

Ty for the changes! If the platform team is ok with the implications of this change I'm 100% on board!

Bikeshed warning: I wonder if we can also provide a way to specify a custom 'default' name but that would likely be out of the scope of this

@graygilmore graygilmore force-pushed the gg-default-environment branch from fbdb994 to 9b889ac Compare June 6, 2025 20:42
@graygilmore
Copy link
Contributor Author

Force Push Patch Notes

  • Rebased with main

Copy link
Contributor

@gonzaloriestra gonzaloriestra left a comment

Choose a reason for hiding this comment

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

Code LGTM. Is the documentation ready?

@gonzaloriestra gonzaloriestra force-pushed the gg-default-environment branch from 9b889ac to 18f9aa2 Compare June 9, 2025 17:42
@graygilmore
Copy link
Contributor Author

Is the documentation ready?

Was waiting on approval on approach here first. I'll fire that up now and address the feedback on this PR, too.

You may now optionally define a default environment in your environment
config file (e.g. `shopify.theme.toml`) that will be automatically injected
into the running command without needing to specify it.
@graygilmore graygilmore force-pushed the gg-default-environment branch from 18f9aa2 to 4139e95 Compare June 9, 2025 17:57
Copy link
Contributor

github-actions bot commented Jun 9, 2025

Differences in type declarations

We detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:

  • Some seemingly private modules might be re-exported through public modules.
  • If the branch is behind main you might see odd diffs, rebase main into this branch.

New type declarations

We found no new type declarations in this PR

Existing type declarations

packages/cli-kit/dist/public/node/base-command.d.ts
@@ -20,6 +20,11 @@ declare abstract class BaseCommand extends Command {
     protected environmentsFilename(): string | undefined;
     protected failMissingNonTTYFlags(flags: FlagOutput, requiredFlags: string[]): void;
     private resultWithEnvironment;
+    /**
+     * Tries to load an environment to forward to the command. If no environment
+     * is specified it will try to load a default environment.
+     */
+    private loadEnvironmentForCommand;
 }
 export declare function addFromParsedFlags(flags: {
     path?: string;
packages/cli-kit/dist/public/node/environments.d.ts
@@ -11,4 +11,5 @@ interface LoadEnvironmentOptions {
  * @returns The loaded environments.
  */
 export declare function loadEnvironment(environmentName: string, fileName: string, options?: LoadEnvironmentOptions): Promise<JsonMap | undefined>;
+export declare function environmentFilePath(fileName: string, options?: LoadEnvironmentOptions): Promise<string | undefined>;
 export {};
\ No newline at end of file

@graygilmore graygilmore enabled auto-merge June 9, 2025 18:06
@gonzaloriestra gonzaloriestra disabled auto-merge June 10, 2025 07:23
@gonzaloriestra gonzaloriestra merged commit 190e2b9 into main Jun 10, 2025
30 checks passed
@gonzaloriestra gonzaloriestra deleted the gg-default-environment branch June 10, 2025 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: Theme command to load the default environment
4 participants