Skip to content

Commit

Permalink
Final touches after refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Dec 17, 2024
1 parent ac57812 commit 9452a2e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 162 deletions.
132 changes: 4 additions & 128 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 7 additions & 18 deletions packages/create-block/lib/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const upperFirst = ( [ firstLetter, ...rest ] ) =>
// Block metadata.
const slug = {
type: 'input',
name: 'slug',
message:
'The block slug used for identification (also the output folder name):',
validate( input ) {
Expand All @@ -25,7 +24,6 @@ const slug = {

const namespace = {
type: 'input',
name: 'namespace',
message:
'The internal namespace for the block name (something unique for your products):',
validate( input ) {
Expand All @@ -39,25 +37,22 @@ const namespace = {

const title = {
type: 'input',
name: 'title',
message: 'The display title for your block:',
filter( input ) {
transformer( input ) {
return input && upperFirst( input );
},
};

const description = {
type: 'input',
name: 'description',
message: 'The short description for your block (optional):',
filter( input ) {
transformer( input ) {
return input && upperFirst( input );
},
};

const dashicon = {
type: 'input',
name: 'dashicon',
message:
'The dashicon to make it easier to identify your block (optional):',
validate( input ) {
Expand All @@ -67,29 +62,28 @@ const dashicon = {

return true;
},
filter( input ) {
transformer( input ) {
return input && input.replace( /dashicon(s)?-/, '' );
},
};

const category = {
type: 'list',
name: 'category',
type: 'select',
message: 'The category name to help users browse and discover your block:',
choices: [ 'text', 'media', 'design', 'widgets', 'theme', 'embed' ],
choices: [ 'text', 'media', 'design', 'widgets', 'theme', 'embed' ].map(
( value ) => ( { value } )
),
};

// Plugin header fields.
const pluginURI = {
type: 'input',
name: 'pluginURI',
message:
'The home page of the plugin (optional). Unique URL outside of WordPress.org:',
};

const version = {
type: 'input',
name: 'version',
message: 'The current version number of the plugin:',
validate( input ) {
// Regular expression was copied from https://semver.org.
Expand All @@ -105,32 +99,27 @@ const version = {

const author = {
type: 'input',
name: 'author',
message:
'The name of the plugin author (optional). Multiple authors may be listed using commas:',
};

const license = {
type: 'input',
name: 'license',
message: 'The short name of the plugin’s license (optional):',
};

const licenseURI = {
type: 'input',
name: 'licenseURI',
message: 'A link to the full text of the license (optional):',
};

const domainPath = {
type: 'input',
name: 'domainPath',
message: 'A custom domain path for the translations (optional):',
};

const updateURI = {
type: 'input',
name: 'updateURI',
message: 'A custom update URI for the plugin (optional):',
};

Expand Down
36 changes: 21 additions & 15 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* External dependencies
*/
const inquirer = require( '@inquirer/prompts' );
const { command } = require( 'execa' );
const glob = require( 'fast-glob' );
const { resolve } = require( 'path' );
const { existsSync } = require( 'fs' );
const { mkdtemp, readFile } = require( 'fs' ).promises;
const inquirer = require( 'inquirer' );
const npmPackageArg = require( 'npm-package-arg' );
const { tmpdir } = require( 'os' );
const { join } = require( 'path' );
Expand Down Expand Up @@ -250,21 +250,27 @@ const getDefaultValues = ( pluginTemplate, variant ) => {
};
};

const runPrompts = async ( pluginTemplate, keys, variant, optionsValues ) => {
const runPrompts = async (
pluginTemplate,
promptNames,
variant,
optionsValues
) => {
const defaultValues = getDefaultValues( pluginTemplate, variant );
await inquirer.prompt(
keys
.filter(
( promptName ) =>
! Object.keys( optionsValues ).includes( promptName )
)
.map( ( promptName ) => {
return {
...prompts[ promptName ],
default: defaultValues[ promptName ],
};
} )
);
const result = {};
for ( const promptName of promptNames ) {
if ( Object.keys( optionsValues ).includes( promptName ) ) {
continue;
}

const { type, ...config } = prompts[ promptName ];
result[ promptName ] = await inquirer[ type ]( {
...config,
default: defaultValues[ promptName ],
} );
}

return result;
};

const getVariantVars = ( variants, variant ) => {
Expand Down
1 change: 0 additions & 1 deletion packages/create-block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"commander": "^9.2.0",
"execa": "^4.0.2",
"fast-glob": "^3.2.7",
"inquirer": "^12.2.0",
"make-dir": "^3.0.0",
"mustache": "^4.0.0",
"npm-package-arg": "^8.1.5",
Expand Down

0 comments on commit 9452a2e

Please sign in to comment.