-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {renderConfirmationPrompt} from '@shopify/cli-kit/node/ui' | ||
Check failure on line 1 in packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L1
|
||
import {Workflow} from './registry.js' | ||
import {generateExtensionTemplate} from '../extension.js' | ||
import {generateExtensionPrompts} from '../../../prompts/generate/extension.js' | ||
Check failure on line 4 in packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L4
|
||
import {buildGenerateOptions, renderSuccessMessage, buildPromptOptions} from '../../generate.js' | ||
import {GenerateExtensionPromptOutput} from '../../../prompts/generate/extension.js' | ||
Check failure on line 6 in packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L6
Check failure on line 6 in packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L6
Check failure on line 6 in packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L6
|
||
|
||
export const discountDetailsFunctionSettingsCollection: Workflow = { | ||
afterGenerate: async (options) => { | ||
const {app, developerPlatformClient, specifications} = options.generateOptions | ||
|
||
const shouldCreateFunction = await renderConfirmationPrompt({ | ||
message: 'Would you like to create a function for this extension?', | ||
defaultValue: true, | ||
}) | ||
|
||
if (shouldCreateFunction) { | ||
// create a function extension | ||
const extensionTemplates = options.extensionTemplates.filter( | ||
(template) => | ||
template.identifier === 'shipping_discounts' || | ||
template.identifier === 'product_discounts' || | ||
template.identifier === 'order_discounts' || | ||
template.identifier == 'discounts_allocator', | ||
Check failure on line 24 in packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L24
|
||
) | ||
|
||
const promptOptions = await buildPromptOptions(extensionTemplates, specifications, app, options.generateOptions) | ||
const promptAnswers = await generateExtensionPrompts(promptOptions) | ||
|
||
const generateExtensionOptions = buildGenerateOptions( | ||
promptAnswers, | ||
app, | ||
options.generateOptions, | ||
developerPlatformClient, | ||
) | ||
const generatedExtension = await generateExtensionTemplate(generateExtensionOptions) | ||
renderSuccessMessage(generatedExtension, app.packageManager) | ||
} | ||
}, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {deepMergeObjects} from '@shopify/cli-kit/common/object' | ||
import {readFile, writeFile} from '@shopify/cli-kit/node/fs' | ||
import {zod} from '@shopify/cli-kit/node/schema' | ||
import {decodeToml, encodeToml} from '@shopify/cli-kit/node/toml' | ||
|
||
export interface PatchTomlOptions { | ||
Check failure on line 6 in packages/app/src/cli/services/generate/workflows/patch-configuration-file.ts GitHub Actions / knip-reporter-annotations-checkpackages/app/src/cli/services/generate/workflows/patch-configuration-file.ts#L6
|
||
path: string | ||
patch: {[key: string]: unknown} | ||
schema?: zod.AnyZodObject | ||
} | ||
|
||
function mergeArrayStrategy(existingArray: unknown[], newArray: unknown[]): unknown[] { | ||
if ( | ||
existingArray.length > 0 && | ||
existingArray[0] && | ||
typeof existingArray[0] === 'object' && | ||
newArray[0] && | ||
typeof newArray[0] === 'object' | ||
) { | ||
return [{...(existingArray[0] as object), ...(newArray[0] as object)}] | ||
Check failure on line 20 in packages/app/src/cli/services/generate/workflows/patch-configuration-file.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/patch-configuration-file.ts#L20
Check failure on line 20 in packages/app/src/cli/services/generate/workflows/patch-configuration-file.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/patch-configuration-file.ts#L20
|
||
} | ||
return newArray | ||
} | ||
|
||
export async function patchAppConfigurationFile({path, patch}: PatchTomlOptions) { | ||
const tomlContents = await readFile(path) | ||
const configuration = decodeToml(tomlContents) | ||
|
||
// Deep merge with custom array strategy | ||
const updatedConfig = deepMergeObjects(configuration, patch, mergeArrayStrategy) | ||
|
||
const encodedString = encodeToml(updatedConfig) | ||
await writeFile(path, encodedString) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {renderConfirmationPrompt} from '@shopify/cli-kit/node/ui' | ||
Check failure on line 1 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L1
|
||
import {Workflow} from './registry.js' | ||
import {generateExtensionTemplate} from '../extension.js' | ||
Check failure on line 3 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L3
|
||
import {generateExtensionPrompts} from '../../../prompts/generate/extension.js' | ||
Check failure on line 4 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L4
Check failure on line 4 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L4
|
||
import {buildGenerateOptions, renderSuccessMessage, buildPromptOptions} from '../../generate.js' | ||
Check failure on line 5 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L5
|
||
import {GenerateExtensionPromptOutput} from '../../../prompts/generate/extension.js' | ||
Check failure on line 6 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L6
Check failure on line 6 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L6
Check failure on line 6 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L6
Check failure on line 6 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts GitHub Actions / ESLint Report Analysispackages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L6
|
||
import {patchAppConfigurationFile} from './patch-configuration-file.js' | ||
|
||
export const productDiscountFunctionCollection: Workflow = { | ||
afterGenerate: async (options) => { | ||
const {app, developerPlatformClient, specifications} = options.generateOptions | ||
const functionTomlFilePath = `${options.generatedExtension.directory}/shopify.extension.toml` | ||
|
||
const shouldLinkExtension = await renderConfirmationPrompt({ | ||
message: 'Would you like to create a UI extension for your function?', | ||
defaultValue: true, | ||
}) | ||
|
||
if (shouldLinkExtension) { | ||
// create a UI extension | ||
const extensionTemplates = options.extensionTemplates.filter( | ||
(template) => template.identifier === 'discount_details_function_settings', | ||
) | ||
|
||
const promptOptions = await buildPromptOptions(extensionTemplates, specifications, app, options.generateOptions) | ||
const promptAnswers = await generateExtensionPrompts(promptOptions) | ||
|
||
const generateExtensionOptions = buildGenerateOptions( | ||
promptAnswers, | ||
app, | ||
options.generateOptions, | ||
developerPlatformClient, | ||
) | ||
const generatedExtension = await generateExtensionTemplate(generateExtensionOptions) | ||
|
||
const patch = { | ||
extensions: [ | ||
{ | ||
ui: { | ||
handle: generatedExtension.handle, | ||
}, | ||
}, | ||
], | ||
} | ||
|
||
await patchAppConfigurationFile({ | ||
path: functionTomlFilePath, | ||
patch, | ||
}) | ||
renderSuccessMessage(generatedExtension, app.packageManager) | ||
} | ||
}, | ||
} |