Skip to content

Commit

Permalink
Merge pull request #4680 from Shopify/use-app-context-in-app-dev
Browse files Browse the repository at this point in the history
Use app context in app dev
  • Loading branch information
isaacroldan authored Oct 25, 2024
2 parents 26be836 + 090246c commit 8612772
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 1,014 deletions.
25 changes: 19 additions & 6 deletions packages/app/src/cli/commands/app/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {dev, DevOptions} from '../../services/dev.js'
import {showApiKeyDeprecationWarning} from '../../prompts/deprecation-warnings.js'
import {checkFolderIsValidApp} from '../../models/app/loader.js'
import AppCommand, {AppCommandOutput} from '../../utilities/app-command.js'
import {linkedAppContext} from '../../services/app-context.js'
import {storeContext} from '../../services/store-context.js'
import {Flags} from '@oclif/core'
import {normalizeStoreFqdn} from '@shopify/cli-kit/node/context/fqdn'
import {globalFlags} from '@shopify/cli-kit/node/cli'
Expand Down Expand Up @@ -162,12 +164,23 @@ If you're using the PHP or Ruby app template, then you need to complete the foll

await checkFolderIsValidApp(flags.path)

const devOptions: DevOptions = {
const appContextResult = await linkedAppContext({
directory: flags.path,
configName: flags.config,
apiKey,
clientId: apiKey,
forceRelink: flags.reset,
userProvidedConfigName: flags.config,
})

const store = await storeContext({
appContextResult,
storeFqdn: flags.store,
reset: flags.reset,
forceReselectStore: flags.reset,
})

const devOptions: DevOptions = {
...appContextResult,
store,
directory: flags.path,
update: !flags['no-update'],
skipDependenciesInstallation: flags['skip-dependencies-installation'],
commandConfig,
Expand All @@ -182,7 +195,7 @@ If you're using the PHP or Ruby app template, then you need to complete the foll
graphiqlKey: flags['graphiql-key'],
}

const result = await dev(devOptions)
return {app: result.app}
await dev(devOptions)
return {app: appContextResult.app}
}
}
6 changes: 4 additions & 2 deletions packages/app/src/cli/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ type CmdFieldsFromMonorail = PickByPrefix<MonorailEventPublic, 'cmd_extensions_'
PickByPrefix<MonorailEventPublic, 'cmd_deploy_'> &
PickByPrefix<MonorailEventPublic, 'cmd_release_'> &
PickByPrefix<MonorailEventPublic, 'app_'> &
PickByPrefix<MonorailEventPublic, 'env_'>
PickByPrefix<MonorailEventPublic, 'env_'> &
PickByPrefix<MonorailEventPublic, 'store_'>

type CmdSensitiveFieldsFromMonorail = PickByPrefix<MonorailEventSensitive, 'app_'> &
PickByPrefix<MonorailEventSensitive, 'cmd_dev_'>
PickByPrefix<MonorailEventSensitive, 'cmd_dev_'> &
PickByPrefix<MonorailEventSensitive, 'store_'>

const metadata = createRuntimeMetadataContainer<
{
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/cli/services/app-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ describe('linkedAppContext', () => {
expect.objectContaining({
partner_id: tryParseInt(mockRemoteApp.organizationId),
api_key: mockRemoteApp.apiKey,
cmd_app_reset_used: false,
}),
)
})
Expand Down
5 changes: 3 additions & 2 deletions packages/app/src/cli/services/app-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ export async function linkedAppContext({
setCachedAppInfo({appId: remoteApp.apiKey, title: remoteApp.title, directory, orgId: remoteApp.organizationId})
}

await logMetadata(remoteApp)
await logMetadata(remoteApp, forceRelink)

return {app: localApp, remoteApp, developerPlatformClient, specifications, organization}
}

async function logMetadata(app: {organizationId: string; apiKey: string}) {
async function logMetadata(app: {organizationId: string; apiKey: string}, resetUsed: boolean) {
await metadata.addPublicMetadata(() => ({
partner_id: tryParseInt(app.organizationId),
api_key: app.apiKey,
cmd_app_reset_used: resetUsed,
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function writeDefaulToml(tmpDir: string) {
}

describe('patchAppConfigurationFile', () => {
test('updates existing configuration with new values and adds new top-levelfields', async () => {
test('updates existing configuration with new values and adds new top-levelfields, replaces arrays', async () => {
await inTemporaryDirectory(async (tmpDir) => {
const configPath = writeDefaulToml(tmpDir)
const patch = {
Expand All @@ -43,6 +43,9 @@ describe('patchAppConfigurationFile', () => {
access_scopes: {
use_legacy_install_flow: false,
},
auth: {
redirect_urls: ['https://example.com/redirect3', 'https://example.com/redirect4'],
},
}

await patchAppConfigurationFile({path: configPath, patch, schema})
Expand All @@ -62,8 +65,8 @@ use_legacy_install_flow = false
[auth]
redirect_urls = [
"https://example.com/redirect",
"https://example.com/redirect2"
"https://example.com/redirect3",
"https://example.com/redirect4"
]
[webhooks]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export interface PatchTomlOptions {
export async function patchAppConfigurationFile({path, patch, schema}: PatchTomlOptions) {
const tomlContents = await readFile(path)
const configuration = decodeToml(tomlContents)
const updatedConfig = deepMergeObjects(configuration, patch)

// Deep merge the configuration with the patch.
// Use replaceArrayStrategy to replace the destination array with the source array. (Arrays are not merged)
const updatedConfig = deepMergeObjects(configuration, patch, replaceArrayStrategy)

// Re-parse the config with the schema to validate the patch
// Make every field optional to not crash on tomls that are missing fields.
Expand All @@ -36,3 +39,7 @@ export async function patchAppConfigurationFile({path, patch, schema}: PatchToml
encodedString = addDefaultCommentsToToml(encodedString)
await writeFile(path, encodedString)
}

export function replaceArrayStrategy(_: unknown[], newArray: unknown[]): unknown[] {
return newArray
}
Loading

0 comments on commit 8612772

Please sign in to comment.