1 | #!/usr/bin/env node
|
2 |
|
3 | var exec = require('child_process').exec;
|
4 | var fs = require('fs');
|
5 | var join = require('path').join;
|
6 | var basename = require('path').basename;
|
7 |
|
8 | var async = require('async');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function isDirectory(file, callback) {
|
17 | fs.stat(file, function(err, stats) {
|
18 | if (err) {
|
19 | var message = [
|
20 | 'Something went wrong on "' + file + '"',
|
21 | 'Message: ' + err.message
|
22 | ].join('\n');
|
23 | console.log(message);
|
24 | return callback(false);
|
25 | }
|
26 | callback(stats.isDirectory());
|
27 | });
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function isGitProject(dir, callback) {
|
37 | fs.exists(join(dir, '.git'), function(ret) {
|
38 | if (!ret) {
|
39 | console.log('\033[36m' + basename(dir) + '/\033[39m');
|
40 | console.log('Not a git repository');
|
41 | }
|
42 | callback(ret);
|
43 | });
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function run(command, options, callback) {
|
53 | options = options || {};
|
54 | exec(command, options, callback);
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function hasRemoteRepo(dir, callback) {
|
64 | var command = 'git remote show';
|
65 | run(command, { cwd: dir }, function(err, stdout, stderr) {
|
66 | if (err || stderr) {
|
67 | var message = [
|
68 | 'Something went wrong on "' + dir + '"',
|
69 | 'Command: ' + command,
|
70 | 'Message: ' + err.message || stderr
|
71 | ].join('\n');
|
72 | console.log(message);
|
73 | return callback(false);
|
74 | }
|
75 | if (!stdout) {
|
76 | console.log('\033[36m' + basename(dir) + '/\033[39m');
|
77 | console.log('Remote tracking repository is not defined');
|
78 | }
|
79 | callback(!!stdout);
|
80 | });
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function gitPull(dir, callback) {
|
90 | var command = 'git pull';
|
91 | run(command, { cwd: dir }, function(err, stdout, stderr) {
|
92 | if (err || stderr) {
|
93 | var message = [
|
94 | 'Something went wrong on "' + dir + '" ...',
|
95 | 'Command: ' + command,
|
96 | 'Message: ' + err.message || stderr
|
97 | ].join('\n');
|
98 | return new Error(message);
|
99 | }
|
100 | console.log('\033[36m' + basename(dir) + '/\033[39m');
|
101 | if (stdout) {
|
102 | process.stdout.write(stdout);
|
103 | }
|
104 | callback();
|
105 | });
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function main(parent) {
|
114 |
|
115 | fs.readdir(parent, function(err, children) {
|
116 | if (err) {
|
117 | return console.log(err.message);
|
118 | }
|
119 |
|
120 |
|
121 | var files = children.map(function(child) {
|
122 | return join(parent, child);
|
123 | });
|
124 |
|
125 |
|
126 | async.filter(files, isDirectory, function(dirs) {
|
127 |
|
128 |
|
129 | async.filter(dirs, isGitProject, function(gitProjects) {
|
130 |
|
131 |
|
132 | async.filter(gitProjects, hasRemoteRepo, function(trackingRepos) {
|
133 |
|
134 | async.each(trackingRepos, gitPull, function(err) {
|
135 | if (err) {
|
136 | console.log(err.message);
|
137 | return;
|
138 | }
|
139 | console.log('Done!');
|
140 | });
|
141 | });
|
142 | });
|
143 | });
|
144 | });
|
145 | }
|
146 |
|
147 |
|
148 | var argv = process.argv.slice(2);
|
149 | main(join(process.cwd(), argv.shift() || '.')); |
\ | No newline at end of file |