Skip to content

Commit e56e3c3

Browse files
Abstract build steps to externalize the build configuration
1 parent 7e39fc5 commit e56e3c3

File tree

2 files changed

+48
-46
lines changed

2 files changed

+48
-46
lines changed

packages/app/src/cli/models/extensions/specifications/app_config_hosted_app_home.test.ts

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import spec from './app_config_hosted_app_home.js'
22
import {placeholderAppConfiguration} from '../../app/app.test-data.js'
3-
import {copyDirectoryContents} from '@shopify/cli-kit/node/fs'
4-
import {describe, expect, test, vi} from 'vitest'
5-
6-
vi.mock('@shopify/cli-kit/node/fs')
3+
import {describe, expect, test} from 'vitest'
74

85
describe('hosted_app_home', () => {
96
describe('transform', () => {
@@ -54,43 +51,43 @@ describe('hosted_app_home', () => {
5451
})
5552
})
5653

57-
describe('copyStaticAssets', () => {
58-
test('should copy static assets from source to output directory', async () => {
59-
vi.mocked(copyDirectoryContents).mockResolvedValue(undefined)
60-
const config = {static_root: 'public'}
61-
const directory = '/app/root'
62-
const outputPath = '/output/dist/bundle.js'
63-
64-
await spec.copyStaticAssets!(config, directory, outputPath)
65-
66-
expect(copyDirectoryContents).toHaveBeenCalledWith('/app/root/public', '/output/dist')
54+
describe('buildConfig', () => {
55+
test('should use copy_files mode', () => {
56+
expect(spec.buildConfig.mode).toBe('copy_files')
6757
})
6858

69-
test('should not copy assets when static_root is not provided', async () => {
70-
const config = {}
71-
const directory = '/app/root'
72-
const outputPath = '/output/dist/bundle.js'
73-
74-
await spec.copyStaticAssets!(config, directory, outputPath)
59+
test('should have copy-static-assets step with tomlKey entry', () => {
60+
if (spec.buildConfig.mode === 'none') {
61+
throw new Error('Expected build_steps mode')
62+
}
7563

76-
expect(copyDirectoryContents).not.toHaveBeenCalled()
64+
expect(spec.buildConfig.steps).toHaveLength(1)
65+
expect(spec.buildConfig.steps![0]).toMatchObject({
66+
id: 'copy-static-assets',
67+
displayName: 'Copy Static Assets',
68+
type: 'copy_files',
69+
config: {
70+
strategy: 'files',
71+
definition: {files: [{tomlKey: 'static_root'}]},
72+
},
73+
})
74+
expect(spec.buildConfig.stopOnError).toBe(true)
7775
})
7876

79-
test('should throw error when copy fails', async () => {
80-
vi.mocked(copyDirectoryContents).mockRejectedValue(new Error('Permission denied'))
81-
const config = {static_root: 'public'}
82-
const directory = '/app/root'
83-
const outputPath = '/output/dist/bundle.js'
77+
test('config should be serializable to JSON', () => {
78+
if (spec.buildConfig.mode === 'none') {
79+
throw new Error('Expected build_steps mode')
80+
}
8481

85-
await expect(spec.copyStaticAssets!(config, directory, outputPath)).rejects.toThrow(
86-
'Failed to copy static assets from /app/root/public to /output/dist: Permission denied',
87-
)
88-
})
89-
})
82+
const serialized = JSON.stringify(spec.buildConfig)
83+
expect(serialized).toBeDefined()
9084

91-
describe('buildConfig', () => {
92-
test('should have hosted_app_home build mode', () => {
93-
expect(spec.buildConfig).toEqual({mode: 'hosted_app_home'})
85+
const deserialized = JSON.parse(serialized)
86+
expect(deserialized.steps).toHaveLength(1)
87+
expect(deserialized.steps[0].config).toEqual({
88+
strategy: 'files',
89+
definition: {files: [{tomlKey: 'static_root'}]},
90+
})
9491
})
9592
})
9693

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import {BaseSchemaWithoutHandle} from '../schemas.js'
22
import {TransformationConfig, createConfigExtensionSpecification} from '../specification.js'
3-
import {copyDirectoryContents} from '@shopify/cli-kit/node/fs'
4-
import {dirname, joinPath} from '@shopify/cli-kit/node/path'
53
import {zod} from '@shopify/cli-kit/node/schema'
64

75
const HostedAppHomeSchema = BaseSchemaWithoutHandle.extend({
@@ -16,18 +14,25 @@ export const HostedAppHomeSpecIdentifier = 'hosted_app_home'
1614

1715
const hostedAppHomeSpec = createConfigExtensionSpecification({
1816
identifier: HostedAppHomeSpecIdentifier,
19-
buildConfig: {mode: 'hosted_app_home'} as const,
17+
buildConfig: {
18+
mode: 'copy_files',
19+
steps: [
20+
{
21+
id: 'copy-static-assets',
22+
displayName: 'Copy Static Assets',
23+
type: 'copy_files',
24+
config: {
25+
strategy: 'files',
26+
definition: {
27+
files: [{tomlKey: 'static_root'}],
28+
},
29+
},
30+
},
31+
],
32+
stopOnError: true,
33+
},
2034
schema: HostedAppHomeSchema,
2135
transformConfig: HostedAppHomeTransformConfig,
22-
copyStaticAssets: async (config, directory, outputPath) => {
23-
if (!config.static_root) return
24-
const sourceDir = joinPath(directory, config.static_root)
25-
const outputDir = dirname(outputPath)
26-
27-
return copyDirectoryContents(sourceDir, outputDir).catch((error) => {
28-
throw new Error(`Failed to copy static assets from ${sourceDir} to ${outputDir}: ${error.message}`)
29-
})
30-
},
3136
})
3237

3338
export default hostedAppHomeSpec

0 commit comments

Comments
 (0)