Skip to content

Commit f649fa5

Browse files
authored
Merge pull request #5149 from Shopify/01-02-execute_adduidtotoml_in_app-context_for_all_commands
Execute addUidToToml in app-context for all commands
2 parents 7aa290c + c431e4f commit f649fa5

File tree

6 files changed

+235
-292
lines changed

6 files changed

+235
-292
lines changed

packages/app/src/cli/models/app/identifiers.test.ts

Lines changed: 0 additions & 254 deletions
Original file line numberDiff line numberDiff line change
@@ -114,260 +114,6 @@ describe('updateAppIdentifiers', () => {
114114
}
115115
})
116116
})
117-
118-
test('adds the missing uid to a simple TOML for atomic deployments', async () => {
119-
await inTemporaryDirectory(async (tmpDir: string) => {
120-
// Given
121-
const uiExtension = await testUIExtension({directory: tmpDir})
122-
const app = testApp({
123-
directory: tmpDir,
124-
allExtensions: [uiExtension],
125-
})
126-
await writeFile(
127-
uiExtension.configurationPath,
128-
`name = "tae"
129-
type = "theme"`,
130-
)
131-
132-
// When
133-
await updateAppIdentifiers(
134-
{
135-
app,
136-
identifiers: {
137-
app: 'FOO',
138-
extensions: {
139-
my_extension: 'BAR',
140-
},
141-
},
142-
command: 'deploy',
143-
developerPlatformClient: testDeveloperPlatformClient({supportsAtomicDeployments: true}),
144-
},
145-
{SHOPIFY_API_KEY: 'FOO', SHOPIFY_MY_EXTENSION_ID: 'BAR'},
146-
)
147-
148-
// Then
149-
const fileContent = await readFile(uiExtension.configurationPath)
150-
expect(fileContent).toEqual(`name = "tae"
151-
uid = "${uiExtension.uid}"
152-
type = "theme"`)
153-
})
154-
})
155-
})
156-
157-
test('does not change a simple TOML when the uid is already present for atomic deployments', async () => {
158-
await inTemporaryDirectory(async (tmpDir: string) => {
159-
// Given
160-
const uiExtension = await testUIExtension({directory: tmpDir})
161-
const app = testApp({
162-
directory: tmpDir,
163-
allExtensions: [uiExtension],
164-
})
165-
await writeFile(
166-
uiExtension.configurationPath,
167-
`name = "tae"
168-
uid = "${uiExtension.uid}"
169-
type = "theme"`,
170-
)
171-
172-
// When
173-
await updateAppIdentifiers(
174-
{
175-
app,
176-
identifiers: {
177-
app: 'FOO',
178-
extensions: {
179-
my_extension: 'BAR',
180-
},
181-
},
182-
command: 'deploy',
183-
developerPlatformClient: testDeveloperPlatformClient({supportsAtomicDeployments: true}),
184-
},
185-
{SHOPIFY_API_KEY: 'FOO', SHOPIFY_MY_EXTENSION_ID: 'BAR'},
186-
)
187-
188-
// Then
189-
const fileContent = await readFile(uiExtension.configurationPath)
190-
expect(fileContent).toEqual(`name = "tae"
191-
uid = "${uiExtension.uid}"
192-
type = "theme"`)
193-
})
194-
})
195-
196-
test('adds the missing uid to a unified config TOML for atomic deployments', async () => {
197-
await inTemporaryDirectory(async (tmpDir: string) => {
198-
// Given
199-
const uiExtension = await testUIExtension({
200-
directory: tmpDir,
201-
configuration: {
202-
name: 'Extension 1',
203-
handle: 'ext1',
204-
type: 'ui_extension',
205-
metafields: [],
206-
},
207-
})
208-
const app = testApp({
209-
directory: tmpDir,
210-
allExtensions: [uiExtension],
211-
})
212-
await writeFile(
213-
uiExtension.configurationPath,
214-
`api_version = "2024-04"
215-
[[extensions]]
216-
name = "Extension 1"
217-
handle = "ext1"
218-
type = "ui_extension"`,
219-
)
220-
221-
// When
222-
await updateAppIdentifiers(
223-
{
224-
app,
225-
identifiers: {
226-
app: 'FOO',
227-
extensions: {
228-
my_extension: 'BAR',
229-
},
230-
},
231-
command: 'deploy',
232-
developerPlatformClient: testDeveloperPlatformClient({supportsAtomicDeployments: true}),
233-
},
234-
{SHOPIFY_API_KEY: 'FOO', SHOPIFY_MY_EXTENSION_ID: 'BAR'},
235-
)
236-
237-
// Then
238-
const fileContent = await readFile(uiExtension.configurationPath)
239-
expect(fileContent).toEqual(`api_version = "2024-04"
240-
[[extensions]]
241-
name = "Extension 1"
242-
handle = "ext1"
243-
uid = "${uiExtension.uid}"
244-
type = "ui_extension"`)
245-
})
246-
})
247-
248-
test('does not change a unified config TOML when the uid is already present for atomic deployments', async () => {
249-
await inTemporaryDirectory(async (tmpDir: string) => {
250-
// Given
251-
const uiExtension = await testUIExtension({
252-
directory: tmpDir,
253-
configuration: {
254-
name: 'Extension 1',
255-
handle: 'ext1',
256-
type: 'ui_extension',
257-
metafields: [],
258-
},
259-
})
260-
const app = testApp({
261-
directory: tmpDir,
262-
allExtensions: [uiExtension],
263-
})
264-
await writeFile(
265-
uiExtension.configurationPath,
266-
`api_version = "2024-04"
267-
[[extensions]]
268-
name = "Extension 1"
269-
handle = "ext1"
270-
uid = "${uiExtension.uid}"
271-
type = "ui_extension"`,
272-
)
273-
274-
// When
275-
await updateAppIdentifiers(
276-
{
277-
app,
278-
identifiers: {
279-
app: 'FOO',
280-
extensions: {
281-
my_extension: 'BAR',
282-
},
283-
},
284-
command: 'deploy',
285-
developerPlatformClient: testDeveloperPlatformClient({supportsAtomicDeployments: true}),
286-
},
287-
{SHOPIFY_API_KEY: 'FOO', SHOPIFY_MY_EXTENSION_ID: 'BAR'},
288-
)
289-
290-
// Then
291-
const fileContent = await readFile(uiExtension.configurationPath)
292-
expect(fileContent).toEqual(`api_version = "2024-04"
293-
[[extensions]]
294-
name = "Extension 1"
295-
handle = "ext1"
296-
uid = "${uiExtension.uid}"
297-
type = "ui_extension"`)
298-
})
299-
})
300-
301-
test('adds the missing uids to a unified config TOML with multiple extensions for atomic deployments', async () => {
302-
await inTemporaryDirectory(async (tmpDir: string) => {
303-
// Given
304-
const uiExtension1 = await testUIExtension({
305-
directory: tmpDir,
306-
configuration: {
307-
name: 'Extension 1',
308-
handle: 'ext1',
309-
type: 'ui_extension',
310-
metafields: [],
311-
},
312-
})
313-
const uiExtension2 = await testUIExtension({
314-
directory: tmpDir,
315-
configuration: {
316-
name: 'Extension 2',
317-
handle: 'ext2',
318-
type: 'ui_extension',
319-
metafields: [],
320-
},
321-
})
322-
const app = testApp({
323-
directory: tmpDir,
324-
allExtensions: [uiExtension1, uiExtension2],
325-
})
326-
await writeFile(
327-
uiExtension1.configurationPath,
328-
`api_version = "2024-04"
329-
[[extensions]]
330-
name = "t:name"
331-
handle = "ext2"
332-
type = "ui_extension"
333-
334-
[[extensions]]
335-
name = "t:name"
336-
handle = "ext1"
337-
type = "ui_extension"`,
338-
)
339-
340-
// When
341-
await updateAppIdentifiers(
342-
{
343-
app,
344-
identifiers: {
345-
app: 'FOO',
346-
extensions: {
347-
my_extension: 'BAR',
348-
},
349-
},
350-
command: 'deploy',
351-
developerPlatformClient: testDeveloperPlatformClient({supportsAtomicDeployments: true}),
352-
},
353-
{SHOPIFY_API_KEY: 'FOO', SHOPIFY_MY_EXTENSION_ID: 'BAR'},
354-
)
355-
356-
// Then
357-
const fileContent = await readFile(uiExtension1.configurationPath)
358-
expect(fileContent).toEqual(`api_version = "2024-04"
359-
[[extensions]]
360-
name = "t:name"
361-
handle = "ext2"
362-
uid = "${uiExtension2.uid}"
363-
type = "ui_extension"
364-
365-
[[extensions]]
366-
name = "t:name"
367-
handle = "ext1"
368-
uid = "${uiExtension1.uid}"
369-
type = "ui_extension"`)
370-
})
371117
})
372118

373119
test('does not change a unified config TOML with multiple when the uid is already present for atomic deployments', async () => {

packages/app/src/cli/models/app/identifiers.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {patchEnvFile} from '@shopify/cli-kit/node/dot-env'
55
import {constantize} from '@shopify/cli-kit/common/string'
66
import {joinPath} from '@shopify/cli-kit/node/path'
77
import {fileExists, readFile, writeFile} from '@shopify/cli-kit/node/fs'
8-
import {deepCompare, getPathValue} from '@shopify/cli-kit/common/object'
9-
import {decodeToml} from '@shopify/cli-kit/node/toml'
8+
import {deepCompare} from '@shopify/cli-kit/common/object'
109
import type {AppInterface} from './app.js'
1110

1211
export interface IdentifiersExtensions {
@@ -48,17 +47,9 @@ interface UpdateAppIdentifiersOptions {
4847
* @returns An copy of the app with the environment updated to reflect the updated identifiers.
4948
*/
5049
export async function updateAppIdentifiers(
51-
{app, identifiers, command, developerPlatformClient}: UpdateAppIdentifiersOptions,
50+
{app, identifiers, command}: UpdateAppIdentifiersOptions,
5251
systemEnvironment = process.env,
5352
): Promise<AppInterface> {
54-
if (developerPlatformClient.supportsAtomicDeployments) {
55-
// We can't update the TOML files in parallel because some extensions might share the same file
56-
for (const extension of app.allExtensions) {
57-
// eslint-disable-next-line no-await-in-loop
58-
await addUidToToml(extension)
59-
}
60-
}
61-
6253
let dotenvFile = app.dotenv
6354

6455
if (!dotenvFile) {
@@ -95,33 +86,6 @@ export async function updateAppIdentifiers(
9586
return app
9687
}
9788

98-
async function addUidToToml(extension: ExtensionInstance) {
99-
if (!extension.isUUIDStrategyExtension) return
100-
101-
const tomlContents = await readFile(extension.configurationPath)
102-
const extensionConfig = decodeToml(tomlContents)
103-
const extensions = getPathValue(extensionConfig, 'extensions') as ExtensionInstance[]
104-
105-
if ('uid' in extensionConfig) return
106-
if (extensions) {
107-
const currentExtension = extensions.find((ext) => ext.handle === extension.handle)
108-
if (currentExtension && 'uid' in currentExtension) return
109-
}
110-
111-
let updatedTomlContents = tomlContents
112-
if (extensions?.length > 1) {
113-
// If the TOML has multiple extensions, we look for the correct handle to add the uid below
114-
const regex = new RegExp(`(\\n?(\\s*)handle\\s*=\\s*"${extension.handle}")`)
115-
updatedTomlContents = tomlContents.replace(regex, `$1\n$2uid = "${extension.uid}"`)
116-
} else {
117-
// If the TOML has only one extension, we add the uid before the type, which is always present
118-
if ('uid' in extensionConfig) return
119-
const regex = /\n?((\s*)type\s*=\s*"\S*")/
120-
updatedTomlContents = tomlContents.replace(regex, `$2\nuid = "${extension.uid}"\n$1`)
121-
}
122-
await writeFile(extension.configurationPath, updatedTomlContents)
123-
}
124-
12589
interface GetAppIdentifiersOptions {
12690
app: AppInterface
12791
}

packages/app/src/cli/services/app-context.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ vi.mock('./generate/fetch-extension-specifications.js')
1818
vi.mock('./app/config/link.js')
1919
vi.mock('./context.js')
2020
vi.mock('./dev/fetch.js')
21+
vi.mock('./app/add-uid-to-extension-toml.js')
2122

2223
async function writeAppConfig(tmp: string, content: string) {
2324
const appConfigPath = joinPath(tmp, 'shopify.app.toml')

packages/app/src/cli/services/app-context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {getCachedAppInfo, setCachedAppInfo} from './local-storage.js'
33
import {fetchSpecifications} from './generate/fetch-extension-specifications.js'
44
import link from './app/config/link.js'
55
import {fetchOrgFromId} from './dev/fetch.js'
6+
import {addUidToTomlsIfNecessary} from './app/add-uid-to-extension-toml.js'
67
import {Organization, OrganizationApp} from '../models/organization.js'
78
import {DeveloperPlatformClient, selectDeveloperPlatformClient} from '../utilities/developer-platform-client.js'
89
import {getAppConfigurationState, loadAppUsingConfigurationState} from '../models/app/loader.js'
@@ -99,6 +100,9 @@ export async function linkedAppContext({
99100

100101
await logMetadata(remoteApp, forceRelink)
101102

103+
// Add UIDs to extension TOML files if using app-management.
104+
await addUidToTomlsIfNecessary(localApp.allExtensions, developerPlatformClient)
105+
102106
return {app: localApp, remoteApp, developerPlatformClient, specifications, organization}
103107
}
104108

0 commit comments

Comments
 (0)