|
| 1 | +// packages/app/src/cli/services/app/config/pull.ts |
| 2 | + |
| 3 | +import {LinkOptions, loadLocalAppOptions, overwriteLocalConfigFileWithRemoteAppConfiguration} from './link.js' |
| 4 | +import {CurrentAppConfiguration, isCurrentAppSchema} from '../../../models/app/app.js' |
| 5 | +import {OrganizationApp} from '../../../models/organization.js' |
| 6 | +import {loadApp, AppConfigurationFileName, getAppConfigurationFileName} from '../../../models/app/loader.js' |
| 7 | +import {configurationFileNames} from '../../../constants.js' |
| 8 | +import {appFromIdentifiers} from '../../context.js' |
| 9 | +import {fetchSpecifications} from '../../generate/fetch-extension-specifications.js' |
| 10 | +import {RemoteAwareExtensionSpecification} from '../../../models/extensions/specification.js' |
| 11 | +import {Flag} from '../../../utilities/developer-platform-client.js' |
| 12 | +import {loadLocalExtensionsSpecifications} from '../../../models/extensions/load-specifications.js' |
| 13 | +import {AbortError} from '@shopify/cli-kit/node/error' |
| 14 | +import {basename} from '@shopify/cli-kit/node/path' |
| 15 | + |
| 16 | +export interface PullOptions { |
| 17 | + directory: string |
| 18 | + configName?: string |
| 19 | +} |
| 20 | + |
| 21 | +export interface PullOutput { |
| 22 | + configuration: CurrentAppConfiguration |
| 23 | + remoteApp: OrganizationApp |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Refresh an already-linked app configuration without prompting for org/app. |
| 28 | + */ |
| 29 | +export default async function pull(options: PullOptions): Promise<PullOutput> { |
| 30 | + const {directory, configName} = options |
| 31 | + |
| 32 | + // 1) Load the current config (default, or the one passed with --config) |
| 33 | + const app = await loadApp({ |
| 34 | + specifications: await loadLocalExtensionsSpecifications(), |
| 35 | + directory, |
| 36 | + mode: 'report', |
| 37 | + userProvidedConfigName: configName, |
| 38 | + remoteFlags: undefined, |
| 39 | + }) |
| 40 | + |
| 41 | + const configuration = app.configuration |
| 42 | + |
| 43 | + if (!isCurrentAppSchema(configuration) || !configuration.client_id) { |
| 44 | + throw new AbortError( |
| 45 | + 'The selected configuration is not linked to a remote app.', |
| 46 | + 'Run `shopify app config link` first to link this configuration to a Shopify app.', |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + // 2) Resolve remote app from the client_id in the config |
| 51 | + const remoteApp = await appFromIdentifiers({apiKey: configuration.client_id}) |
| 52 | + if (!remoteApp) { |
| 53 | + throw new AbortError( |
| 54 | + 'Could not find the remote app linked in this configuration.', |
| 55 | + 'Try linking the configuration again with `shopify app config link`.', |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + const developerPlatformClient = remoteApp.developerPlatformClient |
| 60 | + |
| 61 | + // 3) Fetch remote specs/flags for that app |
| 62 | + const specifications: RemoteAwareExtensionSpecification[] = await fetchSpecifications({ |
| 63 | + developerPlatformClient, |
| 64 | + app: remoteApp, |
| 65 | + }) |
| 66 | + const flags: Flag[] = remoteApp.flags |
| 67 | + |
| 68 | + // 4) Reuse helpers from link.ts to build and write the file |
| 69 | + const linkOptions: LinkOptions = { |
| 70 | + directory, |
| 71 | + configName, |
| 72 | + developerPlatformClient, |
| 73 | + apiKey: configuration.client_id, |
| 74 | + } |
| 75 | + |
| 76 | + const localAppOptions = await loadLocalAppOptions(linkOptions, specifications, flags, remoteApp.apiKey) |
| 77 | + |
| 78 | + // Decide which config file to overwrite: |
| 79 | + // - if config has a path, reuse that file |
| 80 | + // - otherwise, fallback to --config or default app config name |
| 81 | + const configFileName: AppConfigurationFileName = |
| 82 | + (configuration.path && (basename(configuration.path) as AppConfigurationFileName)) || |
| 83 | + getAppConfigurationFileName(configName ?? configurationFileNames.app) |
| 84 | + |
| 85 | + const mergedConfiguration = await overwriteLocalConfigFileWithRemoteAppConfiguration({ |
| 86 | + remoteApp, |
| 87 | + developerPlatformClient, |
| 88 | + specifications, |
| 89 | + flags, |
| 90 | + configFileName, |
| 91 | + appDirectory: localAppOptions.appDirectory ?? directory, |
| 92 | + localAppOptions, |
| 93 | + }) |
| 94 | + |
| 95 | + return {configuration: mergedConfiguration, remoteApp} |
| 96 | +} |
0 commit comments