2017-03-22 16:34:05 +01:00
'use strict'
2015-12-20 05:22:45 +01:00
// load plugins
2017-03-22 16:34:05 +01:00
const $ = require ( 'gulp-load-plugins' ) ( )
2015-12-20 05:22:45 +01:00
// manually require modules that won"t get picked up by gulp-load-plugins
2017-03-22 16:34:05 +01:00
const gulp = require ( 'gulp' ) ,
del = require ( 'del' ) ,
pkg = require ( './package.json' ) ,
parallelize = require ( 'concurrent-transform' ) ,
browser = require ( 'browser-sync' ) . create ( ) ,
spawn = require ( 'child_process' ) . spawn
2015-12-20 05:22:45 +01:00
// handle errors
2017-03-22 16:34:05 +01:00
const onError = ( error ) => {
console . log ( $ . util . colors . red ( '\nYou fucked up:' , error . message , 'on line' , error . lineNumber , '\n' ) )
this . emit ( 'end' )
2015-12-20 05:22:45 +01:00
}
// 'development' is just default, production overrides are triggered
// by adding the production flag to the gulp command e.g. `gulp build --production`
2017-03-22 16:34:05 +01:00
const isProduction = ( $ . util . env . production === true ? true : false ) ,
isStaging = ( $ . util . env . staging === true ? true : false )
2015-12-20 05:22:45 +01:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Terminal Banner
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
console . log ( "" ) ;
2017-03-22 16:34:05 +01:00
console . log ( $ . util . colors . gray ( " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ) )
console . log ( $ . util . colors . cyan ( " __ __ __ " ) )
console . log ( $ . util . colors . cyan ( " |__). _ _|_ _ . _ | \ |__) " ) )
console . log ( $ . util . colors . cyan ( " |__)|(_)(_| )(_||| )|__/|__) " ) )
console . log ( $ . util . colors . cyan ( " _/ " ) )
console . log ( $ . util . colors . gray ( " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ) )
console . log ( "" )
2015-12-20 05:22:45 +01:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Config
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Port to use for the development server
2017-03-22 16:34:05 +01:00
const PORT = 1337
2015-12-20 05:22:45 +01:00
// Browsers to target when prefixing CSS
2017-03-22 16:34:05 +01:00
const COMPATIBILITY = [ 'last 2 versions' , 'Chrome >= 30' , 'Safari >= 6.1' , 'Firefox >= 35' , 'Opera >= 32' , 'iOS >= 8' , 'Android >= 4' , 'ie >= 10' ]
2015-12-20 05:22:45 +01:00
// paths
2017-03-22 16:34:05 +01:00
const SRC = '_src/' ,
DIST = '_dist/'
2015-12-20 05:22:45 +01:00
2016-01-08 12:00:57 +01:00
// deployment
2017-03-22 16:34:05 +01:00
const S3BUCKET = 'www.bigchaindb.com' ,
S3REGION = 'eu-central-1' ,
S3BUCKET _BETA = 'beta.bigchaindb.com' ,
S3REGION _BETA = 'eu-central-1' ,
S3BUCKET _GAMMA = 'gamma.bigchaindb.com' ,
S3REGION _GAMMA = 'eu-central-1'
2016-01-08 12:00:57 +01:00
2015-12-20 05:22:45 +01:00
// SVG sprite
2017-03-22 16:34:05 +01:00
const SPRITECONFIG = {
2015-12-20 05:22:45 +01:00
dest : DIST + 'assets/img/' ,
mode : {
symbol : {
dest : './' ,
sprite : 'sprite.svg'
}
}
}
// code banner
2017-03-22 16:34:05 +01:00
const BANNER = [
2015-12-20 05:22:45 +01:00
'/**' ,
' ** <%= pkg.name %> v<%= pkg.version %>' ,
' ** <%= pkg.description %>' ,
' ** <%= pkg.homepage %>' ,
' **' ,
' ** <%= pkg.author.name %> <<%= pkg.author.email %>>' ,
' **' ,
' ** ' ,
' ** <%= pkg.repository.url %> ' ,
' **/' ,
''
2017-03-22 16:34:05 +01:00
] . join ( '\n' )
2015-12-20 05:22:45 +01:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2017-03-22 16:34:05 +01:00
// gulp tasks
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// Full build
//
// `gulp build` is the development build
// `gulp build --production` is the production build
//
gulp . task ( 'build' , gulp . series (
buildBanner , clean , jekyll ,
gulp . parallel ( html , css , js , images , fonts , videos , svg ) ,
rev , revReplace
) )
function 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 ( " ------------------------------------------" ) )
done ( )
}
//
// Build site, run server, and watch for file changes
//
gulp . task ( 'default' , gulp . series ( 'build' , server , watch ) )
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Functions
2015-12-20 05:22:45 +01:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
// Delete build artifacts
//
2017-03-22 16:34:05 +01:00
function clean ( done ) {
2015-12-20 05:22:45 +01:00
return del ( [
DIST + '**/*' ,
DIST + '.*' // delete all hidden files
2017-03-22 16:34:05 +01:00
] )
done ( )
}
2015-12-20 05:22:45 +01:00
//
// Jekyll
//
2017-03-22 16:34:05 +01:00
function jekyll ( done ) {
2015-12-20 05:22:45 +01:00
2017-03-22 16:34:05 +01:00
browser . notify ( 'Compiling Jekyll' )
2015-12-20 05:22:45 +01:00
if ( isProduction ) {
2017-03-22 16:34:05 +01:00
process . env . JEKYLL _ENV = 'production'
var jekyll _options = 'jekyll build'
2017-03-20 16:16:03 +01:00
} else if ( isStaging ) {
2017-03-22 16:34:05 +01:00
process . env . JEKYLL _ENV = 'staging'
var jekyll _options = 'jekyll build'
2015-12-20 05:22:45 +01:00
} else {
2017-03-22 16:34:05 +01:00
process . env . JEKYLL _ENV = 'development'
var jekyll _options = 'jekyll build --incremental --drafts --future'
2015-12-20 05:22:45 +01:00
}
2017-03-22 16:34:05 +01:00
const jekyll = spawn ( 'bundle' , [ 'exec' , jekyll _options ] , { stdio : 'inherit' } )
2017-03-04 23:08:40 +01:00
2017-03-22 16:34:05 +01:00
return jekyll
. on ( 'error' , ( error ) => onError ( ) )
. on ( 'close' , done )
} ;
2015-12-20 05:22:45 +01:00
//
// HTML
//
2017-03-22 16:34:05 +01:00
function html ( ) {
2015-12-20 05:22:45 +01:00
return gulp . src ( DIST + '/**/*.html' )
2017-03-20 16:16:03 +01:00
. pipe ( $ . if ( isProduction || isStaging , $ . htmlmin ( {
2015-12-20 05:22:45 +01:00
collapseWhitespace : true ,
conservativeCollapse : true ,
removeComments : true ,
useShortDoctype : true ,
collapseBooleanAttributes : true ,
removeRedundantAttributes : true ,
removeEmptyAttributes : true ,
minifyJS : true ,
minifyCSS : true
} ) ) )
2017-03-22 16:34:05 +01:00
. pipe ( gulp . dest ( DIST ) )
} ;
2015-12-20 05:22:45 +01:00
//
// Styles
//
2017-03-22 16:34:05 +01:00
function css ( ) {
2015-12-20 05:22:45 +01:00
return gulp . src ( SRC + '_assets/styles/bigchain.scss' )
. pipe ( $ . sourcemaps . init ( ) )
. pipe ( $ . sass ( ) . on ( 'error' , $ . sass . logError ) )
. pipe ( $ . autoprefixer ( { browsers : COMPATIBILITY } ) )
2017-03-22 16:34:05 +01:00
. pipe ( $ . if ( isProduction || isStaging , $ . cleanCss ( ) ) )
2015-12-20 05:22:45 +01:00
. pipe ( $ . if ( ! isProduction , $ . sourcemaps . write ( ) ) )
2017-03-20 16:16:03 +01:00
. pipe ( $ . if ( isProduction || isStaging , $ . header ( BANNER , { pkg : pkg } ) ) )
2015-12-20 05:22:45 +01:00
. pipe ( $ . rename ( { suffix : '.min' } ) )
. pipe ( gulp . dest ( DIST + 'assets/css/' ) )
2017-03-22 16:34:05 +01:00
. pipe ( browser . stream ( ) )
} ;
2015-12-20 05:22:45 +01:00
//
// JavaScript
//
2017-03-22 16:34:05 +01:00
function js ( ) {
2015-12-20 05:22:45 +01:00
return gulp . src ( [
SRC + '_assets/javascripts/bigchain.js' ,
SRC + '_assets/javascripts/page-*.js'
] )
. pipe ( $ . sourcemaps . init ( ) )
. pipe ( $ . include ( ) )
2017-03-20 16:16:03 +01:00
. pipe ( $ . if ( isProduction || isStaging , $ . uglify ( ) ) ) . on ( 'error' , onError )
. pipe ( $ . if ( ! isProduction || ! isStaging , $ . sourcemaps . write ( ) ) )
. pipe ( $ . if ( isProduction || isStaging , $ . header ( BANNER , { pkg : pkg } ) ) )
2015-12-20 05:22:45 +01:00
. pipe ( $ . rename ( { suffix : '.min' } ) )
2017-03-22 16:34:05 +01:00
. pipe ( gulp . dest ( DIST + 'assets/js/' ) )
} ;
2015-12-20 05:22:45 +01:00
//
// SVG sprite
//
2017-03-22 16:34:05 +01:00
function svg ( ) {
2015-12-20 05:22:45 +01:00
return gulp . src ( SRC + '_assets/images/**/*.svg' )
2017-03-20 16:16:03 +01:00
. pipe ( $ . if ( isProduction || isStaging , $ . imagemin ( {
2015-12-20 05:22:45 +01:00
svgoPlugins : [ {
removeRasterImages : true
} ]
} ) ) )
. pipe ( $ . svgSprite ( SPRITECONFIG ) )
2017-03-22 16:34:05 +01:00
. pipe ( gulp . dest ( DIST + 'assets/img/' ) )
}
2015-12-20 05:22:45 +01:00
//
// Copy Images
//
2017-03-22 16:34:05 +01:00
function images ( ) {
2015-12-20 05:22:45 +01:00
return gulp . src ( SRC + '_assets/images/**/*' )
2017-03-20 16:16:03 +01:00
. pipe ( $ . if ( isProduction || isStaging , $ . imagemin ( {
2015-12-21 19:48:04 +01:00
optimizationLevel : 3 , // png
2015-12-20 05:22:45 +01:00
progressive : true , // jpg
interlaced : true , // gif
multipass : true , // svg
svgoPlugins : [ { removeViewBox : false } ]
} ) ) )
2017-03-22 16:34:05 +01:00
. pipe ( gulp . dest ( DIST + 'assets/img/' ) )
}
2015-12-20 05:22:45 +01:00
2017-01-25 12:23:41 +01:00
//
// Copy Fonts
//
2017-03-22 16:34:05 +01:00
function fonts ( ) {
2017-01-25 12:23:41 +01:00
return gulp . src ( SRC + '_assets/fonts/**/*' )
. pipe ( $ . rename ( { dirname : '' } ) )
2017-03-22 16:34:05 +01:00
. pipe ( gulp . dest ( DIST + 'assets/fonts/' ) )
}
2017-01-25 12:23:41 +01:00
//
// Copy Videos
//
2017-03-22 16:34:05 +01:00
function videos ( ) {
2017-01-25 12:23:41 +01:00
return gulp . src ( SRC + '_assets/videos/**/*' )
2017-03-22 16:34:05 +01:00
. pipe ( gulp . dest ( DIST + 'assets/videos/' ) )
}
2017-01-25 12:23:41 +01:00
2015-12-20 05:22:45 +01:00
//
// Revision static assets
//
2017-03-22 16:34:05 +01:00
function rev ( done ) {
2015-12-20 05:22:45 +01:00
// globbing is slow so do everything conditionally for faster dev build
2017-03-20 16:16:03 +01:00
if ( isProduction || isStaging ) {
2016-01-12 12:05:30 +01:00
return gulp . src ( DIST + '/assets/**/*.{css,js,png,jpg,jpeg,svg,eot,ttf,woff,woff2}' )
2015-12-20 05:22:45 +01:00
. pipe ( $ . rev ( ) )
. pipe ( gulp . dest ( DIST + '/assets/' ) )
// output rev manifest for next replace task
. pipe ( $ . rev . manifest ( ) )
2017-03-22 16:34:05 +01:00
. pipe ( gulp . dest ( DIST + '/assets/' ) )
2015-12-20 05:22:45 +01:00
}
2017-03-22 16:34:05 +01:00
done ( )
} ;
2015-12-20 05:22:45 +01:00
//
// Replace all links to assets in files
// from a manifest file
//
2017-03-22 16:34:05 +01:00
function revReplace ( done ) {
2015-12-20 05:22:45 +01:00
// globbing is slow so do everything conditionally for faster dev build
2017-03-20 16:16:03 +01:00
if ( isProduction || isStaging ) {
2015-12-20 05:22:45 +01:00
var manifest = gulp . src ( DIST + '/assets/rev-manifest.json' ) ;
2017-03-04 23:08:40 +01:00
return gulp . src ( DIST + '/**/*.{html,xml,txt,json,css,js}' )
2015-12-20 05:22:45 +01:00
. pipe ( $ . revReplace ( { manifest : manifest } ) )
2017-03-22 16:34:05 +01:00
. pipe ( gulp . dest ( DIST ) )
2015-12-20 05:22:45 +01:00
}
2017-03-22 16:34:05 +01:00
done ( )
} ;
2015-12-20 05:22:45 +01:00
//
// Dev Server
//
2017-03-22 16:34:05 +01:00
function server ( done ) {
2015-12-20 05:22:45 +01:00
browser . init ( {
server : DIST ,
port : PORT ,
reloadDebounce : 2000
2017-03-22 16:34:05 +01:00
} )
2015-12-20 05:22:45 +01:00
2017-03-22 16:34:05 +01:00
done ( )
} ;
2015-12-20 05:22:45 +01:00
//
2017-03-22 16:34:05 +01:00
// Watch for file changes
2015-12-20 05:22:45 +01:00
//
2017-03-22 16:34:05 +01:00
function watch ( ) {
gulp . watch ( SRC + '_assets/styles/**/*.scss' ) . on ( 'all' , gulp . series ( css ) )
gulp . watch ( SRC + '_assets/javascripts/**/*.js' ) . on ( 'all' , gulp . series ( js , browser . reload ) )
gulp . watch ( SRC + '_assets/images/**/*.{png,jpg,jpeg,gif,webp}' ) . on ( 'all' , gulp . series ( images , browser . reload ) )
gulp . watch ( SRC + '_assets/images/**/*.{svg}' ) . on ( 'all' , gulp . series ( svg , browser . reload ) )
gulp . watch ( SRC + '_assets/videos/**/*.{mp4,webm}' ) . on ( 'all' , gulp . series ( videos , browser . reload ) )
gulp . watch ( [ SRC + '**/*.{html,xml,json,txt,md,yml}' , './_config.yml' , SRC + '_includes/svg/*' ] ) . on ( 'all' , gulp . series ( 'build' , browser . reload ) )
}
2015-12-20 05:22:45 +01:00
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Deployment
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2016-01-08 12:00:57 +01:00
2016-06-23 16:23:53 +02:00
//
// gulp deploy --live
// gulp deploy --beta
2017-03-07 16:24:23 +01:00
// gulp deploy --gamma
2016-06-23 16:23:53 +02:00
//
2017-03-22 16:34:05 +01:00
gulp . task ( 'deploy' , ( 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 ( " ------------------------------------------" ) )
} 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 ( )
return
}
2016-01-08 15:42:05 +01:00
// create publisher, define config
2016-06-23 16:23:53 +02:00
if ( $ . util . env . live === true ) {
var publisher = $ . awspublish . create ( {
params : { "Bucket" : S3BUCKET } ,
"accessKeyId" : process . env . AWS _ACCESS _KEY ,
"secretAccessKey" : process . env . AWS _SECRET _KEY ,
"region" : S3REGION
2017-03-22 16:34:05 +01:00
} )
} else if ( $ . util . env . beta === true ) {
2016-06-23 16:23:53 +02:00
var publisher = $ . awspublish . create ( {
params : { "Bucket" : S3BUCKET _BETA } ,
"accessKeyId" : process . env . AWS _BETA _ACCESS _KEY ,
"secretAccessKey" : process . env . AWS _BETA _SECRET _KEY ,
"region" : S3REGION _BETA
2017-03-22 16:34:05 +01:00
} )
} else if ( $ . util . env . gamma === true ) {
2017-03-07 16:24:23 +01:00
var publisher = $ . awspublish . create ( {
params : { "Bucket" : S3BUCKET _GAMMA } ,
"accessKeyId" : process . env . AWS _GAMMA _ACCESS _KEY ,
"secretAccessKey" : process . env . AWS _GAMMA _SECRET _KEY ,
"region" : S3REGION _GAMMA
2017-03-22 16:34:05 +01:00
} )
2016-06-23 16:23:53 +02:00
}
2016-01-08 15:42:05 +01:00
2016-01-08 12:00:57 +01:00
return gulp . src ( DIST + '**/*' )
. pipe ( $ . awspublishRouter ( {
cache : {
// cache for 5 minutes by default
cacheTime : 300
} ,
routes : {
// all static assets, cached & gzipped
2017-03-04 23:08:40 +01:00
'^assets/(?:.+)\\.(?:js|css|png|jpg|jpeg|gif|ico|svg|ttf|eot|woff|woff2)$' : {
2016-01-08 12:00:57 +01:00
cacheTime : 2592000 , // cache for 1 month
gzip : true
} ,
// every other asset, cached
'^assets/.+$' : {
cacheTime : 2592000 // cache for 1 month
} ,
// all html files, not cached & gzipped
'^.+\\.html' : {
cacheTime : 0 ,
gzip : true
} ,
2017-01-25 12:23:41 +01:00
2016-12-15 11:41:24 +01:00
// all pdf files, not cached
'^.+\\.pdf' : {
cacheTime : 0
} ,
2016-01-08 12:00:57 +01:00
2016-01-14 12:10:46 +01:00
// font mime types
'\.ttf$' : {
key : '$&' ,
headers : { 'Content-Type' : 'application/x-font-ttf' }
} ,
'\.woff$' : {
key : '$&' ,
headers : { 'Content-Type' : 'application/x-font-woff' }
} ,
'\.woff2$' : {
key : '$&' ,
headers : { 'Content-Type' : 'application/x-font-woff2' }
} ,
2016-01-08 12:00:57 +01:00
// pass-through for anything that wasn't matched by routes above, to be uploaded with default options
"^.+$" : "$&"
}
} ) )
2016-06-23 16:23:53 +02:00
. pipe ( parallelize ( publisher . publish ( ) , 100 ) )
2016-01-08 12:00:57 +01:00
. pipe ( publisher . sync ( ) ) // delete files in bucket that are not in local folder
. pipe ( $ . awspublish . reporter ( {
states : [ 'create' , 'update' , 'delete' ]
2017-03-22 16:34:05 +01:00
} ) )
} )