summaryrefslogtreecommitdiffstats
path: root/bin/utils.js
blob: 821112ee21e408ff3c42ab64ae383a2363b803a5 (plain)
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
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;
}

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 = {
    watch: watch,
    logError: logError,
    gitCmd: runGitCommand
};