-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdate-browser-ids.js
35 lines (33 loc) · 1.15 KB
/
update-browser-ids.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
/* eslint-disable no-console */
const request = require('request');
const fs = require('fs');
const path = require('path');
console.log('Updating browser ids...');
const filepath = path.join(__dirname, '../data/id-android.js');
const fileStream = fs.createWriteStream(filepath);
request('https://api.whichbrowser.net/resources/browser-ids.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('exports.ANDROID_BROWSERS = {\n');
result.forEach((val) => fileStream.write(` '${val.browserId}': ${deviceString(val.browserName)},\n`), null);
fileStream.write('};\n\n');
fileStream.write('/* This file is automatically generated, do not edit manually! */\n');
fileStream.end();
console.log(`Browser ids update complete with ${result.length} entries...`);
});
/**
* Escapes \ and ' inside string
*
* @param {string|null} str
*
* @return {string|null}
*/
function deviceString(str) {
if (!str) {
return null;
}
return `'${(str + '').replace(/[\\']/g, '\\$&').replace(/\u0000/g, '\\0')}'`;
}