-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
206 lines (191 loc) · 5.59 KB
/
index.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var exec = require('child_process').exec;
const fs = require('fs');
var join = require('path').join;
var basename = require('path').basename;
var async = require('async');
/**
* Return true if the given file path is a directory.
*
* @param {String} file
* @param {Function} callback
*/
function isDirectory(file, callback) {
fs.stat(file, function (err, stats) {
if (err) {
var message = [
'Something went wrong on "' + file + '"',
'Message: ' + err.message
].join('\n');
console.log(message);
return callback(false);
}
var result = stats.isDirectory();
callback(result);
});
}
/**
* Check if the given directory is a git repo.
*
* @param {String} dir
* @param {Function} callback
*/
function isGitProject(dir, callback) {
fs.exists(join(dir, '.git'), function (ret) {
if (!ret) {
console.log('\033[36m' + basename(dir) + '/\033[39m');
console.log('Not a git repository');
}
callback(ret);
});
}
/**
* Run the given command.
*
* @param {String} command
* @param {Object} options
*/
function run(command, options, callback) {
options = options || {};
exec(command, options, callback);
}
/**
* Check if remote tracking repo is defined.
*
* @param {String} dir
* @param {Function} callback
*/
function hasRemoteRepo(dir, callback) {
var command = 'git remote show';
run(command, {cwd: dir}, function (err, stdout, stderr) {
if (err || stderr) {
var message = '';
message += 'Something went wrong on "' + dir + '" ...';
message += 'Command: ' + command;
if (err) {
message += 'Message: ' + err.message;
} else if (stderr) {
message += 'Message: ' + stderr;
;
}
console.log(message);
return callback(false);
}
if (!stdout) {
console.log('\033[36m' + basename(dir) + '/\033[39m');
console.log('Remote tracking repository is not defined');
}
callback(!!stdout);
});
}
/**
* Run "git pull" on the given directory.
*
* @param {String} dir
* @param {Function} callback
*/
function gitPull(dir, callback) {
var command = 'git pull';
run(command, {cwd: dir}, function (err, stdout, stderr) {
if (err) {
var message = [
'Something went wrong on "' + dir + '" ...',
'Command: ' + command,
'Message: ' + err.message
].join('\n');
return callback(new Error(message));
}
console.log('\033[36m' + basename(dir) + '/\033[39m');
if (stdout) {
process.stdout.write(stdout);
}
if (stderr) {
process.stdout.write(stderr);
}
callback();
});
}
function gitStatus(dir, callback) {
var command = 'git status';
run(command, {cwd: dir}, function (err, stdout, stderr) {
if (err) {
var message = [
'Something went wrong on "' + dir + '" ...',
'Command: ' + command,
'Message: ' + err.message
].join('\n');
return callback(new Error(message));
}
console.log('\033[36m' + basename(dir) + '/\033[39m');
if (stdout) {
process.stdout.write(stdout);
}
if (stderr) {
process.stdout.write(stderr);
}
callback();
});
}
function readFiles(dir, callback) {
fs.readdir(dir, function (err, children) {
if (err) {
return callback(err);
}
var files = children.map(function (child) {
return join(dir, child);
});
// console.log(files);
return callback(null, files);
});
}
/**
* Main function.
*
* @param {String} parent
*/
module.exports = function (parent) {
// console.log(parent);
readFiles(parent, function (err, files) {
if (err) {
return console.log(err.message);
}
// Returns files
async.filter(files, isDirectory, function (dirs) {
// Returns git projects
async.filter(dirs, isGitProject, function (gitProjects) {
// Ignore if project does not have remote tracking repo
async.filter(gitProjects, hasRemoteRepo, function (trackingRepos) {
async.each(trackingRepos, gitStatus, function (err) {
if (err) {
console.log(err.message);
return;
}
console.log('Done!');
});
});
});
});
});
};
exports.gitstatus=function(parent) {
readFiles(parent, function(err, files) {
if (err) {
return console.log(err.message);
}
// Returns files
async.filter(files, isDirectory, function(dirs) {
// Returns git projects
async.filter(dirs, isGitProject, function(gitProjects) {
// Ignore if project does not have remote tracking repo
async.filter(gitProjects, hasRemoteRepo, function(trackingRepos) {
async.each(trackingRepos, gitPull, function(err) {
if (err) {
console.log(err.message);
return;
}
console.log('Done!');
});
});
});
});
});
};