Skip to content

Commit

Permalink
Improve the handling of custom prompts to streamline their integratio…
Browse files Browse the repository at this point in the history
…n into the block creation process.

- Merge built-in and custom prompts into a single array.
- Allow custom prompts to be included wherever keys are specified.
- Ensure default values for custom prompts are correctly retrieved and utilized.
- Simplify the logic for aggregating both built-in and custom prompts into a single comprehensive list.

This change enhances flexibility for developers using custom prompts with create-block.
  • Loading branch information
eduwass committed Dec 10, 2024
1 parent dea1fba commit efe3c12
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
33 changes: 16 additions & 17 deletions packages/create-block/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -195,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
45 changes: 28 additions & 17 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ),
Expand All @@ -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 ) => {
Expand Down

0 comments on commit efe3c12

Please sign in to comment.