-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·125 lines (99 loc) · 3.71 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#! /usr/bin/env node
const VERSION = '0.5.1';
const HOOK_VERSION = '0.2.0';
const fs = require('fs');
const {argv} = process;
const path = require('path');
const {exec, execSync} = require('child_process');
if (argv.length < 3) {
// no arguments were passed
console.log(
`
Usage:
gitnotice init-all - from the working directory, initializes gitnotice hooks for all git projects found recursivly
gitnotice init [directory] - initialize gitnotice hook in directory, or in current project.
gitnotice -v - print the version and exit
`)
}
if (argv.includes('-v')) {
console.log(`GITNOTICE - distributed notifications in your VCS\n`);
console.log(`gitnotice-cli: v${VERSION}`);
console.log(`notice-hook: v${HOOK_VERSION}`);
}
if (argv[2] === 'init-all') {
const inits = initAll(argv[3]);
if (!inits.length) {
return 'No git repositories found';
}
console.log("Initialized Gitnotice in the following repositories:");
console.log(inits.map(i => '✅ ' + i).join("\n"))
}
if (argv[2] === 'init') {
init(argv[3]);
console.log('gitnotice initialized (notice-hook: ' + HOOK_VERSION + ')');
}
if (argv[2] === 'check') {
check(argv[3]);
}
function initAll(directory = '') {
const cwd = path.join(process.cwd(), directory);
if (fs.existsSync(path.join(cwd, '.git'))) {
const initializedIn = init(directory);
return [initializedIn];
}
const subfolders = fs.readdirSync(cwd).filter(item => {
return fs.lstatSync(path.join(cwd, item)).isDirectory()
});
if (!subfolders.length) {
return
}
return [].concat(subfolders.map(subfolder => initAll(path.join(directory, subfolder))).filter(result => result && result.length));
}
function init(directory = '.') {
enforceGitExistsOrExit(directory);
const cwd = path.join(process.cwd(), directory);
// update notice-hook (also adds if it doesn't exist)
_copyProjectFileToDirectory('notice-hook', directory + '/.git/hooks/');
// If there's no post-merge hook, use ours
if (!fs.existsSync(path.join(cwd, '.git/hooks/post-merge'))) {
_copyProjectFileToDirectory('post-merge', directory + '/.git/hooks/')
}
// If the post-merge hook does not call post-merge, add a line to call it.
if (!execSync(`cat ${path.join(cwd, '.git/hooks/post-merge')} | grep "./notice-hook" || exit 0`)) {
execSync(`echo '## gitnotice (https://github.com/untapt/gitnotice)' >> ${path.join(cwd, '.git/hooks/post-merge')}`);
execSync(`echo './notice-hook' >> ${path.join(cwd, '.git/hooks/post-merge')}`);
}
return cwd;
}
function _copyProjectFileToDirectory(file, directory = '.') {
if (!file) {
throw new Error('file name must be specified');
}
const cwd = path.join(process.cwd(), directory);
exec(`cp -f ${path.join(__dirname, file)} .`, {cwd, timeout: 5}, (err, stdout, stderr) => {
if (err) {
console.error(err);
throw err;
}
if (stderr) {
console.error(stderr);
}
});
}
function check(directory = '.') {
enforceGitExistsOrExit(directory);
const cwd = path.join(process.cwd(), directory);
exec(`cat ${path.join(cwd, directory, '.git/hooks/post-merge')} | head -n 2 | tail -n 1`, {cwd}, (err, stdout, stderr) => {
const match = stdout.match(/\.\/notice-hook/);
if (match && match.length > 1) {
console.log(`✅ gitnotice (${match[1]}) initialized in ${path.join(cwd, directory)}`);
}
})
}
function enforceGitExistsOrExit(directory = '.') {
const cwd = path.join(process.cwd(), directory);
if (!fs.existsSync(path.join(cwd, '.git'))) {
console.error("No git project found");
process.exit(2);
}
}