talkdesk-useragent-switcher/node/build.js

75 lines
1.8 KiB
JavaScript
Raw Normal View History

'use strict';
var fs = require('fs');
var path = require('path');
var UAParser = require('./ua-parser.min.js');
var cache = {};
var map = {};
var parser = new UAParser();
2019-03-14 07:20:52 -07:00
var write = ({name, content}, callback) => fs.writeFile('./browsers/' + name, content, 'utf8', e => {
if (e) {
console.log(e);
}
setTimeout(callback, 0);
console.log(name);
});
fs.readdir('./browsers/', async (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlinkSync(path.join('./browsers/', file), err => {
if (err) throw err;
});
}
//
const list = [
...require('./list-1.json'),
2019-05-11 01:28:38 -07:00
...require('./list-2.json'),
2019-05-15 22:18:58 -07:00
...require('./list-3.json'),
...require('./list-4.json')
].filter((s, i, l) => l.indexOf(s) === i);
for (const ua of list) {
parser.setUA(ua);
const o = parser.getResult();
if (o.browser.name && o.os.name) {
2019-03-14 07:20:52 -07:00
if (o.os.name === 'WIndows') {
continue;
}
cache[o.browser.name] = cache[o.browser.name] || {};
map[o.browser.name] = map[o.browser.name] || {};
cache[o.browser.name][o.os.name] = cache[o.browser.name][o.os.name] || [];
map[o.browser.name][o.os.name] = map[o.browser.name][o.os.name] || true;
cache[o.browser.name][o.os.name].push(o);
}
2019-03-14 07:20:52 -07:00
else {
// console.log(ua);
}
}
2019-03-14 07:20:52 -07:00
const contents = [];
for (const browser of Object.keys(cache)) {
for (const os of Object.keys(cache[browser])) {
2019-03-14 07:20:52 -07:00
const name = browser + '-' + os.replace(/\//g, '-') + '.json';
const content = JSON.stringify(cache[browser][os]);
contents.push({
name,
content
});
}
}
2019-03-14 07:20:52 -07:00
const once = () => {
const obj = contents.shift();
if (obj) {
write(obj, once);
}
else {
console.log('done');
fs.writeFile('./map.json', JSON.stringify(map), () => {});
}
};
once();
});
2019-03-14 07:20:52 -07:00