-
Notifications
You must be signed in to change notification settings - Fork 0
/
espruino-tools.js
61 lines (50 loc) · 1.72 KB
/
espruino-tools.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
const https = require('https');
const httpGetUrl = (url) => new Promise((resolve, reject) => https.get(url, res => {
const { statusCode } = res;
let error;
if (statusCode !== 200) {
error = new Error(`Getting ${url}\nStatus Code: ${statusCode}`);
}
if (error) {
res.resume();
reject(error);
return;
}
let rawData = '';
res.setEncoding('utf8');
res.on('data', (chunk) => { rawData += chunk; });
res.on('error', err => reject(err));
res.on('end', () => resolve(rawData));
}));
const httpGET = (url, options) => {
const getURL = options.externals && options.externals.getURL;
return getURL ? getURL(url) : httpGetUrl(url);
};
const fetchEspruinoBoardJSON = (boardName, options) => {
const boardJsonUrl = options.job.BOARD_JSON_URL || "http://www.espruino.com/json";
return httpGET(`${boardJsonUrl}/${boardName}.json`, options);
};
const fetchEspruinoModule = (moduleName, options) => {
const getModule = options.externals && options.externals.getModule;
if (getModule) {
return getModule(moduleName);
}
const moduleUrl = options.job.MODULE_URL || 'https://www.espruino.com/modules';
const moduleExts = options.job.MODULE_EXTENSIONS.split('|')
.filter(ext => !(options.minifyModules === false && ext.match('min.js')));
const fetchModule = exts => {
const ext = exts.shift();
return httpGET(`${moduleUrl}/${moduleName}${ext}`, options).catch(err => {
if (exts.length) {
return fetchModule(exts);
}
throw err;
});
}
return fetchModule(moduleExts);
};
module.exports = {
httpGET,
fetchEspruinoBoardJSON,
fetchEspruinoModule
};