forked from HabitRPG/habitica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskHelper.js
116 lines (101 loc) · 2.94 KB
/
taskHelper.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { exec } from 'child_process';
import psTree from 'ps-tree';
import nconf from 'nconf';
import net from 'net';
import Q from 'q';
import { post } from 'superagent';
import { sync as glob } from 'glob';
import Mocha from 'mocha';
import { resolve } from 'path';
/*
* Get access to configruable values
*/
nconf.argv().env().file({ file: 'config.json' });
export var conf = nconf;
/*
* Kill a child process and any sub-children that process may have spawned.
* This is necessary to ensure that Gulp will terminate when it has completed
* its tasks.
*/
export function kill(proc) {
((pid) => {
psTree(pid, (_, pids) => {
if(pids.length) {
pids.forEach(kill); return
}
try {
exec(/^win/.test(process.platform)
? `taskkill /PID ${pid} /T /F`
: `kill -9 ${pid}`)
}
catch(e) { console.log(e) }
});
}(proc.PID || proc.pid));
};
/*
* Return a promise that will execute when Node is able to connect on a
* specific port. For example, this can be used to halt tasks until Selenium
* has fully spun up. Optionally provide a maximum number of seconds to wait
* before failing.
*/
export function awaitPort(port, max=60) {
let socket, timeout, interval;
let deferred = Q.defer();
timeout = setTimeout(() => {
clearInterval(interval);
deferred.reject(`Timed out after ${max} seconds`);
}, max * 1000);
interval = setInterval(() => {
socket = net.connect({port: port}, () => {
clearInterval(interval);
clearTimeout(timeout);
socket.destroy();
deferred.resolve();
}).on('error', () => { socket.destroy });
}, 1000);
return deferred.promise
};
/*
* Pipe the child's stdin and stderr to the parent process.
*/
export function pipe(child) {
child.stdout.on('data', (data) => { process.stdout.write(data) });
child.stderr.on('data', (data) => { process.stderr.write(data) });
};
/*
* Post request to notify configured slack channel
*/
export function postToSlack(msg, config={}) {
let slackUrl = nconf.get('SLACK_URL');
if (!slackUrl) {
console.error('No slack post url specified. Your message was:');
console.log(msg);
return;
}
post(slackUrl)
.send({
channel: `#${config.channel || '#general'}`,
username: config.username || 'gulp task',
text: msg,
icon_emoji: `:${config.emoji || 'gulp'}:`
})
.end((err, res) => {
if (err) console.error('Unable to post to slack', err);
});
}
export function runMochaTests(files, server, cb) {
require('../test/helpers/globals.helper');
let mocha = new Mocha({reporter: 'spec'});
let tests = glob(files);
tests.forEach((test) => {
delete require.cache[resolve(test)];
mocha.addFile(test);
});
mocha.run((numberOfFailures) => {
if (!process.env.RUN_INTEGRATION_TEST_FOREVER) {
if (server) kill(server);
process.exit(numberOfFailures);
}
cb();
});
}