|
2 | 2 | import {authFixture} from './auth.js' |
3 | 3 | import {getLastPageStatus, isVisibleWithin, navigateToDashboard, refreshIfPageError} from './browser.js' |
4 | 4 | import {CLI_TIMEOUT, BROWSER_TIMEOUT} from './constants.js' |
5 | | -import {updateTomlValues} from '@shopify/toml-patch' |
6 | 5 | import * as toml from '@iarna/toml' |
7 | 6 | import * as path from 'path' |
8 | 7 | import * as fs from 'fs' |
@@ -112,17 +111,43 @@ export function extractClientId(appDir: string): string { |
112 | 111 | } |
113 | 112 |
|
114 | 113 | /** |
115 | | - * Overwrite a created app's shopify.app.toml with a fixture TOML template. |
116 | | - * Uses toml-patch to surgically set client_id and name while |
117 | | - * preserving comments and formatting in the fixture file. |
| 114 | + * Merge an E2E TOML fixture into the app config generated by the template. |
| 115 | + * Template-owned fields not mentioned by the fixture are preserved. |
118 | 116 | */ |
| 117 | +type TomlTable = Parameters<typeof toml.stringify>[0] |
| 118 | + |
| 119 | +function isTomlTable(value: unknown): value is TomlTable { |
| 120 | + return typeof value === 'object' && value !== null && !Array.isArray(value) && !(value instanceof Date) |
| 121 | +} |
| 122 | + |
| 123 | +function mergeTomlTables(base: TomlTable, overlay: TomlTable): TomlTable { |
| 124 | + const merged: TomlTable = {...base} |
| 125 | + for (const [key, overlayValue] of Object.entries(overlay)) { |
| 126 | + const baseValue = merged[key] |
| 127 | + merged[key] = |
| 128 | + isTomlTable(baseValue) && isTomlTable(overlayValue) ? mergeTomlTables(baseValue, overlayValue) : overlayValue |
| 129 | + } |
| 130 | + return merged |
| 131 | +} |
| 132 | + |
| 133 | +export function mergeFixtureToml(generatedTomlContent: string, fixtureTomlContent: string, name: string): string { |
| 134 | + const generated = toml.parse(generatedTomlContent) |
| 135 | + const clientId = generated.client_id as string | undefined |
| 136 | + if (!clientId) { |
| 137 | + throw new Error('Could not find client_id in generated shopify.app.toml') |
| 138 | + } |
| 139 | + |
| 140 | + const fixture = toml.parse(fixtureTomlContent) |
| 141 | + fixture.client_id = clientId |
| 142 | + fixture.name = name |
| 143 | + |
| 144 | + return toml.stringify(mergeTomlTables(generated, fixture)) |
| 145 | +} |
| 146 | + |
119 | 147 | export function injectFixtureToml(appDir: string, fixtureTomlContent: string, name: string): void { |
120 | | - const clientId = extractClientId(appDir) |
121 | | - const patched = updateTomlValues(fixtureTomlContent, [ |
122 | | - [['client_id'], clientId], |
123 | | - [['name'], name], |
124 | | - ]) |
125 | | - fs.writeFileSync(path.join(appDir, 'shopify.app.toml'), patched) |
| 148 | + const tomlPath = path.join(appDir, 'shopify.app.toml') |
| 149 | + const patched = mergeFixtureToml(fs.readFileSync(tomlPath, 'utf8'), fixtureTomlContent, name) |
| 150 | + fs.writeFileSync(tomlPath, patched) |
126 | 151 | } |
127 | 152 |
|
128 | 153 | export async function generateExtension( |
|
0 commit comments