-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupdate-indices.js
113 lines (102 loc) · 3.11 KB
/
update-indices.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
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const types = [
'android',
'asha',
'bada',
'brew',
'feature',
'firefoxos',
'kddi',
'palmos',
's30plus',
's40',
'symbian',
'tizen',
'touchwiz',
'wm',
'wp',
];
const useWeb = true;
/**
* Write to file the regex for the specified type
* @param {string} type
*/
function updateIndeces(type) {
console.log(`creating index for data/models-${type}.js`);
const typeName = `${type.toUpperCase()}_MODELS`;
const list = require(`../data/models-${type}.js`)[typeName];
let index = {};
let keys;
Object.keys(list).forEach((key) => {
if (key.endsWith('!!')) {
// keys = getKeysFromRegexp(key.substring(key.length() - 2));
} else if (key.endsWith('!')) {
// keys = getKeysFromRegexp(key.substring(key.length() - 1));
} else {
keys = [key.toUpperCase().substring(0, 2)];
}
keys.forEach((internalKey) => {
let value = list[internalKey];
if (value === '**') {
value = '';
}
if (!index[`@${value}`]) {
index[`@${value}`] = [];
}
index[`@${value}`].push(key);
});
});
index = ksort(index);
fs.writeFileSync(
path.join(__dirname, `../data/indices/models-${type}.js`),
'/* This file is automatically generated, do not edit manually! */\n\n' +
'/* eslint-disable */\n\n' +
`exports.${type.toUpperCase()}_INDEX = ${JSON.stringify(index)};`
);
console.log(`${type} index contains ${Object.keys(index).length + 1} keys\n`);
}
/**
* Write to file the regex for the specified type
* @param {string} type
*/
function updateIndecesFromWeb(type) {
const request = require('request');
const fs = require('fs');
const path = require('path');
console.log(`Updating indices models-${type}...`);
const filepath = path.join(__dirname, `../data/indices/models-${type}.js`);
const fileStream = fs.createWriteStream(filepath);
request(
`https://raw.githubusercontent.com/WhichBrowser/Parser/master/data/indices/models-${type}.php`,
(err, response = {}) => {
if (err) {
return;
}
let result = response.body.match(/array \(([\s\S]*)\);/)[1];
// Replace '@50' => with '@50':
result = result.replace(/('@.*') =>/g, '$1:');
// replace array ( with [ and ), with ]
result = result.replace(/array \(/g, '[').replace(/\),/g, '],');
result = result.replace(/\d+ =>/g, '');
// Write down file
fileStream.write('/* This file is automatically generated, do not edit manually! */\n\n');
fileStream.write('/* eslint-disable */\n\n');
fileStream.write(`exports.${type.toUpperCase()}_INDEX = {\n${result}};\n\n`);
fileStream.write('/* This file is automatically generated, do not edit manually! */\n');
fileStream.end();
console.log(`Downloaded new application-${type}...`);
}
);
}
/**
* Sort object by keys
* @param {object} obj
* @return {object}
*/
const ksort = (obj) =>
Object.keys(obj)
.sort()
.reduce((acc, val) => Object.assign(acc, { [val]: obj[val] }), {});
types.forEach(useWeb ? updateIndecesFromWeb : updateIndeces);