fimfic2epub/gulpfile.babel.js

195 lines
5 KiB
JavaScript
Raw Normal View History

2016-06-21 09:04:08 +12:00
// gulp and utilities
import gulp from 'gulp'
import gutil from 'gulp-util'
import del from 'del'
import Sequence from 'run-sequence'
import watch from 'gulp-watch'
import lazypipe from 'lazypipe'
2016-06-21 19:14:57 +12:00
import filter from 'gulp-filter'
2018-03-13 10:03:58 +13:00
import merge from 'merge-stream'
import change from 'gulp-change'
import rename from 'gulp-rename'
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'
2018-03-13 10:03:58 +13:00
// import { execFile, exec } from 'child_process'
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'
import webpackConfig from './webpack.config.babel.js'
const sequence = Sequence.use(gulp)
2018-03-13 10:03:58 +13:00
// const inProduction = process.env.NODE_ENV === 'production' || process.argv.indexOf('-p') !== -1
2016-06-21 09:04:08 +12:00
let 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
let webpackDefines = new webpack.DefinePlugin({
FIMFIC2EPUB_VERSION: JSON.stringify(packageVersion)
})
2016-08-15 08:42:57 +12:00
webpackConfig.forEach((c) => {
2018-03-13 10:03:58 +13:00
c.plugins.push(webpackDefines)
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
2016-06-21 18:39:26 +12:00
function webpackTask (callback) {
2018-03-13 10:03:58 +13:00
if (webpackDefines.definitions.FIMFIC2EPUB_VERSION !== JSON.stringify(packageVersion)) {
webpackDefines.definitions.FIMFIC2EPUB_VERSION = JSON.stringify(packageVersion)
wpCompiler = webpack(webpackConfig)
}
// run webpack compiler
2016-06-21 18:39:26 +12:00
wpCompiler.run(function (err, stats) {
if (err) throw new gutil.PluginError('webpack', err)
gutil.log('[webpack]', stats.toString({
colors: true,
hash: false,
version: false,
chunks: false,
chunkModules: false
}))
2016-06-23 18:57:49 +12:00
sequence('pack', callback)
2016-06-21 18:39:26 +12:00
})
2016-06-21 09:04:08 +12:00
}
2018-03-13 10:03:58 +13:00
function convertFontAwesomeVars (contents) {
let vars = {}
let matchVar = /\$fa-var-(.*?): "\\(.*?)";/g
let ma
for (;(ma = matchVar.exec(contents));) {
vars[ma[1]] = String.fromCharCode(parseInt(ma[2], 16))
}
return JSON.stringify(vars)
}
2016-06-21 18:39:26 +12:00
let lintPipe = lazypipe()
2016-06-23 00:48:51 +12:00
.pipe(filter, ['**/*', '!src/lib/**/*'])
2016-06-21 18:39:26 +12:00
.pipe(standard)
2016-06-21 19:14:57 +12:00
.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
2018-03-13 10:03:58 +13:00
gulp.task('version', (done) => {
delete require.cache[require.resolve('./package.json')]
packageVersion = require('./package.json').version
done()
})
2016-06-21 09:04:08 +12:00
// Main tasks
2018-03-13 10:03:58 +13:00
gulp.task('webpack', ['version', 'fontawesome'], webpackTask)
2016-06-21 21:32:49 +12:00
gulp.task('watch:webpack', () => {
2018-03-13 10:03:58 +13:00
return watch(['src/**/*.js', 'src/**/*.styl', './package.json'], watchOpts, () => {
2016-06-21 21:32:49 +12:00
return sequence('webpack')
2016-06-21 18:39:26 +12:00
})
2016-06-21 09:04:08 +12:00
})
gulp.task('lint', () => {
2016-08-15 21:11:20 +12:00
return gulp.src(['gulpfile.babel.js', 'webpack.config.babel.js', 'src/**/*.js', 'bin/fimfic2epub']).pipe(lintPipe())
2016-06-21 09:04:08 +12:00
})
gulp.task('watch:lint', () => {
2018-03-13 10:03:58 +13:00
return watch(['src/**/*.js', 'gulpfile.babel.js', 'webpack.config.babel.js', 'bin/fimfic2epub'], watchOpts, (file) => {
return gulp.src(file.path).pipe(lintPipe())
2016-06-21 18:39:26 +12:00
})
2016-06-21 09:04:08 +12:00
})
// Default task
gulp.task('default', (done) => {
2016-06-21 21:32:49 +12:00
sequence('clean', ['webpack', 'lint'], done)
2016-06-21 09:04:08 +12:00
})
// Watch task
gulp.task('watch', (done) => {
2018-03-13 10:03:58 +13:00
sequence('default', ['watch:lint', 'watch:pack', 'watch:webpack'], done)
2016-06-21 18:39:26 +12:00
})
2016-06-22 01:19:36 +12:00
2018-03-13 10:03:58 +13:00
gulp.task('fontawesome', () => {
let copy = gulp.src('node_modules/font-awesome/fonts/fontawesome-webfont.ttf')
.pipe(gulp.dest('extension/build/fonts/'))
let codes = 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/'))
return merge(copy, codes)
})
2016-06-22 01:19:36 +12:00
gulp.task('pack', (done) => {
2018-03-13 10:03:58 +13:00
sequence(['pack:firefox', 'pack:chrome'], done)
})
gulp.task('watch:pack', () => {
return watch(['extension/**/*', '!extension/build/**/*'], watchOpts, () => {
return sequence('pack')
})
2016-06-22 01:19:36 +12:00
})
2018-03-13 10:03:58 +13:00
gulp.task('pack:firefox', ['version'], () => {
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('./'))
})
2018-03-13 10:03:58 +13:00
gulp.task('pack:chrome', ['version'], (done) => {
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('./'))
2016-06-22 01:19:36 +12:00
})
2016-06-23 01:43:25 +12:00
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) => {
2018-03-13 10:03:58 +13:00
// gutil.log('[pack:safari]', stdout)
2016-06-23 01:43:25 +12:00
if (error || stderr) {
done(new gutil.PluginError('pack:safari', stderr, {showStack: false}))
return
}
done()
})
})
2018-03-13 10:03:58 +13:00
*/