-
Notifications
You must be signed in to change notification settings - Fork 892
/
generate.js
executable file
·102 lines (81 loc) · 3.18 KB
/
generate.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
'use strict';
var childProcess = require('child_process'),
fs = require('fs'),
mote = require('mote'),
wordwrap = require('wordwrap'),
path = require('path'),
moment = require('moment'),
php = 'php'; // on windows something like 'C:\\xampp\\php\\php.exe'
function generateLicenseAndVersionInfo() {
var pkg = require(path.join(__dirname, '..', 'package.json'));
var today = moment().format('YYYY-MM-DD');
return {
licenceComment: '/*!' + pkg.name + ' v' + pkg.version + ' ' + today + '*/\n',
versionInfo: pkg.version + ' ' + today
};
}
function generateCode() {
var phpScript = path.join(__dirname, 'export-config.php');
childProcess.exec(php + ' ' + phpScript, function (error, stdout, stderr) {
if (error) {
console.log(error.stack);
console.log('Error code: '+error.code);
console.log('Signal received: '+error.signal);
return;
}
var template = fs.readFileSync(path.join(__dirname, 'mobile-detect.template.js'), 'utf8'),
replaceToken = '{/*rules*/}',
tokenPos = template.indexOf(replaceToken),
jsCode,
licenceAndVersion = generateLicenseAndVersionInfo();
jsCode = template.substring(0, tokenPos) + stdout + template.substring(tokenPos + replaceToken.length);
jsCode = addComments(jsCode, stdout);
jsCode = '// THIS FILE IS GENERATED - DO NOT EDIT!\n' + licenceAndVersion.licenceComment + jsCode;
jsCode = jsCode.replace(/MOBILE_DETECT_VERSION/, licenceAndVersion.versionInfo);
fs.writeFileSync(path.join(__dirname, '..', 'mobile-detect.js'), jsCode, 'utf-8');
});
}
function addComments(jsCode, rulesJson) {
var rules = JSON.parse(rulesJson),
comments = {},
wrap = wordwrap(80);
['phones', 'tablets', 'oss', 'uas', 'props', 'utils'].forEach(function (key) {
var list = Object.keys(rules[key]).join(', ');
list = wrap(list).split(/\n/g);
comments[key] = list.join('\n * ');
});
return mote.compile(jsCode)({'keys': comments});
}
function generateTestData() {
var uaListJson, uaList,
mobilePerVendor,
number,
out;
uaListJson = fs.readFileSync(path.join(__dirname, '..', '..', 'Mobile-Detect', 'tests', 'ualist.json'), 'utf8');
uaList = JSON.parse(uaListJson);
mobilePerVendor = {};
number = 0;
uaList.user_agents.forEach(function (item) {
var vendor = item.vendor;
if ('mobile' in item) {
delete item.vendor;
item.nr = ++number;
mobilePerVendor[vendor] = mobilePerVendor[vendor] || [];
mobilePerVendor[vendor].push(item);
}
});
out = [
'/* This file was generated - do not edit! */',
'/* hash = ' + uaList.hash + ' */',
'',
'var mobilePerVendor =',
JSON.stringify(mobilePerVendor, null, 4) + ';',
'',
'if (typeof module !== "undefined") {',
' module.exports = mobilePerVendor;',
'}'
].join('\n');
fs.writeFileSync(path.join(__dirname, '..', 'tests', 'data', 'user-agents.js'), out, 'utf-8');
}
generateCode();
generateTestData();