A CLI to create ACF blocks
The create-acf-block CLI registers your block within the acf_register_block_type() function, scaffolds a block render template, and optionally preps CSS and JS.
Why I built the create-acf-block CLI
Recently I was creating a number of ACF (Advanced Custom Field) blocks for a WordPress site and I was annoyed with the repetitive task of:
- Registering the block in
- Creating a file to hold the render template
- Copying and pasting in a starter template
- Creating a CSS and JS file for the block
And you have to do that for each block - over and over again.
In the middle of that I stopped and ran a search for something like "ACF Block CLI" or "Create ACF Block CLI". I was surprised that there was nothing out there. Could I just build one myself? I've never built a CLI before and I didn't really know what was involved.
Conceptually, I knew what I wanted the CLI to do for me and I could picture how I would interact with it:
- Provide the block's slug
- Provide the block's name
- And add a few optional preferences for
Then the CLI would take care of those repetitive tasks I mentioned before.
How I learned to build the create-acf-block CLI
Since I've not built a CLI before I knew I'd need to start reading or take a course or something. That's when I remembered that Ahmad Awais had a Node CLI course. I didn't even have to take the entire course to start working on my CLI. In fact, I had enough knowledge to get started after just 30 minutes of his lessons.
I'll definitely continue with the rest of the course. Ahmad is very enjoyable to learn from and I'm sure I'll be able to refactor and improve my CLI with what I'll learn in the future.
How to use the create-acf-block CLI
When you first run the CLI within your project, you'll be prompted to specify some project preferences:
- The path to your block registration file. This could be your functions.php file or something like
- The path to your block render templates. This is where the CLI will create your block template files.
- Whether to create block assets - CSS and JS.
- Whether to group your block assets. Should your CSS and JS live in a separate folder or group block templates with their assets?
Then, you'll quickly move through the block details:
- Block Name (slug)
- Block Title
- Use innerBlocks? This preps the render template for JSX
These are options that get passed to the
acf_register_block_type() function. Only Block Name and Block Title are required.
How the create-acf-block CLI works
It's pretty simple actually. Here's an overview of the CLI:
Check if the preferences are not set or if the
--preferencesflag is present. If ether are true, present the user with the preferences prompts.
Check to make sure that the user supplied path to their block registration file exists.
Then we check inside that registration file to ensure that there are "comment markers". There's an opening and closing comment to tell the CLI where to insert new blocks within the file.
Present the general block details prompts and pass those responses to the following three functions.
Open the registration file, find the comment markers, and insert the populated
Create thee block render template. If the "Group block assets" preference is true, the folder structure will look something like:
./blocks │ └───hero │ │ block.php │ │ block.css │ │ block.php └───quote │ block.php │ block.css │ block.php
Otherwise, assets will be grouped by file type:
./blocks │ hero.php │ quote.php │ ./css │ hero.css │ quote.css ./js │ hero.js │ quote.js
Optionally create the block CSS and JS and place them in the appropriate folder.
And that's it! I was pretty pleased with how easily this all came together. And it took me less than a days worth of work to figure it out and build it. The best part is that I'm totally using this on three or four projects already. It's very helpful!
If you want you checkout the code you can find it at https://github.com/joseph-farruggio/create-acf-block.