-
Notifications
You must be signed in to change notification settings - Fork 0
/
patcher.js
111 lines (85 loc) · 2.81 KB
/
patcher.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
import fs from "fs-extra"
import path from 'path'
import * as Platform from './platform'
import * as asar from "asar"
import * as Diff from 'diff'
import os from 'os'
import natsort from "natsort";
import c from 'chalk'
const GREEN_ARROW = c.green('==>');
export const findAsarUnix = (...files) => files.find(file => fs.existsSync(file));
export const findAsarMac = () => findAsarUnix("/Applications/Franz.app/Contents/Resources/app.asar")
export const findAsarWindows = () => {
const franzPath = path.join(os.homedir(), "AppData/Local/Programs/franz")
if (!fs.existsSync(franzPath)) return undefined
const app = path.join(franzPath, "resources/app.asar");
return fs.existsSync(app) ? app : undefined;
}
export const findAsar = (dir) => {
if (dir) return path.normalize(dir) + ".asar";
const platform = Platform.getPlatform()
switch(platform) {
case Platform.Platforms.macOS:
return findAsarMac();
case Platform.Platforms.windows:
return findAsarWindows();
}
}
export const getAsarDir = (asarPath = null) => {
const filePath = asarPath || findAsar()
return path.join(
path.dirname(filePath),
path.basename(filePath, path.extname(filePath)),
)
}
export const backupAsar = () => {
const asarPath = findAsar()
const backup = `${asarPath}.${+new Date()}.backup`
fs.copySync(asarPath, backup)
console.log(`${GREEN_ARROW} Backup app.asar ${c.green(asarPath)} -> ${c.green(backup)}`)
return backup;
}
export const unpackAsar = () => {
const asarPath = findAsar()
const dir = getAsarDir(asarPath)
console.log(`${GREEN_ARROW} Unpack ${c.green(asarPath)} -> ${c.green(dir)}`)
asar.extractAll(asarPath, dir);
}
export const packAsar = async () => {
const asarPath = findAsar()
const dir = getAsarDir(asarPath)
console.log(`${GREEN_ARROW} Pack ${c.green(dir)} -> ${c.green(asarPath)}`)
return asar.createPackage(dir, asarPath)
}
export const removeUnpackedDir = () => {
const dir = getAsarDir()
console.log(`${GREEN_ARROW} Delete unpacked folder ${c.green(dir)}`)
fs.removeSync(dir);
}
export function patch() {
const patches = Diff.parsePatch(
fs.readFileSync(
path.resolve('./patch/premium.diff'),
'utf8'
)
)
for(const patch of patches)
applyPatch(patch)
}
function applyPatch(patch) {
const dir = getAsarDir()
const patchDir = path.join(dir, patch.oldFileName)
console.log(`${GREEN_ARROW} Apply patch ${c.green(patchDir)}`)
const sourceData = fs.readFileSync(
patchDir,
'utf8'
)
const sourcePatchedData = Diff.applyPatch(sourceData, patch)
if (sourcePatchedData === false) throw new Error(`Can't patch ${patch.oldFileName}`)
if (patch.oldFileName !== patch.newFileName) fs.unlinkSync(path.join(dir, patch.oldFileName))
fs.writeFileSync(
path.join(dir, patch.newFileName),
sourcePatchedData,
"utf8",
);
}