Repository navigation
Add typed JSON output to theme package #8683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gonzaloriestra
wants to merge
5
commits into
gonzalo/json-theme-profile
from
gonzalo/json-theme-package
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
59b82bb
Add typed JSON output to theme package
gonzaloriestra a01b6cf
Validate absolute theme package artifact paths
gonzaloriestra 65d8ccf
Refresh theme package JSON schema documentation
gonzaloriestra fecc9df
Refresh generated theme package command docs
gonzaloriestra 5c64f25
Reject paths in theme package filenames
gonzaloriestra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import Package from './package.js' | ||
| import {themePackageJsonOutputSchema} from '../../services/package/types.js' | ||
| import {renderThemePackageResult} from '../../services/package/result.js' | ||
| import {Config} from '@oclif/core' | ||
| import {runWithCommandEventsForCommand} from '@shopify/cli-kit/node/command-events' | ||
| import {inTemporaryDirectory, mkdir, writeFile, fileExists} from '@shopify/cli-kit/node/fs' | ||
| import {joinPath, relativizePath, cwd} from '@shopify/cli-kit/node/path' | ||
| import {withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output' | ||
| import {renderSuccess} from '@shopify/cli-kit/node/ui' | ||
| import {expect, test, vi} from 'vitest' | ||
| // Native JSON paths must preserve Windows separators instead of pathe normalization. | ||
| // eslint-disable-next-line no-restricted-imports | ||
| import {resolve as nativePath} from 'node:path' | ||
|
|
||
| vi.mock('@shopify/cli-kit/node/ui') | ||
| vi.mock('@shopify/cli-kit/node/analytics') | ||
| vi.mock('@shopify/cli-kit/node/metadata') | ||
| vi.mock('@shopify/cli-kit/node/environments') | ||
|
|
||
| async function run(argv: string[]) { | ||
| const config = new Config({root: __dirname}) | ||
| await config.load() | ||
| await new Package(argv, config).run() | ||
| } | ||
|
|
||
| test.each([true, false])('writes the package receipt to stdout with version=%s', async (includeVersion) => { | ||
| await inTemporaryDirectory(async (directory) => { | ||
| await mkdir(joinPath(directory, 'config')) | ||
| await writeFile( | ||
| joinPath(directory, 'config/settings_schema.json'), | ||
| JSON.stringify([{name: 'theme_info', theme_name: 'Dawn', ...(includeVersion ? {theme_version: '1.0'} : {})}]), | ||
| ) | ||
| const path = joinPath(directory, includeVersion ? 'Dawn-1.0.zip' : 'Dawn.zip') | ||
|
|
||
| await withCapturedStandardStreams(async ({stdout, stderr}) => { | ||
| await runWithCommandEventsForCommand(['--json'], () => run(['--path', directory, '--json'])) | ||
| expect(stdout()).toBe(`${themePackageJsonOutputSchema.encode({path: nativePath(path)})}\n`) | ||
| expect(stderr()).toBe('') | ||
| }) | ||
| await expect(fileExists(path)).resolves.toBe(true) | ||
| expect(renderSuccess).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| test('preserves the text success banner', () => { | ||
| const path = joinPath(cwd(), 'Dawn.zip') | ||
| renderThemePackageResult({path}, 'text') | ||
| expect(renderSuccess).toHaveBeenCalledWith({ | ||
| body: ['Your local theme was packaged in', {filePath: relativizePath(path)}], | ||
| }) | ||
| }) | ||
|
|
||
| test('does not emit a result when packaging fails', async () => { | ||
| await inTemporaryDirectory(async (directory) => { | ||
| await withCapturedStandardStreams(async ({stdout}) => { | ||
| await expect(run(['--path', directory, '--json'])).rejects.toThrow( | ||
| 'Provide a config/settings_schema.json to package your theme.', | ||
| ) | ||
| expect(stdout()).toBe('') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| test('exposes the schema and rejects invalid package paths', () => { | ||
| expect(Package.jsonOutputSchema).toBe(themePackageJsonOutputSchema) | ||
| expect(Package.flags.json).toBeDefined() | ||
| expect(Package.description).toContain('--json-schema') | ||
| expect(() => themePackageJsonOutputSchema.validate({path: 42})).toThrow() | ||
| expect(() => themePackageJsonOutputSchema.validate({path: 'Dawn.zip'})).toThrow() | ||
| expect(() => themePackageJsonOutputSchema.validate({path: joinPath(cwd(), 'Dawn.zip'), extra: true})).toThrow() | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import {themePackageJsonOutputSchema, type ThemePackageResult} from './types.js' | ||
| import {outputResult} from '@shopify/cli-kit/node/output' | ||
| import {relativizePath} from '@shopify/cli-kit/node/path' | ||
| import {renderSuccess} from '@shopify/cli-kit/node/ui' | ||
| // Native JSON paths must preserve Windows separators instead of pathe normalization. | ||
| // eslint-disable-next-line no-restricted-imports | ||
| import {resolve as resolvePath} from 'node:path' | ||
|
|
||
| export function renderThemePackageResult(result: ThemePackageResult, format: 'text' | 'json'): void { | ||
| if (format === 'json') { | ||
| outputResult(themePackageJsonOutputSchema.encode({...result, path: resolvePath(result.path)})) | ||
| return | ||
| } | ||
|
|
||
| renderSuccess({body: ['Your local theme was packaged in', {filePath: relativizePath(result.path)}]}) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import {defineJsonOutputSchema, type InferJsonOutputSchema} from '@shopify/cli-kit/node/json-output-schema' | ||
| import {zod} from '@shopify/cli-kit/node/schema' | ||
| import {isAbsolutePath} from '@shopify/cli-kit/node/path' | ||
|
|
||
| export const themePackageJsonOutputSchema = defineJsonOutputSchema({ | ||
| name: 'ThemePackageResult', | ||
| schema: zod | ||
| .object({path: zod.string().refine(isAbsolutePath).describe('The absolute native path of the ZIP archive.')}) | ||
| .strict(), | ||
| }) | ||
|
|
||
| export type ThemePackageResult = InferJsonOutputSchema<typeof themePackageJsonOutputSchema> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make the archive writer reject output errors before emitting this receipt? With a valid
settings_schema.jsonand an existingDawn.zipdirectory,--jsonreturns{path: ".../Dawn.zip"}and exits 0, but no ZIP was written. I checked this with the real writer and files.zip()currently resolves when the output stream closes even when opening it fails. This needs to use the fatal JSON error path.🤖 Generated by the review-pr skill