-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimporter.js
67 lines (54 loc) · 1.65 KB
/
importer.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
const URL = process.env.TESTOMATIO_URL || 'https://app.testomat.io';
const isHttps = URL.startsWith('https');
const fs = require('fs');
const path = require('path');
const { request } = isHttps ? require('https') : require('http');
class Importer {
constructor(apiKey, isCodecept) {
if (!apiKey) {
console.error('✖️ Cant pull report, api key not set');
}
this.apiKey = apiKey;
this.files = {};
}
async pull() {
const files = await this.send();
if (!files) return;
files.forEach(({ file, contents }) => {
const filePath = path.resolve(file);
const directoryPath = path.dirname(filePath);
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}
fs.writeFileSync(filePath, contents);
console.log(`- "${file}" updated successfully.`);
});
}
send() {
return new Promise((res, rej) => {
const req = request(`${URL.trim()}/api/pull?api_key=${this.apiKey}`, { method: 'GET' }, (resp) => {
// The whole response has been received. Print out the result.
let message = '';
resp.on('end', () => {
if (resp.statusCode !== 200) {
rej(message);
} else {
res(JSON.parse(message));
}
});
resp.on('data', (chunk) => {
message += chunk.toString();
});
resp.on('aborted', () => {
console.log(' ✖️ Data was not received from Testomat.io');
});
});
req.on('error', (err) => {
console.log(`Error: ${err.message}`);
rej(err);
});
req.end();
});
}
}
module.exports = Importer;