Skip to content

Commit

Permalink
Create block: Improve how prompts and values provided are handled (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Mar 13, 2020
1 parent e825ed9 commit 2616bd4
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 22 deletions.
10 changes: 10 additions & 0 deletions packages/create-block/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## Master

### New Features

- Added readme.txt file to the existing templates to make your entry in the plugin browser most useful ([#20694](https://github.com/WordPress/gutenberg/pull/20694)).
- Added prompts for the `author`, `license` and `version` of the plugin ([#20694](https://github.com/WordPress/gutenberg/pull/20694)).

### Bug Fixes

- Make `version` prompt mandatory and provide validation against semantic versioning ([#20756](https://github.com/WordPress/gutenberg/pull/20756)).
- Omit optional values in the scaffolded files when they aren't provided ([#20756](https://github.com/WordPress/gutenberg/pull/20756)).

## 0.8.3 (2020-02-26)

### Bug Fixes
Expand Down
39 changes: 23 additions & 16 deletions packages/create-block/lib/init-wp-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
const { command } = require( 'execa' );
const { isEmpty, omitBy } = require( 'lodash' );
const { join } = require( 'path' );
const writePkg = require( 'write-pkg' );

Expand All @@ -21,22 +22,28 @@ module.exports = async function( {

info( '' );
info( 'Creating a "package.json" file.' );
await writePkg( cwd, {
name: slug,
version,
description,
author,
license,
main: 'build/index.js',
scripts: {
build: 'wp-scripts build',
'format:js': 'wp-scripts format-js',
'lint:css': 'wp-scripts lint-style',
'lint:js': 'wp-scripts lint-js',
start: 'wp-scripts start',
'packages-update': 'wp-scripts packages-update',
},
} );
await writePkg(
cwd,
omitBy(
{
name: slug,
version,
description,
author,
license,
main: 'build/index.js',
scripts: {
build: 'wp-scripts build',
'format:js': 'wp-scripts format-js',
'lint:css': 'wp-scripts lint-style',
'lint:js': 'wp-scripts lint-js',
start: 'wp-scripts start',
'packages-update': 'wp-scripts packages-update',
},
},
isEmpty
)
);

info( '' );
info( 'Installing packages. It might take a couple of minutes.' );
Expand Down
21 changes: 15 additions & 6 deletions packages/create-block/lib/prompts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
const { upperFirst } = require( 'lodash' );
const { isEmpty, upperFirst } = require( 'lodash' );

const slug = {
type: 'input',
Expand Down Expand Up @@ -55,7 +55,7 @@ const dashicon = {
message:
'The dashicon to make it easier to identify your block (optional):',
validate( input ) {
if ( ! /^[a-z][a-z0-9\-]*$/.test( input ) ) {
if ( ! isEmpty( input ) && ! /^[a-z][a-z0-9\-]*$/.test( input ) ) {
return 'Invalid dashicon name specified. Visit https://developer.wordpress.org/resource/dashicons/ to discover available names.';
}

Expand All @@ -77,25 +77,34 @@ const author = {
type: 'input',
name: 'author',
message:
'The list of contributors containing only WordPress.org usernames (optional):',
'The name of the plugin author (optional). Multiple authors may be listed using commas:',
};

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

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

const version = {
type: 'input',
name: 'version',
message: 'The plugin version (optional):',
message: 'The current version number of the plugin:',
validate( input ) {
// Regular expression was copied from https://semver.org.
const validSemVerPattern = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
if ( ! validSemVerPattern.test( input ) ) {
return 'Invalid Semantic Version provided. Visit https://regex101.com/r/vkijKf/1/ to discover all valid patterns.';
}

return true;
},
};

module.exports = {
Expand Down
9 changes: 9 additions & 0 deletions packages/create-block/lib/templates/es5/$slug.php.mustache
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?php
/**
* Plugin Name: {{title}}
{{#description}}
* Description: {{description}}
{{/description}}
* Version: {{version}}
{{#author}}
* Author: {{author}}
{{/author}}
{{#license}}
* License: {{license}}
{{/license}}
{{#licenseURI}}
* License URI: {{{licenseURI}}}
{{/licenseURI}}
* Text Domain: {{textdomain}}
*
* @package {{namespace}}
Expand Down
6 changes: 6 additions & 0 deletions packages/create-block/lib/templates/es5/readme.txt.mustache
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
=== {{title}} ===
{{#author}}
Contributors: {{author}}
{{/author}}
Tags: block
Requires at least: 5.3.2
Tested up to: 5.3.2
Stable tag: {{version}}
Requires PHP: 7.0.0
{{#license}}
License: {{license}}
{{/license}}
{{#licenseURI}}
License URI: {{{licenseURI}}}
{{/licenseURI}}

{{description}}

Expand Down
9 changes: 9 additions & 0 deletions packages/create-block/lib/templates/esnext/$slug.php.mustache
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?php
/**
* Plugin Name: {{title}}
{{#description}}
* Description: {{description}}
{{/description}}
* Version: {{version}}
{{#author}}
* Author: {{author}}
{{/author}}
{{#license}}
* License: {{license}}
{{/license}}
{{#licenseURI}}
* License URI: {{{licenseURI}}}
{{/licenseURI}}
* Text Domain: {{textdomain}}
*
* @package {{namespace}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
=== {{title}} ===
{{#author}}
Contributors: {{author}}
{{/author}}
Tags: block
Requires at least: 5.3.2
Tested up to: 5.3.2
Stable tag: {{version}}
Requires PHP: 7.0.0
{{#license}}
License: {{license}}
{{/license}}
{{#licenseURI}}
License URI: {{{licenseURI}}}
{{/licenseURI}}

{{description}}

Expand Down

0 comments on commit 2616bd4

Please sign in to comment.