Skip to content

Commit

Permalink
Create Block: Skip init for block.json when no templates provided f…
Browse files Browse the repository at this point in the history
…or block
  • Loading branch information
gziolo committed Dec 18, 2024
1 parent 3159fa2 commit 8126ea3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
27 changes: 16 additions & 11 deletions packages/create-block/lib/init-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
};
14 changes: 7 additions & 7 deletions packages/create-block/lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 8126ea3

Please sign in to comment.