-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdate-models.js
121 lines (110 loc) · 3.69 KB
/
update-models.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* eslint-disable no-console */
const models = [
{
name: 'ANDROID',
needsDeviceConstants: true,
needsDeviceSubTypeConstants: true,
needsFlagsConstants: true,
hasCarrier: true,
},
{ name: 'ASHA' },
{ name: 'BADA' },
{ name: 'BLACKBERRY' },
{ name: 'BREW' },
{
name: 'FEATURE',
needsDeviceConstants: true,
},
{ name: 'FIREFOXOS' },
{
name: 'IOS',
needsDeviceConstants: true,
},
{ name: 'KDDI' },
{
name: 'PALMOS',
needsDeviceConstants: true,
needsDeviceSubTypeConstants: true,
},
{ name: 'S30PLUS' },
{ name: 'S40' },
{
name: 'SYMBIAN',
needsFlagsConstants: true,
hasCarrier: true,
hasFlagObject: true,
},
{
name: 'TIZEN',
needsDeviceConstants: true,
},
{ name: 'TOUCHWIZ' },
{
name: 'WM',
hasCarrier: true,
needsDeviceConstants: true,
},
{
name: 'WP',
needsDeviceConstants: true,
},
];
const request = require('request');
const fs = require('fs');
const path = require('path');
models.forEach((type) => {
console.log(`Updating application-${type.name.toLowerCase()}...`);
const filepath = path.join(__dirname, `../data/models-${type.name.toLowerCase()}.js`);
const fileStream = fs.createWriteStream(filepath);
request(
`https://raw.githubusercontent.com/WhichBrowser/Parser/master/data/models-${type.name.toLowerCase()}.php`,
(err, response = {}) => {
if (err) {
return;
}
let result = response.body.match(/\[[\s\S]*\]/)[0];
// Remove => and add :
result = result.replace(/=>/g, ':');
// Remove first and last []
result = result.replace(/\[\n/g, '{\n').replace(/]$/g, '}');
// Remove ] from subgroups
result = result.replace(/ {2,}],\n/g, ' },\n');
// Remove occurrences of unicode character 'EN SPACE' (U+2002)
result = result.replace(/\u{2002}/gu, ' ');
// 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.needsBrowserConstants) {
result = result.replace(/BrowserType::(.*?)\s?=>/g, '[BrowserType.$1]: ');
}
if (type.needsDeviceConstants) {
result = result.replace(/DeviceType::(\w+)/g, 'DeviceType.$1');
}
if (type.needsDeviceSubTypeConstants) {
result = result.replace(/DeviceSubType::(\w+)/g, 'DeviceSubType.$1');
}
if (type.needsFlagsConstants) {
result = result.replace(/Flag::(\w+)/g, 'Flag.$1');
}
if (type.hasCarrier) {
result = result.replace(/('carrier.*:.*')/g, '{$1}');
}
if (type.hasFlagObject) {
result = result.replace(/('flag.*?:.*?\.[A-Z0-9]+)/g, '{$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.needsDeviceSubTypeConstants &&
fileStream.write("const DeviceSubType = require('../src/constants').deviceSubType;\n\n");
type.needsBrowserConstants &&
fileStream.write("const BrowserType = require('../src/constants').browserType;\n\n");
type.needsFlagsConstants && fileStream.write("const Flag = require('../src/constants').flag;\n\n");
fileStream.write(`exports.${type.name}_MODELS = ${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()}...`);
}
);
});