I like to write almost as much as I like to build. I write about dashboards, maps, freelancing, music, and the outdoors. If this interests you too, please join my mailing list to get updates whenever I publish new content.

Subscribe

Auto-Generating a React Component With Intelligent Boilerplate

Date: 1/22/2021

Time to Read: 1 minutes

The Problem

Over the holidays I refactored my personal site and built a lightweight design system for myself. This meant creating a whole lot of new components. Due to my organizational preference for components, it meant that for each new component I needed to create a new directory, index.tsx, [ComponentName].tsx, and [ComponentName].stories.tsx file. And then to round the process out, I would copy and paste some boilerplate into each file. As the design system grew, so did my impatience with this process.

The Solution

My initial instinct was to create a VSCode snippet for each file type. This solved half of the problem but the whole process still lacked some solid ergonomics. I remembered reading a post from Joel Hooks on how he streamlined the process of creating a new blog post with a npm script (worth a read in my opinion). I thought could probably do something pretty similar for generating components. This ended up being the ticket.

The solution was relatively straightforward largely because I did not need this thing to be bulletproof (can you spot how easily this thing can break?). My favorite thing about this whole script is how it produces "intelligent" boilerplate. Whatever you provide for the componentName arg will be used to generate the directory and file names as well as the names for various structures in the code. Not having to copy and paste boilerplate and update variable names and exports is the real time saver in my opinion. The last thing I did was add a new script to my package.json called create:component.

Usage

From the root of the project I can now just open up my terminal and run something like yarn create:component MapHeader and in less than a second I will have all of my files and intelligent boilerplate.

The Snippets

createComponent.js
1
const fs = require("fs")
2
3
// grab the component name from the command
4
const componentName = process.argv[2]
5
6
// boilerplate for components/${componentName}/index.tsx
7
const indexTemplate = `export { ${componentName} } from "./${componentName}"`
8
9
// boilerplate for the actual component, components/${componentName}
10
const componentTemplate = `
11
import React from 'react'
12
13
export type ${componentName}Props = {}
14
15
export const ${componentName}: React.FC<${componentName}Props> = (props) => {
16
return (
17
18
)
19
}
20
`
21
22
// boilerplate for the component story
23
const storyTemplate = `
24
import React from 'react'
25
import { Story, Meta } from '@storybook/react'
26
import { ${componentName}, ${componentName}Props } from './${componentName}'
27
28
export default {
29
title: '/${componentName}',
30
component: ${componentName},
31
args: {}
32
} as Meta
33
34
const Template: Story<${componentName}Props> = args => <${componentName} {...args} />
35
36
export const Default = Template.bind({})
37
Default.args = {}
38
`
39
// create a directory for the component
40
fs.mkdirSync("./src/components/" + componentName)
41
42
// create files and drop in boilerplate
43
fs.writeFileSync(
44
`./src/components/${componentName}/index.tsx`,
45
indexTemplate.trim()
46
)
47
fs.writeFileSync(
48
`./src/components/${componentName}/${componentName}.tsx`,
49
componentTemplate.trim()
50
)
51
fs.writeFileSync(
52
`./src/components/${componentName}/${componentName}.stories.tsx`,
53
storyTemplate.trim()
54
)
55
package.json
1
"scripts": {
2
"build": "gatsby build",
3
"create:blog": "node scripts/createBlogPost.js",
4
"create:component": "node scripts/createComponent.js",
5
"develop": "gatsby develop",
6
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
7
"start": "npm run develop",
8
"serve": "gatsby serve",
9
"storybook": "start-storybook -p 6006",
10
"storybook:build": "build-storybook"
11
},
12

Join the Newsletter

I periodically send out a newsletter. Interested? Sign up below. You can unsubscribe at any time.

Interested in working together?

Drop me a line
© 2021 Lost Creek Designs