Skip to content
This repository has been archived by the owner on Feb 5, 2020. It is now read-only.

Commit

Permalink
Feature: Update templates to include WordPress plugin metadata by def…
Browse files Browse the repository at this point in the history
…ault
  • Loading branch information
gziolo committed Jan 8, 2020
1 parent b5ff81a commit 0d3145d
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 110 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
## Master

## 0.5.0 (2020-01-08)

### New Features

- Update templates to include WordPress plugin metadata by default.

## 0.4.3 (2020-01-08)

### Bug Fix

- Print available commands only for ESNext template.

## 0.4.0 (2019-12-17)

### New Features
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Create WordPress Block
Create WordPress Block is an officially supported way to create blocks for registering
a block for a WordPress plugin or theme. It offers a modern build setup with no
a block for a WordPress plugin. It offers a modern build setup with no
configuration. It generates PHP, JS, CSS code, and everything else you need to start the project.

It is largely inspired by [create-react-app](https://create-react-app.dev/docs/getting-started).
Expand Down
9 changes: 6 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ const { startCase } = require( './utils' );
program
.name( name )
.description(
'Generates PHP, JS and CSS code for registering a block for a WordPress plugin or theme.\n\n' +
'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 target location for scaffolded files and the internal block name. The rest of the configuration is set to all default values.'
)
.version( version )
.arguments( '[slug]' )
.option( '-t, --template <name>', 'template type name, allowed values: "es5", "esnext"', 'esnext' )
.action( ( slug, { template } ) => {
try {
const defaultAnswers = getDefaultAnswers( template );
if ( slug ) {
const defaultAnswers = getDefaultAnswers( template );
const title = defaultAnswers.slug === slug ?
defaultAnswers.title :
startCase( slug.replace( /-/, ' ' ) );
Expand All @@ -49,7 +49,10 @@ program
inquirer
.prompt( getPrompts( template ) )
.then( async ( answers ) => {
await scaffold( template, answers );
await scaffold( template, {
...defaultAnswers,
...answers,
} );
} );
}
} catch ( e ) {
Expand Down
8 changes: 4 additions & 4 deletions lib/init-wp-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ const writePkg = require( 'write-pkg' );
*/
const { info } = require( './log' );

module.exports = async function( { slug, title } ) {
module.exports = async function( { author, license, slug, title, version } ) {
const cwd = join( process.cwd(), slug );

info( '' );
info( 'Creating a "package.json" file.' );
await writePkg( cwd, {
name: slug,
version: '0.0.1',
version,
description: title,
author: 'The WordPress Contributors',
license: 'GPL-2.0-or-later',
author,
license,
main: 'build/index.js',
scripts: {
build: 'wp-scripts build',
Expand Down
2 changes: 1 addition & 1 deletion lib/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { upperFirst } = require( './utils' );
const namespace = {
type: 'input',
name: 'namespace',
message: 'The internal namespace for the block, the name of the plugin or theme:',
message: 'The internal namespace for the block, the name of the plugin:',
validate( input ) {
if ( ! /^[a-z][a-z0-9\-]*$/.test( input ) ) {
return 'Invalid block namespace specified. Block namespace can contain only lowercase alphanumeric characters or dashes, and start with a letter.';
Expand Down
14 changes: 10 additions & 4 deletions lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@ module.exports = async function( templateName, {
description,
dashicon,
category,
author,
license,
version,
} ) {
clear();
info( `Creating a new WordPress block in "./${ slug }" folder.` );
info( `Creating a new WordPress block in "${ slug }" folder.` );

const outputFiles = getOutputFiles( templateName );
const view = {
namespace,
slug,
plugin: namespace,
machineName: `${ namespace }_${ slug }`.replace( /\-/g, '_' ),
title,
description,
dashicon,
category,
version,
author,
license,
textdomain: namespace,
};

await Promise.all(
Expand All @@ -42,7 +48,7 @@ module.exports = async function( templateName, {
join( __dirname, `templates/${ outputFiles[ fileName ] }.mustache` ),
'utf8'
);
const filePath = `${ slug }/${ fileName }`;
const filePath = `${ slug }/${ fileName.replace( /\$slug/g, slug ) }`;
await makeDir( dirname( filePath ) );
writeFile(
filePath,
Expand All @@ -56,7 +62,7 @@ module.exports = async function( templateName, {
}

clear();
success( `Done: block '${ title }' bootstrapped in ${ slug }.` );
success( `Done: block '${ title }' bootstrapped in "${ slug }" folder.` );
if ( hasWPScriptsEnabled( templateName ) ) {
info( '' );
info( 'Inside that directory, you can run several commands:' );
Expand Down
13 changes: 11 additions & 2 deletions lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const prompts = require( './prompts' );
const namespace = 'create-wordpress-block';
const dashicon = 'smiley';
const category = 'widgets';
const author = 'The WordPress Contributors';
const license = 'GPL-2.0-or-later';
const version = '0.1.0';

const templates = {
es5: {
Expand All @@ -17,12 +20,15 @@ const templates = {
description: 'Example block written with ES5 standard and no JSX – no build step required.',
dashicon,
category,
author,
license,
version,
},
outputFiles: {
'.editorconfig': 'editorconfig',
'editor.css': 'editor-css',
'index.js': 'es5/index-js',
'index.php': 'es5/index-php',
'$slug.php': 'es5/plugin-php',
'style.css': 'style-css',
},
},
Expand All @@ -34,13 +40,16 @@ const templates = {
description: 'Example block written with ESNext standard and JSX support – build step required.',
dashicon,
category,
author,
license,
version,
},
outputFiles: {
'.editorconfig': 'editorconfig',
'.gitignore': 'gitignore',
'editor.css': 'editor-css',
'src/index.js': 'esnext/index-js',
'index.php': 'esnext/index-php',
'$slug.php': 'esnext/plugin-php',
'style.css': 'style-css',
},
wpScriptsEnabled: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php
/**
* Functions to register client-side assets (scripts and stylesheets) for the
* block editor.
* Plugin Name: {{title}}
* Description: {{description}}
* Version: {{version}}
* Author: {{author}}
* License: {{license}}
* Text Domain: {{textdomain}}
*
* @package {{namespace}}
* @package {{namespace}}
*/

/**
Expand All @@ -13,16 +17,12 @@
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/
*/
function {{machineName}}_block_init() {
{{#plugin}}
$dir = dirname( __FILE__ );
{{/plugin}}
$index_js = 'index.js';
wp_register_script(
'{{slug}}-block-editor',
{{#plugin}}
plugins_url( $index_js, __FILE__ ),
{{/plugin}}
array(
'wp-blocks',
'wp-i18n',
Expand All @@ -34,19 +34,15 @@ function {{machineName}}_block_init() {
$editor_css = 'editor.css';
wp_register_style(
'{{slug}}-block-editor',
{{#plugin}}
plugins_url( $editor_css, __FILE__ ),
{{/plugin}}
array(),
filemtime( "$dir/$editor_css" )
);
$style_css = 'style.css';
wp_register_style(
'{{slug}}-block',
{{#plugin}}
plugins_url( $style_css, __FILE__ ),
{{/plugin}}
array(),
filemtime( "$dir/$style_css" )
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php
/**
* Functions to register client-side assets (scripts and stylesheets) for the
* block editor.
* Plugin Name: {{title}}
* Description: {{description}}
* Version: {{version}}
* Author: {{author}}
* License: {{license}}
* Text Domain: {{textdomain}}
*
* @package {{namespace}}
* @package {{namespace}}
*/

/**
Expand All @@ -13,9 +17,7 @@
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/applying-styles-with-stylesheets/
*/
function {{machineName}}_block_init() {
{{#plugin}}
$dir = dirname( __FILE__ );
{{/plugin}}
$script_asset_path = "$dir/build/index.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
Expand All @@ -27,29 +29,23 @@ function {{machineName}}_block_init() {
$script_asset = require( $script_asset_path );
wp_register_script(
'{{slug}}-block-editor',
{{#plugin}}
plugins_url( $index_js, __FILE__ ),
{{/plugin}}
$script_asset['dependencies'],
$script_asset['version']
);

$editor_css = 'editor.css';
wp_register_style(
'{{slug}}-block-editor',
{{#plugin}}
plugins_url( $editor_css, __FILE__ ),
{{/plugin}}
array(),
filemtime( "$dir/$editor_css" )
);

$style_css = 'style.css';
wp_register_style(
'{{slug}}-block',
{{#plugin}}
plugins_url( $style_css, __FILE__ ),
{{/plugin}}
array(),
filemtime( "$dir/$style_css" )
);
Expand Down
72 changes: 0 additions & 72 deletions lib/templates/index-php.mustache

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "create-wordpress-block",
"version": "0.4.3",
"description": "Generates PHP, JS and CSS code for registering a block for a WordPress plugin or theme.",
"description": "Generates PHP, JS and CSS code for registering a block for a WordPress plugin.",
"author": "Grzegorz <[email protected]> (Greg)",
"license": "MIT",
"keywords": [
Expand Down

0 comments on commit 0d3145d

Please sign in to comment.