|
2 | 2 | import {authFixture} from './auth.js' |
3 | 3 | import {navigateToDashboard} from './browser.js' |
4 | 4 | import {CLI_TIMEOUT, BROWSER_TIMEOUT} from './constants.js' |
| 5 | +import {updateTomlValues} from '@shopify/toml-patch' |
| 6 | +import * as toml from '@iarna/toml' |
5 | 7 | import * as path from 'path' |
6 | 8 | import * as fs from 'fs' |
7 | 9 | import type {CLIContext, CLIProcess, ExecResult} from './cli.js' |
@@ -84,23 +86,27 @@ export async function createApp(ctx: { |
84 | 86 | * Read the client_id from a shopify.app.toml file. |
85 | 87 | */ |
86 | 88 | export function extractClientId(appDir: string): string { |
87 | | - const toml = fs.readFileSync(path.join(appDir, 'shopify.app.toml'), 'utf8') |
88 | | - const match = toml.match(/client_id\s*=\s*"([^"]+)"/) |
89 | | - if (!match?.[1]) { |
90 | | - throw new Error(`Could not find client_id in ${path.join(appDir, 'shopify.app.toml')}`) |
| 89 | + const tomlPath = path.join(appDir, 'shopify.app.toml') |
| 90 | + const parsed = toml.parse(fs.readFileSync(tomlPath, 'utf8')) |
| 91 | + const clientId = parsed.client_id as string | undefined |
| 92 | + if (!clientId) { |
| 93 | + throw new Error(`Could not find client_id in ${tomlPath}`) |
91 | 94 | } |
92 | | - return match[1] |
| 95 | + return clientId |
93 | 96 | } |
94 | 97 |
|
95 | 98 | /** |
96 | 99 | * Overwrite a created app's shopify.app.toml with a fixture TOML template. |
97 | | - * The template should contain `__CLIENT_ID__` and `__NAME__` placeholders which get |
98 | | - * replaced with the app's real client_id and the provided name. |
| 100 | + * Uses toml-patch to surgically set client_id and name while |
| 101 | + * preserving comments and formatting in the fixture file. |
99 | 102 | */ |
100 | 103 | export function injectFixtureToml(appDir: string, fixtureTomlContent: string, name: string): void { |
101 | 104 | const clientId = extractClientId(appDir) |
102 | | - const toml = fixtureTomlContent.replace(/__CLIENT_ID__/g, clientId).replace(/__NAME__/g, name) |
103 | | - fs.writeFileSync(path.join(appDir, 'shopify.app.toml'), toml) |
| 105 | + const patched = updateTomlValues(fixtureTomlContent, [ |
| 106 | + [['client_id'], clientId], |
| 107 | + [['name'], name], |
| 108 | + ]) |
| 109 | + fs.writeFileSync(path.join(appDir, 'shopify.app.toml'), patched) |
104 | 110 | } |
105 | 111 |
|
106 | 112 | export async function generateExtension( |
|
0 commit comments