2019-11-18 17:16:03 +00:00
|
|
|
const log = require('fancy-log')
|
|
|
|
const gulp = require('gulp')
|
2020-03-23 23:26:26 +00:00
|
|
|
const gulpSass = require('gulp-sass')
|
2019-11-18 17:16:03 +00:00
|
|
|
const rename = require("gulp-rename")
|
|
|
|
const cleanCSS = require('gulp-clean-css')
|
|
|
|
const rollup = require('gulp-better-rollup')
|
2020-03-23 23:26:26 +00:00
|
|
|
const webserver = require('gulp-webserver')
|
2019-11-18 17:16:03 +00:00
|
|
|
const terser = require('rollup-plugin-terser').terser
|
|
|
|
const browserSync = require('browser-sync').create()
|
|
|
|
const Hexo = require('hexo')
|
|
|
|
const runSequence = require('run-sequence')
|
|
|
|
|
|
|
|
const gitBranch = require('./scripts/git-branch')
|
2018-11-21 10:40:27 +00:00
|
|
|
|
2019-06-19 12:58:26 +00:00
|
|
|
const getEnv = function () {
|
|
|
|
return gitBranch() == 'master' ? 'prod' : 'dev'
|
|
|
|
}
|
|
|
|
|
2020-03-23 23:26:26 +00:00
|
|
|
const generate = (done) => {
|
2019-06-19 12:58:26 +00:00
|
|
|
var hexo = new Hexo(process.cwd(), {
|
|
|
|
config: `_config.${getEnv()}.yml`,
|
|
|
|
watch: false,
|
2019-11-18 17:16:03 +00:00
|
|
|
})
|
2019-06-19 12:58:26 +00:00
|
|
|
|
2018-11-21 10:40:27 +00:00
|
|
|
hexo.init().then(function() {
|
2019-11-18 17:16:03 +00:00
|
|
|
return hexo.call('generate')
|
2018-11-21 10:40:27 +00:00
|
|
|
}).then(function() {
|
2019-11-18 17:16:03 +00:00
|
|
|
return hexo.exit()
|
2018-11-21 10:40:27 +00:00
|
|
|
}).then(function() {
|
2020-03-23 23:26:26 +00:00
|
|
|
return done()
|
2018-11-21 10:40:27 +00:00
|
|
|
}).catch(function(err) {
|
2019-11-18 17:16:03 +00:00
|
|
|
console.log(err)
|
|
|
|
hexo.exit(err)
|
2020-03-23 23:26:26 +00:00
|
|
|
return done(err)
|
2018-11-21 10:40:27 +00:00
|
|
|
})
|
2020-03-23 23:26:26 +00:00
|
|
|
}
|
2018-11-21 10:40:27 +00:00
|
|
|
|
2020-03-23 23:26:26 +00:00
|
|
|
const bundle = () =>
|
2019-11-18 17:16:03 +00:00
|
|
|
gulp.src('js/main.js')
|
|
|
|
.on('error', log.error)
|
|
|
|
.pipe(rollup({ plugins: [terser()] }, 'iife'))
|
|
|
|
.pipe(rename("main.min.js"))
|
|
|
|
.pipe(gulp.dest('public/js'))
|
|
|
|
|
2020-03-23 23:26:26 +00:00
|
|
|
|
|
|
|
const sass = () =>
|
2019-11-18 17:16:03 +00:00
|
|
|
gulp.src("./themes/navy/source/scss/main.scss")
|
|
|
|
.on('error', log.error)
|
2020-03-23 23:26:26 +00:00
|
|
|
.pipe(gulpSass())
|
2019-11-18 17:16:03 +00:00
|
|
|
.pipe(gulp.dest('./public/css'))
|
|
|
|
.pipe(browserSync.stream())
|
2018-11-21 10:40:27 +00:00
|
|
|
|
2020-03-23 23:26:26 +00:00
|
|
|
const css = () =>
|
2018-11-21 10:40:27 +00:00
|
|
|
gulp.src('./public/css/main.css')
|
2019-11-18 17:16:03 +00:00
|
|
|
.on('error', log.error)
|
2018-11-21 10:40:27 +00:00
|
|
|
.pipe(cleanCSS())
|
|
|
|
.pipe(rename("main.min.css"))
|
2019-11-18 17:16:03 +00:00
|
|
|
.pipe(gulp.dest('./public/css/'))
|
2018-11-21 10:40:27 +00:00
|
|
|
|
2020-03-23 23:26:26 +00:00
|
|
|
const devel = () => {
|
|
|
|
gulp.watch('./js/*.js', bundle)
|
|
|
|
gulp.watch(['./source/**/*.{md,yml}', './themes/navy/**/*'], generate)
|
|
|
|
gulp.watch('./themes/navy/source/scss/*.scss', sass, css)
|
|
|
|
}
|
2018-11-21 10:40:27 +00:00
|
|
|
|
2020-03-23 23:26:26 +00:00
|
|
|
const server = () =>
|
|
|
|
gulp.src('./public').pipe(webserver({livereload: true, open: true}));
|
2018-11-21 10:40:27 +00:00
|
|
|
|
2020-03-23 23:26:26 +00:00
|
|
|
exports.bundle = bundle
|
|
|
|
exports.sass = sass
|
|
|
|
exports.css = gulp.series(sass, css)
|
|
|
|
exports.devel = gulp.parallel(server, devel)
|
|
|
|
exports.build = gulp.parallel(generate, bundle, exports.sass)
|
|
|
|
exports.default = exports.build
|