-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixAppKey.js
95 lines (85 loc) · 2.44 KB
/
fixAppKey.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
'use strict'
const fs = require('fs')
const path = require('path')
const FIND='1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s='
const REPLACE='P6EGPtCNW7irtdeIk+vRVzVbWOlctUKJuce1IZkO2N4='
function main() {
let filelist = []
console.log("Searching & fixing up Everlife App Key...")
withJSFiles('node_modules', (err, file) => {
if(err && err.code !== 'ENOENT') exitWithErr(err)
else if(!file) {
replace_file_ndx_1(filelist, 0)
} else filelist.push(file)
})
function replace_file_ndx_1(filelist, ndx) {
if(ndx >= filelist.length) return
replaceAppKey(filelist[ndx], (err) => {
if(err && err.code !== 'ENOENT') exitWithErr(err)
else replace_file_ndx_1(filelist, ndx+1)
})
}
}
/* outcome/
* Walk the given directory and
* callback with each javascript
* or JSON file found.
*/
let depth = 1
function withJSFiles(p, cb) {
if(hitErr()) return
fs.readdir(p, (err, files) => {
if(err) cb(err)
else {
depth += files.length
for(let i = 0;i < files.length;i++) {
let file = path.join(p, files[i])
fs.stat(file, (err, stats) => {
if(err) cb(err)
else {
if(stats.isDirectory()) {
withJSFiles(file, cb)
} else if(stats.isFile()) {
if(file.endsWith(".js") || file.endsWith(".json")) cb(null, file)
depth--
if(!depth) cb()
}
}
})
}
depth--
if(!depth) cb()
}
})
}
function replaceAppKey(file, cb) {
if(hitErr()) return
fs.readFile(file, 'utf8', (err, data) => {
if(err) cb(err)
else {
let ndx = data.indexOf(FIND)
if(ndx >= 0) do_replace_1(data)
else cb()
}
})
function do_replace_1(data) {
console.log(`Replacing in ${file}`)
data = data.replace(FIND, REPLACE)
fs.writeFile(file, data, cb)
}
}
/* outcome/
* If we hit an error, set the exit
* code so that other existing
* processes can shortcircuit and
* the program will exit with an
* exit code.
*/
function exitWithErr(err) {
console.error(err)
process.exitCode = 1
}
function hitErr() {
return process.exitCode
}
main()