Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use app context in app dev #4680

Merged
merged 21 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
59773e9
use appContext and storeContext instead of devContext
isaacroldan Oct 17, 2024
2cd35f0
Merge branch 'use-app-context-in-app-logs' into use-app-context-in-ap…
isaacroldan Oct 17, 2024
3684838
Some changes to dev
isaacroldan Oct 17, 2024
5d1b9e7
Merge branch 'use-app-context-in-app-logs' into use-app-context-in-ap…
isaacroldan Oct 18, 2024
2f177f0
Move some logic to dev
isaacroldan Oct 18, 2024
5b5c0e8
Update code to show reused values
isaacroldan Oct 18, 2024
7d15845
Merge branch 'main' into use-app-context-in-app-dev
isaacroldan Oct 18, 2024
33e2f6a
Update patch toml function to replace arrays instead of merging
isaacroldan Oct 18, 2024
7ef4c41
Make store-context call convertTransferDisabledStore
isaacroldan Oct 18, 2024
15b40d0
Delete ensureDevContext
isaacroldan Oct 18, 2024
4117229
Update store-context tests
isaacroldan Oct 18, 2024
8ceb4f8
Update urls tests
isaacroldan Oct 18, 2024
3e980d9
revert changes to urls.ts
isaacroldan Oct 18, 2024
9d70ed8
Update app-context test
isaacroldan Oct 18, 2024
6e797a3
Return the linked app in dev command
isaacroldan Oct 18, 2024
b7e8be2
Update setup-dev-processes test
isaacroldan Oct 18, 2024
dd388c0
Merge branch 'main' into use-app-context-in-app-dev
isaacroldan Oct 18, 2024
b6214b9
Merge branch 'main' into use-app-context-in-app-dev
isaacroldan Oct 21, 2024
d4e3935
Merge branch 'main' into use-app-context-in-app-dev
isaacroldan Oct 24, 2024
73c0000
Fix metadata and store fetching
isaacroldan Oct 24, 2024
090246c
Add tests for logMetadata in store-context
isaacroldan Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -33,7 +33,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 @@ -42,6 +42,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 @@ -61,8 +64,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 @@ -22,7 +22,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 and keep the same order in the file
// Make every field optional to not crash on invalid tomls that are missing fields.
Expand All @@ -33,3 +36,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