Repository navigation
Add typed JSON output to Hydrogen build tooling #4096
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@shopify/cli-hydrogen': patch | ||
| --- | ||
|
|
||
| Resolve symlinked project paths consistently during client and server builds. |
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,5 @@ | ||
| --- | ||
| '@shopify/cli-hydrogen': minor | ||
| --- | ||
|
|
||
| Add `--json` output to `shopify hydrogen build`, `shopify hydrogen check routes`, and `shopify hydrogen codegen` so scripts can read structured command results. |
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,264 @@ | ||
| import {afterEach, expect, it, vi} from 'vitest'; | ||
| import {realpath} from 'node:fs/promises'; | ||
| import {outputInfo} from '@shopify/cli-kit/node/output'; | ||
| import {captureJsonOutput} from '../../../tests/output.js'; | ||
| import {getRemixConfig} from '../../lib/remix-config.js'; | ||
| import {codegen} from '../../lib/codegen.js'; | ||
| import Check, {runCheckRoutes} from './check.js'; | ||
| import Codegen, {runCodegen} from './codegen.js'; | ||
| import Build from './build.js'; | ||
| import {Parser} from '@oclif/core'; | ||
| import ShopifyCommand from '@shopify/cli-kit/node/base-command'; | ||
| import {runWithCommandEventsForCommand} from '@shopify/cli-kit/node/command-events'; | ||
| import {isHydrogenProject} from '../../lib/hydrogen-command-policy.js'; | ||
| import {getViteConfig, isViteProject} from '../../lib/vite-config.js'; | ||
| import {importVite} from '../../lib/import-utils.js'; | ||
| import {fileSize} from '@shopify/cli-kit/node/fs'; | ||
| import {withCapturedStandardStreams} from '@shopify/cli-kit/node/testing/output'; | ||
| import {writeJsonResult} from '../../lib/json-output.js'; | ||
|
|
||
| vi.mock('../../lib/remix-config.js', () => ({ | ||
| getRemixConfig: vi.fn(), | ||
| getProjectPaths: () => ({root: '/project'}), | ||
| })); | ||
| vi.mock('../../lib/codegen.js', () => ({ | ||
| codegen: vi.fn(), | ||
| spawnCodegenProcess: vi.fn(), | ||
| })); | ||
| vi.mock('../../lib/vite-config.js'); | ||
| vi.mock('../../lib/hydrogen-command-policy.js'); | ||
| vi.mock('../../lib/import-utils.js'); | ||
| vi.mock('node:fs/promises', async (original) => ({ | ||
| ...(await original<typeof import('node:fs/promises')>()), | ||
| realpath: vi.fn(), | ||
| })); | ||
| vi.mock('@shopify/cli-kit/node/fs', async (original) => ({ | ||
| ...(await original<typeof import('@shopify/cli-kit/node/fs')>()), | ||
| removeFile: vi.fn(), | ||
| fileSize: vi.fn(), | ||
| })); | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| it('encodes missing and reserved routes in one document without text banners', async () => { | ||
| vi.mocked(getRemixConfig).mockResolvedValue({ | ||
| routes: {root: {id: 'root'}, cdn: {id: 'cdn', path: 'cdn/private'}}, | ||
| } as any); | ||
| const {stdout, stderr} = await captureJsonOutput(() => | ||
| runCheckRoutes({directory: '/project'}), | ||
| ); | ||
| expect(JSON.parse(stdout)).toMatchObject({ | ||
| missingRoutes: expect.arrayContaining(['cart']), | ||
| reservedRoutes: ['cdn/private'], | ||
| }); | ||
| expect(stderr).toBe(''); | ||
| }); | ||
|
|
||
| it('encodes generated files and preserves diagnostics on stderr', async () => { | ||
| vi.mocked(getRemixConfig).mockResolvedValue({ | ||
| rootDirectory: '/project', | ||
| } as any); | ||
| vi.mocked(codegen).mockImplementation(async () => { | ||
| outputInfo('Generating types'); | ||
| return {'storefrontapi.generated.d.ts': ['app/**/*.tsx']}; | ||
| }); | ||
| const {stdout, stderr} = await captureJsonOutput(() => | ||
| runCodegen({directory: '/project'}), | ||
| ); | ||
| expect(JSON.parse(stdout)).toEqual({ | ||
| files: [ | ||
| { | ||
| path: '/project/storefrontapi.generated.d.ts', | ||
| sources: ['app/**/*.tsx'], | ||
| }, | ||
| ], | ||
| }); | ||
| expect(JSON.parse(stderr)).toMatchObject({ | ||
| type: 'diagnostic', | ||
| message: 'Generating types', | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps codegen failures on the fatal-error path', async () => { | ||
| vi.mocked(codegen).mockRejectedValue(new Error('Invalid query')); | ||
| const {stdout} = await captureJsonOutput(async () => { | ||
| await expect(runCodegen({directory: '/project'})).rejects.toThrow( | ||
| 'Invalid query', | ||
| ); | ||
| }); | ||
| expect(stdout).toBe(''); | ||
| }); | ||
|
|
||
| it('encodes build output paths through the real writer', async () => { | ||
| const result = { | ||
| directory: '/project', | ||
| clientDirectory: '/project/dist/client', | ||
| serverDirectory: '/project/dist/server', | ||
| serverPath: '/project/dist/server/index.js', | ||
| }; | ||
| const {stdout} = await captureJsonOutput(() => | ||
| writeJsonResult(Build.jsonOutputSchema, result), | ||
| ); | ||
| expect(JSON.parse(stdout)).toEqual(result); | ||
| expect(() => | ||
| Build.jsonOutputSchema.encode({...result, serverPath: null} as any), | ||
| ).toThrow(); | ||
| }); | ||
|
|
||
| it.each([Build, Codegen, Check])( | ||
| 'declares JSON flags and discoverable schemas: %s', | ||
| (command) => { | ||
| expect(command.flags.json).toBeDefined(); | ||
| expect(command.description).toContain(command.jsonOutputSchema.name); | ||
| }, | ||
| ); | ||
|
|
||
| it.each([Build, Codegen])( | ||
| 'rejects JSON watch mode before execution: %s', | ||
| async (Command) => { | ||
| const command = new Command([], {} as any); | ||
| vi.spyOn(command as any, 'parse').mockResolvedValue({ | ||
| flags: {json: true, watch: true}, | ||
| }); | ||
| await expect(command.run()).rejects.toThrow( | ||
| '--json cannot be combined with --watch', | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| it('rejects invalid fields and preserves empty collections', () => { | ||
| expect(() => | ||
| Check.jsonOutputSchema.encode({ | ||
| valid: false, | ||
| missingRoutes: [1], | ||
| reservedRoutes: [], | ||
| } as any), | ||
| ).toThrow(); | ||
| expect(() => | ||
| Codegen.jsonOutputSchema.encode({ | ||
| files: [{path: '/project/types', sources: 1}], | ||
| } as any), | ||
| ).toThrow(); | ||
| expect( | ||
| JSON.parse( | ||
| Check.jsonOutputSchema.encode({ | ||
| valid: true, | ||
| missingRoutes: [], | ||
| reservedRoutes: [], | ||
| }), | ||
| ), | ||
| ).toEqual({valid: true, missingRoutes: [], reservedRoutes: []}); | ||
| expect(JSON.parse(Codegen.jsonOutputSchema.encode({files: []}))).toEqual({ | ||
| files: [], | ||
| }); | ||
| }); | ||
|
|
||
| it('honors the parsed Codegen JSON flag without ambient JSON mode', async () => { | ||
| vi.stubEnv('SHOPIFY_FLAG_JSON', '0'); | ||
| vi.mocked(getRemixConfig).mockResolvedValue({ | ||
| rootDirectory: '/project', | ||
| } as any); | ||
| vi.mocked(codegen).mockResolvedValue({'types.d.ts': ['app/**/*.tsx']}); | ||
| const command = new Codegen([], {} as any); | ||
| vi.spyOn(command as any, 'parse').mockResolvedValue({ | ||
| flags: {json: true, path: '/project'}, | ||
| }); | ||
| await withCapturedStandardStreams(async ({stdout}) => { | ||
| await command.run(); | ||
| expect(JSON.parse(stdout())).toEqual({ | ||
| files: [{path: '/project/types.d.ts', sources: ['app/**/*.tsx']}], | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it.each(['--json', 'True', 'y'])( | ||
| 'runs Build with clean JSON diagnostics and no Vite progress: %s', | ||
| async (mode) => { | ||
| vi.mocked(realpath).mockResolvedValue('/project'); | ||
| vi.mocked(isViteProject).mockResolvedValue(true); | ||
| vi.mocked(getViteConfig).mockResolvedValue({ | ||
| userViteConfig: {}, | ||
| remixConfig: {appDirectory: '/project/app'}, | ||
| clientOutDir: '/project/dist/client', | ||
| serverOutDir: '/project/dist/server', | ||
| serverOutFile: '/project/dist/server/index.js', | ||
| } as any); | ||
| vi.mocked(fileSize).mockResolvedValue(100); | ||
| const build = vi.fn(async (config) => { | ||
| config.customLogger.info('\u001b[32mBuild info\u001b[0m'); | ||
| config.customLogger.warn('\u001b[33mBuild warning\u001b[0m'); | ||
| config.customLogger.error('\u001b[31mBuild error\u001b[0m'); | ||
| return {}; | ||
| }); | ||
| vi.mocked(importVite).mockResolvedValue({ | ||
| createLogger: () => ({}), | ||
| build, | ||
| } as any); | ||
| const argv = mode === '--json' ? [mode] : []; | ||
| vi.stubEnv('SHOPIFY_FLAG_JSON', mode === '--json' ? undefined : mode); | ||
| const parsed = await Parser.parse(argv, {flags: Build.flags}); | ||
| expect(parsed.flags.json).toBe(true); | ||
| const command = new Build(argv, {} as any); | ||
| command.id = 'hydrogen:build'; | ||
| vi.mocked(isHydrogenProject).mockReturnValue(true); | ||
| vi.spyOn(ShopifyCommand.prototype as any, 'init').mockResolvedValue( | ||
| undefined, | ||
| ); | ||
| vi.spyOn(command as any, 'removeEnvVar').mockImplementation(() => {}); | ||
| vi.spyOn(command as any, 'parse').mockResolvedValue({ | ||
| flags: { | ||
| ...parsed.flags, | ||
| path: '/project', | ||
| 'lockfile-check': false, | ||
| 'bundle-stats': false, | ||
| 'disable-route-warning': true, | ||
| }, | ||
| }); | ||
| const exit = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation(() => undefined as never); | ||
| const {stdout, stderr} = await withCapturedStandardStreams( | ||
| async (streams) => { | ||
| // Match the launcher's context, including its text mode for mixed-case env values. | ||
| await runWithCommandEventsForCommand(argv, () => command._run()); | ||
| return {stdout: streams.stdout(), stderr: streams.stderr()}; | ||
| }, | ||
| ); | ||
| expect(JSON.parse(stdout)).toEqual({ | ||
| directory: '/project', | ||
| clientDirectory: '/project/dist/client', | ||
| serverDirectory: '/project/dist/server', | ||
| serverPath: '/project/dist/server/index.js', | ||
| }); | ||
| expect(build).toHaveBeenCalledTimes(2); | ||
| for (const [config] of build.mock.calls) | ||
| expect(config.logLevel).toBe('warn'); | ||
| expect( | ||
| stderr | ||
| .trim() | ||
| .split('\n') | ||
| .map((line) => JSON.parse(line)), | ||
| ).toEqual( | ||
| Array.from({length: 2}, () => [ | ||
| expect.objectContaining({ | ||
| type: 'diagnostic', | ||
| level: 'info', | ||
| message: 'Build info', | ||
| }), | ||
| expect.objectContaining({ | ||
| type: 'diagnostic', | ||
| level: 'warning', | ||
| message: 'Build warning', | ||
| }), | ||
| expect.objectContaining({ | ||
| type: 'diagnostic', | ||
| level: 'error', | ||
| message: 'Build error', | ||
| }), | ||
| ]).flat(), | ||
| ); | ||
| expect(exit).toHaveBeenCalledWith(0); | ||
| }, | ||
| ); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.