Skip to content
Closed
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
2 changes: 1 addition & 1 deletion packages/app/src/cli/commands/app/import-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class ImportExtensions extends AppLinkedCommand {
...appContext,
extensions,
extensionTypes: migrationChoice.extensionTypes,
buildTomlObject: migrationChoice.buildTomlObject,
buildExtensionConfig: migrationChoice.buildExtensionConfig,
})
}

Expand Down
36 changes: 11 additions & 25 deletions packages/app/src/cli/models/app/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {getOrCreateAppConfigHiddenPath} from '../../utilities/app/config/hidden-
import {ApplicationURLs, generateApplicationURLs} from '../../services/dev/urls.js'
import {showMultipleCLIWarningIfNeeded} from '@shopify/cli-kit/node/multiple-installation-warning'
import {fileExists, readFile, glob, findPathUp, fileExistsSync} from '@shopify/cli-kit/node/fs'
import {TomlFile, TomlParseError} from '@shopify/cli-kit/node/toml/toml-file'
import {zod} from '@shopify/cli-kit/node/schema'
import {readAndParseDotEnv, DotEnvFile} from '@shopify/cli-kit/node/dot-env'
import {
Expand All @@ -49,7 +50,7 @@ import {
} from '@shopify/cli-kit/node/node-package-manager'
import {resolveFramework} from '@shopify/cli-kit/node/framework'
import {hashString} from '@shopify/cli-kit/node/crypto'
import {JsonMapType, decodeToml} from '@shopify/cli-kit/node/toml'
import {JsonMapType} from '@shopify/cli-kit/node/toml'
import {joinPath, dirname, basename, relativePath, relativizePath} from '@shopify/cli-kit/node/path'
import {AbortError} from '@shopify/cli-kit/node/error'
import {outputContent, outputDebug, OutputMessage, outputToken} from '@shopify/cli-kit/node/output'
Expand Down Expand Up @@ -82,27 +83,19 @@ const noopAbortOrReport: AbortOrReport = (_errorMessage, fallback, _configuratio
export async function loadConfigurationFileContent(
filepath: string,
abortOrReport: AbortOrReport = abort,
decode: (input: string) => JsonMapType = decodeToml,
): Promise<JsonMapType> {
if (!(await fileExists(filepath))) {
return abortOrReport(outputContent`Couldn't find an app toml file at ${outputToken.path(filepath)}`, {}, filepath)
}

try {
const configurationContent = await readFile(filepath)
return decode(configurationContent)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
// TOML errors have line, pos and col properties
if (err.line !== undefined && err.pos !== undefined && err.col !== undefined) {
return abortOrReport(
outputContent`Fix the following error in ${outputToken.path(filepath)}:\n${err.message}`,
{},
filepath,
)
} else {
throw err
const file = await TomlFile.read(filepath)
return file.content
} catch (err) {
if (err instanceof TomlParseError) {
return abortOrReport(outputContent`${err.message}`, {}, filepath)
}
throw err
}
}

Expand All @@ -115,12 +108,11 @@ export async function parseConfigurationFile<TSchema extends zod.ZodType>(
schema: TSchema,
filepath: string,
abortOrReport: AbortOrReport = abort,
decode: (input: string) => JsonMapType = decodeToml,
preloadedContent?: JsonMapType,
): Promise<zod.TypeOf<TSchema> & {path: string}> {
const fallbackOutput = {} as zod.TypeOf<TSchema>

const configurationObject = preloadedContent ?? (await loadConfigurationFileContent(filepath, abortOrReport, decode))
const configurationObject = preloadedContent ?? (await loadConfigurationFileContent(filepath, abortOrReport))

if (!configurationObject) return fallbackOutput

Expand Down Expand Up @@ -517,13 +509,8 @@ class AppLoader<TConfig extends AppConfiguration, TModuleSpec extends ExtensionS
)
}

private parseConfigurationFile<TSchema extends zod.ZodType>(
schema: TSchema,
filepath: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
decode: (input: any) => any = decodeToml,
) {
return parseConfigurationFile(schema, filepath, this.abortOrReport.bind(this), decode)
private parseConfigurationFile<TSchema extends zod.ZodType>(schema: TSchema, filepath: string) {
return parseConfigurationFile(schema, filepath, this.abortOrReport.bind(this))
}

private validateWebs(webs: Web[]): void {
Expand Down Expand Up @@ -1027,7 +1014,6 @@ async function loadAppConfigurationFromState<
schemaForConfigurationFile,
configState.configurationPath,
abort,
decodeToml,
file,
)) as LoadedAppConfigFromConfigState<TConfig>
const allClientIdsByConfigName = await getAllLinkedConfigClientIds(configState.appDirectory, {
Expand Down
20 changes: 10 additions & 10 deletions packages/app/src/cli/prompts/import-extensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ describe('allMigrationChoices', () => {
expect(choice).toHaveProperty('label')
expect(choice).toHaveProperty('value')
expect(choice).toHaveProperty('extensionTypes')
expect(choice).toHaveProperty('buildTomlObject')
expect(choice).toHaveProperty('buildExtensionConfig')
expect(Array.isArray(choice.extensionTypes)).toBe(true)
expect(choice.extensionTypes.length).toBeGreaterThan(0)
expect(typeof choice.buildTomlObject).toBe('function')
expect(typeof choice.buildExtensionConfig).toBe('function')
})
})

Expand Down Expand Up @@ -134,7 +134,7 @@ describe('selectMigrationChoice', () => {
label: 'Test Extension',
value: 'test',
extensionTypes: ['test_type'],
buildTomlObject: vi.fn(),
buildExtensionConfig: vi.fn(),
}
const result = await selectMigrationChoice([singleChoice])
expect(result).toBe(singleChoice)
Expand All @@ -147,13 +147,13 @@ describe('selectMigrationChoice', () => {
label: 'Choice 1',
value: 'choice1',
extensionTypes: ['type1'],
buildTomlObject: vi.fn(),
buildExtensionConfig: vi.fn(),
},
{
label: 'Choice 2',
value: 'choice2',
extensionTypes: ['type2'],
buildTomlObject: vi.fn(),
buildExtensionConfig: vi.fn(),
},
]

Expand All @@ -177,13 +177,13 @@ describe('selectMigrationChoice', () => {
label: 'Choice 1',
value: 'choice1',
extensionTypes: ['type1'],
buildTomlObject: vi.fn(),
buildExtensionConfig: vi.fn(),
},
{
label: 'Choice 2',
value: 'choice2',
extensionTypes: ['type2'],
buildTomlObject: vi.fn(),
buildExtensionConfig: vi.fn(),
},
]

Expand All @@ -204,19 +204,19 @@ describe('selectMigrationChoice', () => {
label: 'Payments Extensions',
value: 'payments',
extensionTypes: ['payments_app'],
buildTomlObject: vi.fn(),
buildExtensionConfig: vi.fn(),
},
{
label: 'Flow Extensions',
value: 'flow',
extensionTypes: ['flow_action_definition'],
buildTomlObject: vi.fn(),
buildExtensionConfig: vi.fn(),
},
{
label: 'Marketing Activity Extensions',
value: 'marketing activity',
extensionTypes: ['marketing_activity_extension'],
buildTomlObject: vi.fn(),
buildExtensionConfig: vi.fn(),
},
]

Expand Down
24 changes: 12 additions & 12 deletions packages/app/src/cli/prompts/import-extensions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {buildTomlObject as buildPaymentsTomlObject} from '../services/payments/extension-to-toml.js'
import {buildTomlObject as buildFlowTomlObject} from '../services/flow/extension-to-toml.js'
import {buildTomlObject as buildAdminLinkTomlObject} from '../services/admin-link/extension-to-toml.js'
import {buildTomlObject as buildMarketingActivityTomlObject} from '../services/marketing_activity/extension-to-toml.js'
import {buildTomlObject as buildSubscriptionLinkTomlObject} from '../services/subscription_link/extension-to-toml.js'
import {buildExtensionConfig as buildPaymentsConfig} from '../services/payments/extension-config-builder.js'
import {buildExtensionConfig as buildFlowConfig} from '../services/flow/extension-config-builder.js'
import {buildExtensionConfig as buildAdminLinkConfig} from '../services/admin-link/extension-config-builder.js'
import {buildExtensionConfig as buildMarketingActivityConfig} from '../services/marketing_activity/extension-config-builder.js'
import {buildExtensionConfig as buildSubscriptionLinkConfig} from '../services/subscription_link/extension-config-builder.js'
import {ExtensionRegistration} from '../api/graphql/all_app_extension_registrations.js'
import {CurrentAppConfiguration} from '../models/app/app.js'
import {AbortError} from '@shopify/cli-kit/node/error'
Expand All @@ -12,11 +12,11 @@ export interface MigrationChoice {
label: string
value: string
extensionTypes: string[]
buildTomlObject: (
buildExtensionConfig: (
ext: ExtensionRegistration,
allExtensions: ExtensionRegistration[],
appConfiguration: CurrentAppConfiguration,
) => string
) => object
}

export const allMigrationChoices: MigrationChoice[] = [
Expand All @@ -31,31 +31,31 @@ export const allMigrationChoices: MigrationChoice[] = [
'payments_app_redeemable',
'payments_extension',
],
buildTomlObject: buildPaymentsTomlObject,
buildExtensionConfig: buildPaymentsConfig,
},
{
label: 'Flow Extensions',
value: 'flow',
extensionTypes: ['flow_action_definition', 'flow_trigger_definition', 'flow_trigger_discovery_webhook'],
buildTomlObject: buildFlowTomlObject,
buildExtensionConfig: buildFlowConfig,
},
{
label: 'Marketing Activity Extensions',
value: 'marketing activity',
extensionTypes: ['marketing_activity_extension'],
buildTomlObject: buildMarketingActivityTomlObject,
buildExtensionConfig: buildMarketingActivityConfig,
},
{
label: 'Subscription Link Extensions',
value: 'subscription link',
extensionTypes: ['subscription_link', 'subscription_link_extension'],
buildTomlObject: buildSubscriptionLinkTomlObject,
buildExtensionConfig: buildSubscriptionLinkConfig,
},
{
label: 'Admin Link extensions',
value: 'link extension',
extensionTypes: ['app_link', 'bulk_action'],
buildTomlObject: buildAdminLinkTomlObject,
buildExtensionConfig: buildAdminLinkConfig,
},
]

Expand Down
Loading
Loading