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 3b81c76 commit ffd2c4b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 38 deletions.
3 changes: 1 addition & 2 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 Down Expand Up @@ -109,7 +109,6 @@ const generateExtensionPrompts = async (
// 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
39 changes: 4 additions & 35 deletions packages/app/src/cli/services/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,62 +41,32 @@ export interface GenerateOptions {
async function generate(options: GenerateOptions) {
const {app, developerPlatformClient, remoteApp, specifications, template} = options

// console.log('GENERATE', options)

const availableSpecifications = specifications.map((spec) => spec.identifier)
const extensionTemplates = await fetchExtensionTemplates(developerPlatformClient, remoteApp, availableSpecifications)

const promptOptions = await buildPromptOptions(extensionTemplates, specifications, app, options)
const promptAnswers = await generateExtensionPrompts(promptOptions)

// Add related extension children for product discounts
if (promptAnswers.extensionTemplate.identifier === 'product_discounts') {
const settingsTemplate = extensionTemplates.find((template) => template.identifier === 'discount_function_settings')
if (settingsTemplate) {
const mockSettingsPromptAnswers: GenerateExtensionPromptOutput = {
extensionTemplate: settingsTemplate,
extensionContent: {
name: `${promptAnswers.extensionContent.name}-settings`,
flavor: 'react',
relatedExtensions: [],
},
}

// Generate the settings extension
const settingsGenerateOptions = buildGenerateOptions(
mockSettingsPromptAnswers,
app,
options,
developerPlatformClient,
)
await generateExtensionTemplate(settingsGenerateOptions)
}
}

console.log('PROMPT ANSWERS', promptAnswers)
// 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)

const workflow = workflowRegistry[generatedExtension.extensionTemplate.identifier]
await workflow?.afterGenerate({
generateOptions: options,
extensionTemplateOptions: generateExtensionOptions,
extensionTemplates: extensionTemplates,

Check failure on line 59 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#L59

[object-shorthand] Expected property shorthand.
})

renderSuccessMessage(generatedExtension, app.packageManager)
}

async function buildPromptOptions(
export async function buildPromptOptions(
extensionTemplates: ExtensionTemplate[],
specifications: ExtensionSpecification[],
app: AppInterface,
options: GenerateOptions,
): Promise<GenerateExtensionPromptOptions> {
// console.log('BUILDING PROMPT OPTIONS', arguments)
const extensionTemplate = await handleTypeParameter(options.template, app, extensionTemplates, specifications)
validateExtensionFlavor(extensionTemplate, options.flavor)

Expand Down Expand Up @@ -128,7 +98,6 @@ function checkLimits(
}

function limitReached(app: AppInterface, specifications: ExtensionSpecification[], template: ExtensionTemplate) {
// console.log('CHECKING LIMITS', specifications)
const type = template.type
const specification = specifications.find((spec) => spec.identifier === type || spec.externalIdentifier === type)
const existingExtensions = app.extensionsForType({identifier: type, externalIdentifier: type})
Expand All @@ -145,7 +114,7 @@ async function saveAnalyticsMetadata(promptAnswers: GenerateExtensionPromptOutpu
}))
}

function buildGenerateOptions(
export function buildGenerateOptions(
promptAnswers: GenerateExtensionPromptOutput,
app: AppInterface,
options: GenerateOptions,
Expand All @@ -160,7 +129,7 @@ function buildGenerateOptions(
}
}

function renderSuccessMessage(extension: GeneratedExtension, packageManager: AppInterface['packageManager']) {
export function renderSuccessMessage(extension: GeneratedExtension, packageManager: AppInterface['packageManager']) {
const formattedSuccessfulMessage = formatSuccessfulRunMessage(
extension.extensionTemplate,
extension.directory,
Expand Down
1 change: 0 additions & 1 deletion packages/app/src/cli/services/generate/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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 85 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#L85

[@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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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) => {
console.log('OPTIONS', options)

Check failure on line 10 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#L10

[no-console] Unexpected console statement.
const {app, developerPlatformClient, remoteApp, specifications, template} = options.generateOptions

Check failure on line 11 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#L11

[@typescript-eslint/no-unused-vars] 'remoteApp' is assigned a value but never used. Allowed unused vars must match /^_/u.

Check failure on line 11 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#L11

[@typescript-eslint/no-unused-vars] 'template' is assigned a value but never used. Allowed unused vars must match /^_/u.

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 25 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#L25

[eqeqeq] Expected '===' and instead saw '=='.
)
console.log('FUNCTION specs', specifications)

Check failure on line 27 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#L27

[no-console] Unexpected console statement.
console.log('FUNCTION TEMPLATES', extensionTemplates)

Check failure on line 28 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#L28

[no-console] Unexpected console statement.

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)
}
},
}
4 changes: 4 additions & 0 deletions packages/app/src/cli/services/generate/workflows/registry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {editorExtensionCollection} from './editor-extension-collection.js'
import {GenerateOptions} from '../../generate.js'
import {GenerateExtensionTemplateOptions} from '../../generate/extension.js'
import {discountDetailsFunctionSettingsCollection} from './discount-details-function-settings-collection.js'

Check failure on line 4 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#L4

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

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

export interface Workflow {
Expand All @@ -17,4 +20,5 @@ interface WorkflowRegistry {

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

0 comments on commit ffd2c4b

Please sign in to comment.