-
Notifications
You must be signed in to change notification settings - Fork 85
/
gulpfile.js
189 lines (157 loc) · 5.64 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
'use strict';
const startApp = require("gulp-run-electron")
const fs = require('fs');
const gulp = require('gulp');
const useref = require('gulp-useref');
const clean = require('gulp-clean');
const sourcemaps = require('gulp-sourcemaps')
const lazypipe = require('lazypipe')
const less = require('gulp-less');
const concat = require('gulp-concat');
const path = require('path');
const merge = require('merge-stream');
const iconfont = require('gulp-iconfont');
const webpack = require('webpack-stream')
const compiler = require('webpack');
const PROD = process.env.NODE_ENV === 'production'
const OUT = "./app";
const CLEAN = [`${OUT}/*`, `!${OUT}/package.json`, `!${OUT}/node_modules`];
const SEMANTIC = './bower_components/semantic/src'
function dummy(done) {
if (done instanceof Function) {
done()
}
}
function watch({ updateMainCallback = dummy, updateRendererCallback = dummy }) {
// Restart application
gulp.watch('src/app.js', gulp.series('build:app', updateMainCallback))
gulp.watch('src/lib/*.js', gulp.series('build:lib', updateMainCallback))
// Reload renderer process
gulp.watch(['src/**/*.html'], gulp.series('build:useref', updateRendererCallback))
gulp.watch(['src/views/**/*.html'], gulp.series('build:views', updateRendererCallback))
gulp.watch(['src/css/**/*'], gulp.series('build:less', updateRendererCallback))
gulp.watch(['src/scripts/workers/*.js'], gulp.series('build:workers', updateRendererCallback))
return compileWebpack({ watch: true })
}
function compileWebpack({ watch = false }) {
const config = require('./webpack.config.js')
return gulp.src('src/main.ts')
.pipe(webpack({ ...config, watch }, compiler))
.pipe(gulp.dest(OUT))
}
gulp.task('watch', function() {
return watch({})
})
gulp.task('build:webpack', function() {
return compileWebpack({ watch: false })
})
gulp.task('clean', function() {
return gulp.src(CLEAN, {read: false})
.pipe(clean());
})
gulp.task('build:useref', function() {
var conf = {dev: c => PROD ? '' : c}
var maps = lazypipe().pipe(sourcemaps.init, { loadMaps: true})
return gulp.src('src/*.html')
.pipe(useref(conf, maps))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(OUT))
})
gulp.task('build:app', function() {
return gulp.src('src/app.js')
.pipe(gulp.dest(OUT));
})
gulp.task('build:views', function() {
return gulp.src('src/views/**/*', {base: 'src'})
.pipe(gulp.dest(OUT))
})
gulp.task('build:workers', function() {
return gulp.src('src/scripts/workers/*.js', {base: 'src'})
.pipe(gulp.dest(OUT))
})
gulp.task('build:lib', function() {
return gulp.src('src/lib/**/*', {base: 'src'})
.pipe(gulp.dest(OUT))
})
gulp.task('build:fonts', function() {
let runTimestamp = Math.round(Date.now()/1000)
return gulp.src(['src/css/fonts/icons/*.svg'], {base:'src'})
.pipe(iconfont({
fontName: 'bittorrent',
normalize: true,
fontHeight: 1024,
prependedUnicode: true,
formats: ['ttf', 'eot', 'woff', 'svg'],
timestamp: runTimestamp,
}))
.on('glyphs', function(glyphs, options) {
/* Creates a uppercase hex number with at least length digits from a given number */
function unicodeEscape(str) {
return str.replace(/[\s\S]/g, function (escape) {
return '\\u' + ('0000' + escape.charCodeAt().toString(16)).slice(-4);
});
}
/*
for (let g of glyphs) {
console.log(g.name.padEnd(24), unicodeEscape(g.unicode[0]))
}
*/
})
.pipe(gulp.dest(path.join(OUT, 'css', 'fonts')))
})
gulp.task('build:others', function() {
return gulp.src(['src/img/**/*'], {base: 'src'})
.pipe(gulp.dest(OUT))
})
gulp.task('build:build', function() {
return gulp.src(['build/**/*'])
.pipe(gulp.dest(path.join(OUT, 'build')))
})
gulp.task('build:assets' , function () {
return gulp.src('./bower_components/semantic/dist/themes/default/assets/**')
.pipe(gulp.dest(OUT + '/css/themes/default/assets'))
})
gulp.task('semantic:src', function() {
return gulp.src([
path.join(SEMANTIC, '**'),
'!'+path.join(SEMANTIC, 'themes/**'),
'!'+path.join(SEMANTIC, 'themes/')
], {base: SEMANTIC}).pipe(gulp.dest('src/css/semantic'))
})
gulp.task('semantic:default', function() {
let themes = ['default']
return gulp.src(themes.map(t => path.join(SEMANTIC, 'themes', t, '**')),
{base: path.join(SEMANTIC, 'themes')})
.pipe(gulp.dest('src/css/themes'))
})
gulp.task('build:semantic', gulp.parallel('semantic:src', 'semantic:default'))
gulp.task('build:static', gulp.parallel('build:useref', 'build:app', 'build:views', 'build:lib', 'build:others', 'build:assets', 'build:workers', 'build:build'))
gulp.task('build:less', function() {
let dir = 'src/css/themes'
let themes = fs.readdirSync(dir)
.filter(function(file) {
let stat = fs.statSync(path.join(dir, file))
return stat.isDirectory() && file !== 'default'
})
let tasks = themes.map(function(theme) {
return gulp.src(['src/css/semantic/semantic.less', 'src/css/styles.less'])
.pipe(less({
paths: [SEMANTIC],
globalVars: {
"@renderTheme": theme
}
}))
.pipe(concat(theme + '.css'))
.pipe(gulp.dest(path.join(OUT, 'css', 'themes')))
})
return merge(tasks)
})
gulp.task('build:styles', gulp.series(gulp.parallel('build:semantic', 'build:fonts'), 'build:less'))
gulp.task('build', gulp.parallel('build:static', 'build:styles', 'build:webpack'));
gulp.task('install', gulp.parallel('build:semantic'))
gulp.task('develop', gulp.series("build", function() {
watch({})
return gulp.src(OUT)
.pipe(startApp(["--debug"], { cwd: OUT }))
}))
gulp.task('default', gulp.parallel('develop'));