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
|
var Q = require('q');
var _ = require('lodash');
var exec = require('child_process').exec;
var http = require('http');
var send = require('send');
var path = require('path');
var Gaze = require('gaze').Gaze;
function watch(dir) {
var d = Q.defer();
dir = path.resolve(dir);
var gaze = new Gaze("**/*.md", {
cwd: dir
});
gaze.once("all", function(e, filepath) {
gaze.close();
d.resolve(filepath);
});
gaze.once("error", function(err) {
gaze.close();
d.reject(err);
});
return d.promise;
}
// exit wraps a promise
// and forcefully quits the program when the promise is resolved
function exit(promise) {
promise
.then(function() {
// Prevent badly behaving plugins
// from making the process hang
process.exit(0);
}, function(err) {
// Log error
logError(err);
// Exit process with failure code
process.exit(-1);
});
}
// CLI action wrapper, calling exit when finished
function action(f) {
return function() {
// Call func and get optional promise
var p = f.apply(null, arguments);
// Exit process
return exit(Q(p));
}
}
function logError(err) {
var message = err.message || err;
if (process.env.DEBUG != null) message = err.stack || message;
console.log(message);
return Q.reject(err);
};
function runGitCommand(command, args) {
var d = Q.defer(), child;
args = ["git", command].concat(args).join(" ");
child = exec(args, function (error, stdout, stderr) {
if (error !== null) {
error.stdout = stdout;
error.stderr = stderr;
d.reject(error);
} else {
d.resolve({
stdout: stdout,
stderr: stderr
})
}
});
return d.promise;
};
// Exports
module.exports = {
exit: exit,
action: action,
watch: watch,
logError: logError,
gitCmd: runGitCommand
};
|