Introduction
Creating new components is something we have to do frequently in front-end projects.
Each component needs a directory, index file, component file, stories, and tests.
Manually creating these files is time-consuming and introduces the most dangerous enemy of productivity: friction.
In this post, we will look at how we can use Auto, my side-project, to automate creating new components in a React project.
What is Auto?
Auto is a command-line automation tool I’ve been working on in my spare time that allows you to write your scripts using TypeScript.
You can install it globally and locally, and it supports both global and project-local script repositories.
Auto provides a lot of useful abstractions and helpers that make it easy to write context-aware scripts that interact with the filesystem, execute shell commands, and more.
Auto scripts
Auto scripts are TypeScript files that export the result of a call to auto
, a global function that takes a script definition object as its only argument.
This object contains the script’s metadata, as well as its implementation, and it looks like this:
Setting up a component generator
Install Auto in your project
To install Auto, add it as a dev dependency to your project:
This is a quick overview of the commands that Auto provides:
auto ls
- list all available scriptsauto run <script-id>
- run a scriptauto repl
- start a REPL
You can alias auto run
to npm run gen
or yarn gen
in package.json
to make it easier to run your scripts.
Create a script repository
Auto looks for a "auto"
or ".auto"
directory in the root of your project. There’s no fixed structure that you need to follow; Auto will detect all the valid script files and ignore the others.
Because Auto works by injecting globals into your script, it will prompt you to auto-generate a tsconfig.json
file inside your script repositories, so that you get full type support when writing your scripts.
Create a component generator
Now that we have Auto installed and configured, we can start writing our script.
Let’s first define what we want to achieve:
- Run a command that prompts us for the name and destination of the component.
- Create a series of files and directories based on templates.
- Derive the path and content of each file from the component name and template.
Auto provides several helpers and abstractions that will help us achieve all these goals:
params
- a map of parameters the user will be prompted for, and that will be passed to the script when it runsproject
- a representation of the current project that includes a helper for writing filest
- a templating function that replaces__key__
placeholders with the provided valuesfiles
- an array that contains all the other files in the same directory as the script
Define a template for our component
Let’s start by making a directory for our script and creating the template files.
Create the script directory:
Create the component file template:
Create the test template:
Create the story template:
Create the index file template:
Create the generator script
Now that we have our template files, we can start writing the script to generate components.
You can name the script file however you want, but it’s a good practice to reference the script id in the file name.
Test the script
Now that our script is ready, we should check that it’s recognized correctly.
To do that, we can run npx auto ls
and see if our script is listed and marked as local
:
To run the script, execute npx auto run <script-id>
. Auto will prompt you for the defined parameters and then run the script.
That’s it
I hope you like the idea of Auto and that you’ll find it helpful! I’m looking forward to your feedback and contributions!
Notes: