summaryrefslogtreecommitdiffstats
path: root/lib/utils/command.js
blob: 4269d6cfbe7dcd23f8fb75664551084e95e97d3c (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
var childProcess = require('child_process');
var Promise = require('./promise');

// On borwser, command execution is not possible
var isAvailable = childProcess && childProcess.exec;

// Execute a command
function exec(command, options) {
    if (!isAvailable) {
        return Promise.reject(new Error('Command execution is not possible on this platform'));
    }

    return Promise.nfcall(childProcess.exec, command, options);
}

// Spawn an executable
function spawn(command, args, options) {
    if (!isAvailable) {
        return Promise.reject(new Error('Command execution is not possible on this platform'));
    }

    var d = Promise.deferred();
    var child = childProcess.spawn(command, args, options);

    child.on('error', function(error) {
        return d.reject(error);
    });

    child.on('close', function(code) {
        if (code === 0) {
            d.resolve();
        } else {
            d.reject(new Error('Error with command "'+command+'"'));
        }
    });

    return d.promise;
}

module.exports = {
    isAvailable: isAvailable,
    exec: exec,
    spawn: spawn
};