Skip to content

Commit

Permalink
Create block: Code quality improvements for the block scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Jan 27, 2020
1 parent b42cde3 commit 7cc8ef3
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 129 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
/packages/babel-plugin-makepot @ntwb @nerrad @ajitbohra
/packages/babel-preset-default @youknowriad @gziolo @ntwb @nerrad @ajitbohra
/packages/browserslist-config @gziolo @ntwb @nerrad @ajitbohra
/packages/create-block @gziolo
/packages/custom-templated-path-webpack-plugin @ntwb @nerrad @ajitbohra
/packages/docgen @nosolosw
/packages/e2e-test-utils @gziolo @ntwb @nerrad @ajitbohra
Expand Down
39 changes: 18 additions & 21 deletions packages/create-block/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
*/
const program = require( 'commander' );
const inquirer = require( 'inquirer' );
const { startCase } = require( 'lodash' );

/**
* Internal dependencies
*/
const CLIError = require( './cli-error' );
const {
error,
info,
} = require( './log' );
const log = require( './log' );
const { version } = require( '../package.json' );
const scaffold = require( './scaffold' );
const {
getDefaultAnswers,
getDefaultValues,
getPrompts,
} = require( './templates' );
const { startCase } = require( './utils' );

const commandName = `wp-create-block`;
program
Expand All @@ -34,41 +31,41 @@ program
.option( '-t, --template <name>', 'template type name, allowed values: "es5", "esnext"', 'esnext' )
.action( async ( slug, { template } ) => {
try {
const defaultAnswers = getDefaultAnswers( template );
const defaultValues = getDefaultValues( template );
if ( slug ) {
const title = defaultAnswers.slug === slug ?
defaultAnswers.title :
const title = defaultValues.slug === slug ?
defaultValues.title :
startCase( slug.replace( /-/, ' ' ) );
const answers = {
...defaultAnswers,
...defaultValues,
slug,
title,
};
await scaffold( template, answers );
} else {
const answers = await inquirer.prompt( getPrompts( template ) );
await scaffold( template, {
...defaultAnswers,
...defaultValues,
...answers,
} );
}
} catch ( e ) {
if ( e instanceof CLIError ) {
info( '' );
error( e.message );
} catch ( error ) {
if ( error instanceof CLIError ) {
log.info( '' );
log.error( error.message );
process.exit( 1 );
} else {
throw e;
throw error;
}
}
} );

program.on( '--help', function() {
info( '' );
info( 'Examples:' );
info( ` $ ${ commandName }` );
info( ` $ ${ commandName } todo-list` );
info( ` $ ${ commandName } --template es5 todo-list` );
log.info( '' );
log.info( 'Examples:' );
log.info( ` $ ${ commandName }` );
log.info( ` $ ${ commandName } todo-list` );
log.info( ` $ ${ commandName } --template es5 todo-list` );
} );

program.parse( process.argv );
7 changes: 1 addition & 6 deletions packages/create-block/lib/init-wp-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
const { command } = require( 'execa' );
const { install } = require( 'pkg-install' );
const { join } = require( 'path' );
const writePkg = require( 'write-pkg' );

Expand Down Expand Up @@ -34,12 +33,8 @@ module.exports = async function( { author, license, slug, title, version } ) {

info( '' );
info( 'Installing packages. It might take a couple of minutes.' );
await install( [
'@wordpress/scripts',
], {
await command( 'npm install @wordpress/scripts --save-dev', {
cwd,
dev: true,
prefer: 'npm',
} );

info( '' );
Expand Down
7 changes: 0 additions & 7 deletions packages/create-block/lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
*/
const chalk = require( 'chalk' );

const clear = () => {
process.stdout.write(
process.platform === 'win32' ? '\x1B[2J\x1B[0f' : '\x1B[2J\x1B[3J\x1B[H'
);
};

const code = ( input ) => {
console.log( chalk.cyan( input ) );
};
Expand All @@ -27,7 +21,6 @@ const success = ( input ) => {

module.exports = {
code,
clear,
error,
info,
success,
Expand Down
4 changes: 2 additions & 2 deletions packages/create-block/lib/prompts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
* External dependencies
*/
const { upperFirst } = require( './utils' );
const { upperFirst } = require( 'lodash' );

const slug = {
type: 'input',
Expand Down
8 changes: 4 additions & 4 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { render } = require( 'mustache' );
* Internal dependencies
*/
const initWPScripts = require( './init-wp-scripts' );
const { code, clear, info, success } = require( './log' );
const { code, info, success } = require( './log' );
const { hasWPScriptsEnabled, getOutputFiles } = require( './templates' );

module.exports = async function( templateName, {
Expand All @@ -24,7 +24,7 @@ module.exports = async function( templateName, {
license,
version,
} ) {
clear();
info( '' );
info( `Creating a new WordPress block in "${ slug }" folder.` );

const outputFiles = getOutputFiles( templateName );
Expand Down Expand Up @@ -61,8 +61,8 @@ module.exports = async function( templateName, {
await initWPScripts( view );
}

clear();
success( `Done: block '${ title }' bootstrapped in "${ slug }" folder.` );
info( '' );
success( `Done: block "${ title }" bootstrapped in the "${ slug }" folder.` );
if ( hasWPScriptsEnabled( templateName ) ) {
info( '' );
info( 'Inside that directory, you can run several commands:' );
Expand Down
14 changes: 7 additions & 7 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const version = '0.1.0';

const templates = {
es5: {
defaultAnswers: {
defaultValues: {
namespace,
slug: 'es5-example',
title: 'ES5 Example',
Expand All @@ -33,7 +33,7 @@ const templates = {
},
},
esnext: {
defaultAnswers: {
defaultValues: {
namespace,
slug: 'esnext-example',
title: 'ESNext Example',
Expand Down Expand Up @@ -66,20 +66,20 @@ const getTemplate = ( templateName ) => {
return templates[ templateName ];
};

const getDefaultAnswers = ( templateName ) => {
return getTemplate( templateName ).defaultAnswers;
const getDefaultValues = ( templateName ) => {
return getTemplate( templateName ).defaultValues;
};

const getOutputFiles = ( templateName ) => {
return getTemplate( templateName ).outputFiles;
};

const getPrompts = ( templateName ) => {
const defaultAnswers = getDefaultAnswers( templateName );
const defaultValues = getDefaultValues( templateName );
return Object.keys( prompts ).map( ( promptName ) => {
return {
...prompts[ promptName ],
default: defaultAnswers[ promptName ],
default: defaultValues[ promptName ],
};
} );
};
Expand All @@ -89,7 +89,7 @@ const hasWPScriptsEnabled = ( templateName ) => {
};

module.exports = {
getDefaultAnswers,
getDefaultValues,
getOutputFiles,
getPrompts,
hasWPScriptsEnabled,
Expand Down
27 changes: 0 additions & 27 deletions packages/create-block/lib/test/utils.js

This file was deleted.

12 changes: 0 additions & 12 deletions packages/create-block/lib/utils.js

This file was deleted.

86 changes: 43 additions & 43 deletions packages/create-block/package.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
{
"name": "@wordpress/create-block",
"version": "0.5.1",
"description": "Generates PHP, JS and CSS code for registering a block for a WordPress plugin.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [
"wordpress",
"block",
"scaffold"
],
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/create-block/README.md",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/create-block"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
},
"engines": {
"node": ">=10.0",
"npm": ">=6.1"
},
"files": [
"lib"
],
"main": "index.js",
"bin": {
"wp-create-block": "./index.js"
},
"dependencies": {
"chalk": "^2.4.2",
"commander": "^4.1.0",
"execa": "^4.0.0",
"inquirer": "^7.0.3",
"make-dir": "^3.0.0",
"mustache": "^4.0.0",
"pkg-install": "^1.0.0",
"write-pkg": "^4.0.0"
},
"publishConfig": {
"access": "public"
}
"name": "@wordpress/create-block",
"version": "0.5.1",
"description": "Generates PHP, JS and CSS code for registering a block for a WordPress plugin.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [
"wordpress",
"block",
"scaffold"
],
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/create-block/README.md",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/create-block"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
},
"engines": {
"node": ">=10.0",
"npm": ">=6.1"
},
"files": [
"lib"
],
"main": "index.js",
"bin": {
"wp-create-block": "./index.js"
},
"dependencies": {
"chalk": "^2.4.2",
"commander": "^4.1.0",
"execa": "^4.0.0",
"inquirer": "^7.0.3",
"lodash": "^4.17.15",
"make-dir": "^3.0.0",
"mustache": "^4.0.0",
"write-pkg": "^4.0.0"
},
"publishConfig": {
"access": "public"
}
}

0 comments on commit 7cc8ef3

Please sign in to comment.