Skip to content

Commit

Permalink
Create Block: Use glob to find all template files
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed May 8, 2020
1 parent 70a4af6 commit 69da572
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 55 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,17 @@ module.exports = async function(
licenseURI,
textdomain: namespace,
};
const outputFiles = await getOutputFiles( templateName );
await Promise.all(
getOutputFiles( templateName ).map( async ( file ) => {
outputFiles.map( async ( file ) => {
const template = await readFile(
join(
__dirname,
`templates/${ templateName }/${ file }.mustache`
),
join( __dirname, `templates/${ templateName }/${ file }` ),
'utf8'
);
// Output files can have names that depend on the slug provided.
const outputFilePath = `${ slug }/${ file.replace(
/\$slug/g,
slug
) }`;
const outputFilePath = `${ slug }/${ file
.replace( /\$slug/g, slug )
.replace( '.mustache', '' ) }`;
await makeDir( dirname( outputFilePath ) );
writeFile( outputFilePath, render( template, view ) );
} )
Expand Down
73 changes: 27 additions & 46 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,34 @@
/**
* External dependencies
*/
const glob = require( 'fast-glob' );
const { join } = require( 'path' );

/**
* Internal dependencies
*/
const CLIError = require( './cli-error' );
const prompts = require( './prompts' );

const namespace = 'create-block';
const dashicon = 'smiley';
const category = 'widgets';
const author = 'The WordPress Contributors';
const license = 'GPL-2.0-or-later';
const licenseURI = 'https://www.gnu.org/licenses/gpl-2.0.html';
const version = '0.1.0';

const templates = {
es5: {
defaultValues: {
namespace,
slug: 'es5-example',
title: 'ES5 Example',
description:
'Example block written with ES5 standard and no JSX – no build step required.',
dashicon,
category,
author,
license,
licenseURI,
version,
},
outputFiles: [
'.editorconfig',
'editor.css',
'index.js',
'$slug.php',
'style.css',
'readme.txt',
],
templatesPath: './templates/es5',
wpScripts: false,
},
esnext: {
defaultValues: {
namespace,
slug: 'esnext-example',
title: 'ESNext Example',
description:
'Example block written with ESNext standard and JSX support – build step required.',
dashicon,
category,
author,
license,
licenseURI,
version,
},
outputFiles: [
'.editorconfig',
'.gitignore',
'editor.css',
'src/edit.js',
'src/index.js',
'src/save.js',
'$slug.php',
'style.css',
'readme.txt',
],
wpScriptsEnabled: true,
templatesPath: './templates/esnext',
},
};

Expand All @@ -77,11 +44,25 @@ const getTemplate = ( templateName ) => {
};

const getDefaultValues = ( templateName ) => {
return getTemplate( templateName ).defaultValues;
return {
namespace: 'create-block',
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',
...getTemplate( templateName ).defaultValues,
};
};

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

return await glob( '**/*.mustache', {
cwd: join( __dirname, templatesPath ),
dot: true,
} );
};

const getPrompts = ( templateName ) => {
Expand All @@ -95,7 +76,7 @@ const getPrompts = ( templateName ) => {
};

const hasWPScriptsEnabled = ( templateName ) => {
return getTemplate( templateName ).wpScriptsEnabled || false;
return ! ( getTemplate( templateName ).wpScripts === false );
};

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions packages/create-block/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"check-node-version": "^3.1.1",
"commander": "^4.1.0",
"execa": "^4.0.0",
"fast-glob": "^2.2.7",
"inquirer": "^7.1.0",
"lodash": "^4.17.15",
"make-dir": "^3.0.0",
Expand Down

0 comments on commit 69da572

Please sign in to comment.