πŸ§šβ€β™€οΈ 5λΆ„λ§Œμ— Cli λ§Œλ“€μ–΄λ³΄κΈ°

고석진
6 min readJun 11, 2020

React λ˜λŠ” Vue 같은 μΉœκ΅¬λ“€μ„ 접해보셨닀면 μ•„λž˜μ™€ 같은 λͺ…λ Ήμ–΄λ₯Ό μž…λ ₯ν•˜μ—¬ ν”„λ‘œμ νŠΈλ₯Ό 생성해봀을 것이닀.

$ create-react-app my-app
$ vue create my-app

μ΄λ ‡κ²Œ command μƒμ—μ„œ μž‘μ—…μ„ μ²˜λ¦¬ν•˜λŠ” 방식을 cli λΌκ³ ν•œλ‹€.
cli λž€ command line interface 의 μ•½μžμ΄λ‹€.

Npm Finder

npm finder λΌλŠ” cli λ₯Ό λ§Œλ“€μ–΄ μ•„λž˜μ˜ λͺ…λ Ήμ–΄λ₯Ό μž…λ ₯ν•˜λ©΄ npm μ‚¬μ΄νŠΈμ˜ ν•΄λ‹Ή λͺ¨λ“ˆ νŽ˜μ΄μ§€λ₯Ό μ°Ύμ•„ λ„μ›Œμ£Όλ €ν•œλ‹€.

$ n-finder react

Commander

λͺ¨λ“  μž‘μ—…μ€ node.js 기반으둜 이뀄지기 λ•Œλ¬Έμ— node κ°€ μ„€μΉ˜λ˜μ–΄ μžˆμ–΄μ•Ό ν•œλ‹€.

$ npm init -y

command μ—μ„œ λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•˜κ³  λ‹€μ–‘ν•œ option κ³Ό 값듀을 κ°€μ Έμ˜€κΈ° μœ„ν•΄μ„  commander λΌλŠ” λͺ¨λ“ˆμ΄ ν•„μš”ν•˜λ‹€.

$ npm install --save commander

commander λ₯Ό μ΄μš©ν•˜μ—¬ index.js λ₯Ό κ΅¬μ„±ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

!/usr/bin/env node λŠ” κΌ­ μΆ”κ°€ν•΄μ•Όν•œλ‹€.
배포 ν›„ global 둜 μ„€μΉ˜ν•˜μ—¬ μ‚¬μš© ν•  λ•Œ μ—†μœΌλ©΄ cli κ°€ λ™μž‘ν•˜μ§€ μ•ŠλŠ”λ‹€.

#!/usr/bin/env nodeconst program = require("commander")
const pkg = require("./package.json")
program
.version(pkg.version)
.arguments("<module-name>")
.action(module => console.log(`module: ${module}`))
.parse(process.argv)

μ•„λž˜μ™€ 같이 index.js λ₯Ό μ‹€ν–‰ν•˜λ©° react λΌλŠ” 값을 λ„˜κ²¨μ£Όλ©΄ module: react λΌλŠ” 값이 μ°νžˆλŠ” 것을 λ³Ό 수 μžˆλ‹€.

node index.js react

commander 의 ν•¨μˆ˜λ₯Ό 역할을 κ°„λ‹¨ν•˜κ²Œ μ‚΄νŽ΄λ³΄κ² λ‹€.

  • version: node index.js -V 와 같이 ν•΄λ‹Ή λͺ¨λ“ˆμ˜ version 을 좜λ ₯ν•  수 μžˆλ„λ‘ ν•œλ‹€.
  • arguments: option 없이 호좜될 κ°’
  • action: arguments 값을 μž…λ ₯받은 ν›„μ˜ λ™μž‘μ„ μ •μ˜
  • parse: μ–΄λ””μ—μ„œ 값을 μž…λ ₯λ°›λŠ”μ§€ λ‚˜νƒ€λ‚Έλ‹€.

open

commander λ₯Ό μ΄μš©ν•˜μ—¬ 값을 λ°›μ•˜μœΌλ‹ˆ 이 값을 μ΄μš©ν•˜μ—¬ npm μ‚¬μ΄νŠΈμ—μ„œ ν•΄λ‹Ή λͺ¨λ“ˆ νŽ˜μ΄μ§€λ₯Ό open ν•˜κΈ°λ§Œ ν•˜λ©΄λœλ‹€ .
λΈŒλΌμš°μ €λ₯Ό μ—΄κΈ° μœ„ν•΄ open λͺ¨λ“ˆμ„ μ‚¬μš©ν•œλ‹€.

$ npm i --save open

NPM νŽ˜μ΄μ§€μ—μ„œ λͺ¨λ“ˆμ„ μ°Ύμ•„κ°€κΈ° μœ„ν•œ BASE μ£Όμ†ŒλŠ” https://www.npmjs.com/package 이닀.
λͺ¨λ“ˆμ„ μ°ΎκΈ° μœ„ν•΄μ„œλŠ” λ‹€μŒκ³Ό 같은 μ£Όμ†Œλ‘œ μ ‘κ·Όν•΄μ•Όν•œλ‹€. https://www.npmjs.com/package/λͺ¨λ“ˆμ΄λ¦„

open 은 μ‹€ν–‰μ‹œ λ‘κ°€μ§€μ˜ 인자λ₯Ό λ°›λŠ”λ‹€.
μ²«λ²ˆμ§ΈλŠ” open ν•  μ£Όμ†Œ, λ‘λ²ˆμ§Έ μΈμžλŠ” λΈŒλΌμš°μ € 정보λ₯Ό λ°›λŠ”λ‹€. (크둬, νŒŒμ΄μ–΄ν­μŠ€ 등…)

$ open(url, browser)

크둬을 μ‚¬μš©ν•˜κ²Œ 될텐데 chrome 이 μ•„λ‹Œ Google chrome 으둜 적어주어야 ν•œλ‹€.

const program = require("commander")
const pkg = require("./package.json")
const open = require("open")
const BASE_NPM_URL = "https://www.npmjs.com/package"program
.version(pkg.version)
.arguments("<module-name>")
.action(module => open(`${BASE_NPM_URL}/${module}`, "Google chrome"))
.parse(process.argv)

이제 μ‹€ν–‰ν•΄λ³΄μ‹œλ©΄ κ²€μƒ‰ν•œ λͺ¨λ“ˆμ˜ νŽ˜μ΄μ§€κ°€ λœ¨λŠ”κ²ƒμ„ λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

$ node index.js react

Deploy

λ¨Όμ € μš°λ¦¬κ°€ λ§Œλ“  ν”„λ‘œμ νŠΈλ₯Ό git 에 μ˜¬λ €μ•Όν•œλ‹€.
github 에 ν”„λ‘œμ νŠΈκ°€ μ˜¬λΌκ°€ μžˆλ‹€λŠ” κ°€μ •ν•˜μ— μ„€λͺ…ν•˜κ² μŠ΅λ‹ˆλ‹€.

.npmignore λ₯Ό μΆ”κ°€ν•˜μ—¬ 배포에 μ œμ™Έλ  νŒŒμΌμ„ μž‘μ„±ν•΄μ•Όν•œλ‹€.
λ§Œμ•½ .npmignore κ°€ μ—†κ³  .gitignore κ°€ μžˆλ‹€λ©΄ gitignore 의 κ·œμΉ™μ„ λ”°λ₯Έλ‹€.

.DS_Store
.git*
.idea
.vscode

npm 에 λ°°ν¬ν•˜κΈ° μœ„ν•΄μ„œλŠ” npm user λ₯Ό λ“±λ‘ν•΄μ•Όν•œλ‹€.

$ npm adduser

λ§ˆμ§€λ§‰μœΌλ‘œ package.json 에 정보λ₯Ό μ±„μš°λ©΄ λœλ‹€.

{
"name": "npm-finder",
"version": "1.0.0",
"description": "This is CLI to find the module page in npm",
"main": "index.js",
"scripts": {},
"bin": {
"nfinder": "index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/appear/npm-finder.git"
},
"author": {
"name": "olaf",
"email": "appear.ko@gmail.com",
"url": "https://appear.github.io/"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/appear/npm-finder/issues"
},
"homepage": "https://github.com/appear/npm-finder#readme",
"dependencies": {
"commander": "^2.19.0",
"open": "0.0.5"
},
"keywords": ["npm", "finder", "cli"]
}

npm publish λΌλŠ” λͺ…λ Ήμ–΄λ‘œ 배포 ν•  수 μžˆλ‹€.

$ npm publish

https://www.npmjs.com/package/npm-finder

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

고석진
고석진

Written by 고석진

μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό μ‚¬λž‘ν•˜λŠ” ν”„λ‘ νŠΈ 개발자 μž…λ‹ˆλ‹€ :)

No responses yet

Write a response