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

Add new option to link for new apps #4808

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
79 changes: 79 additions & 0 deletions packages/app/src/cli/services/app/config/link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,85 @@ embedded = false
})
})

test('existing config is respected when isNewApp is true, config is current and client_id is not the same as remote app', async () => {
await inTemporaryDirectory(async (tmp) => {
// Given
const developerPlatformClient = buildDeveloperPlatformClient()
const options: LinkOptions = {
directory: tmp,
developerPlatformClient,
isNewApp: true,
configName: 'shopify.app.toml',
}
const localApp = {
configuration: {
path: joinPath(tmp, 'shopify.app.toml'),
name: 'my app',
client_id: 'invalid_client_id_from_template',
webhooks: {
api_version: '2023-04',
subscriptions: [{topics: ['products/create'], uri: 'https://my-app.com/webhooks'}],
},
application_url: 'https://myapp.com',
build: {
automatically_update_urls_on_dev: true,
dev_store_url: 'my-store.myshopify.com',
include_config_on_deploy: true,
},
access_scopes: {
scopes: 'write_products',
},
} as CurrentAppConfiguration,
}

vi.mocked(loadApp).mockResolvedValue(await mockApp(tmp, localApp, [], 'current'))
vi.mocked(fetchOrCreateOrganizationApp).mockResolvedValue(mockRemoteApp({developerPlatformClient}))
const remoteConfiguration = {
...DEFAULT_REMOTE_CONFIGURATION,
handle: 'handle',
}
vi.mocked(fetchAppRemoteConfiguration).mockResolvedValue(remoteConfiguration)

// When
await link(options)

// Then
const content = await readFile(joinPath(tmp, 'shopify.app.toml'))
const expectedContent = `# Learn more about configuring your app at https://shopify.dev/docs/apps/tools/cli/configuration

client_id = "12345"
name = "app1"
handle = "handle"
application_url = "https://example.com"
embedded = true

[build]
automatically_update_urls_on_dev = true
dev_store_url = "my-store.myshopify.com"
include_config_on_deploy = true

[access_scopes]
# Learn more at https://shopify.dev/docs/apps/tools/cli/configuration#access_scopes
scopes = "write_products"
use_legacy_install_flow = true

[auth]
redirect_urls = [ "https://example.com/callback1" ]

[webhooks]
api_version = "2023-07"

[[webhooks.subscriptions]]
topics = [ "products/create" ]
uri = "https://my-app.com/webhooks"

[pos]
embedded = false
`
expect(content).toEqual(expectedContent)
})
})

async function mockApp(
directory: string,
app?: Partial<AppInterface>,
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/cli/services/app/config/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
configName?: string
baseConfigName?: string
developerPlatformClient?: DeveloperPlatformClient
isNewApp?: boolean
}

interface LinkOutput {
Expand Down Expand Up @@ -130,7 +131,7 @@
let developerPlatformClient = await sniffServiceOptionsAndAppConfigToSelectPlatformClient(options)

const {creationOptions, appDirectory: possibleAppDirectory} = await getAppCreationDefaultsFromLocalApp(options)
const appDirectory = possibleAppDirectory || options.directory

Check warning on line 134 in packages/app/src/cli/services/app/config/link.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/app/config/link.ts#L134

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.

if (options.apiKey) {
// Remote API Key provided by the caller, so use that app specifically
Expand Down Expand Up @@ -280,7 +281,7 @@
appDirectory: app.directory,
packageManager: app.packageManager,
}
} else if (app.configuration.client_id === remoteAppApiKey) {
} else if (app.configuration.client_id === remoteAppApiKey || options.isNewApp) {
return {
state: 'reusable-current-app',
configFormat: 'current',
Expand Down Expand Up @@ -348,7 +349,7 @@
const currentToml = existingTomls[remoteApp.apiKey]
if (currentToml) return currentToml

return selectConfigName(localAppInfo.appDirectory || options.directory, remoteApp.title)

Check warning on line 352 in packages/app/src/cli/services/app/config/link.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/app/config/link.ts#L352

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
}

/**
Expand Down Expand Up @@ -485,8 +486,8 @@

function addRemoteAppHomeConfig(remoteApp: OrganizationApp) {
const homeConfig = {
application_url: remoteApp.applicationUrl?.replace(/\/$/, '') || '',

Check warning on line 489 in packages/app/src/cli/services/app/config/link.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/app/config/link.ts#L489

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
embedded: remoteApp.embedded === undefined ? true : remoteApp.embedded,

Check warning on line 490 in packages/app/src/cli/services/app/config/link.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/app/config/link.ts#L490

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a ternary expression, as it is simpler to read.
}
return remoteApp.preferencesUrl
? {
Expand All @@ -512,8 +513,8 @@

function addRemoteAppWebhooksConfig(remoteApp: OrganizationApp) {
const hasAnyPrivacyWebhook =
remoteApp.gdprWebhooks?.customerDataRequestUrl ||

Check warning on line 516 in packages/app/src/cli/services/app/config/link.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/app/config/link.ts#L516

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
remoteApp.gdprWebhooks?.customerDeletionUrl ||

Check warning on line 517 in packages/app/src/cli/services/app/config/link.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/app/config/link.ts#L517

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
remoteApp.gdprWebhooks?.shopDeletionUrl

const privacyComplianceContent = {
Expand All @@ -526,7 +527,7 @@

return {
webhooks: {
api_version: remoteApp.webhookApiVersion || '2023-07',

Check warning on line 530 in packages/app/src/cli/services/app/config/link.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/app/config/link.ts#L530

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
...(hasAnyPrivacyWebhook ? privacyComplianceContent : {}),
},
}
Expand Down Expand Up @@ -562,7 +563,7 @@
function addPosConfig(remoteApp: OrganizationApp) {
return {
pos: {
embedded: remoteApp.posEmbedded || false,

Check warning on line 566 in packages/app/src/cli/services/app/config/link.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/app/config/link.ts#L566

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
},
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/app/src/cli/services/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ async function init(options: InitOptions) {
organizationId: app.organizationId,
configName: 'shopify.app.toml',
developerPlatformClient: options.developerPlatformClient,
isNewApp: true,
},
false,
)
Expand Down
Loading