UNPKG

3.54 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var exec = require('child_process').exec;
4var fs = require('fs');
5var join = require('path').join;
6var basename = require('path').basename;
7
8var async = require('async');
9
10/**
11 * Return true if the given file path is a directory.
12 *
13 * @param {String} file
14 * @param {Function} callback
15 */
16function 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 * Check if the given directory is a git repo.
32 *
33 * @param {String} dir
34 * @param {Function} callback
35 */
36function 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 * Run the given command.
48 *
49 * @param {String} command
50 * @param {Object} options
51 */
52function run(command, options, callback) {
53 options = options || {};
54 exec(command, options, callback);
55}
56
57/**
58 * Check if remote tracking repo is defined.
59 *
60 * @param {String} dir
61 * @param {Function} callback
62 */
63function 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 * Run "git pull" on the given directory.
85 *
86 * @param {String} dir
87 * @param {Function} callback
88 */
89function 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 * Main function.
110 *
111 * @param {String} parent
112 */
113function main(parent) {
114 // Retrieve files in a parent directory
115 fs.readdir(parent, function(err, children) {
116 if (err) {
117 return console.log(err.message);
118 }
119
120 // Concatenate file name and its absolute path
121 var files = children.map(function(child) {
122 return join(parent, child);
123 });
124
125 // Returns files
126 async.filter(files, isDirectory, function(dirs) {
127
128 // Returns git projects
129 async.filter(dirs, isGitProject, function(gitProjects) {
130
131 // Ignore if project does not have remote tracking repo
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// Parse command line arguments
148var argv = process.argv.slice(2);
149main(join(process.cwd(), argv.shift() || '.'));
\No newline at end of file