Skip to content

Commit

Permalink
Improve the strucutre and handling of templates
Browse files Browse the repository at this point in the history
Props to @aduth for the proposal: #19773 (comment).
  • Loading branch information
gziolo committed Jan 30, 2020
1 parent edd9c69 commit 4029e21
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 22 deletions.
13 changes: 6 additions & 7 deletions packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = async function( templateName, {
info( '' );
info( `Creating a new WordPress block in "${ slug }" folder.` );

const outputFiles = getOutputFiles( templateName );
const view = {
namespace,
slug,
Expand All @@ -41,17 +40,17 @@ module.exports = async function( templateName, {
license,
textdomain: namespace,
};

await Promise.all(
Object.keys( outputFiles ).map( async ( fileName ) => {
getOutputFiles( templateName ).map( async ( file ) => {
const template = await readFile(
join( __dirname, `templates/${ outputFiles[ fileName ] }.mustache` ),
join( __dirname, `templates/${ templateName }/${ file }.mustache` ),
'utf8'
);
const filePath = `${ slug }/${ fileName.replace( /\$slug/g, slug ) }`;
await makeDir( dirname( filePath ) );
// Output files can have names that depend on the slug provided.
const outputFilePath = `${ slug }/${ file.replace( /\$slug/g, slug ) }`;
await makeDir( dirname( outputFilePath ) );
writeFile(
filePath,
outputFilePath,
render( template, view )
);
} )
Expand Down
30 changes: 15 additions & 15 deletions packages/create-block/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const templates = {
license,
version,
},
outputFiles: {
'.editorconfig': 'editorconfig',
'editor.css': 'editor-css',
'index.js': 'es5/index-js',
'$slug.php': 'es5/plugin-php',
'style.css': 'style-css',
},
outputFiles: [
'.editorconfig',
'editor.css',
'index.js',
'$slug.php',
'style.css',
],
},
esnext: {
defaultValues: {
Expand All @@ -44,14 +44,14 @@ const templates = {
license,
version,
},
outputFiles: {
'.editorconfig': 'editorconfig',
'.gitignore': 'gitignore',
'editor.css': 'editor-css',
'src/index.js': 'esnext/index-js',
'$slug.php': 'esnext/plugin-php',
'style.css': 'style-css',
},
outputFiles: [
'.editorconfig',
'.gitignore',
'editor.css',
'src/index.js',
'$slug.php',
'style.css',
],
wpScriptsEnabled: true,
},
};
Expand Down
22 changes: 22 additions & 0 deletions packages/create-block/lib/templates/esnext/.editorconfig.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
indent_size = 4

[{*.json,*.yml}]
indent_style = space
indent_size = 2
[*.md]
trim_trailing_whitespace = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* The following styles get applied inside the editor only.
*
* Replace them with your own styles or remove the file completely.
*/

.wp-block-{{namespace}}-{{slug}} {
border: 1px dotted #f00;
}
12 changes: 12 additions & 0 deletions packages/create-block/lib/templates/esnext/style.css.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* The following styles get applied both on the front of your site
* and in the editor.
*
* Replace them with your own styles or remove the file completely.
*/

.wp-block-{{namespace}}-{{slug}} {
background-color: #000;
color: #fff;
padding: 2px;
}

0 comments on commit 4029e21

Please sign in to comment.