๐Ÿ› /Dev Tool

[storybook] ๋ฆฌ์•กํŠธ๋กœ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ์‹œ ์œ ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ๊ด€๋ฆฌ ๋„๊ตฌ, ์Šคํ† ๋ฆฌ๋ถ์„ ๋„์ž…ํ•ด๋ณด๊ธฐ - 3

ํ•œ๋‚˜ 2022. 2. 4. 16:00

๋ฆฌ์•กํŠธ๋กœ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ์‹œ ์œ ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ๊ด€๋ฆฌ ๋„๊ตฌ, ์Šคํ† ๋ฆฌ๋ถ์„ ๋„์ž…ํ•ด๋ณด๊ธฐ - 3

(CRA + TypeScript + Prettier + ESLint + husky + lint-staged + Storybook + chromatic)

TypeScript๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” Create React App ์ค€๋น„๋Š” ์ฒซ ๋ฒˆ์งธ ์‹œ๋ฆฌ์ฆˆ์—์„œ ๋‹ค๋ค˜๊ณ , ESLiint์™€ Prettier๋Š” ๋‘ ๋ฒˆ์งธ ์‹œ๋ฆฌ์ฆˆ์—์„œ ๋‹ค๋ฃจ์—ˆ๋‹ค.

 

2022.01.28 - [๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ] - [Storybook] ๋ฆฌ์•กํŠธ๋กœ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ์‹œ ์œ ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ๊ด€๋ฆฌ ๋„๊ตฌ, ์Šคํ† ๋ฆฌ๋ถ์„ ๋„์ž…ํ•ด๋ณด๊ธฐ - 1

2022.02.04 - [๐Ÿ› /Dev Tool] - [storybook] ๋ฆฌ์•กํŠธ๋กœ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ์‹œ ์œ ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ๊ด€๋ฆฌ ๋„๊ตฌ, ์Šคํ† ๋ฆฌ๋ถ์„ ๋„์ž…ํ•ด๋ณด๊ธฐ - 2

 


husky

  • Git ์€ hook ์ด๋ผ๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. commit, push ๊ฐ™์€ Git๊ณผ ๊ด€๋ จ๋œ ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚  ๋•Œ ์ „ํ›„๋กœ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • husky ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Git Hook์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•˜๋‹ค.

์šฐ์„  husky๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„๋ณด์ž.

yarn add -D husky

lint-staged

  • husky์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋œ๋‹ค. Git์— Staged๋œ ํŒŒ์ผ๋“ค์— ๋Œ€ํ•ด ํŠน์ • ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.
  • Staged files๋ž€, ํŒŒ์ผ์ด ์ˆ˜์ •๋˜๊ณ , Git add ๋ช…๋ น์–ด๋กœ ์‹คํ–‰๋œ ํŒŒ์ผ์„ ๋งํ•œ๋‹ค. ๋งŒ์ผ staged๋œ ํŒŒ์ผ์„ ํ•œ ๋ฒˆ ๋” ์ˆ˜์ •ํ•  ๊ฒฝ์šฐ, 'staged' ์ƒํƒœ๋กœ ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์‹œ ํ•œ ๋ฒˆ git add ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
yarn add -D lint-staged

husky, lint-staged ์ ์šฉํ•˜๊ธฐ

  • package.json์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์ •ํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

{
  "scripts": {
    ...
    "prepare": "husky install"
  },
  "devDependencies": {
    ...
    "eslint": "^8.8.0",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-react": "^7.28.0",
    "husky": ">=6",
    "lint-staged": ">=10",
    "prettier": "^2.5.1",
    ...
  },
  "lint-staged": {
    "src/**/*.{ts,tsx}": [
      "eslint --ext .tsx --ext .ts ./src --fix"
    ],
    "./src/**": [
      "prettier --write ."
    ]
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  }
}

์ด์ œ ์ˆ˜์ •ํ•œ ํŒŒ์ผ์„ stage์— ์˜ฌ๋ฆด ๋•Œ๋งˆ๋‹ค hook์— ๊ฑธ๋ ค์„œ, ESLint๊ฐ€ ๋„๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์ผ ์ œ๋Œ€๋กœ ์ฝ”๋“œ๊ฐ€ ํ†ต๊ณผ๋˜์ง€ ๋ชปํ•˜๋ฉด commit์„ ํ•  ์ˆ˜ ์—†๊ฒŒ ๋˜์–ด ์žˆ๋‹ค.

 

์›ํ•œ๋‹ค๋ฉด ์ด๋ฅผ Github Action์„ ํ†ตํ•ด์„œ๋„ workflow๋ฅผ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค.