Skip to content

Commit

Permalink
up to date
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroCho committed Apr 19, 2020
1 parent 80206e2 commit ffec4a4
Show file tree
Hide file tree
Showing 52 changed files with 7,636 additions and 2,942 deletions.
3 changes: 2 additions & 1 deletion ch11/11.2/nodebird/routes/middlewares.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('isNotLoggedIn', () => {
isAuthenticated: jest.fn(() => true),
};
isNotLoggedIn(req, res, next);
expect(res.redirect).toBeCalledWith('/?error=로그인한 상태입니다.');
const message = encodeURIComponent('로그인한 상태입니다.');
expect(res.redirect).toBeCalledWith(`/?error=${message}`);
});

test('로그인 되어있지 않으면 isNotLoggedIn이 next를 호출해야 함', () => {
Expand Down
3 changes: 2 additions & 1 deletion ch11/11.3/nodebird/routes/middlewares.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('isNotLoggedIn', () => {
isAuthenticated: jest.fn(() => true),
};
isNotLoggedIn(req, res, next);
expect(res.redirect).toBeCalledWith('/?error=로그인한 상태입니다.');
const message = encodeURIComponent('로그인한 상태입니다.');
expect(res.redirect).toBeCalledWith(`/?error=${message}`);
});

test('로그인 되어있지 않으면 isNotLoggedIn이 next를 호출해야 함', () => {
Expand Down
3 changes: 2 additions & 1 deletion ch11/11.4/nodebird/routes/middlewares.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('isNotLoggedIn', () => {
isAuthenticated: jest.fn(() => true),
};
isNotLoggedIn(req, res, next);
expect(res.redirect).toBeCalledWith('/?error=로그인한 상태입니다.');
const message = encodeURIComponent('로그인한 상태입니다.');
expect(res.redirect).toBeCalledWith(`/?error=${message}`);
});

test('로그인 되어있지 않으면 isNotLoggedIn이 next를 호출해야 함', () => {
Expand Down
3 changes: 2 additions & 1 deletion ch11/11.5/nodebird/routes/middlewares.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('isNotLoggedIn', () => {
isAuthenticated: jest.fn(() => true),
};
isNotLoggedIn(req, res, next);
expect(res.redirect).toBeCalledWith('/?error=로그인한 상태입니다.');
const message = encodeURIComponent('로그인한 상태입니다.');
expect(res.redirect).toBeCalledWith(`/?error=${message}`);
});

test('로그인 되어있지 않으면 isNotLoggedIn이 next를 호출해야 함', () => {
Expand Down
3 changes: 3 additions & 0 deletions ch12/12.2/gif-chat/views/error.html
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>
3 changes: 3 additions & 0 deletions ch12/12.3/gif-chat/views/error.html
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>
82 changes: 38 additions & 44 deletions ch14/14.2/node-cli/command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
const program = require('commander');
const { program } = require('commander');
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
Expand Down Expand Up @@ -80,59 +80,53 @@ const makeTemplate = (type, name, directory) => {
}
};

let triggered = false;
program
.version('0.0.1', '-v, --version')
.usage('[options]');
.name('cli');

program
.command('template <type>')
.usage('--name <name> --path [path]')
.usage('<type> --filename [filename] --path [path]')
.description('템플릿을 생성합니다.')
.alias('tmpl')
.option('-n, --name <name>', '파일명을 입력하세요.', 'index')
.option('-f, --filename [filename]', '파일명을 입력하세요.', 'index')
.option('-d, --directory [path]', '생성 경로를 입력하세요', '.')
.action((type, options) => {
makeTemplate(type, options.name, options.directory);
triggered = true;
});

program
.command('*', { noHelp: true })
.action(() => {
console.error(chalk.bold.red('해당 명령어를 찾을 수 없습니다.'));
program.help();
triggered = true;
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);

if (!triggered) {
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)('터미널을 종료합니다.'));
}
});
}
4 changes: 2 additions & 2 deletions ch14/14.2/node-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"chalk": "^3.0.0",
"commander": "^4.0.1",
"inquirer": "^7.0.1"
"commander": "^5.0.0",
"inquirer": "^7.1.0"
}
}
132 changes: 132 additions & 0 deletions ch14/test/node-cli/command.js
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);
25 changes: 25 additions & 0 deletions ch14/test/node-cli/index.js
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);

16 changes: 16 additions & 0 deletions ch14/test/node-cli/package.json
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"
}
}
Loading

0 comments on commit ffec4a4

Please sign in to comment.