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:

  1. Require the necessary modules.
  2. Create wrappers around the console.log and console.error functions using chalk.
  3. Get arguments from the terminal.
  4. Define the path where Vuex modules reside.
  5. Perform a check to confirm that module names were passed as an argument.
  6. Create a modules directory in the store folder if it doesn’t exist.
  7. Exit with an error if a module with the same name already exists.
  8. Prepare content for module files.
  9. Prepare full paths for each module file.
  10. Create a module directory and files with the moduleName passed as an argument.

Get hands-on with 1200+ tech skills courses.