Skip to content

Commit

Permalink
Stripped any API use out of prompt functions
Browse files Browse the repository at this point in the history
  • Loading branch information
shauns committed May 21, 2024
1 parent 2b1ef2a commit 70db46e
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 40 deletions.
28 changes: 0 additions & 28 deletions packages/app/src/cli/prompts/config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
/* eslint-disable no-await-in-loop */
import {AppSchema, CurrentAppConfiguration} from '../models/app/app.js'
import {rewriteConfiguration} from '../services/app/write-app-configuration-file.js'
import {AppConfigurationFileName} from '../models/app/loader.js'
import {
RenderTextPromptOptions,
renderConfirmationPrompt,
renderInfo,
renderSelectPrompt,
renderTextPrompt,
} from '@shopify/cli-kit/node/ui'
import {fileExists, glob} from '@shopify/cli-kit/node/fs'
import {basename, joinPath} from '@shopify/cli-kit/node/path'
import {slugify} from '@shopify/cli-kit/common/string'
import {err, ok, Result} from '@shopify/cli-kit/node/result'
import {encodeToml} from '@shopify/cli-kit/node/toml'
import {deepCompare, deepDifference} from '@shopify/cli-kit/common/object'
import colors from '@shopify/cli-kit/node/colors'
import {zod} from '@shopify/cli-kit/node/schema'

export async function selectConfigName(directory: string, defaultName = ''): Promise<AppConfigurationFileName> {
const namePromptOptions = buildTextPromptOptions(defaultName)
Expand Down Expand Up @@ -82,25 +76,3 @@ export function validate(value: string): string | undefined {
// Max filename size for Windows/Mac including the prefix/postfix
if (result.length > 238) return 'The file name is too long.'
}

export function buildDiffConfigContent(
localConfig: CurrentAppConfiguration,
remoteConfig: unknown,
schema: zod.ZodTypeAny = AppSchema,
renderNoChanges = true,
) {
const [updated, baseline] = deepDifference(
{...(rewriteConfiguration(schema, localConfig) as object), build: undefined},
{...(rewriteConfiguration(schema, remoteConfig) as object), build: undefined},
)

if (deepCompare(updated, baseline)) {
if (renderNoChanges) renderInfo({headline: 'No changes to update.'})
return undefined
}

return {
baselineContent: encodeToml(baseline),
updatedContent: encodeToml(updated),
}
}
5 changes: 3 additions & 2 deletions packages/app/src/cli/prompts/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {Organization, OrganizationStore} from '../models/organization.js'
import {testDeveloperPlatformClient, testOrganizationApp} from '../models/app/app.test-data.js'
import {getTomls} from '../utilities/app/config/getTomls.js'
import {searchForAppsByNameFactory} from '../services/dev/prompt-helpers.js'
import {describe, expect, vi, test, beforeEach} from 'vitest'
import {renderAutocompletePrompt, renderConfirmationPrompt, renderTextPrompt} from '@shopify/cli-kit/node/ui'
import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output'
Expand Down Expand Up @@ -92,7 +93,7 @@ describe('selectApp', () => {
vi.mocked(renderAutocompletePrompt).mockResolvedValue(APP2.apiKey)

// When
const got = await selectAppPrompt(testDeveloperPlatformClient(), apps, true, ORG1.id)
const got = await selectAppPrompt(searchForAppsByNameFactory(testDeveloperPlatformClient(), ORG1.id), apps, true)

// Then
expect(got).toEqual(APP2)
Expand All @@ -116,7 +117,7 @@ describe('selectApp', () => {
const apps = [APP1, APP2]
vi.mocked(renderAutocompletePrompt).mockResolvedValue(APP2.apiKey)

const got = await selectAppPrompt(testDeveloperPlatformClient(), apps, true, ORG1.id, {
const got = await selectAppPrompt(searchForAppsByNameFactory(testDeveloperPlatformClient(), ORG1.id), apps, true, {
directory: '/',
})

Expand Down
6 changes: 2 additions & 4 deletions packages/app/src/cli/prompts/dev.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Organization, MinimalOrganizationApp, OrganizationStore, MinimalAppIdentifiers} from '../models/organization.js'
import {getTomls} from '../utilities/app/config/getTomls.js'
import {setCachedCommandTomlMap} from '../services/local-storage.js'
import {DeveloperPlatformClient} from '../utilities/developer-platform-client.js'
import {renderAutocompletePrompt, renderConfirmationPrompt, renderTextPrompt} from '@shopify/cli-kit/node/ui'
import {outputCompleted} from '@shopify/cli-kit/node/output'

Expand All @@ -18,10 +17,9 @@ export async function selectOrganizationPrompt(organizations: Organization[]): P
}

export async function selectAppPrompt(
developerPlatformClient: DeveloperPlatformClient,
onSearchForAppsByName: (term: string) => Promise<{apps: MinimalOrganizationApp[]; hasMorePages: boolean}>,
apps: MinimalOrganizationApp[],
hasMorePages: boolean,
orgId: string,
options?: {
directory?: string
},
Expand All @@ -45,7 +43,7 @@ export async function selectAppPrompt(
choices: currentAppChoices.map(toAnswer),
hasMorePages,
search: async (term) => {
const result = await developerPlatformClient.appsForOrg(orgId, term)
const result = await onSearchForAppsByName(term)
currentAppChoices = result.apps

return {
Expand Down
7 changes: 6 additions & 1 deletion packages/app/src/cli/services/app/select-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import {
selectDeveloperPlatformClient,
} from '../../utilities/developer-platform-client.js'
import {selectOrg} from '../context.js'
import {searchForAppsByNameFactory} from '../dev/prompt-helpers.js'
import {deepMergeObjects} from '@shopify/cli-kit/common/object'

export async function selectApp(): Promise<OrganizationApp> {
const org = await selectOrg()
const developerPlatformClient = selectDeveloperPlatformClient({organization: org})
const {apps, hasMorePages} = await developerPlatformClient.appsForOrg(org.id)
const selectedApp = await selectAppPrompt(developerPlatformClient, apps, hasMorePages, org.id)
const selectedApp = await selectAppPrompt(
searchForAppsByNameFactory(developerPlatformClient, org.id),
apps,
hasMorePages,
)
const fullSelectedApp = await developerPlatformClient.appFromId(selectedApp)
return fullSelectedApp!
}
Expand Down
29 changes: 26 additions & 3 deletions packages/app/src/cli/services/context/breakdown-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {versionDiffByVersion} from '../release/version-diff.js'
import {AppVersionsDiffExtensionSchema} from '../../api/graphql/app_versions_diff.js'
import {AppInterface, CurrentAppConfiguration, filterNonVersionedAppFields} from '../../models/app/app.js'
import {MinimalOrganizationApp} from '../../models/organization.js'
import {buildDiffConfigContent} from '../../prompts/config.js'
import {IdentifiersExtensions} from '../../models/app/identifiers.js'
import {
extensionTypeStrategy,
Expand All @@ -17,6 +16,11 @@ import {
RemoteExtensionRegistrations,
} from '../../api/graphql/all_app_extension_registrations.js'
import {ExtensionSpecification} from '../../models/extensions/specification.js'
import {rewriteConfiguration} from '../app/write-app-configuration-file.js'
import {SpecsAppConfiguration} from '../../models/extensions/specifications/types/app_config.js'
import {deepCompare, deepDifference} from '@shopify/cli-kit/common/object'
import {encodeToml} from '@shopify/cli-kit/node/toml'
import {zod} from '@shopify/cli-kit/node/schema'

export interface ConfigExtensionIdentifiersBreakdown {
existingFieldNames: string[]
Expand Down Expand Up @@ -154,7 +158,7 @@ async function resolveRemoteConfigExtensionIdentifiersBreakdown(
app: AppInterface,
versionAppModules?: AppModuleVersion[],
) {
const remoteConfig =
const remoteConfig: Partial<SpecsAppConfiguration> =
(await fetchAppRemoteConfiguration(
remoteApp,
developerPlatformClient,
Expand All @@ -168,7 +172,6 @@ async function resolveRemoteConfigExtensionIdentifiersBreakdown(
baselineConfig as CurrentAppConfiguration,
remoteConfig,
app.configSchema,
false,
)

// List of field included in the config except the ones that only affect the CLI and are not pushed to the server
Expand Down Expand Up @@ -208,6 +211,26 @@ async function resolveRemoteConfigExtensionIdentifiersBreakdown(
}
}

function buildDiffConfigContent(
localConfig: CurrentAppConfiguration,
remoteConfig: Partial<SpecsAppConfiguration>,
schema: zod.ZodTypeAny,
) {
const [updated, baseline] = deepDifference(
{...(rewriteConfiguration(schema, localConfig) as object), build: undefined},
{...(rewriteConfiguration(schema, remoteConfig) as object), build: undefined},
)

if (deepCompare(updated, baseline)) {
return undefined
}

return {
baselineContent: encodeToml(baseline),
updatedContent: encodeToml(updated),
}
}

/**
* This method extracts the list of global fields or global sections from the string that represents a toml section like
* this:
Expand Down
8 changes: 8 additions & 0 deletions packages/app/src/cli/services/dev/prompt-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {DeveloperPlatformClient} from '../../utilities/developer-platform-client.js'

export function searchForAppsByNameFactory(developerPlatformClient: DeveloperPlatformClient, orgId: string) {
return async (term: string) => {
const result = await developerPlatformClient.appsForOrg(orgId, term)
return {apps: result.apps, hasMorePages: result.hasMorePages}
}
}
2 changes: 1 addition & 1 deletion packages/app/src/cli/services/dev/select-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('selectOrCreateApp', () => {

// Then
expect(got).toEqual(APP1)
expect(selectAppPrompt).toHaveBeenCalledWith(developerPlatformClient, APPS, false, ORG1.id, {
expect(selectAppPrompt).toHaveBeenCalledWith(expect.any(Function), APPS, false, {
directory: undefined,
})
})
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/cli/services/dev/select-app.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {searchForAppsByNameFactory} from './prompt-helpers.js'
import {appNamePrompt, createAsNewAppPrompt, selectAppPrompt} from '../../prompts/dev.js'
import {Organization, MinimalOrganizationApp, OrganizationApp} from '../../models/organization.js'
import {getCachedCommandInfo, setCachedCommandTomlPreference} from '../local-storage.js'
Expand Down Expand Up @@ -35,7 +36,7 @@ export async function selectOrCreateApp(
const name = await appNamePrompt(localAppName)
return developerPlatformClient.createApp(org, name, options)
} else {
const app = await selectAppPrompt(developerPlatformClient, apps, hasMorePages, org.id, {
const app = await selectAppPrompt(searchForAppsByNameFactory(developerPlatformClient, org.id), apps, hasMorePages, {
directory: options?.directory,
})

Expand Down

0 comments on commit 70db46e

Please sign in to comment.