This repository was archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·57 lines (50 loc) · 1.43 KB
/
index.js
File metadata and controls
executable file
·57 lines (50 loc) · 1.43 KB
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
var child_process = require('child_process'),
crypto = require('crypto'),
path = require('path');
function subst(cmd, keys) {
if (keys) {
return cmd.replace(/\{(\w+)\}/g, function(match, item) {
return keys[item];
});
} else {
return cmd;
}
}
module.exports.logger = console.log.bind(console);
module.exports.tmpname = function tmpname(pattern) {
var tmpbase = process.env['TMPDIR'] || '/tmp';
var tmpfile = pattern +
crypto.randomBytes(6).toString('base64').replace('/', '-');
return path.resolve(tmpbase, tmpfile);
}
module.exports.run = function run(cmd, keys) {
return function (/* ..., callback */) {
var callback = arguments[arguments.length - 1];
cmd = subst(cmd, keys);
module.exports.logger('executing: ' + cmd);
child_process.exec(cmd, callback);
};
}
module.exports.sync = function sync(f) {
return function (/* ..., callback */) {
var callback = arguments[arguments.length - 1];
try {
var value = f.apply(this, arguments);
} catch(e) {
return callback(e);
}
callback(null, value);
};
}
module.exports.cd = function cd(dir, keys) {
return module.exports.sync(function() {
dir = subst(dir, keys);
module.exports.logger('changing directory to: ' + dir);
process.chdir(dir);
});
}
module.exports.assign = function assign(key, keys) {
return module.exports.sync(function(stdout) {
keys[key] = stdout.trim();
});
}