-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdate-profiles.js
66 lines (63 loc) · 1.86 KB
/
update-profiles.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
/* eslint-disable no-console */
const request = require('request');
const fs = require('fs');
const path = require('path');
console.log('Updating profiles...');
const filepath = path.join(__dirname, '../data/profiles.js');
const fileStream = fs.createWriteStream(filepath);
request('https://api.whichbrowser.net/resources/profiles.json', (err, response = {}) => {
if (err) {
return;
}
const result = JSON.parse(response.body);
fileStream.write('/* This file is automatically generated, do not edit manually! */\n\n');
fileStream.write('/* eslint-disable max-len */\n\n');
fileStream.write("const DeviceType = require('../src/constants').deviceType;\n\n");
fileStream.write('exports.PROFILES = {\n');
result.forEach(
(profile) =>
fileStream.write(
` ${escapeString(profile.url)}: ` +
`[${escapeString(profile.deviceManufacturer)}, ${escapeString(profile.deviceModel)}, ` +
`${escapeString(profile.osName)}` +
`${deviceType(profile.deviceType) ? `, ${deviceType(profile.deviceType)}` : ''}],\n`
),
null
);
fileStream.write('};\n\n');
fileStream.write('/* eslint-enable max-len */\n\n');
fileStream.write('/* This file is automatically generated, do not edit manually! */\n');
fileStream.end();
console.log(`Downloaded ${result.length} profiles...`);
});
/**
* Escapes \ and ' inside string
*
* @param {string|null} str
*
* @return {string|null}
*/
function escapeString(str) {
if (!str) {
return null;
}
return `'${(str + '')
.replace(/[\\']/g, '\\$&')
.replace(/\u{0000}/gu, '\\0')
.replace(/\u{2002}/gu, ' ')}'`;
}
/**
* Return right constants for devices
*
* @param {string|null} type
*
* @return {string|null}
*/
function deviceType(type) {
switch (type) {
case 'mobile':
return 'DeviceType.MOBILE';
case 'tablet':
return 'DeviceType.TABLET';
}
}