-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.babel.js
216 lines (185 loc) · 5.87 KB
/
gulpfile.babel.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
// gulp and utilities
import gulp from 'gulp'
import del from 'del'
import watch from 'gulp-watch'
import filter from 'gulp-filter'
import change from 'gulp-change'
import rename from 'gulp-rename'
import header from 'gulp-header'
import chmod from 'gulp-chmod'
import PluginError from 'plugin-error'
import log from 'fancy-log'
import jsonedit from 'gulp-json-editor'
import zip from 'gulp-zip'
import removeNPMAbsolutePaths from 'removeNPMAbsolutePaths'
// script
import standard from 'gulp-standard'
import webpack from 'webpack'
import webpackConfig from './webpack.config.js'
const inProduction = process.env.NODE_ENV === 'production' || process.argv.indexOf('-p') !== -1
const isStandalone = process.argv.includes('--standalone')
if (isStandalone) {
webpackConfig.shift()
webpackConfig.shift()
webpackConfig.shift()
} else {
webpackConfig.pop()
}
const watchOpts = {
readDelay: 500,
verbose: true,
read: false
}
let packageVersion = require('./package.json').version
const webpackDefines = new webpack.DefinePlugin({
FIMFIC2EPUB_VERSION: JSON.stringify(packageVersion)
})
// No need to bloat the build with a list of all tlds...
const replaceTlds = new webpack.NormalModuleReplacementPlugin(/^tlds$/, '../../../src/false')
webpackConfig.forEach((c) => {
c.plugins.push(webpackDefines)
c.plugins.push(replaceTlds)
})
let wpCompiler = webpack(webpackConfig)
function webpackTask () {
return new Promise((resolve, reject) => {
if (webpackDefines.definitions.FIMFIC2EPUB_VERSION !== JSON.stringify(packageVersion)) {
webpackDefines.definitions.FIMFIC2EPUB_VERSION = JSON.stringify(packageVersion)
wpCompiler = webpack(webpackConfig)
}
let p = Promise.resolve()
if (inProduction) {
p = removeNPMAbsolutePaths('node_modules')
}
p.then((results) => {
// run webpack compiler
wpCompiler.run(function (err, stats) {
if (err) throw new PluginError('webpack', err)
log('[webpack]', stats.toString({
colors: true,
hash: false,
version: false,
chunks: false,
timings: false,
modules: false,
chunkModules: false,
cached: false,
maxModules: 0
}))
resolve()
})
}).catch((err) => { throw err })
})
}
function convertFontAwesomeVars (contents) {
const vars = {}
const matchVar = /\$fa-var-(.*?): "\\(.*?)";/g
let ma
for (;(ma = matchVar.exec(contents));) {
vars[ma[1]] = String.fromCharCode(parseInt(ma[2], 16))
}
return JSON.stringify(vars)
}
function lintPipe (stream) {
return stream
.pipe(filter(['**/*', '!src/lib/**/*']))
.pipe(standard())
.pipe(standard.reporter('default', { breakOnError: false }))
}
// Cleanup task
gulp.task('clean', () => del([
'build/',
'extension/build/',
'dist/',
'extension.zip',
'extension.xpi',
'extension.crx',
'fimfic2epub.safariextension/'
]))
gulp.task('version', () => {
delete require.cache[require.resolve('./package.json')]
packageVersion = require('./package.json').version
return Promise.resolve()
})
gulp.task('fontawesome', () => {
return gulp.src('node_modules/font-awesome/scss/_variables.scss')
.pipe(change(convertFontAwesomeVars))
.pipe(rename({
basename: 'font-awesome-codes',
extname: '.json',
dirname: ''
}))
.pipe(gulp.dest('build/'))
})
gulp.task('binaries', gulp.series('version', function binariesTask () {
return gulp.src(['build/fimfic2epub.js'])
.pipe(rename({ extname: '' }))
.pipe(header('#!/usr/bin/env node\n// fimfic2epub ' + packageVersion + '\n'))
.pipe(chmod(0o777))
.pipe(gulp.dest('build/'))
}))
gulp.task('pack:firefox', gulp.series('version', function packFirefox () {
const manifest = filter('extension/manifest.json', { restore: true })
return gulp.src('extension/**/*')
.pipe(manifest)
.pipe(jsonedit((json) => {
json.version = packageVersion
if (json.content_scripts) {
// tweak the manifest so Firefox can read it
json.applications = {
gecko: {
id: '[email protected]'
}
}
delete json.background.persistent
}
return json
}))
.pipe(manifest.restore)
.pipe(zip('extension.xpi'))
.pipe(gulp.dest('./'))
}))
gulp.task('pack:chrome', gulp.series('version', function packChrome () {
const manifest = filter('extension/manifest.json', { restore: true })
return gulp.src('extension/**/*')
.pipe(manifest)
.pipe(jsonedit({
version: packageVersion
}))
.pipe(manifest.restore)
.pipe(zip('extension.zip'))
.pipe(gulp.dest('./'))
}))
gulp.task('pack', gulp.parallel('binaries', 'pack:firefox', 'pack:chrome'))
// Main tasks
gulp.task('webpack', gulp.series(gulp.parallel('version', 'fontawesome'), webpackTask, isStandalone ? 'binaries' : 'pack'))
gulp.task('watch:webpack', () => {
return watch(['src/**/*.js', 'src/**/*.styl', './package.json'], watchOpts, gulp.series('webpack'))
})
gulp.task('lint', () => {
return lintPipe(gulp.src(['gulpfile.babel.js', 'webpack.config.js', 'src/**/*.js']))
})
gulp.task('watch:lint', () => {
return watch(['src/**/*.js', 'gulpfile.babel.js', 'webpack.config.js'], watchOpts, (file) => {
return lintPipe(gulp.src(file.path))
})
})
// Default task
gulp.task('default', gulp.series('clean', gulp.parallel('webpack', 'lint')))
gulp.task('watch:pack', () => {
return watch(['extension/**/*', '!extension/build/**/*'], watchOpts, gulp.series('pack'))
})
// Watch task
gulp.task('watch', gulp.series('default', gulp.parallel('watch:lint', 'watch:pack', 'watch:webpack')))
/*
gulp.task('pack:safari', (done) => {
exec('rm -rf fimfic2epub.safariextension/; cp -r extension/ fimfic2epub.safariextension', [], (error, stdout, stderr) => {
// log('[pack:safari]', stdout)
if (error || stderr) {
done(new PluginError('pack:safari', stderr, {showStack: false}))
return
}
done()
})
})
*/