Automated Module Scaffolding
Learn why we need scaffolding and how we can automate it.
We'll cover the following
Why we automate module scaffolding
When creating a new Vuex module, there are quite a few things we have to do, such as creating files for the state and getters, adding imports and exports for each file, and so on. The great thing about being a developer is that we can automate boring and tedious stuff.
Let’s create a node script that creates Vuex modules for us automatically. Any scripts such as this one can be kept outside of the src
directory in a folder called scripts
. Let’s call our script generateVuexModule.js
. We need to install a library called chalk. It will be used to change console messages’ colors to make it easier to distinguish error and success messages.
Let’s create the generateVuexModule.js
.
The code for the scripts has around 81 lines. The steps in the code are as follows:
- Require the necessary modules.
- Create wrappers around the
console.log
andconsole.error
functions using chalk. - Get arguments from the terminal.
- Define the path where Vuex modules reside.
- Perform a check to confirm that module names were passed as an argument.
- Create a
modules
directory in thestore
folder if it doesn’t exist. - Exit with an error if a module with the same name already exists.
- Prepare content for module files.
- Prepare full paths for each module file.
- Create a
module
directory and files with themoduleName
passed as an argument.
Get hands-on with 1400+ tech skills courses.