From 8126ea3c2e92f82a2279106d100f819e985fa551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Zi=C3=B3=C5=82kowski?= Date: Wed, 18 Dec 2024 15:25:58 +0100 Subject: [PATCH] Create Block: Skip init for `block.json` when no templates provided for block --- packages/create-block/lib/init-block.js | 27 +++++++++++++++---------- packages/create-block/lib/output.js | 14 ++++++------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/packages/create-block/lib/init-block.js b/packages/create-block/lib/init-block.js index 8074ef1c8d953d..47a2fda1e4b355 100644 --- a/packages/create-block/lib/init-block.js +++ b/packages/create-block/lib/init-block.js @@ -79,17 +79,22 @@ async function initBlockJSON( { } module.exports = async function ( outputTemplates, view ) { - await Promise.all( - Object.keys( outputTemplates ).map( async ( outputFile ) => { - await writeOutputTemplate( - outputTemplates[ outputFile ], - join( - view.plugin ? view.folderName : '', - outputFile.replace( /\$slug/g, view.slug ) - ), - view - ); - } ) + const results = await Promise.all( + Object.keys( outputTemplates ).map( + async ( outputFile ) => + await writeOutputTemplate( + outputTemplates[ outputFile ], + join( + view.plugin ? view.folderName : '', + outputFile.replace( /\$slug/g, view.slug ) + ), + view + ) + ) ); + if ( ! results.includes( true ) ) { + return; + } + await initBlockJSON( view ); }; diff --git a/packages/create-block/lib/output.js b/packages/create-block/lib/output.js index 609a028aa8034e..11c03207051165 100644 --- a/packages/create-block/lib/output.js +++ b/packages/create-block/lib/output.js @@ -10,19 +10,19 @@ const writeOutputAsset = async ( inputFile, outputFile, view ) => { const outputFilePath = join( view.rootDirectory, 'assets', outputFile ); await makeDir( dirname( outputFilePath ) ); writeFile( outputFilePath, inputFile ); + return true; }; const writeOutputTemplate = async ( inputFile, outputFile, view ) => { // If the rendered template is empty, don't write it. This is how we can conditionally add template files. const renderedFile = render( inputFile, view ); - if ( renderedFile.trim().length ) { - const outputFilePath = join( view.rootDirectory, outputFile ); - await makeDir( dirname( outputFilePath ) ); - writeFile( - outputFilePath.replace( /\$slug/g, view.slug ), - renderedFile - ); + if ( ! renderedFile.trim().length ) { + return false; } + const outputFilePath = join( view.rootDirectory, outputFile ); + await makeDir( dirname( outputFilePath ) ); + writeFile( outputFilePath.replace( /\$slug/g, view.slug ), renderedFile ); + return true; }; module.exports = {