- 원격 저장소 루트에
pre-push.js
작성- 실제 작업 프로세스
- 파일 구조 파악해서 계층형 데이터 생성 -> 이걸로 마크다운 생성
javascript
로 작성되었기 때문에node.js
런타임이 필요함
- 로컬 저장소
.git/hooks
하위에pre-push
스크립트 파일 작성push
될때 트리거되는 스크립트pre-push.js
를 실행하고,git cli
로 마무리하는 녀석
Atom Editor
+git-plus
쓰는 나로썬 너무 조으다 :)
Last active
March 8, 2019 04:08
-
-
Save ucpwang/24c20a201b5a28aaceec2d70588f173b to your computer and use it in GitHub Desktop.
git-hook(client-hook)을 이용하여 push 전에 README.md 파일에 목차 넣어서 자동으로 갱신해주기
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
#!/bin/sh | |
echo "automation update -> 'README.md'" | |
NODE_RESULT=$(node ./pre-push.js 2>&1) | |
if [ $? -ne 0 ]; then | |
echo | |
echo "${NODE_RESULT}" | |
exit 1 | |
fi | |
GIT_RESULT=$(git add ./README.md && git commit -m "automation update -> 'README.md'" 2>&1) | |
echo "${GIT_RESULT}" |
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
#!/usr/bin/env node | |
const req = require('request'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const __PREFIX_PATH = './'; | |
// 정보만 취득하는 함수 | |
const getFileInfo = (dir, filelist = []) => { | |
let files = fs.readdirSync(dir); | |
for (let file of files) { | |
let filePath = path.join(dir, file); | |
if (filePath === '.git' || | |
filePath === '.gitignore' || | |
filePath === 'node_modules' || | |
filePath === 'README.md' || | |
filePath === 'package-lock.json' || | |
filePath === 'package.json' || | |
filePath === 'pre-push.js' | |
) continue; | |
filelist.push({ | |
file: filePath, | |
depth: filePath.split('/').length, | |
files: fs.statSync(filePath).isDirectory() ? getFileInfo(filePath, dir.files) : [] | |
}); | |
} | |
return filelist; | |
}; | |
// 작성 준비 | |
let lines = [ | |
'# daily-works / `jacob.c` 업무일지' | |
]; | |
// 출력용 문자열 목록화 함수 | |
const recursivePrintLinePush = (o) => { | |
let markup = o.depth === 1 ? '###' : '-'; | |
lines.push(`${Array(o.depth > 1 ? o.depth - 1 : o.depth).join(' ')}${markup} [${o.file}](${__PREFIX_PATH + o.file})`); | |
if (o.files.length > 0) { | |
o.files.forEach(recursivePrintLinePush); | |
} | |
} | |
// 실행 | |
getFileInfo(__PREFIX_PATH).forEach(recursivePrintLinePush); | |
// README.md 파일 다시 쓰기 | |
fs.writeFile(`${__PREFIX_PATH}README.md`, lines.join('\n'), 'utf8', (err, data) => { | |
if (err) console.log(err); | |
}); | |
// 알림 보내기 | |
req.post( | |
'[사내 알림 API]', | |
{ json: { to: '나에게', msg: '[daily-works] 작성 완료' } }, | |
(err, res, body) => { | |
if (err) console.error(err); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment