-
Notifications
You must be signed in to change notification settings - Fork 1
/
overwolf.webpack.js
120 lines (98 loc) · 3.47 KB
/
overwolf.webpack.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
114
115
116
117
118
119
120
// from https://github.com/Jazcash/TFT-Scout/blob/master/overwolf.webpack.js
// published under the ISC license by https://github.com/Jazcash
const path = require("path"),
fs = require("fs"),
semver = require("semver"),
zip = require("zip-a-folder")
const handleErrors = (error, compilation) => {
error = new Error(error)
compilation.errors.push(error)
throw error
}
const PluginName = "OverwolfPlugin"
module.exports = class OverwolfPlugin {
constructor(env) {
this.env = env
}
apply(compiler) {
compiler.hooks.run.tapPromise(PluginName, async (compilation) => {
try {
const newVersion = this.env.setVersion
if (newVersion && semver.valid(newVersion))
await this.setVersion(newVersion)
} catch (e) {
handleErrors(e, compilation)
}
})
compiler.hooks.afterEmit.tapPromise(PluginName, async (compilation) => {
try {
const makeOpk = this.env.makeOpk
if (makeOpk)
await this.makeOPK(
typeof makeOpk === "string" ? makeOpk : ""
)
} catch (e) {
handleErrors(e, compilation)
}
})
}
async makeOPK(suffix = "") {
const packagePath = path.resolve(__dirname, "./package.json"),
manifestPath = path.resolve(__dirname, "./public/manifest.json"),
dist = path.join(__dirname, "dist/")
const [pkg, manifest] = await Promise.all([
this.readFile(packagePath),
this.readFile(manifestPath),
])
if (!pkg) throw "could not read package.json"
if (!manifest) throw "could not read manifest.json"
const version = pkg.version,
name = manifest.meta.name,
opkPath = path.join(
__dirname,
`releases/${name}-${version}${suffix ? `.${suffix}` : ""}.opk`
)
await this.deleteFile(opkPath)
await zip.zip(dist, opkPath)
}
async setVersion(newVersion) {
const packagePath = path.resolve(__dirname, "./package.json"),
manifestPath = path.resolve(__dirname, "./public/manifest.json")
const [pkg, manifest] = await Promise.all([
this.readFile(packagePath),
this.readFile(manifestPath),
])
if (!pkg) throw "could not read package.json"
if (!manifest) throw "could not read manifest.json"
pkg.version = newVersion
manifest.meta.version = newVersion
const pkgJSON = JSON.stringify(pkg, null, " "),
manifestJSON = JSON.stringify(manifest, null, " ")
await Promise.all([
this.writeFile(packagePath, pkgJSON),
this.writeFile(manifestPath, manifestJSON),
])
}
readFile(filePath) {
return new Promise((resolve) => {
fs.readFile(filePath, (err, response) => {
try {
if (err) resolve(null)
else resolve(JSON.parse(response))
} catch (e) {
resolve(null)
}
})
})
}
writeFile(filePath, content) {
return new Promise((resolve) => {
fs.writeFile(filePath, content, resolve)
})
}
deleteFile(filePath) {
return new Promise((resolve) => {
fs.unlink(filePath, resolve)
})
}
}