This article assumes you're already familiar with creation of CLI plugins. If not, please read the previous article on Creating a CLI plugin.
In this article, we'll create a plugin which generates a folder for us. Not very exciting, but it will show you how we created our own scaffolding plugins. After this, you'll be able to easily expand on the basics and create plugins for your project.
Introduction to scaffolding
Before we begin developing a new plugin, we need to explain how this works. When you run
yarn webiny scaffold, you'll be prompted with a list of available scaffolding plugins to select from. As we know from the CLI Overview article, Webiny CLI doesn't have the
scaffold command built-in. That command comes from
@webiny/cli-plugin-scaffold package, and you can see that plugin in your
That plugin, in turn, loads plugins of type
cli-plugin-scaffold-template. You can create an inception of plugins! You create a plugin, which loads other plugins, which load other plugins, etc. That's how the entire Webiny is built.
So now you know how things are connected. Let's begin developing the scaffolding plugin.
Scaffold plugins must implement the following type:
CLIContext type definition, see Creating a CLI plugin article.
We'll skip the instructions on where to put your code, and how to enable your plugin. If you want to learn that, read the Creating a CLI plugin article which explains the basics of creating a CLI plugin.
We use inquirer validation to ensure that the folder's name contains only alphanumeric characters. We'll also check whether the folder already exists using the path supplied by
The last thing to do is to enable your plugin in
webiny.root.js. If you don't know how to do that, refer to Enabling the plugin in the previous article.
Now we're able to run
webiny scaffold within our project's root, select our newly created scaffold plugin, enter a folder name and have our folder created for us. Of course, it will warn and prompt us to insert a different name if the validation fails.
Congratulations! You've just finished your first scaffolding plugin. 🚀
To see a more complex example, take a look at our cli-plugin-scaffold-graphql-service package, on our Github.