Skip to content

Instantly share code, notes, and snippets.

@ucpwang
Last active March 8, 2019 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ucpwang/24c20a201b5a28aaceec2d70588f173b to your computer and use it in GitHub Desktop.
Save ucpwang/24c20a201b5a28aaceec2d70588f173b to your computer and use it in GitHub Desktop.
git-hook(client-hook)을 이용하여 push 전에 README.md 파일에 목차 넣어서 자동으로 갱신해주기
  • 원격 저장소 루트에 pre-push.js 작성
    • 실제 작업 프로세스
    • 파일 구조 파악해서 계층형 데이터 생성 -> 이걸로 마크다운 생성
    • javascript로 작성되었기 때문에 node.js런타임이 필요함
  • 로컬 저장소 .git/hooks하위에 pre-push 스크립트 파일 작성
    • push 될때 트리거되는 스크립트
    • pre-push.js를 실행하고, git cli로 마무리하는 녀석
  • Atom Editor + git-plus쓰는 나로썬 너무 조으다 :)

작업 결과 화면

#!/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}"
#!/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