-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdate-applications.js
70 lines (64 loc) · 2.48 KB
/
update-applications.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
68
69
70
/* eslint-disable no-console */
const types = [
{
name: 'BOTS',
},
{
name: 'BROWSERS',
hasSubElements: true,
needsDeviceConstants: true,
needsBrowserConstants: true,
},
{
name: 'OTHERS',
hasSubElements: true,
needsDeviceConstants: true,
needsBrowserConstants: true,
},
];
const request = require('request');
const fs = require('fs');
const path = require('path');
types.forEach((type) => {
console.log(`Updating application-${type.name.toLowerCase()}...`);
const filepath = path.join(__dirname, `../data/applications-${type.name.toLowerCase()}.js`);
const fileStream = fs.createWriteStream(filepath);
request(
`https://raw.githubusercontent.com/WhichBrowser/Parser/master/data/applications-${type.name.toLowerCase()}.php`,
(err, response = {}) => {
if (err) {
return;
}
let result = response.body.match(/\[[\s\S]*\]/)[0];
// Remove => and add :
result = result
.replace(/\[(.*)]/g, '{$1}')
.replace(/'(.*?)'.*?=>.*?('.*?'|\d+|Constants[\w\\:]*[A-Z]|true|false)/g, '$1: $2');
// remove ' from regex borders
result = result.replace(/regexp:.+?'(.*?)'/g, 'regexp: $1');
// Fixing regex eg. /Yahoo\! Mindset/u -> /Yahoo! Mindset/u and /jsRSS++\/([0-9.]*)/u -> /jsRSS\+\+\/([0-9.]*)/u
result = result.replace(/\\!/g, '!').replace(/(regexp:\s+?.*?)\+\+(.*?)/g, '$1\\+\\+$2');
if (type.hasSubElements) {
result = result.replace(/^\[/, '{');
result = result.replace(/]$/, '}');
if (type.needsBrowserConstants) {
result = result.replace(/Constants\\BrowserType::(.*?)\s?=>/g, '[BrowserType.$1]: ');
}
if (type.needsDeviceConstants) {
result = result.replace(/Constants\\DeviceType::(\w+)/g, 'DeviceType.$1');
}
}
// Write down file
fileStream.write('/* This file is automatically generated, do not edit manually! */\n\n');
fileStream.write('/* eslint-disable */\n\n');
type.needsDeviceConstants &&
fileStream.write("const DeviceType = require('../src/constants').deviceType;\n\n");
type.needsBrowserConstants &&
fileStream.write("const BrowserType = require('../src/constants').browserType;\n\n");
fileStream.write(`exports.${type.name} = ${result};\n\n`);
fileStream.write('/* This file is automatically generated, do not edit manually! */\n');
fileStream.end();
console.log(`Downloaded new application-${type.name.toLowerCase()}...`);
}
);
});