diff --git a/.changeset/pink-gorillas-train.md b/.changeset/pink-gorillas-train.md new file mode 100644 index 0000000000..54c8f06aaa --- /dev/null +++ b/.changeset/pink-gorillas-train.md @@ -0,0 +1,5 @@ +--- +'@shopify/cli-hydrogen': patch +--- + +Adds the ability to specify an Environment's branch name to interact with Hydrogen storefront environment variables diff --git a/packages/cli/oclif.manifest.json b/packages/cli/oclif.manifest.json index ceb1106d37..3a77f91565 100644 --- a/packages/cli/oclif.manifest.json +++ b/packages/cli/oclif.manifest.json @@ -1 +1 @@ -{"version":"4.1.2","commands":{"hydrogen:build":{"id":"hydrogen:build","description":"Builds a Hydrogen storefront for production.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"sourcemap":{"name":"sourcemap","type":"boolean","description":"Generate sourcemaps for the build.","allowNo":false},"disable-route-warning":{"name":"disable-route-warning","type":"boolean","description":"Disable warning about missing standard routes.","allowNo":false},"base":{"name":"base","type":"option","hidden":true,"multiple":false},"entry":{"name":"entry","type":"option","hidden":true,"multiple":false},"target":{"name":"target","type":"option","hidden":true,"multiple":false}},"args":[]},"hydrogen:check":{"id":"hydrogen:check","description":"Returns diagnostic information about a Hydrogen storefront.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false}},"args":[{"name":"resource","description":"The resource to check. Currently only 'routes' is supported.","required":true,"options":["routes"]}]},"hydrogen:codegen-unstable":{"id":"hydrogen:codegen-unstable","description":"Generate types for the Storefront API queries found in your project.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"codegen-config-path":{"name":"codegen-config-path","type":"option","description":"Specify a path to a codegen configuration file. Defaults to `/codegen.ts` if it exists.","required":false,"multiple":false},"watch":{"name":"watch","type":"boolean","description":"Watch the project for changes to update types on file save.","required":false,"allowNo":false}},"args":[]},"hydrogen:dev":{"id":"hydrogen:dev","description":"Runs Hydrogen storefront in an Oxygen worker for development.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"port":{"name":"port","type":"option","description":"Port to run the server on.","multiple":false,"default":3000},"codegen-unstable":{"name":"codegen-unstable","type":"boolean","description":"Generate types for the Storefront API queries found in your project. It updates the types on file save.","required":false,"allowNo":false},"codegen-config-path":{"name":"codegen-config-path","type":"option","description":"Specify a path to a codegen configuration file. Defaults to `/codegen.ts` if it exists.","required":false,"multiple":false,"dependsOn":["codegen-unstable"]},"disable-virtual-routes":{"name":"disable-virtual-routes","type":"boolean","description":"Disable rendering fallback routes when a route file doesn't exist.","allowNo":false},"shop":{"name":"shop","type":"option","char":"s","description":"Shop URL. It can be the shop prefix (janes-apparel) or the full myshopify.com URL (janes-apparel.myshopify.com, https://janes-apparel.myshopify.com).","multiple":false},"debug":{"name":"debug","type":"boolean","description":"Attaches a Node inspector","allowNo":false},"host":{"name":"host","type":"option","hidden":true,"multiple":false}},"args":[]},"hydrogen:g":{"id":"hydrogen:g","description":"Shortcut for `hydrogen generate`. See `hydrogen generate --help` for more information.","strict":false,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{},"args":[]},"hydrogen:init":{"id":"hydrogen:init","description":"Creates a new Hydrogen storefront.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false},"path":{"name":"path","type":"option","description":"The path to the directory of the new Hydrogen storefront.","multiple":false},"language":{"name":"language","type":"option","description":"Sets the template language to use. One of `js` or `ts`.","multiple":false},"template":{"name":"template","type":"option","description":"Sets the template to use. One of `demo-store` or `hello-world`.","multiple":false},"install-deps":{"name":"install-deps","type":"boolean","description":"Auto install dependencies using the active package manager","allowNo":true}},"args":[]},"hydrogen:link":{"id":"hydrogen:link","description":"Link a local project to one of your shop's Hydrogen storefronts.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false},"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"shop":{"name":"shop","type":"option","char":"s","description":"Shop URL. It can be the shop prefix (janes-apparel) or the full myshopify.com URL (janes-apparel.myshopify.com, https://janes-apparel.myshopify.com).","multiple":false},"storefront":{"name":"storefront","type":"option","char":"h","description":"The name of a Hydrogen Storefront (e.g. \"Jane's Apparel\")","multiple":false}},"args":[]},"hydrogen:list":{"id":"hydrogen:list","description":"Returns a list of Hydrogen storefronts available on a given shop.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"shop":{"name":"shop","type":"option","char":"s","description":"Shop URL. It can be the shop prefix (janes-apparel) or the full myshopify.com URL (janes-apparel.myshopify.com, https://janes-apparel.myshopify.com).","multiple":false}},"args":[]},"hydrogen:preview":{"id":"hydrogen:preview","description":"Runs a Hydrogen storefront in an Oxygen worker for production.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"port":{"name":"port","type":"option","description":"Port to run the server on.","multiple":false,"default":3000}},"args":[]},"hydrogen:shortcut":{"id":"hydrogen:shortcut","description":"Creates a global `h2` shortcut for the Hydrogen CLI","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{},"args":[]},"hydrogen:unlink":{"id":"hydrogen:unlink","description":"Unlink a local project from a Hydrogen storefront.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false}},"args":[]},"hydrogen:env:pull":{"id":"hydrogen:env:pull","description":"Populate your .env with variables from your Hydrogen storefront.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"shop":{"name":"shop","type":"option","char":"s","description":"Shop URL. It can be the shop prefix (janes-apparel) or the full myshopify.com URL (janes-apparel.myshopify.com, https://janes-apparel.myshopify.com).","multiple":false},"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false}},"args":[]},"hydrogen:generate:route":{"id":"hydrogen:generate:route","description":"Generates a standard Shopify route.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"adapter":{"name":"adapter","type":"option","description":"Remix adapter used in the route. The default is `@shopify/remix-oxygen`.","multiple":false},"typescript":{"name":"typescript","type":"boolean","description":"Generate TypeScript files","allowNo":false},"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false},"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false}},"args":[{"name":"route","description":"The route to generate. One of home,page,cart,products,collections,policies,robots,sitemap,account,all.","required":true,"options":["home","page","cart","products","collections","policies","robots","sitemap","account","all"]}]},"hydrogen:generate:routes":{"id":"hydrogen:generate:routes","description":"Generates all supported standard shopify routes.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"adapter":{"name":"adapter","type":"option","description":"Remix adapter used in the route. The default is `@shopify/remix-oxygen`.","multiple":false},"typescript":{"name":"typescript","type":"boolean","description":"Generate TypeScript files","allowNo":false},"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false},"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false}},"args":[]}}} \ No newline at end of file +{"version":"4.1.2","commands":{"hydrogen:build":{"id":"hydrogen:build","description":"Builds a Hydrogen storefront for production.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"sourcemap":{"name":"sourcemap","type":"boolean","description":"Generate sourcemaps for the build.","allowNo":false},"disable-route-warning":{"name":"disable-route-warning","type":"boolean","description":"Disable warning about missing standard routes.","allowNo":false},"base":{"name":"base","type":"option","hidden":true,"multiple":false},"entry":{"name":"entry","type":"option","hidden":true,"multiple":false},"target":{"name":"target","type":"option","hidden":true,"multiple":false}},"args":[]},"hydrogen:check":{"id":"hydrogen:check","description":"Returns diagnostic information about a Hydrogen storefront.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false}},"args":[{"name":"resource","description":"The resource to check. Currently only 'routes' is supported.","required":true,"options":["routes"]}]},"hydrogen:codegen-unstable":{"id":"hydrogen:codegen-unstable","description":"Generate types for the Storefront API queries found in your project.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"codegen-config-path":{"name":"codegen-config-path","type":"option","description":"Specify a path to a codegen configuration file. Defaults to `/codegen.ts` if it exists.","required":false,"multiple":false},"watch":{"name":"watch","type":"boolean","description":"Watch the project for changes to update types on file save.","required":false,"allowNo":false}},"args":[]},"hydrogen:dev":{"id":"hydrogen:dev","description":"Runs Hydrogen storefront in an Oxygen worker for development.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"port":{"name":"port","type":"option","description":"Port to run the server on.","multiple":false,"default":3000},"codegen-unstable":{"name":"codegen-unstable","type":"boolean","description":"Generate types for the Storefront API queries found in your project. It updates the types on file save.","required":false,"allowNo":false},"codegen-config-path":{"name":"codegen-config-path","type":"option","description":"Specify a path to a codegen configuration file. Defaults to `/codegen.ts` if it exists.","required":false,"multiple":false,"dependsOn":["codegen-unstable"]},"disable-virtual-routes":{"name":"disable-virtual-routes","type":"boolean","description":"Disable rendering fallback routes when a route file doesn't exist.","allowNo":false},"shop":{"name":"shop","type":"option","char":"s","description":"Shop URL. It can be the shop prefix (janes-apparel) or the full myshopify.com URL (janes-apparel.myshopify.com, https://janes-apparel.myshopify.com).","multiple":false},"debug":{"name":"debug","type":"boolean","description":"Attaches a Node inspector","allowNo":false},"host":{"name":"host","type":"option","hidden":true,"multiple":false},"env-branch":{"name":"env-branch","type":"option","char":"e","description":"Specify an environment's branch name when using remote environment variables.","hidden":true,"multiple":false}},"args":[]},"hydrogen:g":{"id":"hydrogen:g","description":"Shortcut for `hydrogen generate`. See `hydrogen generate --help` for more information.","strict":false,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{},"args":[]},"hydrogen:init":{"id":"hydrogen:init","description":"Creates a new Hydrogen storefront.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false},"path":{"name":"path","type":"option","description":"The path to the directory of the new Hydrogen storefront.","multiple":false},"language":{"name":"language","type":"option","description":"Sets the template language to use. One of `js` or `ts`.","multiple":false},"template":{"name":"template","type":"option","description":"Sets the template to use. One of `demo-store` or `hello-world`.","multiple":false},"install-deps":{"name":"install-deps","type":"boolean","description":"Auto install dependencies using the active package manager","allowNo":true}},"args":[]},"hydrogen:link":{"id":"hydrogen:link","description":"Link a local project to one of your shop's Hydrogen storefronts.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false},"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"shop":{"name":"shop","type":"option","char":"s","description":"Shop URL. It can be the shop prefix (janes-apparel) or the full myshopify.com URL (janes-apparel.myshopify.com, https://janes-apparel.myshopify.com).","multiple":false},"storefront":{"name":"storefront","type":"option","char":"h","description":"The name of a Hydrogen Storefront (e.g. \"Jane's Apparel\")","multiple":false}},"args":[]},"hydrogen:list":{"id":"hydrogen:list","description":"Returns a list of Hydrogen storefronts available on a given shop.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"shop":{"name":"shop","type":"option","char":"s","description":"Shop URL. It can be the shop prefix (janes-apparel) or the full myshopify.com URL (janes-apparel.myshopify.com, https://janes-apparel.myshopify.com).","multiple":false}},"args":[]},"hydrogen:preview":{"id":"hydrogen:preview","description":"Runs a Hydrogen storefront in an Oxygen worker for production.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"port":{"name":"port","type":"option","description":"Port to run the server on.","multiple":false,"default":3000}},"args":[]},"hydrogen:shortcut":{"id":"hydrogen:shortcut","description":"Creates a global `h2` shortcut for the Hydrogen CLI","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{},"args":[]},"hydrogen:unlink":{"id":"hydrogen:unlink","description":"Unlink a local project from a Hydrogen storefront.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false}},"args":[]},"hydrogen:env:pull":{"id":"hydrogen:env:pull","description":"Populate your .env with variables from your Hydrogen storefront.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","hidden":true,"aliases":[],"flags":{"env-branch":{"name":"env-branch","type":"option","char":"e","description":"Specify an environment's branch name when using remote environment variables.","hidden":true,"multiple":false},"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false},"shop":{"name":"shop","type":"option","char":"s","description":"Shop URL. It can be the shop prefix (janes-apparel) or the full myshopify.com URL (janes-apparel.myshopify.com, https://janes-apparel.myshopify.com).","multiple":false},"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false}},"args":[]},"hydrogen:generate:route":{"id":"hydrogen:generate:route","description":"Generates a standard Shopify route.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"adapter":{"name":"adapter","type":"option","description":"Remix adapter used in the route. The default is `@shopify/remix-oxygen`.","multiple":false},"typescript":{"name":"typescript","type":"boolean","description":"Generate TypeScript files","allowNo":false},"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false},"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false}},"args":[{"name":"route","description":"The route to generate. One of home,page,cart,products,collections,policies,robots,sitemap,account,all.","required":true,"options":["home","page","cart","products","collections","policies","robots","sitemap","account","all"]}]},"hydrogen:generate:routes":{"id":"hydrogen:generate:routes","description":"Generates all supported standard shopify routes.","strict":true,"pluginName":"@shopify/cli-hydrogen","pluginAlias":"@shopify/cli-hydrogen","pluginType":"core","aliases":[],"flags":{"adapter":{"name":"adapter","type":"option","description":"Remix adapter used in the route. The default is `@shopify/remix-oxygen`.","multiple":false},"typescript":{"name":"typescript","type":"boolean","description":"Generate TypeScript files","allowNo":false},"force":{"name":"force","type":"boolean","char":"f","description":"Overwrite the destination directory and files if they already exist.","allowNo":false},"path":{"name":"path","type":"option","description":"The path to the directory of the Hydrogen storefront. The default is the current directory.","multiple":false}},"args":[]}}} \ No newline at end of file diff --git a/packages/cli/src/commands/hydrogen/dev.ts b/packages/cli/src/commands/hydrogen/dev.ts index bc9b80322f..595f836a5c 100644 --- a/packages/cli/src/commands/hydrogen/dev.ts +++ b/packages/cli/src/commands/hydrogen/dev.ts @@ -50,6 +50,7 @@ export default class Dev extends Command { default: false, }), host: deprecated('--host')(), + ['env-branch']: commonFlags['env-branch'], }; async run(): Promise { @@ -71,6 +72,7 @@ async function runDev({ codegenConfigPath, disableVirtualRoutes, shop, + envBranch, debug = false, }: { port?: number; @@ -79,6 +81,7 @@ async function runDev({ codegenConfigPath?: string; disableVirtualRoutes?: boolean; shop?: string; + envBranch?: string; debug?: false; }) { if (!process.env.NODE_ENV) process.env.NODE_ENV = 'development'; @@ -109,7 +112,11 @@ async function runDev({ const hasLinkedStorefront = !!(await getConfig(root))?.storefront?.id; const environmentVariables = hasLinkedStorefront - ? await combinedEnvironmentVariables({root, shop}) + ? await combinedEnvironmentVariables({ + root, + shop, + envBranch, + }) : undefined; let miniOxygenStarted = false; diff --git a/packages/cli/src/commands/hydrogen/env/pull.test.ts b/packages/cli/src/commands/hydrogen/env/pull.test.ts index 74b57e2e88..3407779101 100644 --- a/packages/cli/src/commands/hydrogen/env/pull.test.ts +++ b/packages/cli/src/commands/hydrogen/env/pull.test.ts @@ -13,7 +13,6 @@ import {renderConfirmationPrompt} from '@shopify/cli-kit/node/ui'; import {getAdminSession} from '../../../lib/admin-session.js'; import {pullRemoteEnvironmentVariables} from '../../../lib/pull-environment-variables.js'; import {getConfig} from '../../../lib/shopify-config.js'; -import {linkStorefront} from '../link.js'; import {pullVariables} from './pull.js'; @@ -71,10 +70,11 @@ describe('pullVariables', () => { it('calls pullRemoteEnvironmentVariables', async () => { await inTemporaryDirectory(async (tmpDir) => { - await pullVariables({path: tmpDir}); + await pullVariables({path: tmpDir, envBranch: 'staging'}); expect(pullRemoteEnvironmentVariables).toHaveBeenCalledWith({ root: tmpDir, + envBranch: 'staging', }); }); }); diff --git a/packages/cli/src/commands/hydrogen/env/pull.ts b/packages/cli/src/commands/hydrogen/env/pull.ts index 36b9a638b0..7ad98c2be3 100644 --- a/packages/cli/src/commands/hydrogen/env/pull.ts +++ b/packages/cli/src/commands/hydrogen/env/pull.ts @@ -5,7 +5,7 @@ import {outputSuccess, outputWarn} from '@shopify/cli-kit/node/output'; import {fileExists, writeFile} from '@shopify/cli-kit/node/fs'; import {resolvePath} from '@shopify/cli-kit/node/path'; -import {commonFlags} from '../../../lib/flags.js'; +import {commonFlags, flagsToCamelObject} from '../../../lib/flags.js'; import {pullRemoteEnvironmentVariables} from '../../../lib/pull-environment-variables.js'; import {getConfig} from '../../../lib/shopify-config.js'; @@ -16,6 +16,7 @@ export default class Pull extends Command { static hidden = true; static flags = { + ['env-branch']: commonFlags['env-branch'], path: commonFlags.path, shop: commonFlags.shop, force: commonFlags.force, @@ -23,22 +24,29 @@ export default class Pull extends Command { async run(): Promise { const {flags} = await this.parse(Pull); - await pullVariables(flags); + await pullVariables({...flagsToCamelObject(flags)}); } } interface Flags { + envBranch?: string; force?: boolean; path?: string; shop?: string; } -export async function pullVariables({force, path, shop: flagShop}: Flags) { +export async function pullVariables({ + envBranch, + force, + path, + shop: flagShop, +}: Flags) { const actualPath = path ?? process.cwd(); const environmentVariables = await pullRemoteEnvironmentVariables({ root: actualPath, flagShop, + envBranch, }); if (!environmentVariables.length) { diff --git a/packages/cli/src/lib/combined-environment-variables.test.ts b/packages/cli/src/lib/combined-environment-variables.test.ts index 2b035bc555..cf463e8c42 100644 --- a/packages/cli/src/lib/combined-environment-variables.test.ts +++ b/packages/cli/src/lib/combined-environment-variables.test.ts @@ -35,9 +35,14 @@ describe('combinedEnvironmentVariables()', () => { test('calls pullRemoteEnvironmentVariables', async () => { await inTemporaryDirectory(async (tmpDir) => { - await combinedEnvironmentVariables({root: tmpDir, shop: 'my-shop'}); + await combinedEnvironmentVariables({ + envBranch: 'main', + root: tmpDir, + shop: 'my-shop', + }); expect(pullRemoteEnvironmentVariables).toHaveBeenCalledWith({ + envBranch: 'main', root: tmpDir, flagShop: 'my-shop', silent: true, diff --git a/packages/cli/src/lib/combined-environment-variables.ts b/packages/cli/src/lib/combined-environment-variables.ts index 59b78495c9..b539b69f1c 100644 --- a/packages/cli/src/lib/combined-environment-variables.ts +++ b/packages/cli/src/lib/combined-environment-variables.ts @@ -12,14 +12,20 @@ import {pullRemoteEnvironmentVariables} from './pull-environment-variables.js'; import {getConfig} from './shopify-config.js'; interface Arguments { + envBranch?: string; root: string; shop?: string; } -export async function combinedEnvironmentVariables({root, shop}: Arguments) { +export async function combinedEnvironmentVariables({ + envBranch, + root, + shop, +}: Arguments) { const remoteEnvironmentVariables = await pullRemoteEnvironmentVariables({ root, flagShop: shop, silent: true, + envBranch, }); const formattedRemoteVariables = remoteEnvironmentVariables?.reduce( diff --git a/packages/cli/src/lib/flags.ts b/packages/cli/src/lib/flags.ts index 87a4ff36f4..a77169b19a 100644 --- a/packages/cli/src/lib/flags.ts +++ b/packages/cli/src/lib/flags.ts @@ -29,6 +29,13 @@ export const commonFlags = { env: 'SHOPIFY_SHOP', parse: async (input) => normalizeStoreFqdn(input), }), + ['env-branch']: Flags.string({ + description: + "Specify an environment's branch name when using remote environment variables.", + env: 'SHOPIFY_HYDROGEN_ENVIRONMENT_BRANCH', + char: 'e', + hidden: true, + }), }; export function flagsToCamelObject(obj: Record) { diff --git a/packages/cli/src/lib/graphql/admin/pull-variables.ts b/packages/cli/src/lib/graphql/admin/pull-variables.ts index ee1f77923a..0f83a38cd5 100644 --- a/packages/cli/src/lib/graphql/admin/pull-variables.ts +++ b/packages/cli/src/lib/graphql/admin/pull-variables.ts @@ -1,8 +1,8 @@ export const PullVariablesQuery = `#graphql - query ListStorefronts($id: ID!) { + query PullVariables($id: ID!, $branch: String) { hydrogenStorefront(id: $id) { id - environmentVariables { + environmentVariables(branchName: $branch) { id isSecret key diff --git a/packages/cli/src/lib/pull-environment-variables.test.ts b/packages/cli/src/lib/pull-environment-variables.test.ts index 396bdfee0f..9efa185df0 100644 --- a/packages/cli/src/lib/pull-environment-variables.test.ts +++ b/packages/cli/src/lib/pull-environment-variables.test.ts @@ -78,13 +78,17 @@ describe('pullRemoteEnvironmentVariables', () => { it('makes a GraphQL call to fetch environment variables', async () => { await inTemporaryDirectory(async (tmpDir) => { - await pullRemoteEnvironmentVariables({root: tmpDir}); + await pullRemoteEnvironmentVariables({ + root: tmpDir, + envBranch: 'staging', + }); expect(adminRequest).toHaveBeenCalledWith( PullVariablesQuery, ADMIN_SESSION, { id: 'gid://shopify/HydrogenStorefront/2', + branch: 'staging', }, ); }); @@ -116,9 +120,7 @@ describe('pullRemoteEnvironmentVariables', () => { await pullRemoteEnvironmentVariables({root: tmpDir}); - expect(outputMock.info()).toMatch( - /No Preview environment variables found\./, - ); + expect(outputMock.info()).toMatch(/No environment variables found\./); }); }); diff --git a/packages/cli/src/lib/pull-environment-variables.ts b/packages/cli/src/lib/pull-environment-variables.ts index fdfb76ee33..2f54ab17ff 100644 --- a/packages/cli/src/lib/pull-environment-variables.ts +++ b/packages/cli/src/lib/pull-environment-variables.ts @@ -22,6 +22,7 @@ import { } from './graphql/admin/pull-variables.js'; interface Arguments { + envBranch?: string; root: string; /** * Optional shop override that developers would have passed using the --shop @@ -36,6 +37,7 @@ interface Arguments { } export async function pullRemoteEnvironmentVariables({ + envBranch, root, flagShop, silent, @@ -78,14 +80,16 @@ export async function pullRemoteEnvironmentVariables({ if (!silent) { outputInfo( - `Fetching Preview environment variables from ${configStorefront.title}...`, + `Fetching environment variables from ${configStorefront.title}...`, ); } + const result: PullVariablesSchema = await adminRequest( PullVariablesQuery, adminSession, { id: configStorefront.id, + branch: envBranch, }, ); @@ -117,7 +121,7 @@ export async function pullRemoteEnvironmentVariables({ if (!storefront.environmentVariables.length) { if (!silent) { - outputInfo(`No Preview environment variables found.`); + outputInfo(`No environment variables found.`); } return []; }