Skip to content
Open
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
11 changes: 10 additions & 1 deletion docs-shopify.dev/generated/generated_docs_data_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -8599,6 +8599,15 @@
"isOptional": true,
"environmentValue": "SHOPIFY_FLAG_FORCE"
},
{
"filePath": "docs-shopify.dev/commands/interfaces/theme-publish.interface.ts",
"syntaxKind": "PropertySignature",
"name": "-j, --json",
"value": "''",
"description": "Output the result as JSON. Automatically disables color output.",
"isOptional": true,
"environmentValue": "SHOPIFY_FLAG_JSON"
},
{
"filePath": "docs-shopify.dev/commands/interfaces/theme-publish.interface.ts",
"syntaxKind": "PropertySignature",
Expand All @@ -8618,7 +8627,7 @@
"environmentValue": "SHOPIFY_FLAG_THEME_ID"
}
],
"value": "export interface themepublish {\n /**\n * Alias of the Shopify account to use for authentication.\n * @environment SHOPIFY_FLAG_AUTH_ALIAS\n */\n '--auth-alias <value>'?: string\n\n /**\n * The environment to apply to the current command.\n * @environment SHOPIFY_FLAG_ENVIRONMENT\n */\n '-e, --environment <value>'?: string\n\n /**\n * Skip confirmation. Required if non interactive.\n * @environment SHOPIFY_FLAG_FORCE\n */\n '-f, --force'?: ''\n\n /**\n * Print the command's JSON schemas.\n * @environment SHOPIFY_FLAG_JSON_SCHEMA\n */\n '--json-schema'?: ''\n\n /**\n * Disable color output.\n * @environment SHOPIFY_FLAG_NO_COLOR\n */\n '--no-color'?: ''\n\n /**\n * Password generated from the Theme Access app or an Admin API token.\n * @environment SHOPIFY_CLI_THEME_TOKEN\n */\n '--password <value>'?: string\n\n /**\n * The path where you want to run the command. Defaults to the current working directory.\n * @environment SHOPIFY_FLAG_PATH\n */\n '--path <value>'?: string\n\n /**\n * Store URL. It can be the store prefix (example) or the full myshopify.com URL (example.myshopify.com, https://example.myshopify.com).\n * @environment SHOPIFY_FLAG_STORE\n */\n '-s, --store <value>'?: string\n\n /**\n * Theme ID or name of the remote theme. Required if non interactive.\n * @environment SHOPIFY_FLAG_THEME_ID\n */\n '-t, --theme <value>'?: string\n\n /**\n * Increase the verbosity of the output. May include sensitive data.\n * @environment SHOPIFY_FLAG_VERBOSE\n */\n '--verbose'?: ''\n}"
"value": "export interface themepublish {\n /**\n * Alias of the Shopify account to use for authentication.\n * @environment SHOPIFY_FLAG_AUTH_ALIAS\n */\n '--auth-alias <value>'?: string\n\n /**\n * The environment to apply to the current command.\n * @environment SHOPIFY_FLAG_ENVIRONMENT\n */\n '-e, --environment <value>'?: string\n\n /**\n * Skip confirmation. Required if non interactive.\n * @environment SHOPIFY_FLAG_FORCE\n */\n '-f, --force'?: ''\n\n /**\n * Output the result as JSON. Automatically disables color output.\n * @environment SHOPIFY_FLAG_JSON\n */\n '-j, --json'?: ''\n\n /**\n * Print the command's JSON schemas.\n * @environment SHOPIFY_FLAG_JSON_SCHEMA\n */\n '--json-schema'?: ''\n\n /**\n * Disable color output.\n * @environment SHOPIFY_FLAG_NO_COLOR\n */\n '--no-color'?: ''\n\n /**\n * Password generated from the Theme Access app or an Admin API token.\n * @environment SHOPIFY_CLI_THEME_TOKEN\n */\n '--password <value>'?: string\n\n /**\n * The path where you want to run the command. Defaults to the current working directory.\n * @environment SHOPIFY_FLAG_PATH\n */\n '--path <value>'?: string\n\n /**\n * Store URL. It can be the store prefix (example) or the full myshopify.com URL (example.myshopify.com, https://example.myshopify.com).\n * @environment SHOPIFY_FLAG_STORE\n */\n '-s, --store <value>'?: string\n\n /**\n * Theme ID or name of the remote theme. Required if non interactive.\n * @environment SHOPIFY_FLAG_THEME_ID\n */\n '-t, --theme <value>'?: string\n\n /**\n * Increase the verbosity of the output. May include sensitive data.\n * @environment SHOPIFY_FLAG_VERBOSE\n */\n '--verbose'?: ''\n}"
}
},
"themepull": {
Expand Down
104 changes: 103 additions & 1 deletion packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8952,7 +8952,7 @@ Set a remote theme as the live theme.

```
USAGE
$ shopify theme publish [--auth-alias <value>] [-e <value>...] [-f] [--json-schema] [--no-color] [--password
$ shopify theme publish [--auth-alias <value>] [-e <value>...] [-f] [-j] [--json-schema] [--no-color] [--password
<value>] [--path <value>] [-s <value>] [-t <value>] [--verbose]

FLAGS
Expand All @@ -8964,6 +8964,10 @@ FLAGS
Skip confirmation. Required if non interactive.
[env: SHOPIFY_FLAG_FORCE]

-j, --json
Output the result as JSON. Automatically disables color output.
[env: SHOPIFY_FLAG_JSON]

-s, --store=<value>
Store URL. It can be the store prefix (example) or the full myshopify.com URL (example.myshopify.com,
https://example.myshopify.com).
Expand Down Expand Up @@ -9010,6 +9014,104 @@ DESCRIPTION

If you want to publish your local theme, then you need to run `shopify theme push` first. You're asked to confirm that
you want to publish the specified theme. You can skip this confirmation using the `--force` flag.

Use `--json-schema` to print the result, error, and event schemas.

Output from `--json` conforms to the `ThemePublishResult` schema.

```json
{
"anyOf": [
{
"$ref": "#/definitions/ThemePublishEnvironment/properties/result"
},
{
"type": "object",
"properties": {
"environments": {
"type": "array",
"items": {
"$ref": "#/definitions/ThemePublishEnvironment"
}
}
},
"required": [
"environments"
],
"additionalProperties": false
}
],
"title": "ThemePublishResult",
"definitions": {
"PublishedTheme": {
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"role": {
"type": "string"
},
"processing": {
"type": "boolean"
},
"createdAtRuntime": {
"type": "boolean"
},
"src": {
"type": "string"
},
"shop": {
"type": "string"
}
},
"required": [
"id",
"name",
"role",
"processing",
"createdAtRuntime",
"shop"
],
"additionalProperties": false
},
"ThemePublishEnvironment": {
"type": "object",
"properties": {
"environment": {
"type": "string"
},
"result": {
"type": "object",
"properties": {
"status": {
"type": "string",
"const": "success"
},
"theme": {
"$ref": "#/definitions/PublishedTheme"
}
},
"required": [
"status",
"theme"
],
"additionalProperties": false
}
},
"required": [
"environment",
"result"
],
"additionalProperties": false
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
}
```
```

## `shopify theme pull`
Expand Down
11 changes: 10 additions & 1 deletion packages/cli/oclif.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11061,7 +11061,7 @@
"args": {
},
"customPluginName": "@shopify/theme",
"description": "Publishes an unpublished theme from your theme library.\n\nIf no theme ID is specified, then you're prompted to select the theme that you want to publish from the list of themes in your store.\n\nYou can run this command only in a directory that matches the \"default Shopify theme folder structure\" (https://shopify.dev/docs/themes/tools/cli#directory-structure).\n\nIf you want to publish your local theme, then you need to run `shopify theme push` first. You're asked to confirm that you want to publish the specified theme. You can skip this confirmation using the `--force` flag.",
"description": "Publishes an unpublished theme from your theme library.\n\nIf no theme ID is specified, then you're prompted to select the theme that you want to publish from the list of themes in your store.\n\nYou can run this command only in a directory that matches the \"default Shopify theme folder structure\" (https://shopify.dev/docs/themes/tools/cli#directory-structure).\n\nIf you want to publish your local theme, then you need to run `shopify theme push` first. You're asked to confirm that you want to publish the specified theme. You can skip this confirmation using the `--force` flag.\n\nUse `--json-schema` to print the result, error, and event schemas.\n\nOutput from `--json` conforms to the `ThemePublishResult` schema.\n\n```json\n{\n \"anyOf\": [\n {\n \"$ref\": \"#/definitions/ThemePublishEnvironment/properties/result\"\n },\n {\n \"type\": \"object\",\n \"properties\": {\n \"environments\": {\n \"type\": \"array\",\n \"items\": {\n \"$ref\": \"#/definitions/ThemePublishEnvironment\"\n }\n }\n },\n \"required\": [\n \"environments\"\n ],\n \"additionalProperties\": false\n }\n ],\n \"title\": \"ThemePublishResult\",\n \"definitions\": {\n \"PublishedTheme\": {\n \"type\": \"object\",\n \"properties\": {\n \"id\": {\n \"type\": \"number\"\n },\n \"name\": {\n \"type\": \"string\"\n },\n \"role\": {\n \"type\": \"string\"\n },\n \"processing\": {\n \"type\": \"boolean\"\n },\n \"createdAtRuntime\": {\n \"type\": \"boolean\"\n },\n \"src\": {\n \"type\": \"string\"\n },\n \"shop\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"id\",\n \"name\",\n \"role\",\n \"processing\",\n \"createdAtRuntime\",\n \"shop\"\n ],\n \"additionalProperties\": false\n },\n \"ThemePublishEnvironment\": {\n \"type\": \"object\",\n \"properties\": {\n \"environment\": {\n \"type\": \"string\"\n },\n \"result\": {\n \"type\": \"object\",\n \"properties\": {\n \"status\": {\n \"type\": \"string\",\n \"const\": \"success\"\n },\n \"theme\": {\n \"$ref\": \"#/definitions/PublishedTheme\"\n }\n },\n \"required\": [\n \"status\",\n \"theme\"\n ],\n \"additionalProperties\": false\n }\n },\n \"required\": [\n \"environment\",\n \"result\"\n ],\n \"additionalProperties\": false\n }\n },\n \"$schema\": \"http://json-schema.org/draft-07/schema#\"\n}\n```",
"descriptionWithMarkdown": "Publishes an unpublished theme from your theme library.\n\nIf no theme ID is specified, then you're prompted to select the theme that you want to publish from the list of themes in your store.\n\nYou can run this command only in a directory that matches the [default Shopify theme folder structure](https://shopify.dev/docs/themes/tools/cli#directory-structure).\n\nIf you want to publish your local theme, then you need to run `shopify theme push` first. You're asked to confirm that you want to publish the specified theme. You can skip this confirmation using the `--force` flag.",
"enableJsonFlag": false,
"flags": {
Expand Down Expand Up @@ -11090,6 +11090,15 @@
"name": "force",
"type": "boolean"
},
"json": {
"allowNo": false,
"char": "j",
"description": "Output the result as JSON. Automatically disables color output.",
"env": "SHOPIFY_FLAG_JSON",
"hidden": false,
"name": "json",
"type": "boolean"
},
"json-schema": {
"allowNo": false,
"description": "Print the command's JSON schemas.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const commandExceptions = [
'packages/theme/src/cli/commands/theme/package.ts',
'packages/theme/src/cli/commands/theme/preview.ts',
'packages/theme/src/cli/commands/theme/profile.ts',
'packages/theme/src/cli/commands/theme/publish.ts',
'packages/theme/src/cli/commands/theme/pull.ts',
'packages/theme/src/cli/commands/theme/push.ts',
'packages/theme/src/cli/commands/theme/rename.ts',
Expand Down
146 changes: 146 additions & 0 deletions packages/theme/src/cli/commands/theme/publish.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import Publish from './publish.js'
import {themePublishJsonOutputSchema} from '../../services/publish/types.js'
import {findOrSelectTheme} from '../../utilities/theme-selector.js'
import {Config} from '@oclif/core'
import {ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session'
import {themePublish} from '@shopify/cli-kit/node/themes/api'
import {withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output'
import {runWithCommandEventsForCommand} from '@shopify/cli-kit/node/command-events'
import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs'
import {joinPath} from '@shopify/cli-kit/node/path'
import {outputWarn} from '@shopify/cli-kit/node/output'
import {describe, expect, test, vi} from 'vitest'

vi.mock('@shopify/cli-kit/node/session')
vi.mock('@shopify/cli-kit/node/themes/api')
vi.mock('@shopify/cli-kit/node/metadata')
vi.mock('../../utilities/theme-selector.js')
vi.mock('../../utilities/theme-store.js', () => ({ensureThemeStore: ({store}: {store: string}) => store}))

const originalTheme = {id: 1, name: 'Original', role: 'unpublished', processing: false, createdAtRuntime: false}
const publishedTheme = {...originalTheme, role: 'live'}
const store = 'test.myshopify.com'

async function run(argv: string[]) {
const config = new Config({root: __dirname})
await config.load()
vi.mocked(ensureAuthenticatedThemes).mockImplementation(async (storeFqdn) => ({token: 'token', storeFqdn}))
return runWithCommandEventsForCommand(argv, () => new Publish(argv, config).run())
}

async function inEnvironments(run: () => Promise<void>) {
await inTemporaryDirectory(async (directory) => {
await writeFile(
joinPath(directory, 'shopify.theme.toml'),
`
[environments.first]
store = "first.myshopify.com"
password = "token"
[environments.second]
store = "second.myshopify.com"
password = "token"
`,
)
const cwd = vi.spyOn(process, 'cwd').mockReturnValue(directory)
try {
await run()
} finally {
cwd.mockRestore()
}
})
}

describe('theme publish JSON output', () => {
test('exposes its schema and JSON flag in help', () => {
expect(Publish.jsonOutputSchema).toBe(themePublishJsonOutputSchema)
expect(Publish.flags.json).toBeDefined()
expect(Publish.description).toContain('ThemePublishResult')
})

test.each([undefined, '', 'https://example.com/theme.zip'])(
'returns the updated theme and omits missing src (%s)',
async (src) => {
vi.mocked(findOrSelectTheme).mockResolvedValue(originalTheme)
vi.mocked(themePublish).mockResolvedValue({...publishedTheme, src})
await withCapturedStandardStreams(async ({stdout, stderr}) => {
await run(['--store', store, '--theme', '1', '--force', '--json'])
expect(JSON.parse(stdout())).toEqual({
status: 'success',
theme: {...publishedTheme, ...(src === undefined ? {} : {src}), shop: store},
})
expect(stderr()).toBe('')
})
},
)

test('routes diagnostics to stderr', async () => {
vi.mocked(findOrSelectTheme).mockResolvedValue(originalTheme)
vi.mocked(themePublish).mockImplementation(async () => {
outputWarn('Retrying request')
return publishedTheme
})
await withCapturedStandardStreams(async ({stdout, stderr}) => {
await run(['--store', store, '--theme', '1', '--force', '--json'])
expect(JSON.parse(stdout()).theme.role).toBe('live')
expect(JSON.parse(stderr())).toMatchObject({type: 'diagnostic', level: 'warning', message: 'Retrying request'})
})
})

test('leaves execution failures to the shared error handler without writing a result', async () => {
vi.mocked(findOrSelectTheme).mockResolvedValue(originalTheme)
vi.mocked(themePublish).mockRejectedValue(new Error('Publishing failed'))
await withCapturedStandardStreams(async ({stdout}) => {
await expect(run(['--store', store, '--theme', '1', '--force', '--json'])).rejects.toThrow('Publishing failed')
expect(stdout()).toBe('')
})
})

test.each(['none', 'partial', 'all'])('collects one document in configured order (%s failures)', async (failures) => {
vi.mocked(findOrSelectTheme).mockResolvedValue(originalTheme)
vi.mocked(themePublish).mockImplementation(async (_id, session) => {
if (failures === 'all' || (failures === 'partial' && session.storeFqdn.startsWith('first.'))) {
throw new Error('Publishing failed')
}
return publishedTheme
})
const exitCode = process.exitCode
await inEnvironments(async () => {
await withCapturedStandardStreams(async ({stdout, stderr}) => {
await run(['--environment', 'first', '--environment', 'second', '--theme', '1', '--force', '--json'])
const environments: string[] = []
if (failures === 'none') environments.push('first')
if (failures !== 'all') environments.push('second')
expect(JSON.parse(stdout())).toEqual({
environments: environments.map((environment) => ({
environment,
result: {status: 'success', theme: {...publishedTheme, shop: `${environment}.myshopify.com`}},
})),
})
if (failures === 'none') {
expect(stderr()).toBe('')
} else {
const errors = stderr()
.trim()
.split('\n')
.map((line) => JSON.parse(line))
expect(errors).toHaveLength(failures === 'all' ? 2 : 1)
expect(errors[0]).toMatchObject({type: 'diagnostic', level: 'error', code: 'theme-environment-failed'})
}
expect(process.exitCode).toBe(exitCode)
})
})
})

test.each([
{status: 'success', theme: {...publishedTheme, id: '1', shop: store}},
{status: 'success', theme: {...publishedTheme, shop: null}},
{status: 'success', theme: {...publishedTheme, src: false, shop: store}},
{
environments: [
{environment: 'first', result: {status: 'success', theme: {...publishedTheme, role: null, shop: store}}},
],
},
])('rejects malformed results %#', (result) => {
expect(() => themePublishJsonOutputSchema.validate(result)).toThrow()
})
})
Loading
Loading