Skip to content

Commit 9921794

Browse files
Add typed JSON output to theme publish
1 parent 2a2bd4b commit 9921794

10 files changed

Lines changed: 397 additions & 28 deletions

File tree

‎docs-shopify.dev/generated/generated_docs_data_v2.json‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8590,6 +8590,15 @@
85908590
"isOptional": true,
85918591
"environmentValue": "SHOPIFY_FLAG_FORCE"
85928592
},
8593+
{
8594+
"filePath": "docs-shopify.dev/commands/interfaces/theme-publish.interface.ts",
8595+
"syntaxKind": "PropertySignature",
8596+
"name": "-j, --json",
8597+
"value": "''",
8598+
"description": "Output the result as JSON. Automatically disables color output.",
8599+
"isOptional": true,
8600+
"environmentValue": "SHOPIFY_FLAG_JSON"
8601+
},
85938602
{
85948603
"filePath": "docs-shopify.dev/commands/interfaces/theme-publish.interface.ts",
85958604
"syntaxKind": "PropertySignature",
@@ -8609,7 +8618,7 @@
86098618
"environmentValue": "SHOPIFY_FLAG_THEME_ID"
86108619
}
86118620
],
8612-
"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}"
8621+
"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}"
86138622
}
86148623
},
86158624
"themepull": {

‎packages/cli/README.md‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8948,7 +8948,7 @@ Set a remote theme as the live theme.
89488948
89498949
```
89508950
USAGE
8951-
$ shopify theme publish [--auth-alias <value>] [-e <value>...] [-f] [--json-schema] [--no-color] [--password
8951+
$ shopify theme publish [--auth-alias <value>] [-e <value>...] [-f] [-j] [--json-schema] [--no-color] [--password
89528952
<value>] [--path <value>] [-s <value>] [-t <value>] [--verbose]
89538953

89548954
FLAGS
@@ -8960,6 +8960,10 @@ FLAGS
89608960
Skip confirmation. Required if non interactive.
89618961
[env: SHOPIFY_FLAG_FORCE]
89628962

8963+
-j, --json
8964+
Output the result as JSON. Automatically disables color output.
8965+
[env: SHOPIFY_FLAG_JSON]
8966+
89638967
-s, --store=<value>
89648968
Store URL. It can be the store prefix (example) or the full myshopify.com URL (example.myshopify.com,
89658969
https://example.myshopify.com).
@@ -9006,6 +9010,104 @@ DESCRIPTION
90069010

90079011
If you want to publish your local theme, then you need to run `shopify theme push` first. You're asked to confirm that
90089012
you want to publish the specified theme. You can skip this confirmation using the `--force` flag.
9013+
9014+
Use `--json-schema` to print the result, error, and event schemas.
9015+
9016+
Output from `--json` conforms to the `ThemePublishResult` schema.
9017+
9018+
```json
9019+
{
9020+
"anyOf": [
9021+
{
9022+
"$ref": "#/definitions/ThemePublishEnvironment/properties/result"
9023+
},
9024+
{
9025+
"type": "object",
9026+
"properties": {
9027+
"environments": {
9028+
"type": "array",
9029+
"items": {
9030+
"$ref": "#/definitions/ThemePublishEnvironment"
9031+
}
9032+
}
9033+
},
9034+
"required": [
9035+
"environments"
9036+
],
9037+
"additionalProperties": false
9038+
}
9039+
],
9040+
"title": "ThemePublishResult",
9041+
"definitions": {
9042+
"PublishedTheme": {
9043+
"type": "object",
9044+
"properties": {
9045+
"id": {
9046+
"type": "number"
9047+
},
9048+
"name": {
9049+
"type": "string"
9050+
},
9051+
"role": {
9052+
"type": "string"
9053+
},
9054+
"processing": {
9055+
"type": "boolean"
9056+
},
9057+
"createdAtRuntime": {
9058+
"type": "boolean"
9059+
},
9060+
"src": {
9061+
"type": "string"
9062+
},
9063+
"shop": {
9064+
"type": "string"
9065+
}
9066+
},
9067+
"required": [
9068+
"id",
9069+
"name",
9070+
"role",
9071+
"processing",
9072+
"createdAtRuntime",
9073+
"shop"
9074+
],
9075+
"additionalProperties": false
9076+
},
9077+
"ThemePublishEnvironment": {
9078+
"type": "object",
9079+
"properties": {
9080+
"environment": {
9081+
"type": "string"
9082+
},
9083+
"result": {
9084+
"type": "object",
9085+
"properties": {
9086+
"status": {
9087+
"type": "string",
9088+
"const": "success"
9089+
},
9090+
"theme": {
9091+
"$ref": "#/definitions/PublishedTheme"
9092+
}
9093+
},
9094+
"required": [
9095+
"status",
9096+
"theme"
9097+
],
9098+
"additionalProperties": false
9099+
}
9100+
},
9101+
"required": [
9102+
"environment",
9103+
"result"
9104+
],
9105+
"additionalProperties": false
9106+
}
9107+
},
9108+
"$schema": "http://json-schema.org/draft-07/schema#"
9109+
}
9110+
```
90099111
```
90109112
90119113
## `shopify theme pull`

‎packages/cli/oclif.manifest.json‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11047,7 +11047,7 @@
1104711047
"args": {
1104811048
},
1104911049
"customPluginName": "@shopify/theme",
11050-
"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.",
11050+
"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```",
1105111051
"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.",
1105211052
"enableJsonFlag": false,
1105311053
"flags": {
@@ -11076,6 +11076,15 @@
1107611076
"name": "force",
1107711077
"type": "boolean"
1107811078
},
11079+
"json": {
11080+
"allowNo": false,
11081+
"char": "j",
11082+
"description": "Output the result as JSON. Automatically disables color output.",
11083+
"env": "SHOPIFY_FLAG_JSON",
11084+
"hidden": false,
11085+
"name": "json",
11086+
"type": "boolean"
11087+
},
1107911088
"json-schema": {
1108011089
"allowNo": false,
1108111090
"description": "Print the command's JSON schemas.",

‎packages/eslint-plugin-cli/rules/json-output-command-exceptions.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const commandExceptions = [
4949
'packages/theme/src/cli/commands/theme/package.ts',
5050
'packages/theme/src/cli/commands/theme/preview.ts',
5151
'packages/theme/src/cli/commands/theme/profile.ts',
52-
'packages/theme/src/cli/commands/theme/publish.ts',
5352
'packages/theme/src/cli/commands/theme/pull.ts',
5453
'packages/theme/src/cli/commands/theme/push.ts',
5554
'packages/theme/src/cli/commands/theme/rename.ts',
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import Publish from './publish.js'
2+
import {themePublishJsonOutputSchema} from '../../services/publish/types.js'
3+
import {findOrSelectTheme} from '../../utilities/theme-selector.js'
4+
import {Config} from '@oclif/core'
5+
import {ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session'
6+
import {themePublish} from '@shopify/cli-kit/node/themes/api'
7+
import {withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output'
8+
import {runWithCommandEventsForCommand} from '@shopify/cli-kit/node/command-events'
9+
import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs'
10+
import {joinPath} from '@shopify/cli-kit/node/path'
11+
import {outputWarn} from '@shopify/cli-kit/node/output'
12+
import {describe, expect, test, vi} from 'vitest'
13+
14+
vi.mock('@shopify/cli-kit/node/session')
15+
vi.mock('@shopify/cli-kit/node/themes/api')
16+
vi.mock('@shopify/cli-kit/node/metadata')
17+
vi.mock('../../utilities/theme-selector.js')
18+
vi.mock('../../utilities/theme-store.js', () => ({ensureThemeStore: ({store}: {store: string}) => store}))
19+
20+
const originalTheme = {id: 1, name: 'Original', role: 'unpublished', processing: false, createdAtRuntime: false}
21+
const publishedTheme = {...originalTheme, role: 'live'}
22+
const store = 'test.myshopify.com'
23+
24+
async function run(argv: string[]) {
25+
const config = new Config({root: __dirname})
26+
await config.load()
27+
vi.mocked(ensureAuthenticatedThemes).mockImplementation(async (storeFqdn) => ({token: 'token', storeFqdn}))
28+
return runWithCommandEventsForCommand(argv, () => new Publish(argv, config).run())
29+
}
30+
31+
async function inEnvironments(run: () => Promise<void>) {
32+
await inTemporaryDirectory(async (directory) => {
33+
await writeFile(
34+
joinPath(directory, 'shopify.theme.toml'),
35+
`
36+
[environments.first]
37+
store = "first.myshopify.com"
38+
password = "token"
39+
[environments.second]
40+
store = "second.myshopify.com"
41+
password = "token"
42+
`,
43+
)
44+
const cwd = vi.spyOn(process, 'cwd').mockReturnValue(directory)
45+
try {
46+
await run()
47+
} finally {
48+
cwd.mockRestore()
49+
}
50+
})
51+
}
52+
53+
describe('theme publish JSON output', () => {
54+
test('exposes its schema and JSON flag in help', () => {
55+
expect(Publish.jsonOutputSchema).toBe(themePublishJsonOutputSchema)
56+
expect(Publish.flags.json).toBeDefined()
57+
expect(Publish.description).toContain('ThemePublishResult')
58+
})
59+
60+
test.each([undefined, '', 'https://example.com/theme.zip'])(
61+
'returns the updated theme and omits missing src (%s)',
62+
async (src) => {
63+
vi.mocked(findOrSelectTheme).mockResolvedValue(originalTheme)
64+
vi.mocked(themePublish).mockResolvedValue({...publishedTheme, src})
65+
await withCapturedStandardStreams(async ({stdout, stderr}) => {
66+
await run(['--store', store, '--theme', '1', '--force', '--json'])
67+
expect(JSON.parse(stdout())).toEqual({
68+
status: 'success',
69+
theme: {...publishedTheme, ...(src === undefined ? {} : {src}), shop: store},
70+
})
71+
expect(stderr()).toBe('')
72+
})
73+
},
74+
)
75+
76+
test('routes diagnostics to stderr', async () => {
77+
vi.mocked(findOrSelectTheme).mockResolvedValue(originalTheme)
78+
vi.mocked(themePublish).mockImplementation(async () => {
79+
outputWarn('Retrying request')
80+
return publishedTheme
81+
})
82+
await withCapturedStandardStreams(async ({stdout, stderr}) => {
83+
await run(['--store', store, '--theme', '1', '--force', '--json'])
84+
expect(JSON.parse(stdout()).theme.role).toBe('live')
85+
expect(JSON.parse(stderr())).toMatchObject({type: 'diagnostic', level: 'warning', message: 'Retrying request'})
86+
})
87+
})
88+
89+
test('leaves execution failures to the shared error handler without writing a result', async () => {
90+
vi.mocked(findOrSelectTheme).mockResolvedValue(originalTheme)
91+
vi.mocked(themePublish).mockRejectedValue(new Error('Publishing failed'))
92+
await withCapturedStandardStreams(async ({stdout}) => {
93+
await expect(run(['--store', store, '--theme', '1', '--force', '--json'])).rejects.toThrow('Publishing failed')
94+
expect(stdout()).toBe('')
95+
})
96+
})
97+
98+
test.each(['none', 'partial', 'all'])('collects one document in configured order (%s failures)', async (failures) => {
99+
vi.mocked(findOrSelectTheme).mockResolvedValue(originalTheme)
100+
vi.mocked(themePublish).mockImplementation(async (_id, session) => {
101+
if (failures === 'all' || (failures === 'partial' && session.storeFqdn.startsWith('first.'))) {
102+
throw new Error('Publishing failed')
103+
}
104+
return publishedTheme
105+
})
106+
const exitCode = process.exitCode
107+
await inEnvironments(async () => {
108+
await withCapturedStandardStreams(async ({stdout, stderr}) => {
109+
await run(['--environment', 'first', '--environment', 'second', '--theme', '1', '--force', '--json'])
110+
const environments: string[] = []
111+
if (failures === 'none') environments.push('first')
112+
if (failures !== 'all') environments.push('second')
113+
expect(JSON.parse(stdout())).toEqual({
114+
environments: environments.map((environment) => ({
115+
environment,
116+
result: {status: 'success', theme: {...publishedTheme, shop: `${environment}.myshopify.com`}},
117+
})),
118+
})
119+
if (failures === 'none') {
120+
expect(stderr()).toBe('')
121+
} else {
122+
const errors = stderr()
123+
.trim()
124+
.split('\n')
125+
.map((line) => JSON.parse(line))
126+
expect(errors).toHaveLength(failures === 'all' ? 2 : 1)
127+
expect(errors[0]).toMatchObject({type: 'diagnostic', level: 'error', code: 'theme-environment-failed'})
128+
}
129+
expect(process.exitCode).toBe(exitCode)
130+
})
131+
})
132+
})
133+
134+
test.each([
135+
{status: 'success', theme: {...publishedTheme, id: '1', shop: store}},
136+
{status: 'success', theme: {...publishedTheme, shop: null}},
137+
{status: 'success', theme: {...publishedTheme, src: false, shop: store}},
138+
{
139+
environments: [
140+
{environment: 'first', result: {status: 'success', theme: {...publishedTheme, role: null, shop: store}}},
141+
],
142+
},
143+
])('rejects malformed results %#', (result) => {
144+
expect(() => themePublishJsonOutputSchema.validate(result)).toThrow()
145+
})
146+
})

0 commit comments

Comments
 (0)