Skip to content

Commit

Permalink
Add workflow for discount function settings
Browse files Browse the repository at this point in the history
  • Loading branch information
devisscher committed Dec 12, 2024
1 parent bb785a9 commit 8dfb3bc
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 21 deletions.
19 changes: 10 additions & 9 deletions packages/app/src/cli/prompts/generate/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function buildChoices(extensionTemplates: ExtensionTemplate[], unavailabl
return templateSpecChoices.sort(compareChoices)
}

const generateExtensionPrompts = async (
export const generateExtensionPrompts = async (

Check failure on line 76 in packages/app/src/cli/prompts/generate/extension.ts

View workflow job for this annotation

GitHub Actions / knip-reporter-annotations-check

packages/app/src/cli/prompts/generate/extension.ts#L76

'generateExtensionPrompts' is a duplicate of 'default'
options: GenerateExtensionPromptOptions,
): Promise<GenerateExtensionPromptOutput> => {
let extensionTemplates = options.extensionTemplates
Expand All @@ -90,11 +90,15 @@ const generateExtensionPrompts = async (
throw new AbortError('You have reached the limit for the number of extensions you can create.')
}

// eslint-disable-next-line require-atomic-updates
templateType = await renderAutocompletePrompt({
message: 'Type of extension?',
choices: buildChoices(extensionTemplates, options.unavailableExtensions),
})
if (extensionTemplates.length === 1) {
templateType = extensionTemplates[0]?.identifier
} else {
// eslint-disable-next-line require-atomic-updates
templateType = await renderAutocompletePrompt({
message: 'Type of extension?',
choices: buildChoices(extensionTemplates, options.unavailableExtensions),
})
}
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand All @@ -105,10 +109,7 @@ const generateExtensionPrompts = async (
const extensionContent = {
name,
flavor,
// extensionChildren [identifiers]
extensionChildren: extensionTemplate.relatedExtensions?.map((elem) => elem.name) ?? [],
}
// TODO: here perhaps return a flag, that lets us know that there are possible next steps

return {extensionTemplate, extensionContent}
}
Expand Down
23 changes: 15 additions & 8 deletions packages/app/src/cli/services/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,21 @@ async function generate(options: GenerateOptions) {
const promptOptions = await buildPromptOptions(extensionTemplates, specifications, app, options)
const promptAnswers = await generateExtensionPrompts(promptOptions)

// Call module related to that child extension
await saveAnalyticsMetadata(promptAnswers, template)

// We can add an extra generated extension here.
const generateExtensionOptions = buildGenerateOptions(promptAnswers, app, options, developerPlatformClient)
const generatedExtension = await generateExtensionTemplate(generateExtensionOptions)

renderSuccessMessage(generatedExtension, app.packageManager)
const workflow = workflowRegistry[generatedExtension.extensionTemplate.identifier]
if (!workflow) {
renderSuccessMessage(generatedExtension, app.packageManager)
}

const workflowResult = await workflow?.afterGenerate({
generateOptions: options,
extensionTemplateOptions: generateExtensionOptions,
generatedExtension,
extensionTemplateOptions: generateExtensionOptions,
extensionTemplates: extensionTemplates,

Check failure on line 63 in packages/app/src/cli/services/generate.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate.ts#L63

[object-shorthand] Expected property shorthand.
})

if (!workflowResult?.success) {
Expand All @@ -71,7 +70,7 @@ async function generate(options: GenerateOptions) {
renderSuccessMessage(generatedExtension, app.packageManager, workflowResult)
}

async function buildPromptOptions(
export async function buildPromptOptions(
extensionTemplates: ExtensionTemplate[],
specifications: ExtensionSpecification[],
app: AppInterface,
Expand Down Expand Up @@ -123,7 +122,7 @@ async function saveAnalyticsMetadata(promptAnswers: GenerateExtensionPromptOutpu
}))
}

function buildGenerateOptions(
export function buildGenerateOptions(
promptAnswers: GenerateExtensionPromptOutput,
app: AppInterface,
options: GenerateOptions,
Expand All @@ -138,7 +137,11 @@ function buildGenerateOptions(
}
}

function renderSuccessMessage(extension: GeneratedExtension, packageManager: AppInterface['packageManager'], workflowResult?: WorkflowResult) {
function renderSuccessMessage(
extension: GeneratedExtension,
packageManager: AppInterface['packageManager'],
workflowResult?: WorkflowResult,
) {
const formattedSuccessfulMessage = formatSuccessfulRunMessage(
extension.extensionTemplate,
extension.directory,
Expand Down Expand Up @@ -169,7 +172,11 @@ function formatSuccessfulRunMessage(
): RenderAlertOptions {
const workflowMessage = workflowResult?.message
const options: RenderAlertOptions = {
headline: workflowMessage?.headline || ['Your extension was created in', {filePath: extensionDirectory}, {char: '.'}],
headline: workflowMessage?.headline || [

Check warning on line 175 in packages/app/src/cli/services/generate.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate.ts#L175

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
'Your extension was created in',
{filePath: extensionDirectory},
{char: '.'},
],
nextSteps: workflowMessage?.nextSteps || [],

Check warning on line 180 in packages/app/src/cli/services/generate.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate.ts#L180

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

Check warning on line 181 in packages/app/src/cli/services/generate.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate.ts#L181

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
}
Expand Down
10 changes: 7 additions & 3 deletions packages/app/src/cli/services/generate/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function getTemplateLanguage(flavor: ExtensionFlavorValue | undefined): Template
export interface GeneratedExtension {
directory: string
extensionTemplate: ExtensionTemplate
handle: string
}

interface ExtensionInitOptions {
Expand All @@ -81,7 +82,6 @@ export async function generateExtensionTemplate(
const extensionFlavor = options.extensionTemplate.supportedFlavors.find(
(flavor) => flavor.value === extensionFlavorValue,
)
// TODO For grouping we might want to setup directories/groups for extensions
const directory = await ensureExtensionDirectoryExists({app: options.app, name: extensionName})
const url = options.cloneUrl || options.extensionTemplate.url

Check warning on line 86 in packages/app/src/cli/services/generate/extension.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/extension.ts#L86

[@typescript-eslint/prefer-nullish-coalescing] Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator.
const uid = options.developerPlatformClient.supportsAtomicDeployments ? randomUUID() : undefined
Expand All @@ -100,7 +100,11 @@ export async function generateExtensionTemplate(
}),
}
await extensionInit(initOptions)
return {directory: relativizePath(directory), extensionTemplate: options.extensionTemplate}
return {
directory: relativizePath(directory),
extensionTemplate: options.extensionTemplate,
handle: slugify(extensionName),
}
}

async function extensionInit(options: ExtensionInitOptions) {
Expand Down Expand Up @@ -225,7 +229,7 @@ async function uiExtensionInit({
onGetTemplateRepository,
}: ExtensionInitOptions) {
const templateLanguage = getTemplateLanguage(extensionFlavor?.value)

console.log('extensionName', name)

Check failure on line 232 in packages/app/src/cli/services/generate/extension.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/extension.ts#L232

[no-console] Unexpected console statement.
const tasks = [
{
title: `Generating extension`,
Expand Down
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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L1

[import/order] `@shopify/cli-kit/node/ui` import should occur after import of `../../../prompts/generate/extension.js`
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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L4

[import/no-duplicates] '../../../prompts/generate/extension.js' imported multiple times.
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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L6

[unused-imports/no-unused-imports] 'GenerateExtensionPromptOutput' is defined but never used.

Check failure on line 6 in packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L6

[@typescript-eslint/no-unused-vars] 'GenerateExtensionPromptOutput' is defined but never used. Allowed unused vars must match /^_/u.

Check failure on line 6 in packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L6

[import/no-duplicates] '../../../prompts/generate/extension.js' imported multiple times.

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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/discount-details-function-settings-collection.ts#L24

[eqeqeq] Expected '===' and instead saw '=='.
)

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,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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L1

[import/order] `@shopify/cli-kit/node/ui` import should occur after import of `./patch-configuration-file.js`
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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L3

[import/order] `../extension.js` import should occur after import of `./patch-configuration-file.js`
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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L4

[import/order] `../../../prompts/generate/extension.js` import should occur after import of `./patch-configuration-file.js`

Check failure on line 4 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L4

[import/no-duplicates] '../../../prompts/generate/extension.js' imported multiple times.
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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L5

[import/order] `../../generate.js` import should occur after import of `./patch-configuration-file.js`
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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L6

[import/order] `../../../prompts/generate/extension.js` import should occur after import of `./patch-configuration-file.js`

Check failure on line 6 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L6

[unused-imports/no-unused-imports] 'GenerateExtensionPromptOutput' is defined but never used.

Check failure on line 6 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L6

[@typescript-eslint/no-unused-vars] 'GenerateExtensionPromptOutput' is defined but never used. Allowed unused vars must match /^_/u.

Check failure on line 6 in packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/product-discount-function-collection.ts#L6

[import/no-duplicates] '../../../prompts/generate/extension.js' imported multiple times.
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)
}
},
}
8 changes: 7 additions & 1 deletion packages/app/src/cli/services/generate/workflows/registry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {editorExtensionCollection} from './editor-extension-collection.js'
import {GenerateOptions} from '../../generate.js'
import {GeneratedExtension, GenerateExtensionTemplateOptions} from '../../generate/extension.js'
import { RenderAlertOptions } from '@shopify/cli-kit/node/ui'
import {RenderAlertOptions} from '@shopify/cli-kit/node/ui'
import {discountDetailsFunctionSettingsCollection} from './discount-details-function-settings-collection.js'

Check failure on line 5 in packages/app/src/cli/services/generate/workflows/registry.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/registry.ts#L5

[import/order] `./discount-details-function-settings-collection.js` import should occur before import of `../../generate.js`
import {productDiscountFunctionCollection} from './product-discount-function-collection.js'

Check failure on line 6 in packages/app/src/cli/services/generate/workflows/registry.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/registry.ts#L6

[import/order] `./product-discount-function-collection.js` import should occur before import of `../../generate.js`
import {ExtensionTemplate} from '../../../models/app/template.js'

Check failure on line 7 in packages/app/src/cli/services/generate/workflows/registry.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/app/src/cli/services/generate/workflows/registry.ts#L7

[import/order] `../../../models/app/template.js` import should occur before import of `@shopify/cli-kit/node/ui`

interface AfterGenerateOptions {
generateOptions: GenerateOptions
extensionTemplateOptions: GenerateExtensionTemplateOptions
extensionTemplates: ExtensionTemplate[]
generatedExtension: GeneratedExtension
}

Expand All @@ -24,4 +28,6 @@ interface WorkflowRegistry {

export const workflowRegistry: WorkflowRegistry = {
editor_extension_collection: editorExtensionCollection,
discount_details_function_settings: discountDetailsFunctionSettingsCollection,
product_discounts: productDiscountFunctionCollection,
}

0 comments on commit 8dfb3bc

Please sign in to comment.