forked from jellyfin/jellyfin-tizen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
127 lines (110 loc) · 3.92 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
var fs = require('fs');
var gulp = require('gulp');
var gulpif = require('gulp-if');
var del = require('del');
var dom = require('gulp-dom');
var path = require('path');
var scan = require('gulp-scan');
// Allow overriding of jellyfin-web directory
var WEB_DIR = process.env.JELLYFIN_WEB_DIR || 'node_modules/jellyfin-web/dist';
WEB_DIR = path.resolve(WEB_DIR);
console.info('Using jellyfin-web from', WEB_DIR);
var paths = {
assets: {
src: [
WEB_DIR + '/**/*',
'!' + WEB_DIR + '/index.html',
'!' + WEB_DIR + '/*.woff2' // exclude Noto Sans fonts (not used)
],
dest: 'www/'
},
index: {
src: WEB_DIR + '/index.html',
dest: 'www/'
}
};
// Clean the www directory
function clean() {
return del([
'www'
]);
}
// Search for used fonts and add them to assets
function searchFonts() {
const assets = paths.assets.src;
return gulp.src(WEB_DIR + '/main*.js')
.pipe(scan({
term: /[a-z0-9._-]*\.woff2/gi,
fn: function (match) {
const font = WEB_DIR + '/' + match;
if (!assets.includes(font) && fs.existsSync(font)) {
console.debug(`Found font ${match}`);
assets.push(font);
}
}
}));
}
// Copy unmodified assets
function copy() {
return gulp.src(paths.assets.src)
.pipe(gulp.dest(paths.assets.dest));
}
// Add required tags to index.html
function modifyIndex() {
return gulp.src(paths.index.src)
.pipe(dom(function() {
// inject CSP meta tag
var meta = this.createElement('meta');
meta.setAttribute('http-equiv', 'Content-Security-Policy');
meta.setAttribute('content', 'default-src * \'self\' \'unsafe-inline\' \'unsafe-eval\' data: gap: file: filesystem: ws: wss:;');
this.head.appendChild(meta);
// Search for injected main.bundle
let apploader = this.querySelector('script[src^=main]');
if (apploader) {
console.debug('Found injected main.bundle');
apploader.setAttribute('defer', '');
} else {
// Search for injected apploader
apploader = this.body.querySelector('script[src*="apploader"]');
if (apploader) {
console.debug('Found injected apploader');
apploader.setAttribute('defer', '');
} else {
console.debug('Inject apploader');
// inject apploader.js
apploader = this.createElement('script');
apploader.setAttribute('src', 'scripts/apploader.js');
apploader.setAttribute('defer', '');
this.body.appendChild(apploader);
}
}
const injectTarget = apploader.parentNode;
// inject webapis.js
const webapis = this.createElement('script');
webapis.setAttribute('src', '$WEBAPIS/webapis/webapis.js');
injectTarget.insertBefore(webapis, apploader);
// inject appMode script
var appMode = this.createElement('script');
appMode.text = 'window.appMode=\'cordova\';';
injectTarget.insertBefore(appMode, apploader);
// inject tizen.js
var tizen = this.createElement('script');
tizen.setAttribute('src', '../tizen.js');
tizen.setAttribute('defer', '');
injectTarget.insertBefore(tizen, apploader);
return this;
}))
.pipe(gulp.dest(paths.index.dest))
}
// Default build task
var build = gulp.series(
clean,
searchFonts,
gulp.parallel(copy, modifyIndex)
);
// Export tasks so they can be run individually
exports.clean = clean;
exports.copy = copy;
exports.modifyIndex = modifyIndex;
// Export default task
exports.default = build;