Skip to content

Commit

Permalink
Create Block: Make it possible to provide most popular options as CLI…
Browse files Browse the repository at this point in the history
… options
  • Loading branch information
gziolo committed Apr 24, 2020
1 parent 605f47d commit 3ad8ede
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Master

### New Features

- Add more CLI options: `--namespace`, `--title`, `--description` and `--category`. The goal is to make it easier to override default values used for scaffolding ([#21751](https://github.com/WordPress/gutenberg/pull/21751)).

### Enhancements

- Update `esnext` template to scaffold 3 JavaScript source files to illustrate how ES modules help to better organize code.
Expand Down
14 changes: 9 additions & 5 deletions packages/create-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ The following command generates PHP, JS and CSS code for registering a block.
$ npm init @wordpress/block [options] [slug]
```

`[slug]` is optional. When provided it triggers the quick mode where it is used as the block slug used for its identification, the output location for scaffolded files, and the name of the WordPress plugin. The rest of the configuration is set to all default values.
`[slug]` is optional. When provided it triggers the quick mode where it is used as the block slug used for its identification, the output location for scaffolded files, and the name of the WordPress plugin. The rest of the configuration is set to all default values unless overriden with some of the options listed below.

Options:
```bash
-t, --template <name> template type name, allowed values: "es5", "esnext" (default: "esnext")
-V, --version output the version number
-h, --help output usage information
```
-V, --version output the version number
-t, --template <name> template type name, allowed values: "es5", "esnext" (default: "esnext")
--namespace <namespace> internal namespace for the block name
--title <title> display title for the block
--description <description> short description for the block
--category <category> category name for the block
-h, --help output usage information
```
_Please note that `--version` and `--help` options don't work with `npm init`. You have to use `npx` instead, as presented in the examples._
Expand Down
44 changes: 37 additions & 7 deletions packages/create-block/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ program
.name( commandName )
.description(
'Generates PHP, JS and CSS code for registering a block for a WordPress plugin.\n\n' +
'[slug] is optional. When provided it triggers the quick mode where it is used ' +
'as the block slug used for its identification, the output location for scaffolded files, ' +
'and the name of the WordPress plugin. The rest of the configuration is set to all default values.'
'[slug] is optional. When provided it triggers the quick mode where ' +
'it is used as the block slug used for its identification, the output ' +
'location for scaffolded files, and the name of the WordPress plugin.' +
'The rest of the configuration is set to all default values unless ' +
'overriden with some of the options listed below.'
)
.version( version )
.arguments( '[slug]' )
Expand All @@ -31,22 +33,48 @@ program
'template type name, allowed values: "es5", "esnext"',
'esnext'
)
.action( async ( slug, { template } ) => {
.option(
'--namespace <namespace>',
'internal namespace for the block name'
)
.option( '--title <title>', 'display title for the block' )
.option( '--description <description>', 'short description for the block' )
.option( '--category <category>', 'category name for the block' )
.action( async ( slug, commandObject ) => {
const { template } = commandObject;
const optionsValues = [
'namespace',
'title',
'description',
'category',
]
.filter( ( optionName ) => commandObject[ optionName ] )
.reduce( ( accumulator, optionName ) => {
accumulator[ optionName ] = commandObject[ optionName ];
return accumulator;
}, {} );

await checkSystemRequirements( engines );
try {
const defaultValues = getDefaultValues( template );
if ( slug ) {
const answers = {
...defaultValues,
slug,
// Transforms slug to title.
// Transforms slug to title as a fallback.
title: startCase( slug ),
...optionsValues,
};
await scaffold( template, answers );
} else {
const answers = await inquirer.prompt( getPrompts( template ) );
const propmpts = getPrompts( template ).filter(
( { name } ) =>
! Object.keys( optionsValues ).includes( name )
);
const answers = await inquirer.prompt( propmpts );
await scaffold( template, {
...defaultValues,
...optionsValues,
...answers,
} );
}
Expand All @@ -64,6 +92,8 @@ program
log.info( 'Examples:' );
log.info( ` $ ${ commandName }` );
log.info( ` $ ${ commandName } todo-list` );
log.info( ` $ ${ commandName } --template es5 todo-list` );
log.info(
` $ ${ commandName } todo-list --template es5 --title "TODO List"`
);
} )
.parse( process.argv );

0 comments on commit 3ad8ede

Please sign in to comment.