code style

This commit is contained in:
Matthias Kretschmann 2017-08-24 11:39:07 +02:00
parent 29e3e3953b
commit 6faec8dbb6
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 156 additions and 118 deletions

View File

@ -36,11 +36,11 @@ if [ "$TRAVIS_PULL_REQUEST" != "false" ] && [ "$TRAVIS_BRANCH" == "master" ]; th
##
elif [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
gulp build --production
gulp build --staging
else
gulp build --production
gulp build --staging
fi;

View File

@ -15,22 +15,23 @@ if [ "$TRAVIS_PULL_REQUEST" != "false" ] && [ "$TRAVIS_BRANCH" == "master" ]; th
##
elif [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
gulp deploy --live;
#gulp deploy --live;
gulp deploy --beta;
else
echo "$(tput setaf 64)" # green
tput setaf 64 # green
echo "---------------------------------------------"
echo " ✓ nothing to deploy "
echo "---------------------------------------------"
echo "$(tput sgr0)" # reset
tput sgr0 # reset
fi;
echo "$(tput setaf 64)" # green
tput setaf 64 # green
echo "---------------------------------------------"
echo " ✓ done deployment "
echo "---------------------------------------------"
echo "$(tput sgr0)" # reset
tput sgr0 # reset
exit;

View File

@ -1,35 +1,38 @@
// load plugins
const $ = require('gulp-load-plugins')()
/* eslint-disable no-console */
// manually import modules that won't get picked up by gulp-load-plugins
import fs from 'fs'
import { src, dest, watch, parallel, series } from 'gulp'
import del from 'del'
import parallelize from 'concurrent-transform'
import browser from 'browser-sync'
import critical from 'critical'
import fs from 'fs'
import yaml from 'js-yaml'
import request from 'request'
import del from 'del'
import parallelize from 'concurrent-transform'
import browser from 'browser-sync'
import critical from 'critical'
import yaml from 'js-yaml'
import request from 'request'
// required to get our mix of old and ES6+ js to work with ugify-js 3
import uglifyjs from 'uglify-es'
import composer from 'gulp-uglify/composer'
const minify = composer(uglifyjs, console)
import uglifyjs from 'uglify-es'
import composer from 'gulp-uglify/composer'
// get all the configs: `pkg` and `site`
import pkg from './package.json'
// load plugins
const spawn = require('child_process').spawn
const $ = require('gulp-load-plugins')()
const minify = composer(uglifyjs, console)
const site = yaml.safeLoad(fs.readFileSync('./_config.yml'))
// handle errors
const onError = (error) => {
console.log($.util.colors.red('\nYou fucked up:', error.message, 'on line' , error.lineNumber, '\n'))
console.log($.util.colors.red('\nYou fucked up:', error.message, 'on line', error.lineNumber, '\n'))
this.emit('end')
}
// 'development' is just default, production overrides are triggered
// by adding the production flag to the gulp command e.g. `gulp build --production`
const isProduction = ($.util.env.production === true ? true : false),
isStaging = ($.util.env.staging === true ? true : false)
const isProduction = ($.util.env.production === true)
const isStaging = ($.util.env.staging === true)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -40,36 +43,36 @@ const isProduction = ($.util.env.production === true ? true : false),
const PORT = 1337
// paths
const SRC = site.source + '/',
DIST = site.destination + '/'
const SRC = `${site.source}/`
const DIST = `${site.destination}/`
// deployment
const S3BUCKET = 'beta.ipdb.io',
S3REGION = 'eu-central-1',
S3BUCKET_BETA = 'beta.ipdb.io',
S3REGION_BETA = 'eu-central-1'
const S3BUCKET = 'beta.ipdb.io'
const S3REGION = 'eu-central-1'
const S3BUCKET_BETA = 'beta.ipdb.io'
const S3REGION_BETA = 'eu-central-1'
// SVG sprite
const SPRITECONFIG = {
dest: DIST + 'assets/img/',
mode: {
symbol: {
dest: './',
sprite: 'sprite.svg'
}
}
dest: `${DIST}assets/img/`,
mode: {
symbol: {
dest: './',
sprite: 'sprite.svg'
}
}
}
// code banner
const BANNER = [
'/**',
' ** <%= pkg.name %>',
' ** <%= pkg.description %>',
' ** <%= pkg.homepage %>',
' **',
' ** <%= pkg.author.name %> <<%= pkg.author.email %>>',
' **/',
''
'/**',
' ** <%= pkg.name %>',
' ** <%= pkg.description %>',
' ** <%= pkg.homepage %>',
' **',
' ** <%= pkg.author.name %> <<%= pkg.author.email %>>',
' **/',
''
].join('\n')
@ -82,8 +85,8 @@ const BANNER = [
//
export const clean = () =>
del([
DIST + '**/*',
DIST + '.*' // delete all hidden files
`${DIST}**/*`,
`${DIST}.*` // delete all hidden files
])
@ -91,31 +94,31 @@ export const clean = () =>
// Jekyll
//
export const jekyll = (done) => {
browser.notify('Compiling Jekyll')
let jekyllOptions
if (isProduction) {
process.env.JEKYLL_ENV = 'production'
var jekyll_options = 'jekyll build'
jekyllOptions = 'jekyll build'
} else if (isStaging) {
process.env.JEKYLL_ENV = 'staging'
var jekyll_options = 'jekyll build'
jekyllOptions = 'jekyll build'
} else {
process.env.JEKYLL_ENV = 'development'
var jekyll_options = 'jekyll build --incremental --drafts --future'
jekyllOptions = 'jekyll build --incremental --drafts --future'
}
let spawn = require('child_process').spawn,
jekyll = spawn('bundle', ['exec', jekyll_options], { stdio: 'inherit' })
const jekyllInstance = spawn('bundle', ['exec', jekyllOptions], { stdio: 'inherit' })
jekyll.on('error', (error) => onError() ).on('close', done)
jekyllInstance.on('error', (error) => onError(error)).on('close', done)
}
//
// HTML
//
export const html = () => src(DIST + '**/*.html')
export const html = () => src(`${DIST}**/*.html`)
.pipe($.if(isProduction || isStaging, $.htmlmin({
collapseWhitespace: true,
conservativeCollapse: true,
@ -134,9 +137,9 @@ export const html = () => src(DIST + '**/*.html')
// Styles
//
export const css = () => src([
SRC + '_assets/scss/ipdb.scss',
SRC + '_assets/scss/page-*.scss'
])
`${SRC}_assets/scss/ipdb.scss`,
`${SRC}_assets/scss/page-*.scss`
])
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.init()))
.pipe($.sass({
includePaths: ['node_modules']
@ -144,9 +147,9 @@ export const css = () => src([
.pipe($.autoprefixer())
.pipe($.if(isProduction || isStaging, $.cleanCss()))
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.write()))
.pipe($.if(isProduction || isStaging, $.header(BANNER, { pkg: pkg })))
.pipe($.if(isProduction || isStaging, $.header(BANNER, { pkg })))
.pipe($.rename({ suffix: '.min' }))
.pipe(dest(DIST + 'assets/css/'))
.pipe(dest(`${DIST}assets/css/`))
.pipe(browser.stream())
// inline critical-path CSS
@ -179,42 +182,43 @@ export const criticalCss = (done) => {
//
export const js = () =>
src([
SRC + '_assets/js/ipdb.js',
SRC + '_assets/js/page-*.js'
`${SRC}_assets/js/ipdb.js`,
`${SRC}_assets/js/page-*.js`
])
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.init()))
.pipe($.include({
includePaths: ['node_modules', SRC + '_assets/js']
})).on('error', onError)
.pipe($.if(isProduction || isStaging, minify())).on('error', onError)
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.write()))
.pipe($.if(isProduction || isStaging, $.header(BANNER, { pkg: pkg })))
.pipe($.rename({suffix: '.min'}))
.pipe(dest(DIST + 'assets/js/'))
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.init()))
.pipe($.include({
includePaths: ['node_modules', `${SRC}_assets/js`]
})).on('error', onError)
.pipe($.if(isProduction || isStaging, minify()))
.on('error', onError)
.pipe($.if(!(isProduction || isStaging), $.sourcemaps.write()))
.pipe($.if(isProduction || isStaging, $.header(BANNER, { pkg })))
.pipe($.rename({ suffix: '.min' }))
.pipe(dest(`${DIST}assets/js/`))
//
// SVG sprite
//
export const svg = () => src(SRC + '_assets/img/*.svg')
export const svg = () => src(`${SRC}_assets/img/*.svg`)
.pipe($.if(isProduction || isStaging, $.imagemin({
svgoPlugins: [{ removeRasterImages: true }]
})))
.pipe($.svgSprite(SPRITECONFIG))
.pipe(dest(DIST + 'assets/img/'))
.pipe(dest(`${DIST}assets/img/`))
//
// Copy Images
//
export const images = () => src(SRC + '_assets/img/**/*')
export const images = () => src(`${SRC}_assets/img/**/*`)
.pipe($.if(isProduction || isStaging, $.imagemin([
$.imagemin.gifsicle({ interlaced: true }),
$.imagemin.jpegtran({ progressive: true }),
$.imagemin.optipng({ optimizationLevel: 5 }),
$.imagemin.svgo({plugins: [{ removeViewBox: true }]})
$.imagemin.gifsicle({ interlaced: true }),
$.imagemin.jpegtran({ progressive: true }),
$.imagemin.optipng({ optimizationLevel: 5 }),
$.imagemin.svgo({ plugins: [{ removeViewBox: true }] })
])))
.pipe(dest(DIST + 'assets/img/'))
.pipe(dest(`${DIST}assets/img/`))
//
@ -223,12 +227,12 @@ export const images = () => src(SRC + '_assets/img/**/*')
export const rev = (done) => {
// globbing is slow so do everything conditionally for faster dev build
if (isProduction || isStaging) {
return src(DIST + 'assets/**/*.{css,js,png,jpg,jpeg,svg,eot,ttf,woff,woff2}')
return src(`${DIST}assets/**/*.{css,js,png,jpg,jpeg,svg,eot,ttf,woff,woff2}`)
.pipe($.rev())
.pipe(dest(DIST + 'assets/'))
.pipe(dest(`${DIST}assets/`))
// output rev manifest for next replace task
.pipe($.rev.manifest())
.pipe(dest(DIST + 'assets/'))
.pipe(dest(`${DIST}assets/`))
}
done()
}
@ -241,10 +245,10 @@ export const rev = (done) => {
export const revReplace = (done) => {
// globbing is slow so do everything conditionally for faster dev build
if (isProduction || isStaging) {
let manifest = src(DIST + 'assets/rev-manifest.json')
const manifest = src(`${DIST}assets/rev-manifest.json`)
return src(DIST + '**/*.{html,css,js}')
.pipe($.revReplace({ manifest: manifest }))
return src(`${DIST}**/*.{html,css,js}`)
.pipe($.revReplace({ manifest }))
.pipe(dest(DIST))
}
done()
@ -268,21 +272,36 @@ export const server = (done) => {
// Watch for file changes
//
export const watchSrc = () => {
watch(SRC + '_assets/scss/**/*.scss').on('all', series(css))
watch(SRC + '_assets/js/**/*.js').on('all', series(js, browser.reload))
watch(SRC + '_assets/img/**/*.{png,jpg,jpeg,gif,webp}').on('all', series(images, browser.reload))
watch(SRC + '_assets/img/**/*.{svg}').on('all', series(svg, browser.reload))
watch([SRC + '**/*.{html,xml,json,txt,md,yml}', './*.yml', SRC + '_includes/svg/*']).on('all', series('build', browser.reload))
watch(`${SRC}_assets/scss/**/*.scss`).on('all', series(css))
watch(`${SRC}_assets/js/**/*.js`).on('all', series(js, browser.reload))
watch(`${SRC}_assets/img/**/*.{png,jpg,jpeg,gif,webp}`).on('all', series(images, browser.reload))
watch(`${SRC}_assets/img/**/*.{svg}`).on('all', series(svg, browser.reload))
watch([
`${SRC}**/*.{html,xml,json,txt,md,yml}`,
'./*.yml',
`${SRC}_includes/svg/*`
]).on('all', series('build', browser.reload))
}
//
// Build banner
//
/* eslint-disable max-len */
const buildBanner = (done) => {
console.log($.util.colors.gray(" ------------------------------------------"))
console.log($.util.colors.green(' Building ' + ($.util.env.production ? 'production' : $.util.env.staging ? 'staging' : 'dev') + ' version...'))
console.log($.util.colors.gray(" ------------------------------------------"))
let buildEnvironment
if ($.util.env.production) {
buildEnvironment = 'production'
} else if ($.util.env.staging) {
buildEnvironment = 'staging'
} else {
buildEnvironment = 'dev'
}
console.log($.util.colors.gray(' ------------------------------------------'))
console.log($.util.colors.green(` Building ${buildEnvironment} version...`))
console.log($.util.colors.gray(' ------------------------------------------'))
done()
}
@ -292,15 +311,26 @@ const buildBanner = (done) => {
// Deploy banner
//
const deployBanner = (done) => {
if (($.util.env.live || $.util.env.beta || $.util.env.gamma) === true ) {
console.log($.util.colors.gray(" ------------------------------------------"))
console.log($.util.colors.green(' Deploying to ' + ($.util.env.live ? 'Live' : $.util.env.beta ? 'Beta' : 'Gamma') + '... '))
console.log($.util.colors.gray(" ------------------------------------------"))
let deployTarget
if ($.util.env.production) {
deployTarget = 'Live'
} else if ($.util.env.staging) {
deployTarget = 'Beta'
} else {
deployTarget = 'Gamma'
}
if (($.util.env.live || $.util.env.beta || $.util.env.gamma) === true) {
console.log($.util.colors.gray(' ------------------------------------------'))
console.log($.util.colors.green(` Deploying to ${deployTarget}... `))
console.log($.util.colors.gray(' ------------------------------------------'))
} else {
console.log($.util.colors.red('\nHold your horses! You need to specify a deployment target like so: gulp deploy --beta. Possible targets are: --live, --beta, --gamma\n'))
}
done()
}
/* eslint-enable max-len */
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -313,14 +343,20 @@ const deployBanner = (done) => {
// `gulp build` is the development build
// `gulp build --production` is the production build
//
export const build = series(buildBanner, clean, jekyll, parallel(html, css, js, images, svg), rev, revReplace, criticalCss)
export const build = series(
buildBanner, clean, jekyll,
parallel(html, css, js, images, svg),
rev, revReplace, criticalCss
)
//
// Build site, run server, and watch for file changes
//
// `gulp dev`
//
export const dev = series(build, server, watchSrc)
export const dev = series(
build, server, watchSrc
)
// Set `gulp dev` as default: `gulp`
export default dev
@ -336,18 +372,19 @@ export default dev
// gulp deploy --gamma
//
export const s3 = () => {
// create publisher, define config
let publisher
if ($.util.env.live === true) {
var publisher = $.awspublish.create({
params: { 'Bucket': S3BUCKET },
publisher = $.awspublish.create({
'params': { 'Bucket': S3BUCKET },
'accessKeyId': process.env.AWS_ACCESS_KEY,
'secretAccessKey': process.env.AWS_SECRET_KEY,
'region': S3REGION
})
} else if ($.util.env.beta === true) {
var publisher = $.awspublish.create({
params: { 'Bucket': S3BUCKET_BETA },
publisher = $.awspublish.create({
'params': { 'Bucket': S3BUCKET_BETA },
'accessKeyId': process.env.AWS_BETA_ACCESS_KEY,
'secretAccessKey': process.env.AWS_BETA_SECRET_KEY,
'region': S3REGION_BETA
@ -356,7 +393,7 @@ export const s3 = () => {
return
}
return src(DIST + '**/*')
src(`${DIST}**/*`)
.pipe($.awspublishRouter({
cache: {
// cache for 5 minutes by default
@ -371,7 +408,7 @@ export const s3 = () => {
// every other asset, cached
'^assets/.+$': {
cacheTime: 2592000 // cache for 1 month
cacheTime: 2592000 // cache for 1 month
},
// all html files, not cached & gzipped
@ -386,21 +423,22 @@ export const s3 = () => {
},
// font mime types
'\.ttf$': {
'.ttf$': {
key: '$&',
headers: { 'Content-Type': 'application/x-font-ttf' }
},
'\.woff$': {
'.woff$': {
key: '$&',
headers: { 'Content-Type': 'application/x-font-woff' }
},
'\.woff2$': {
'.woff2$': {
key: '$&',
headers: { 'Content-Type': 'application/x-font-woff2' }
},
// pass-through for anything that wasn't matched by routes above, to be uploaded with default options
"^.+$": "$&"
// pass-through for anything that wasn't matched by routes above,
// to be uploaded with default options
'^.+$': '$&'
}
}))
.pipe(parallelize(publisher.publish(), 100)).on('error', onError)
@ -415,11 +453,10 @@ export const s3 = () => {
// Ping search engines on live deployment
//
export const seo = (done) => {
const googleUrl = 'http://www.google.com/webmasters/tools/ping?sitemap='
const bingUrl = 'http://www.bing.com/webmaster/ping.aspx?siteMap='
const googleUrl = 'http://www.google.com/webmasters/tools/ping?sitemap=',
bingUrl = 'http://www.bing.com/webmaster/ping.aspx?siteMap='
const response = (error, response) => {
const showResponse = (error, response) => {
if (error) {
$.util.log($.util.colors.red(error))
} else {
@ -431,10 +468,10 @@ export const seo = (done) => {
}
}
// if ($.util.env.live === true) {
// request(googleUrl + site.url + '/sitemap.xml', response)
// request(bingUrl + site.url + '/sitemap.xml', response)
// }
if ($.util.env.live === true) {
request(`${googleUrl + site.url}/sitemap.xml`, showResponse)
request(`${bingUrl + site.url}/sitemap.xml`, showResponse)
}
done()
}