Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Block: Support Custom Prompts in External Templates #67806

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions packages/create-block/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ program

const filterOptionsProvided = ( { name } ) =>
! Object.keys( optionsValues ).includes( name );

// Get all prompts in one call
const blockPrompts = getPrompts(
pluginTemplate,
[
Expand All @@ -159,9 +161,11 @@ program
'description',
'dashicon',
'category',
...(pluginTemplate.customPrompts ? Object.keys(pluginTemplate.customPrompts) : [])
eduwass marked this conversation as resolved.
Show resolved Hide resolved
],
variant
).filter( filterOptionsProvided );

const blockAnswers = await inquirer.prompt( blockPrompts );

const pluginAnswers = plugin
Expand Down Expand Up @@ -189,6 +193,7 @@ program
'domainPath',
'updateURI',
],
...(pluginTemplate.customPrompts ? Object.keys(pluginTemplate.customPrompts) : []),
eduwass marked this conversation as resolved.
Show resolved Hide resolved
variant
).filter( filterOptionsProvided );
const result =
Expand Down
2 changes: 2 additions & 0 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module.exports = async (
customBlockJSON,
example,
transformer,
...customPromptValues
}
) => {
slug = slug.toLowerCase();
Expand Down Expand Up @@ -97,6 +98,7 @@ module.exports = async (
example,
textdomain: slug,
rootDirectory,
...customPromptValues
} );

const view = {
Expand Down
51 changes: 45 additions & 6 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,24 @@ const configToTemplate = async ( {
assetsPath,
defaultValues = {},
variants = {},
customPrompts = {},
...deprecated
} ) => {
if ( defaultValues === null || typeof defaultValues !== 'object' ) {
throw new CLIError( 'Template found but invalid definition provided.' );
}

if (customPrompts !== null && typeof customPrompts !== 'object') {
eduwass marked this conversation as resolved.
Show resolved Hide resolved
throw new CLIError( 'Invalid custom prompts definition provided.' );
}

// Validate custom prompts format
for (const [name, prompt] of Object.entries(customPrompts)) {
eduwass marked this conversation as resolved.
Show resolved Hide resolved
if (!prompt.type || !prompt.name || !prompt.message) {
eduwass marked this conversation as resolved.
Show resolved Hide resolved
throw new CLIError(`Invalid custom prompt "${name}". Each prompt must have type, name, and message properties.`);
eduwass marked this conversation as resolved.
Show resolved Hide resolved
}
}

if ( deprecated.templatesPath ) {
pluginTemplatesPath = deprecated.templatesPath;
defaultValues = {
Expand All @@ -154,6 +166,7 @@ const configToTemplate = async ( {
outputAssets: assetsPath ? await getOutputAssets( assetsPath ) : {},
defaultValues,
variants,
customPrompts,
};
};

Expand Down Expand Up @@ -225,6 +238,14 @@ const getPluginTemplate = async ( templateName ) => {
};

const getDefaultValues = ( pluginTemplate, variant ) => {
// Get custom prompt default values
const customPromptDefaults = {};
if (pluginTemplate.customPrompts) {
eduwass marked this conversation as resolved.
Show resolved Hide resolved
for (const [name, prompt] of Object.entries(pluginTemplate.customPrompts)) {
eduwass marked this conversation as resolved.
Show resolved Hide resolved
customPromptDefaults[name] = prompt.default || '';
eduwass marked this conversation as resolved.
Show resolved Hide resolved
}
}

return {
$schema: 'https://schemas.wp.org/trunk/block.json',
apiVersion: 3,
Expand All @@ -243,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 ),
Expand All @@ -251,12 +273,29 @@ const getDefaultValues = ( pluginTemplate, variant ) => {

const getPrompts = ( pluginTemplate, keys, variant ) => {
const defaultValues = getDefaultValues( pluginTemplate, variant );
return keys.map( ( promptName ) => {
return {
...prompts[ promptName ],
default: defaultValues[ promptName ],
};
} );
let allPrompts = [];

// Add built-in prompts if keys are provided
if (keys && keys.length > 0) {
eduwass marked this conversation as resolved.
Show resolved Hide resolved
for (const key of keys) {
eduwass marked this conversation as resolved.
Show resolved Hide resolved
// If it's a built-in prompt
if (prompts[key]) {
eduwass marked this conversation as resolved.
Show resolved Hide resolved
allPrompts.push({
eduwass marked this conversation as resolved.
Show resolved Hide resolved
...prompts[key],
eduwass marked this conversation as resolved.
Show resolved Hide resolved
default: defaultValues[key],
eduwass marked this conversation as resolved.
Show resolved Hide resolved
});
}
// If it's a custom prompt
else if (pluginTemplate.customPrompts?.[key]) {
eduwass marked this conversation as resolved.
Show resolved Hide resolved
allPrompts.push({
eduwass marked this conversation as resolved.
Show resolved Hide resolved
...pluginTemplate.customPrompts[key],
eduwass marked this conversation as resolved.
Show resolved Hide resolved
default: defaultValues[key] || pluginTemplate.customPrompts[key].default || '',
eduwass marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
}

return allPrompts;
};

const getVariantVars = ( variants, variant ) => {
Expand Down
Loading