Skip to content

Commit 5f29271

Browse files
committed
Refactor code into multiple files
1 parent 84d5852 commit 5f29271

File tree

3 files changed

+118
-97
lines changed

3 files changed

+118
-97
lines changed

index.js

Lines changed: 18 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
var os = require('os')
5-
var fs = require('fs')
6-
var path = require('path')
74
var queue = require('queue-async')(1)
8-
var exec = require('child_process').exec
9-
var debug = require('debug')('git-status')
105
var columnify = require('columnify')
11-
var afterAll = require('after-all-results')
12-
var filter = require('patterns')([/^\./, /\/\./])
136
var pkg = require('./package')
7+
var scan = require('./lib/scan')
8+
var git = require('./lib/git')
149

1510
var argv = require('minimist')(process.argv.slice(2))
1611
var dirs = argv._.length ? argv._ : [process.cwd()]
1712

1813
if (argv.version || argv.v) return version()
1914
if (argv.help || argv.h) return help()
2015

16+
dirs
17+
.map(function (dir) {
18+
return { cwd: dir, repos: scan(dir) }
19+
})
20+
.forEach(function (result) {
21+
result.repos.forEach(function (repo) {
22+
queue.defer(git.check, result.cwd, repo)
23+
})
24+
})
25+
26+
queue.awaitAll(function (err, results) {
27+
if (err) throw err
28+
outputResults(results)
29+
})
30+
2131
function version () {
2232
console.log(pkg.version)
2333
process.exit()
@@ -38,21 +48,7 @@ function help () {
3848
process.exit()
3949
}
4050

41-
var isDir = function (path) {
42-
try {
43-
return fs.statSync(path).isDirectory()
44-
} catch (e) {
45-
return false
46-
}
47-
}
48-
49-
var joinWith = function (a) {
50-
return function (b) {
51-
return path.join(a, b)
52-
}
53-
}
54-
55-
var outputResults = function (results) {
51+
function outputResults (results) {
5652
results = results.filter(function (result) {
5753
return result.branch !== 'master' ||
5854
result.ahead || Number.isNaN(result.ahead) ||
@@ -69,78 +65,3 @@ var outputResults = function (results) {
6965

7066
console.log(results)
7167
}
72-
73-
var status = function (cwd, cb) {
74-
exec('git status -s', { cwd: cwd }, function (err, stdout, stderr) {
75-
if (err) return cb(err)
76-
var status = { dirty: 0, untracked: 0 }
77-
stdout.trim().split(os.EOL).forEach(function (file) {
78-
if (file.substr(0, 2) === '??') status.untracked++
79-
else status.dirty++
80-
})
81-
cb(null, status)
82-
})
83-
}
84-
85-
var branch = function (cwd, cb) {
86-
exec('cat .git/HEAD', { cwd: cwd }, function (err, stdout, stderr) {
87-
if (err) return cb(err)
88-
stdout = stdout.trim()
89-
var branch = stdout.indexOf('ref:') === 0 ? stdout.substr(16) : stdout
90-
cb(null, branch)
91-
})
92-
}
93-
94-
var ahead = function (cwd, cb) {
95-
exec('git rev-list HEAD --not --remotes', { cwd: cwd }, function (err, stdout, stderr) {
96-
if (err) return cb(null, NaN) // depending on the state of the git repo, the command might return non-0 exit code
97-
stdout = stdout.trim()
98-
cb(null, !stdout ? 0 : parseInt(stdout.split(os.EOL).length, 10))
99-
})
100-
}
101-
102-
var processGitDir = function (cwd, dir, cb) {
103-
debug('processing git project', dir)
104-
105-
var next = afterAll(function (err, results) {
106-
if (err) return cb(err)
107-
108-
var branch = results[0]
109-
var ahead = results[1]
110-
var status = results[2]
111-
112-
cb(null, {
113-
dir: path.relative(cwd, dir),
114-
branch: branch,
115-
ahead: ahead,
116-
dirty: status.dirty,
117-
untracked: status.untracked
118-
})
119-
})
120-
121-
branch(dir, next())
122-
ahead(dir, next())
123-
status(dir, next())
124-
}
125-
126-
var processDir = function (cwd, dir) {
127-
if (!dir) dir = cwd
128-
dir = path.resolve(dir)
129-
130-
if (filter.match(dir)) return debug('skipping filtered directory', dir)
131-
else debug('searching directory', dir)
132-
133-
var content = fs.readdirSync(dir)
134-
135-
if (~content.indexOf('.git')) queue.defer(processGitDir, cwd, dir)
136-
else content.map(joinWith(dir)).filter(isDir).forEach(processDir.bind(null, cwd))
137-
}
138-
139-
dirs.forEach(function (dir) {
140-
processDir(dir)
141-
})
142-
143-
queue.awaitAll(function (err, results) {
144-
if (err) throw err
145-
outputResults(results)
146-
})

lib/git.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict'
2+
3+
var os = require('os')
4+
var path = require('path')
5+
var exec = require('child_process').exec
6+
var afterAll = require('after-all-results')
7+
var debug = require('debug')('git-status')
8+
9+
exports.check = function (cwd, dir, cb) {
10+
debug('processing git project', dir)
11+
12+
var next = afterAll(function (err, results) {
13+
if (err) return cb(err)
14+
15+
var branch = results[0]
16+
var ahead = results[1]
17+
var status = results[2]
18+
19+
cb(null, {
20+
dir: path.relative(cwd, dir),
21+
branch: branch,
22+
ahead: ahead,
23+
dirty: status.dirty,
24+
untracked: status.untracked
25+
})
26+
})
27+
28+
branch(dir, next())
29+
ahead(dir, next())
30+
status(dir, next())
31+
}
32+
33+
var status = function (cwd, cb) {
34+
exec('git status -s', { cwd: cwd }, function (err, stdout, stderr) {
35+
if (err) return cb(err)
36+
var status = { dirty: 0, untracked: 0 }
37+
stdout.trim().split(os.EOL).forEach(function (file) {
38+
if (file.substr(0, 2) === '??') status.untracked++
39+
else status.dirty++
40+
})
41+
cb(null, status)
42+
})
43+
}
44+
45+
var branch = function (cwd, cb) {
46+
exec('cat .git/HEAD', { cwd: cwd }, function (err, stdout, stderr) {
47+
if (err) return cb(err)
48+
stdout = stdout.trim()
49+
var branch = stdout.indexOf('ref:') === 0 ? stdout.substr(16) : stdout
50+
cb(null, branch)
51+
})
52+
}
53+
54+
var ahead = function (cwd, cb) {
55+
exec('git rev-list HEAD --not --remotes', { cwd: cwd }, function (err, stdout, stderr) {
56+
if (err) return cb(null, NaN) // depending on the state of the git repo, the command might return non-0 exit code
57+
stdout = stdout.trim()
58+
cb(null, !stdout ? 0 : parseInt(stdout.split(os.EOL).length, 10))
59+
})
60+
}

lib/scan.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
var fs = require('fs')
4+
var path = require('path')
5+
var debug = require('debug')('git-status')
6+
var filter = require('patterns')([/^\./, /\/\./])
7+
8+
module.exports = function (cwd, dir) {
9+
if (!dir) dir = cwd
10+
dir = path.resolve(dir)
11+
12+
if (filter.match(dir)) {
13+
debug('skipping filtered directory', dir)
14+
return []
15+
} else {
16+
debug('searching directory', dir)
17+
}
18+
19+
var content = fs.readdirSync(dir)
20+
21+
if (~content.indexOf('.git')) return [dir]
22+
23+
return content.map(joinWith(dir)).filter(isDir).reduce(function (result, dir) {
24+
return result.concat(module.exports(cwd, dir))
25+
}, [])
26+
}
27+
28+
var isDir = function (path) {
29+
try {
30+
return fs.statSync(path).isDirectory()
31+
} catch (e) {
32+
return false
33+
}
34+
}
35+
36+
var joinWith = function (a) {
37+
return function (b) {
38+
return path.join(a, b)
39+
}
40+
}

0 commit comments

Comments
 (0)