Skip to content
Closed
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
53 changes: 50 additions & 3 deletions packages/create-block/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $ npm init @wordpress/block [options] [slug]
Options:
```
-V, --version output the version number
-t, --template <name> template type name, allowed values: "es5", "esnext" (default: "esnext")
-t, --template <name> template name: "es5", "esnext" or the name of an external npm package (default: "esnext")
--namespace <value> internal namespace for the block name
--title <value> display title for the block
--short-description <value> short description for the block
Expand All @@ -65,9 +65,9 @@ More examples:

When you scaffold a block, you must provide at least a `slug` name, the `namespace` which usually corresponds to either the `theme` or `plugin` name, and the `category`. In most cases, we recommended pairing blocks with plugins rather than themes, because only using plugin ensures that all blocks still work when your theme changes.

## Available Commands
## Available Commands [ESNext template]

Inside that bootstrapped directory _(it doesn't apply to `es5` template)_, you can run several commands:
When bootstraped with the `esnext` template, you can run several commands inside the directory:

```bash
$ npm start
Expand Down Expand Up @@ -99,6 +99,53 @@ $ npm run packages-update
```
Updates WordPress packages to the latest version. [Learn more](/packages/scripts#packages-update).

## External Templates
Since version 0.12.0 it is possible to use external templates hosted on NPM. These templates need to contain `.mustache` files that will be used in the scaffolding and one `template.json` for the metadata.

### Available Variables:
- `namespace`
- `slug`
- `title`
- `textdomain`
- `description`
- `category`
- `dashicon`
- `license`
- `licenseURI`
- `namespaceSnakeCase`
- `slugSnakeCase`
Comment on lines +115 to +116

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespaceSnakeCase and slugSnakeCase ar sort of special here. We should omit them and find a way to apply those modifications inside the template.


### `template.json`
```json
{
"defaultValues": {
"namespace": "create-block",
"slug": "esnext-example",
"title": "ESNext Example",
"description":
"Example block written with ESNext standard and JSX support – build step required.",
"dashicon": "smiley",
"category": "widgets",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"licenseURI": "https://www.gnu.org/licenses/gpl-2.0.html",
"version": "0.1.0"
},
"outputFiles": [

@gziolo gziolo May 8, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the future PR:

I'm very interested to find a way to automate the discovery of templates with glob call or something like this. It would be great to be able to default to templates folder that the template author can override:

{
    "templatesFolder": "./my-folder"
}

".editorconfig",
".gitignore",
"editor.css",
"src/edit.js",
"src/index.js",
"src/save.js",
"$slug.php",
"style.css",
"readme.txt"
],
"wpScriptsEnabled": true

@gziolo gziolo May 8, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the future PR:

Thinking about it now when documented, it should be opt-out rather than opt-in, we want people to use wp-scripts :)

}
```

## WP-CLI

Another way of making a developer’s life easier is to use [WP-CLI](https://wp-cli.org), which provides a command-line interface for many actions you might perform on the WordPress instance. One of the commands `wp scaffold block` was used as the baseline for this tool and ES5 template in particular.
Expand Down
33 changes: 22 additions & 11 deletions packages/create-block/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const CLIError = require( './cli-error' );
const log = require( './log' );
const { engines, version } = require( '../package.json' );
const scaffold = require( './scaffold' );
const { getDefaultValues, getPrompts } = require( './templates' );
const {
getBlockTemplate,
getDefaultValues,
getPrompts,
} = require( './templates' );

const commandName = `wp-create-block`;
program
Expand All @@ -24,13 +28,13 @@ program
'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.'
'overridden with some of the options listed below.'
)
.version( version )
.arguments( '[slug]' )
.option(
'-t, --template <name>',
'template type name, allowed values: "es5", "esnext"',
'block template type name, allowed values: "es5", "esnext"',
'esnext'
)
.option( '--namespace <value>', 'internal namespace for the block name' )
Expand All @@ -41,14 +45,21 @@ program
.action(
async (
slug,
{ category, namespace, shortDescription, template, title }
{
category,
namespace,
shortDescription: description,
template: templateName,
title,
}
) => {
await checkSystemRequirements( engines );
try {
const defaultValues = getDefaultValues( template );
const blockTemplate = await getBlockTemplate( templateName );
const defaultValues = getDefaultValues( blockTemplate );
const optionsValues = pickBy( {
category,
description: shortDescription,
description,
namespace,
title,
} );
Expand All @@ -60,14 +71,14 @@ program
title: startCase( slug ),
...optionsValues,
};
await scaffold( template, answers );
await scaffold( blockTemplate, answers );
} else {
const propmpts = getPrompts( template ).filter(
const prompts = getPrompts( blockTemplate ).filter(
( { name } ) =>
! Object.keys( optionsValues ).includes( name )
);
const answers = await inquirer.prompt( propmpts );
await scaffold( template, {
const answers = await inquirer.prompt( prompts );
await scaffold( blockTemplate, {
...defaultValues,
...optionsValues,
...answers,
Expand All @@ -83,7 +94,7 @@ program
}
}
)
.on( '--help', function() {
.on( '--help', () => {
log.info( '' );
log.info( 'Examples:' );
log.info( ` $ ${ commandName }` );
Expand Down
35 changes: 16 additions & 19 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
* External dependencies
*/
const { dirname, join } = require( 'path' );
const { writeFile } = require( 'fs' ).promises;
const { snakeCase } = require( 'lodash' );
const makeDir = require( 'make-dir' );
const { readFile, writeFile } = require( 'fs' ).promises;
const { render } = require( 'mustache' );
const { snakeCase } = require( 'lodash' );
const { dirname } = require( 'path' );

/**
* Internal dependencies
*/
const initWPScripts = require( './init-wp-scripts' );
const { code, info, success } = require( './log' );
const { hasWPScriptsEnabled, getOutputFiles } = require( './templates' );
const { hasWPScriptsEnabled } = require( './templates' );

module.exports = async function(
templateName,
module.exports = async (
blockTemplate,
{
namespace,
slug,
Expand All @@ -28,13 +28,14 @@ module.exports = async function(
licenseURI,
version,
}
) {
) => {
slug = slug.toLowerCase();
namespace = namespace.toLowerCase();

info( '' );
info( `Creating a new WordPress block in "${ slug }" folder.` );

const { outputTemplates } = blockTemplate;
const view = {
namespace,
namespaceSnakeCase: snakeCase( namespace ),
Expand All @@ -51,33 +52,29 @@ module.exports = async function(
textdomain: namespace,
};
await Promise.all(
getOutputFiles( templateName ).map( async ( file ) => {
const template = await readFile(
join(
__dirname,
`templates/${ templateName }/${ file }.mustache`
),
'utf8'
);
Object.keys( outputTemplates ).map( async ( outputFile ) => {
// Output files can have names that depend on the slug provided.
const outputFilePath = `${ slug }/${ file.replace(
const outputFilePath = `${ slug }/${ outputFile.replace(
/\$slug/g,
slug
) }`;
await makeDir( dirname( outputFilePath ) );
writeFile( outputFilePath, render( template, view ) );
writeFile(
outputFilePath,
render( outputTemplates[ outputFile ], view )
);
} )
);

if ( hasWPScriptsEnabled( templateName ) ) {
if ( hasWPScriptsEnabled( blockTemplate ) ) {
await initWPScripts( view );
}

info( '' );
success(
`Done: block "${ title }" bootstrapped in the "${ slug }" folder.`
);
if ( hasWPScriptsEnabled( templateName ) ) {
if ( hasWPScriptsEnabled( blockTemplate ) ) {
info( '' );
info( 'Inside that directory, you can run several commands:' );
info( '' );
Expand Down
Loading