From 1e0708ab37ee1533167e119a6e88def97747a17c 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 | 56 +++++++++++++++++++ packages/theme/src/cli/commands/theme/init.ts | 18 ++++-- 6 files changed, 95 insertions(+), 5 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 a63ead60ab6..54bb662779c 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 587e511fb83..51a4255abad 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -1934,6 +1934,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 78bfbc0e701..26495ae0b6d 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..4509d7b4f7d --- /dev/null +++ b/packages/theme/src/cli/commands/theme/init.test.ts @@ -0,0 +1,56 @@ +import Init, {DEFAULT_THEME_REPO_URL, FRAMEWORK_THEME_REPO_URL} 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(FRAMEWORK_THEME_REPO_URL, 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(DEFAULT_THEME_REPO_URL, 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..e69b628dc45 100644 --- a/packages/theme/src/cli/commands/theme/init.ts +++ b/packages/theme/src/cli/commands/theme/init.ts @@ -7,6 +7,9 @@ import {generateRandomNameForSubdirectory} from '@shopify/cli-kit/node/fs' import {renderTextPrompt} from '@shopify/cli-kit/node/ui' import {joinPath} from '@shopify/cli-kit/node/path' +export const DEFAULT_THEME_REPO_URL = 'https://github.com/Shopify/dawn.git' +export const FRAMEWORK_THEME_REPO_URL = 'https://github.com/Shopify/theme-blocks-accelerator.git' + export default class Init extends ThemeCommand { static summary = 'Clones a Git repository to use as a starting point for building a new theme.' @@ -34,9 +37,8 @@ export default class Init extends ThemeCommand { path: themeFlags.path, 'clone-url': Flags.string({ char: 'u', - default: 'https://github.com/Shopify/dawn.git', - description: - "The Git URL to clone from. Defaults to Shopify's example theme, Dawn: https://github.com/Shopify/dawn.git", + default: DEFAULT_THEME_REPO_URL, + description: `The Git URL to clone from. Defaults to Shopify's example theme, Dawn: ${DEFAULT_THEME_REPO_URL}`, env: 'SHOPIFY_FLAG_CLONE_URL', }), latest: Flags.boolean({ @@ -44,14 +46,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'] !== DEFAULT_THEME_REPO_URL + const repoUrl = !cloneUrlProvided && flags['dev-preview'] ? FRAMEWORK_THEME_REPO_URL : flags['clone-url'] + if (flags.latest) { await cloneRepoAndCheckoutLatestTag(repoUrl, destination) } else {