fimfic2epub/gulpfile.babel.js

217 lines
5.9 KiB
JavaScript
Raw Normal View History

2016-06-21 09:04:08 +12:00
// gulp and utilities
import gulp from 'gulp'
import del from 'del'
import watch from 'gulp-watch'
2016-06-21 19:14:57 +12:00
import filter from 'gulp-filter'
2018-03-13 10:03:58 +13:00
import change from 'gulp-change'
import rename from 'gulp-rename'
2019-10-08 22:46:23 +13:00
import header from 'gulp-header'
import chmod from 'gulp-chmod'
2019-10-08 22:38:51 +13:00
import PluginError from 'plugin-error'
import log from 'fancy-log'
2016-06-21 09:04:08 +12:00
2016-06-22 01:19:36 +12:00
import jsonedit from 'gulp-json-editor'
import zip from 'gulp-zip'
import removeNPMAbsolutePaths from 'removeNPMAbsolutePaths'
2016-06-22 01:19:36 +12:00
2016-06-21 09:04:08 +12:00
// script
2016-06-21 18:39:26 +12:00
import standard from 'gulp-standard'
2016-06-21 09:04:08 +12:00
import webpack from 'webpack'
2019-10-08 21:44:04 +13:00
import webpackConfig from './webpack.config.js'
2016-06-21 09:04:08 +12:00
const inProduction = process.env.NODE_ENV === 'production' || process.argv.indexOf('-p') !== -1
2016-06-21 09:04:08 +12:00
const isStandalone = process.argv.includes('--standalone')
if (isStandalone) {
webpackConfig.shift()
webpackConfig.shift()
webpackConfig.shift()
2018-03-15 06:04:50 +13:00
} else {
webpackConfig.pop()
}
2019-10-08 22:31:42 +13:00
const watchOpts = {
2016-06-21 18:39:26 +12:00
readDelay: 500,
2018-03-13 10:03:58 +13:00
verbose: true,
read: false
2016-06-21 09:04:08 +12:00
}
2018-03-13 10:03:58 +13:00
let packageVersion = require('./package.json').version
2019-10-08 22:31:42 +13:00
const webpackDefines = new webpack.DefinePlugin({
2018-03-13 10:03:58 +13:00
FIMFIC2EPUB_VERSION: JSON.stringify(packageVersion)
})
2018-03-15 23:33:52 +13:00
// No need to bloat the build with a list of all tlds...
2020-10-26 23:13:16 +13:00
const replaceTlds = new webpack.NormalModuleReplacementPlugin(/^tlds$/, '../../../src/false')
2018-03-15 23:33:52 +13:00
2016-08-15 08:42:57 +12:00
webpackConfig.forEach((c) => {
2018-03-13 10:03:58 +13:00
c.plugins.push(webpackDefines)
2018-03-15 23:33:52 +13:00
c.plugins.push(replaceTlds)
2016-08-15 08:42:57 +12:00
})
2016-06-21 09:04:08 +12:00
2018-03-13 10:03:58 +13:00
let wpCompiler = webpack(webpackConfig)
2016-06-21 09:04:08 +12:00
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)
}
2018-03-13 10:03:58 +13:00
let p = Promise.resolve()
if (inProduction) {
p = removeNPMAbsolutePaths('node_modules')
}
p.then((results) => {
// run webpack compiler
wpCompiler.run(function (err, stats) {
2019-10-08 22:38:51 +13:00
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 })
})
2016-06-21 09:04:08 +12:00
}
2018-03-13 10:03:58 +13:00
function convertFontAwesomeVars (contents) {
2019-10-08 22:31:42 +13:00
const vars = {}
const matchVar = /\$fa-var-(.*?): "\\(.*?)";/g
2018-03-13 10:03:58 +13:00
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 }))
}
2016-06-21 09:04:08 +12:00
2016-06-21 18:39:26 +12:00
// Cleanup task
2016-08-15 21:11:20 +12:00
gulp.task('clean', () => del([
2018-03-13 10:03:58 +13:00
'build/',
'extension/build/',
'dist/',
2016-08-15 21:11:20 +12:00
'extension.zip',
'extension.xpi',
'extension.crx',
'fimfic2epub.safariextension/'
]))
2016-06-21 09:04:08 +12:00
gulp.task('version', () => {
2018-03-13 10:03:58 +13:00
delete require.cache[require.resolve('./package.json')]
packageVersion = require('./package.json').version
return Promise.resolve()
2016-06-21 18:39:26 +12:00
})
2018-03-13 10:03:58 +13:00
gulp.task('fontawesome', () => {
2018-03-15 06:04:50 +13:00
return gulp.src('node_modules/font-awesome/scss/_variables.scss')
2018-03-13 10:03:58 +13:00
.pipe(change(convertFontAwesomeVars))
.pipe(rename({
basename: 'font-awesome-codes',
extname: '.json',
dirname: ''
}))
.pipe(gulp.dest('build/'))
})
2016-06-22 01:19:36 +12:00
gulp.task('binaries', gulp.series('version', function binariesTask () {
2019-04-11 01:42:39 +12:00
return gulp.src(['build/fimfic2epub.js'])
.pipe(rename({ extname: '' }))
2019-10-08 22:46:23 +13:00
.pipe(header('#!/usr/bin/env node\n// fimfic2epub ' + packageVersion + '\n'))
.pipe(chmod(0o777))
2020-08-10 21:34:53 +12:00
.pipe(gulp.dest('build/'))
}))
gulp.task('pack:firefox', gulp.series('version', function packFirefox () {
const manifest = filter('extension/manifest.json', { restore: true })
2016-06-22 01:19:36 +12:00
return gulp.src('extension/**/*')
.pipe(manifest)
2018-03-13 10:03:58 +13:00
.pipe(jsonedit((json) => {
json.version = packageVersion
2016-06-22 01:19:36 +12:00
if (json.content_scripts) {
2016-06-23 01:43:25 +12:00
// tweak the manifest so Firefox can read it
2016-06-22 01:19:36 +12:00
json.applications = {
gecko: {
id: 'fimfic2epub@mozilla.org'
}
}
delete json.background.persistent
}
return json
}))
.pipe(manifest.restore)
.pipe(zip('extension.xpi'))
.pipe(gulp.dest('./'))
}))
2016-06-22 01:19:36 +12:00
gulp.task('pack:chrome', gulp.series('version', function packChrome () {
const manifest = filter('extension/manifest.json', { restore: true })
2018-03-13 10:03:58 +13:00
return gulp.src('extension/**/*')
.pipe(manifest)
.pipe(jsonedit({
version: packageVersion
}))
.pipe(manifest.restore)
.pipe(zip('extension.zip'))
.pipe(gulp.dest('./'))
}))
2019-04-11 01:42:39 +12:00
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', () => {
2019-10-08 21:44:04 +13:00
return lintPipe(gulp.src(['gulpfile.babel.js', 'webpack.config.js', 'src/**/*.js']))
})
gulp.task('watch:lint', () => {
2019-10-08 21:44:04 +13:00
return watch(['src/**/*.js', 'gulpfile.babel.js', 'webpack.config.js'], watchOpts, (file) => {
return lintPipe(gulp.src(file.path))
})
2016-06-22 01:19:36 +12:00
})
2016-06-23 01:43:25 +12:00
// 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')))
2018-03-13 10:03:58 +13:00
/*
2016-06-23 01:43:25 +12:00
gulp.task('pack:safari', (done) => {
2016-08-15 21:11:20 +12:00
exec('rm -rf fimfic2epub.safariextension/; cp -r extension/ fimfic2epub.safariextension', [], (error, stdout, stderr) => {
2019-10-08 22:38:51 +13:00
// log('[pack:safari]', stdout)
2016-06-23 01:43:25 +12:00
if (error || stderr) {
2019-10-08 22:38:51 +13:00
done(new PluginError('pack:safari', stderr, {showStack: false}))
2016-06-23 01:43:25 +12:00
return
}
done()
})
})
2018-03-13 10:03:58 +13:00
*/