Howdy, I am Ben Tyler. I specialize in and developing aesthetic and functional websites and applications. I love collaborating on projects, so if you need to hire a developer or an adviser for a project, please get in touch!
Get in touch!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
1const fs = require("fs")2
3// grab the component name from the command4const componentName = process.argv[2]5
6// boilerplate for components/${componentName}/index.tsx7const indexTemplate = `export { ${componentName} } from "./${componentName}"`8
9// boilerplate for the actual component, components/${componentName}10const componentTemplate = `11import React from 'react'12
13export type ${componentName}Props = {}14
15export const ${componentName}: React.FC<${componentName}Props> = (props) => {16 return (17
18 )19}20`21
22// boilerplate for the component story23const storyTemplate = `24import React from 'react'25import { Story, Meta } from '@storybook/react'26import { ${componentName}, ${componentName}Props } from './${componentName}'27
28export default {29 title: '/${componentName}',30 component: ${componentName},31 args: {}32} as Meta33
34const Template: Story<${componentName}Props> = args => <${componentName} {...args} />35
36export const Default = Template.bind({})37Default.args = {}38`39// create a directory for the component40fs.mkdirSync("./src/components/" + componentName)41
42// create files and drop in boilerplate43fs.writeFileSync(44 `./src/components/${componentName}/index.tsx`,45 indexTemplate.trim()46)47fs.writeFileSync(48 `./src/components/${componentName}/${componentName}.tsx`,49 componentTemplate.trim()50)51fs.writeFileSync(52 `./src/components/${componentName}/${componentName}.stories.tsx`,53 storyTemplate.trim()54)55
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