-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
88 lines (78 loc) · 2.37 KB
/
build.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
'use strict'
const fs = require('fs')
const https = require('https')
const config = {
options: {
hostname: 'raw.githubusercontent.com',
port: 443,
path: '/WordPress/WordPress/master/wp-includes/formatting.php',
method: 'GET'
},
template: './templates/index.js'
}
downloadFile(config.options)
.then((result) => {
fs.writeFileSync('./cache/' + result.filename, result.data)
const functionSrc = extractRemoveAccentsFunction(result.data)
fs.writeFileSync('./cache/remove_accents.php', functionSrc)
const jsSrc = parseTemplate(config.template, {chars: getChars(functionSrc), conditions: getConditions(functionSrc)})
fs.writeFileSync('./' + config.template.split('/').slice(-1)[0], jsSrc)
})
.catch((reason) => {
console.log(reason)
})
function downloadFile (options) {
return new Promise((resolve, reject) => {
https.request(options, (res) => {
if (res.statusCode !== 200) {
resolve(`Wrong status code: ${res.statusCode}`)
}
const filename = options.path.split('/').slice(-1)[0]
res.setEncoding('utf8')
let rawData = ''
res.on('data', (chunk) => { rawData += chunk })
res.on('end', () => {
resolve({
filename: filename,
data: rawData
})
})
}).on('error', (e) => {
resolve(e.message)
}).end()
})
}
function extractRemoveAccentsFunction (str) {
const regex = /function remove_accents\( \$string \) \{(\n|.)*?\n\}/
return regex.exec(str)[0]
}
function getChars (str) {
const regex = /\$chars = array\(((\n|.)*?)\);/g
let chars = regex.exec(str)[1]
return chars
.replace(/\/\/.*\n/g, '')
.replace(/[\t\n]/g, '')
.replace(/\s\=>/g, ':')
.replace(/ ?, ?/g, ', ')
.trim()
.replace(/,+$/g, '')
}
function getConditions (str) {
const regex = /\$locale = get_locale\(\);((\n|.)*?)\$string = strtr\(\$string, \$chars\);/g
let conditions = regex.exec(str)[1]
return conditions
.replace(/\/\/.*\n/g, '')
.replace(/\t+/g, ' ')
.replace(/[$;]/g, '')
.replace(/[^=]==[^=]/g, '===')
.replace(/elseif/g, 'else if')
.trim()
}
function parseTemplate (tpl, repl, encoding = 'utf8') {
let jsSrc = fs.readFileSync(tpl, encoding)
for (const key of Object.keys(repl)) {
const regex = new RegExp('\\/\\*\\s*\\$\\{' + key + '\\}\\s*\\*\\/', 'g')
jsSrc = jsSrc.replace(regex, repl[key])
}
return jsSrc
}