diff --git a/packages/create-block/lib/index.js b/packages/create-block/lib/index.js index ee20f31a7b3163..fb3f3110459f90 100644 --- a/packages/create-block/lib/index.js +++ b/packages/create-block/lib/index.js @@ -150,23 +150,21 @@ program const filterOptionsProvided = ( { name } ) => ! Object.keys( optionsValues ).includes( name ); - const blockPrompts = [ - // Get built-in block prompts - ...getPrompts( - pluginTemplate, - [ - 'slug', - 'namespace', - 'title', - 'description', - 'dashicon', - 'category', - ], - variant - ), - // Get custom prompts - ...getPrompts(pluginTemplate, null, variant) - ].filter( filterOptionsProvided ); + + // Get all prompts in one call + const blockPrompts = getPrompts( + pluginTemplate, + [ + 'slug', + 'namespace', + 'title', + 'description', + 'dashicon', + 'category', + ...(pluginTemplate.customPrompts ? Object.keys(pluginTemplate.customPrompts) : []) + ], + variant + ).filter( filterOptionsProvided ); const blockAnswers = await inquirer.prompt( blockPrompts ); @@ -195,6 +193,7 @@ program 'domainPath', 'updateURI', ], + ...(pluginTemplate.customPrompts ? Object.keys(pluginTemplate.customPrompts) : []), variant ).filter( filterOptionsProvided ); const result = diff --git a/packages/create-block/lib/scaffold.js b/packages/create-block/lib/scaffold.js index 73b9f549908867..71471036e44bcf 100644 --- a/packages/create-block/lib/scaffold.js +++ b/packages/create-block/lib/scaffold.js @@ -54,6 +54,7 @@ module.exports = async ( customBlockJSON, example, transformer, + ...customPromptValues } ) => { slug = slug.toLowerCase(); @@ -97,6 +98,7 @@ module.exports = async ( example, textdomain: slug, rootDirectory, + ...customPromptValues } ); const view = { diff --git a/packages/create-block/lib/templates.js b/packages/create-block/lib/templates.js index fc29ffde6213cf..c03d10b7770f60 100644 --- a/packages/create-block/lib/templates.js +++ b/packages/create-block/lib/templates.js @@ -238,6 +238,14 @@ const getPluginTemplate = async ( templateName ) => { }; const getDefaultValues = ( pluginTemplate, variant ) => { + // Get custom prompt default values + const customPromptDefaults = {}; + if (pluginTemplate.customPrompts) { + for (const [name, prompt] of Object.entries(pluginTemplate.customPrompts)) { + customPromptDefaults[name] = prompt.default || ''; + } + } + return { $schema: 'https://schemas.wp.org/trunk/block.json', apiVersion: 3, @@ -256,6 +264,7 @@ const getDefaultValues = ( pluginTemplate, variant ) => { editorStyle: 'file:./index.css', style: 'file:./style-index.css', transformer: ( view ) => view, + ...customPromptDefaults, ...pluginTemplate.defaultValues, ...pluginTemplate.variants?.[ variant ], variantVars: getVariantVars( pluginTemplate.variants, variant ), @@ -264,27 +273,29 @@ const getDefaultValues = ( pluginTemplate, variant ) => { const getPrompts = ( pluginTemplate, keys, variant ) => { const defaultValues = getDefaultValues( pluginTemplate, variant ); - - // Get built-in prompts if keys are provided - const builtInPrompts = keys ? keys.map( ( promptName ) => { - return { - ...prompts[ promptName ], - default: defaultValues[ promptName ], - }; - }) : []; + let allPrompts = []; - // Add custom prompts if they exist - const customPromptsList = []; - if (pluginTemplate.customPrompts) { - for (const [name, prompt] of Object.entries(pluginTemplate.customPrompts)) { - customPromptsList.push({ - ...prompt, - default: defaultValues[name], - }); + // Add built-in prompts if keys are provided + if (keys && keys.length > 0) { + for (const key of keys) { + // If it's a built-in prompt + if (prompts[key]) { + allPrompts.push({ + ...prompts[key], + default: defaultValues[key], + }); + } + // If it's a custom prompt + else if (pluginTemplate.customPrompts?.[key]) { + allPrompts.push({ + ...pluginTemplate.customPrompts[key], + default: defaultValues[key] || pluginTemplate.customPrompts[key].default || '', + }); + } } } - return [...builtInPrompts, ...customPromptsList]; + return allPrompts; }; const getVariantVars = ( variants, variant ) => {