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 all 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 ) : [] )
],
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 ) : [] ),
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' ) {
throw new CLIError( 'Invalid custom prompts definition provided.' );
}

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

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 ) {
for ( const [ name, prompt ] of Object.entries( pluginTemplate.customPrompts ) ) {
customPromptDefaults[ name ] = prompt.default || '';
}
}

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 ) {
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 allPrompts;
};

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