From 4ed31c829e35ef2e92573aa198f358b072f2654d Mon Sep 17 00:00:00 2001 From: James Meng Date: Wed, 11 Dec 2024 10:27:56 -0800 Subject: [PATCH] Add --dev-preview flag to theme init for framework theme support --- .../interfaces/theme-init.interface.ts | 6 ++ .../generated/generated_docs_data.json | 11 +++- packages/cli/README.md | 1 + packages/cli/oclif.manifest.json | 8 +++ .../theme/src/cli/commands/theme/init.test.ts | 59 +++++++++++++++++++ packages/theme/src/cli/commands/theme/init.ts | 10 +++- 6 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 packages/theme/src/cli/commands/theme/init.test.ts diff --git a/docs-shopify.dev/commands/interfaces/theme-init.interface.ts b/docs-shopify.dev/commands/interfaces/theme-init.interface.ts index 5e590925a64..8c7ef6c2047 100644 --- a/docs-shopify.dev/commands/interfaces/theme-init.interface.ts +++ b/docs-shopify.dev/commands/interfaces/theme-init.interface.ts @@ -6,6 +6,12 @@ export interface themeinit { */ '-u, --clone-url '?: string + /** + * Uses the new framework theme as the default clone-url + * @environment SHOPIFY_FLAG_CLONE_URL + */ + '-p, --dev-preview'?: '' + /** * Downloads the latest release of the `clone-url` * @environment SHOPIFY_FLAG_LATEST diff --git a/docs-shopify.dev/generated/generated_docs_data.json b/docs-shopify.dev/generated/generated_docs_data.json index c725ab97c80..3abf5db2753 100644 --- a/docs-shopify.dev/generated/generated_docs_data.json +++ b/docs-shopify.dev/generated/generated_docs_data.json @@ -5337,6 +5337,15 @@ "isOptional": true, "environmentValue": "SHOPIFY_FLAG_LATEST" }, + { + "filePath": "docs-shopify.dev/commands/interfaces/theme-init.interface.ts", + "syntaxKind": "PropertySignature", + "name": "-p, --dev-preview", + "value": "\"\"", + "description": "Uses the new framework theme as the default clone-url", + "isOptional": true, + "environmentValue": "SHOPIFY_FLAG_CLONE_URL" + }, { "filePath": "docs-shopify.dev/commands/interfaces/theme-init.interface.ts", "syntaxKind": "PropertySignature", @@ -5347,7 +5356,7 @@ "environmentValue": "SHOPIFY_FLAG_CLONE_URL" } ], - "value": "export interface themeinit {\n /**\n * The Git URL to clone from. Defaults to Shopify's example theme, Dawn: https://github.com/Shopify/dawn.git\n * @environment SHOPIFY_FLAG_CLONE_URL\n */\n '-u, --clone-url '?: string\n\n /**\n * Downloads the latest release of the `clone-url`\n * @environment SHOPIFY_FLAG_LATEST\n */\n '-l, --latest'?: ''\n\n /**\n * Disable color output.\n * @environment SHOPIFY_FLAG_NO_COLOR\n */\n '--no-color'?: ''\n\n /**\n * The path to your theme directory.\n * @environment SHOPIFY_FLAG_PATH\n */\n '--path '?: string\n\n /**\n * Increase the verbosity of the output.\n * @environment SHOPIFY_FLAG_VERBOSE\n */\n '--verbose'?: ''\n}" + "value": "export interface themeinit {\n /**\n * The Git URL to clone from. Defaults to Shopify's example theme, Dawn: https://github.com/Shopify/dawn.git\n * @environment SHOPIFY_FLAG_CLONE_URL\n */\n '-u, --clone-url '?: string\n\n /**\n * Uses the new framework theme as the default clone-url\n * @environment SHOPIFY_FLAG_CLONE_URL\n */\n '-p, --dev-preview'?: ''\n\n /**\n * Downloads the latest release of the `clone-url`\n * @environment SHOPIFY_FLAG_LATEST\n */\n '-l, --latest'?: ''\n\n /**\n * Disable color output.\n * @environment SHOPIFY_FLAG_NO_COLOR\n */\n '--no-color'?: ''\n\n /**\n * The path to your theme directory.\n * @environment SHOPIFY_FLAG_PATH\n */\n '--path '?: string\n\n /**\n * Increase the verbosity of the output.\n * @environment SHOPIFY_FLAG_VERBOSE\n */\n '--verbose'?: ''\n}" } } } diff --git a/packages/cli/README.md b/packages/cli/README.md index ca7521e5ea2..c9c7b72b2bb 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -1933,6 +1933,7 @@ ARGUMENTS FLAGS -l, --latest Downloads the latest release of the `clone-url` + -p, --dev-preview Uses the new framework theme as the default clone-url -u, --clone-url= [default: https://github.com/Shopify/dawn.git] The Git URL to clone from. Defaults to Shopify's example theme, Dawn: https://github.com/Shopify/dawn.git --no-color Disable color output. diff --git a/packages/cli/oclif.manifest.json b/packages/cli/oclif.manifest.json index 9fdedb43b58..2f5173012d2 100644 --- a/packages/cli/oclif.manifest.json +++ b/packages/cli/oclif.manifest.json @@ -5427,6 +5427,14 @@ "name": "clone-url", "type": "option" }, + "dev-preview": { + "allowNo": false, + "char": "p", + "description": "Uses the new framework theme as the default clone-url", + "env": "SHOPIFY_FLAG_CLONE_URL", + "name": "dev-preview", + "type": "boolean" + }, "latest": { "allowNo": false, "char": "l", diff --git a/packages/theme/src/cli/commands/theme/init.test.ts b/packages/theme/src/cli/commands/theme/init.test.ts new file mode 100644 index 00000000000..53afa43782c --- /dev/null +++ b/packages/theme/src/cli/commands/theme/init.test.ts @@ -0,0 +1,59 @@ +import Init from './init.js' +import {cloneRepo, cloneRepoAndCheckoutLatestTag} from '../../services/init.js' +import {describe, expect, vi, test} from 'vitest' +import {Config} from '@oclif/core' + +vi.mock('../../services/init.js') +vi.mock('@shopify/cli-kit/node/ui') + +const CommandConfig = new Config({root: './'}) + +describe('Init', () => { + const path = '.' + + async function run(argv: string[]) { + await CommandConfig.load() + const init = new Init([`--path=${path}`, ...argv], CommandConfig) + return init.run() + } + + describe('dev-preview flag', () => { + test('uses framework theme when dev-preview flag is true and default clone-url is not provided', async () => { + // Given + const flags = ['--dev-preview'] + + // When + await run(flags) + + // Then + expect(cloneRepo).toHaveBeenCalledWith('https://github.com/Shopify/framework-theme.git', expect.any(String)) + }) + + test('uses provided clone-url when custom url is provided, regardless of dev-preview flag', async () => { + // Given + const flags = ['--dev-preview', '--clone-url=https://github.com/org/theme.git'] + + // When + await run(flags) + + // Then + expect(cloneRepo).toHaveBeenCalledWith('https://github.com/org/theme.git', expect.any(String)) + }) + }) + + describe('latest flag', () => { + test('uses cloneRepoAndCheckoutLatestTag when latest flag is true', async () => { + // Given + const flags = ['--latest'] + + // When + await run(flags) + + // Then + expect(cloneRepoAndCheckoutLatestTag).toHaveBeenCalledWith( + 'https://github.com/Shopify/dawn.git', + expect.any(String), + ) + }) + }) +}) diff --git a/packages/theme/src/cli/commands/theme/init.ts b/packages/theme/src/cli/commands/theme/init.ts index 0af4ea3cbe1..5881e50f398 100644 --- a/packages/theme/src/cli/commands/theme/init.ts +++ b/packages/theme/src/cli/commands/theme/init.ts @@ -44,14 +44,22 @@ export default class Init extends ThemeCommand { description: 'Downloads the latest release of the `clone-url`', env: 'SHOPIFY_FLAG_LATEST', }), + 'dev-preview': Flags.boolean({ + char: 'p', + default: false, + description: 'Uses the new framework theme as the default clone-url', + env: 'SHOPIFY_FLAG_CLONE_URL', + }), } async run(): Promise { const {args, flags} = await this.parse(Init) const name = args.name || (await this.promptName(flags.path)) - const repoUrl = flags['clone-url'] const destination = joinPath(flags.path, name) + const cloneUrlProvided = flags['clone-url'] !== 'https://github.com/Shopify/dawn.git' + const repoUrl = !cloneUrlProvided && flags['dev-preview'] ? FRAMEWORK_THEME_REPO_URL : flags['clone-url'] + if (flags.latest) { await cloneRepoAndCheckoutLatestTag(repoUrl, destination) } else {