diff --git a/package-lock.json b/package-lock.json index f84bf01bfd0af..2327024e32bbd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9997,6 +9997,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", diff --git a/packages/create-block/lib/scaffold.js b/packages/create-block/lib/scaffold.js index be9b09588bde6..5fc37a19b3028 100644 --- a/packages/create-block/lib/scaffold.js +++ b/packages/create-block/lib/scaffold.js @@ -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 ) ); } ) diff --git a/packages/create-block/lib/templates.js b/packages/create-block/lib/templates.js index 7beadd7008cfe..aa28fb1c0c4a1 100644 --- a/packages/create-block/lib/templates.js +++ b/packages/create-block/lib/templates.js @@ -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', }, }; @@ -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 ) => { @@ -95,7 +76,7 @@ const getPrompts = ( templateName ) => { }; const hasWPScriptsEnabled = ( templateName ) => { - return getTemplate( templateName ).wpScriptsEnabled || false; + return ! ( getTemplate( templateName ).wpScripts === false ); }; module.exports = { diff --git a/packages/create-block/package.json b/packages/create-block/package.json index 3b949c42fcd51..329a7fcc157f4 100644 --- a/packages/create-block/package.json +++ b/packages/create-block/package.json @@ -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",