[Create Block] Adding a `--block-only` flag to scaffold only block files.
What?
This PR introduces the --block-only flag to allow users to use the package to scaffold only block files.
Why?
Currently, there is no way to scaffold additional blocks once the plugin has been created. Being able to quickly create subsequent blocks would be very helpful.
How?
When the --block-only flag is added, the script skips any plugin scaffolding and only outputs the files listed in the blockTemplatesPath ( just the block files ) into folderName as defined by the template with the default being ./src. It should be noted that the script is not "aware" of its location. Calling npx @wordpress/create-block block-name --no-block will create a new block inside the folderNamedirectory where it's called
The slug passed is used as the directory and block name any other flags not related to package.json items should function as expected for the block, i.e namespace.
Testing Instructions
- From the root of
packages/create-block, runnode index.js testto create a new plugin with a single block cdinto thetestdirectory- Run
node ../index.js my-test-block --block-onlyto scaffold a new block - Confirm that the
srcdirectory has the existing block files and a new directory calledmy-test-blockthat contains new block files - Run
npm startto confirm that the build works
Screenshots or screencast
This PR introduces a lot of conditionals and it would be a really good idea to start abstracting out some of the functionality to make it easier to work with.
Based on a conversation with @gziolo in slack we think that changing the flag to --no-plugin makes more sense and is clearer in the context of running the full command i.e npx @wordpress/create-block --no-plugin
I found an issue while testing where if a --template was passed that didn't support the blockTemplatesPath property an assets directory was created containing the actual template files.
As a result, I have added some logic to check to see if there are block template files and if not, error out with a message.
This can be tested by using the following command from the root of the create-block package.
- Template with support:
node ../index.js my-test-block ---template=@ryanwelcher/dynamic-block-template --no-plugin - Template without support:
node ../index.js my-test-block --template=@ryanwelcher/multiple-blocks-template --no-plugin
I can confirm that the newly added check for block templates works as expected:
It makes me wonder whether we should bail out early when loading the config for the template. In effect we would have two checks:
- Show error when there is no
blockTemplatesPathprovided together with--no-pluginflag:The template selected doesn't support--no-plugin` option. - If both
--no-pluginand theblockTemplatesPathwas provided and there were no block files loaded, we could just show the message thatNo block files found in the template. The template "XYZ" is incorrectly configured.
https://github.com/WordPress/gutenberg/blob/9a4b95e37da8b4c4b271f510895690a4ea1fa780/packages/create-block/lib/prompts.js#L11
here we could make it more universal for the time being so it fits also the case when user passes --no-plugin flag:
The block slug used for identification (also the output folder name):
In the long run, if we were to support multiple blocks scaffolding in one go, we might want to have two distinct slug prompts - one for the block and the other one for the plugin. I'm not sure about that yet, but I wanted to flag it.
https://github.com/WordPress/gutenberg/blob/9a4b95e37da8b4c4b271f510895690a4ea1fa780/packages/create-block/lib/prompts.js#L11
here we could make it more universal for the time being so it fits also the case when user passes
--no-pluginflag:
The block slug used for identification (also the output folder name):
I'll make this update before merging.
In the long run, if we were to support multiple blocks scaffolding in one go, we might want to have two distinct slug prompts - one for the block and the other one for the plugin. I'm not sure about that yet, but I wanted to flag it.
I LOVE this idea. It's been something that always bugged me that the plugin and the block had the same name. Being able to pass different names for the plugin would be really handy. I'll open a new issue for it.
related https://github.com/WordPress/gutenberg/issues/43235