-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverwolf.webpack.js
More file actions
155 lines (123 loc) · 3.78 KB
/
overwolf.webpack.js
File metadata and controls
155 lines (123 loc) · 3.78 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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 {
version;
constructor(env) {
this.env = env
const
packagePath = path.resolve(__dirname, './package.json'),
manifestPath = path.resolve(__dirname, './app/manifest.json');
const
pkg = this.readFile(packagePath),
manifest = this.readFile(manifestPath);
if ( !pkg )
throw 'could not read package.json';
if ( !manifest )
throw 'could not read manifest.json';
var version = pkg.version;
var newDate = new Date();
var release = newDate.getUTCFullYear();
var major = newDate.getUTCMonth() + 1;
var minor = newDate.getUTCDate();
var revision = newDate.getUTCHours();
var build = newDate.getUTCMinutes();
var newVersion = `${release.toString().split(20)[1]}.${major}.${minor}`;
this.version = newVersion === version ? `${newVersion}.revision-${revision}.build-${build}` : `${newVersion}`;
}
apply(compiler) {
compiler.hooks.run.tapPromise(PluginName, async (compilation) => {
try {
if ( this.version && semver.valid(this.version) ) {
await this.setVersion(this.version);
}
} catch(e) {
handleErrors(e, compilation);
}
});
compiler.hooks.afterEmit.tapPromise(PluginName, async (compilation) => {
try {
const makeOpk = this.env.makeOpk;
if ( makeOpk ) {
await this.makeOPK();
}
} catch(e) {
handleErrors(e, compilation);
}
});
}
async makeOPK(suffix = undefined) {
const
packagePath = path.resolve(__dirname, './package.json'),
manifestPath = path.resolve(__dirname, './app/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';
const version = pkg.version, name = pkg.name,
opkPath = path.join(__dirname, `packages/${name}_${version}${(suffix) ? `.${suffix}` : ''}.opk`),
distPath = path.join(__dirname, 'build/');
await this.deleteFile(opkPath);
await zip.zip(distPath, opkPath);
}
async setVersion(newVersion) {
const packagePath = path.resolve(__dirname, './package.json'),
manifestPath = path.resolve(__dirname, './app/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';
const
version = pkg.version,
name = pkg.name;
this.version = newVersion;
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);
})}
}