forked from ZeroCho/nodejs-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
52 changed files
with
7,636 additions
and
2,942 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>{{message}}</h1> | ||
<h2>{{error.status}}</h2> | ||
<pre>{{error.stack}}</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1>{{message}}</h1> | ||
<h2>{{error.status}}</h2> | ||
<pre>{{error.stack}}</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#!/usr/bin/env node | ||
const { program } = require('commander'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const inquirer = require('inquirer'); | ||
const chalk = require('chalk'); | ||
|
||
const htmlTemplate = ` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta chart="utf-8" /> | ||
<title>Template</title> | ||
</head> | ||
<body> | ||
<h1>Hello</h1> | ||
<p>CLI</p> | ||
</body> | ||
</html> | ||
`; | ||
|
||
const routerTemplate = ` | ||
const express = require('express'); | ||
const router = express.Router(); | ||
router.get('/', (req, res, next) => { | ||
try { | ||
res.send('ok'); | ||
} catch (error) { | ||
console.error(error); | ||
next(error); | ||
} | ||
}); | ||
module.exports = router; | ||
`; | ||
|
||
const exist = (dir) => { | ||
try { | ||
fs.accessSync(dir, fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
const mkdirp = (dir) => { | ||
const dirname = path | ||
.relative('.', path.normalize(dir)) | ||
.split(path.sep) | ||
.filter(p => !!p); | ||
dirname.forEach((d, idx) => { | ||
const pathBuilder = dirname.slice(0, idx + 1).join(path.sep); | ||
if (!exist(pathBuilder)) { | ||
fs.mkdirSync(pathBuilder); | ||
} | ||
}); | ||
}; | ||
|
||
const makeTemplate = (type, name, directory) => { | ||
mkdirp(directory); | ||
if (type === 'html') { | ||
const pathToFile = path.join(directory, `${name}.html`); | ||
if (exist(pathToFile)) { | ||
console.error(chalk.bold.red('이미 해당 파일이 존재합니다')); | ||
} else { | ||
fs.writeFileSync(pathToFile, htmlTemplate); | ||
console.log(chalk.green(pathToFile, '생성 완료')); | ||
} | ||
} else if (type === 'express-router') { | ||
const pathToFile = path.join(directory, `${name}.js`); | ||
if (exist(pathToFile)) { | ||
console.error(chalk.bold.red('이미 해당 파일이 존재합니다')); | ||
} else { | ||
fs.writeFileSync(pathToFile, routerTemplate); | ||
console.log(chalk.green(pathToFile, '생성 완료')); | ||
} | ||
} else { | ||
console.error(chalk.bold.red('html 또는 express-router 둘 중 하나를 입력하세요.')); | ||
} | ||
}; | ||
|
||
program | ||
.version('0.0.1', '-v, --version') | ||
.name('cli'); | ||
|
||
program | ||
.command('template <type>') | ||
.usage('<type> --filename [filename] --path [path]') | ||
.description('템플릿을 생성합니다.') | ||
.alias('tmpl') | ||
.option('-f, --filename [filename]', '파일명을 입력하세요.', 'index') | ||
.option('-d, --directory [path]', '생성 경로를 입력하세요', '.') | ||
.action((type, options) => { | ||
makeTemplate(type, options.filename, options.directory); | ||
}); | ||
|
||
program | ||
.action((cmd, args) => { | ||
if (args) { | ||
console.log(chalk.bold.red('해당 명령어를 찾을 수 없습니다.')); | ||
program.help(); | ||
} else { | ||
inquirer.prompt([{ | ||
type: 'list', | ||
name: 'type', | ||
message: '템플릿 종류를 선택하세요.', | ||
choices: ['html', 'express-router'], | ||
}, { | ||
type: 'input', | ||
name: 'name', | ||
message: '파일의 이름을 입력하세요.', | ||
default: 'index', | ||
}, { | ||
type: 'input', | ||
name: 'directory', | ||
message: '파일이 위치할 폴더의 경로를 입력하세요.', | ||
default: '.', | ||
}, { | ||
type: 'confirm', | ||
name: 'confirm', | ||
message: '생성하시겠습니까?', | ||
}]) | ||
.then((answers) => { | ||
if (answers.confirm) { | ||
makeTemplate(answers.type, answers.name, answers.directory); | ||
console.log(chalk.rgb(128, 128, 128)('터미널을 종료합니다.')); | ||
} | ||
}); | ||
} | ||
}) | ||
.parse(process.argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env node | ||
const readline = require('readline'); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
console.clear(); | ||
const answerCallback = (answer) => { | ||
if (answer === 'y') { | ||
console.log('감사합니다!'); | ||
rl.close(); | ||
} else if (answer === 'n') { | ||
console.log('죄송합니다!'); | ||
rl.close(); | ||
} else { | ||
console.clear(); | ||
console.log('y 또는 n만 입력하세요.'); | ||
rl.question('예제가 재미있습니까? (y/n) ', answerCallback); | ||
} | ||
}; | ||
|
||
rl.question('예제가 재미있습니까? (y/n) ', answerCallback); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "node-cli", | ||
"version": "0.0.1", | ||
"description": "nodejs cli program", | ||
"main": "index.js", | ||
"author": "ZeroCho", | ||
"license": "ISC", | ||
"bin": { | ||
"cli": "./command.js" | ||
}, | ||
"dependencies": { | ||
"chalk": "^3.0.0", | ||
"commander": "^5.0.0", | ||
"inquirer": "^7.1.0" | ||
} | ||
} |
Oops, something went wrong.