forked from Ndragomirov/twitch-now
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
420 lines (378 loc) · 9.84 KB
/
gulpfile.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
var gulp = require("gulp");
var handlebars = require("gulp-handlebars");
var del = require("del");
var wrap = require("gulp-wrap");
var declare = require("gulp-declare");
var merge = require("merge-stream");
var concat = require("gulp-concat");
var zip = require("gulp-zip");
var fs = require("fs");
var request = require("request");
// Function to replace secret inside constants
let replaceSecret = (osname) => {
// Write secret from .env to build/chrome/common/
try {
var secret = fs
.readFileSync(".env")
.toString()
.split("\n")
.find((keyValue) => keyValue.includes("TWITCH_SECRET"))
.split("=")[1];
let constants = fs
.readFileSync("build/" + osname + "/common/lib/constants.js")
.toString();
constants = constants.replace(
'client_secret: ""',
'client_secret: "' + secret + '"'
);
fs.writeFileSync("build/" + osname + "/common/lib/constants.js", constants);
} catch (err) {
throw new Error(
".env is not present with good format. Please read Readme.md file."
);
}
};
let upgradeVersion = (kindOfUpgrade, previousVersion) => {
let index = 0;
switch (kindOfUpgrade) {
case "minor":
index = 2;
break;
case "medium":
index = 1;
break;
case "major":
break;
default:
throw new Error("kindOfUpgrade argument not correct");
}
let splitted = previousVersion.split(".");
splitted[index] = `${parseInt(splitted[index]) + 1}`;
for (let i = index + 1; i < splitted.length; i++) splitted[i] = "0";
return splitted.join(".");
};
let upgradePackageVersion = (kindOfUpgrade) => {
var packageContent = JSON.parse(fs.readFileSync("package.json"));
var packageLockContent = JSON.parse(fs.readFileSync("package-lock.json"));
let upgradedVersion = upgradeVersion(kindOfUpgrade, packageContent.version);
packageContent.version = upgradedVersion;
packageLockContent.version = upgradedVersion;
fs.writeFileSync("package.json", JSON.stringify(packageContent, null, 4));
fs.writeFileSync(
"package-lock.json",
JSON.stringify(packageLockContent, null, 4)
);
};
let upgradeManifestVersion = (osname, kindOfUpgrade) => {
var manifestContent = JSON.parse(fs.readFileSync(osname + "/manifest.json"));
manifestContent.version = upgradeVersion(
kindOfUpgrade,
manifestContent.version
);
fs.writeFileSync(
osname + "/manifest.json",
JSON.stringify(manifestContent, null, 4)
);
};
// Upgrade tasks for package
gulp.task("upgrade:package:minor", function (done) {
upgradePackageVersion("minor");
done();
});
gulp.task("upgrade:package:medium", function (done) {
upgradePackageVersion("medium");
done();
});
gulp.task("upgrade:package:major", function (done) {
upgradePackageVersion("major");
done();
});
// Upgrade tasks for Chrome
gulp.task("upgrade:chrome:minor", function (done) {
upgradeManifestVersion("chrome", "minor");
done();
});
gulp.task("upgrade:chrome:medium", function (done) {
upgradeManifestVersion("chrome", "medium");
done();
});
gulp.task("upgrade:chrome:major", function (done) {
upgradeManifestVersion("chrome", "major");
done();
});
// Upgrade tasks for Firefox
gulp.task("upgrade:firefox:minor", function (done) {
upgradeManifestVersion("firefox", "minor");
done();
});
gulp.task("upgrade:firefox:medium", function (done) {
upgradeManifestVersion("firefox", "medium");
done();
});
gulp.task("upgrade:firefox:major", function (done) {
upgradeManifestVersion("firefox", "major");
done();
});
// Upgrade tasks for Opera
gulp.task("upgrade:opera:minor", function (done) {
upgradeManifestVersion("opera", "minor");
done();
});
gulp.task("upgrade:opera:medium", function (done) {
upgradeManifestVersion("opera", "medium");
done();
});
gulp.task("upgrade:opera:major", function (done) {
upgradeManifestVersion("opera", "major");
done();
});
// Upgrade tasks for all OS
gulp.task(
"upgrade:minor",
gulp.series(
"upgrade:package:minor",
"upgrade:chrome:minor",
"upgrade:firefox:minor",
"upgrade:opera:minor",
function (done) {
done();
}
)
);
gulp.task(
"upgrade:medium",
gulp.series(
"upgrade:package:medium",
"upgrade:chrome:medium",
"upgrade:firefox:medium",
"upgrade:opera:medium",
function (done) {
done();
}
)
);
gulp.task(
"upgrade:major",
gulp.series(
"upgrade:package:major",
"upgrade:chrome:major",
"upgrade:firefox:major",
"upgrade:opera:major",
function (done) {
done();
}
)
);
// Copy operations
gulp.task("copy:opera", function () {
return gulp
.src(["build/chrome/**", "opera/**"])
.pipe(gulp.dest("build/opera/"));
});
gulp.task("copy:firefox", function () {
return gulp
.src(["build/chrome/**", "firefox/**"])
.pipe(gulp.dest("build/firefox/"));
});
gulp.task("copy:chrome", function () {
var c1 = gulp.src(["common/**"]).pipe(gulp.dest("build/chrome/common/"));
var c2 = gulp.src(["_locales/**"]).pipe(gulp.dest("build/chrome/_locales/"));
var c3 = gulp.src(["chrome/**"]).pipe(gulp.dest("build/chrome/"));
return merge(c1, c2, c3);
});
// Secret replacement operations
gulp.task("secret:opera", function (done) {
replaceSecret("opera");
done();
});
gulp.task("secret:firefox", function (done) {
replaceSecret("firefox");
done();
});
gulp.task("secret:chrome", function (done) {
replaceSecret("chrome");
done();
});
// Code concatenation operations
gulp.task("concat:popupjs", function () {
return gulp
.src([
"common/lib/onerror.js",
"common/lib/utils.js",
"common/lib/3rd/jquery.js",
"common/lib/3rd/jquery.visible.js",
"common/lib/3rd/baron.js",
"common/lib/3rd/bootstrap.js",
"common/lib/3rd/underscore.js",
"common/lib/3rd/backbone.js",
"common/lib/3rd/handlebars.js",
"common/lib/3rd/prettydate.js",
"common/lib/3rd/i18n.js",
"common/dist/templates.js",
"common/lib/handlebars-helpers.js",
"common/lib/popup.js",
"common/lib/routes.js",
"common/lib/init.js",
])
.pipe(concat("popup.comb.js"))
.pipe(gulp.dest("common/dist/"));
});
gulp.task("concat:popupcss", function () {
return gulp
.src([
"common/css/reset.css",
"common/css/bootstrap.min.css",
"common/css/basic.css",
"common/css/simple-view.css",
"common/css/white-view.css",
"common/css/stream-view.css",
"common/css/channel-view.css",
"common/css/game-view.css",
"common/css/settings-view.css",
"common/css/menu-view.css",
"common/css/info-view.css",
"common/css/fontello.css",
"common/css/rtl.css",
"common/css/baron.css",
])
.pipe(concat("popup.comb.css"))
.pipe(gulp.dest("common/dist/"));
});
// Clean operations
gulp.task("clean:dist", function (done) {
del.sync(["dist/*"]);
done();
});
gulp.task("clean:build", function (done) {
del.sync(["build/*"]);
done();
});
gulp.task("clean:modules", function (done) {
del.sync(["node_modules/*"]);
done();
});
gulp.task(
"clean:all",
gulp.series("clean:dist", "clean:build", "clean:modules", function (done) {
done();
})
);
// Clean by browser
gulp.task("clean:opera", function (done) {
del.sync(["build/opera/*"]);
done();
});
gulp.task("clean:chrome", function (done) {
del.sync(["build/chrome/*"]);
done();
});
gulp.task("clean:firefox", function (done) {
del.sync(["build/firefox/*"]);
done();
});
// Compression tasks for distribution
gulp.task("compress:chrome", function () {
var v = JSON.parse(fs.readFileSync("package.json")).version;
return gulp
.src("build/chrome/**")
.pipe(zip("twitch-companion-chrome-" + v + ".zip"))
.pipe(gulp.dest("dist/"));
});
gulp.task("compress:opera", function () {
var v = JSON.parse(fs.readFileSync("package.json")).version;
return gulp
.src("build/opera/**")
.pipe(zip("twitch-companion-opera-" + v + ".zip"))
.pipe(gulp.dest("dist/"));
});
gulp.task("compress:firefox", function () {
var v = JSON.parse(fs.readFileSync("package.json")).version;
return gulp
.src("build/firefox/**")
.pipe(zip("twitch-companion-firefox-" + v + ".zip"))
.pipe(gulp.dest("dist/"));
});
gulp.task("compress:source", function () {
var v = JSON.parse(fs.readFileSync("package.json")).version;
return gulp
.src(["*", "*/**", "!node_modules/**", "!build/**", "!dist/**", "!.env"])
.pipe(zip("twitch-companion-source-code-" + v + ".zip"))
.pipe(gulp.dest("."));
});
// Task to manage handlebars templates
gulp.task("handlebars", function () {
return gulp
.src("templates/*.html")
.pipe(handlebars())
.pipe(wrap("Handlebars.template(<%= contents %>)"))
.pipe(
declare({
namespace: "Handlebars.templates",
noRedeclare: true, // Avoid duplicate declarations
})
)
.pipe(concat("templates.js"))
.pipe(gulp.dest("common/dist/"));
});
// Task to fetch contributors
gulp.task("contributors", function (cb) {
request(
{
method: "GET",
url: "https://api.github.com/repos/lpauzies/twitch-companion/contributors",
headers: {
"User-Agent": "whatever",
},
},
function (err, res, body) {
if (err || !body) {
return cb(err || new Error("No body"));
}
fs.writeFile(
"common/dist/contributors.js",
"var contributorList = " + body + ";",
cb
);
}
);
});
// Gulp main task for build
gulp.task(
"chrome",
gulp.series(
"clean:chrome",
"handlebars",
"contributors",
"concat:popupcss",
"concat:popupjs",
"copy:chrome",
"secret:chrome",
function (done) {
done();
}
)
);
gulp.task(
"opera",
gulp.series(
"chrome",
"clean:opera",
"copy:opera",
"secret:opera",
function (done) {
done();
}
)
);
gulp.task(
"firefox",
gulp.series(
"chrome",
"clean:firefox",
"copy:firefox",
"secret:firefox",
function (done) {
done();
}
)
);