-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
136 lines (111 loc) · 3.99 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var path = require('path');
var http = require('http');
var ncp = require('ncp').ncp;
var openBrowser = require('open');
var connect = require('connect');
var Metalsmith = require('metalsmith');
var gulp = require('gulp');
var mkdirp = require('mkdirp');
var clean = require('gulp-clean');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var gutil = require('gulp-util');
var log = gutil.log;
var colors = gutil.colors;
var site = require(path.resolve(__dirname, 'site.json'));
var siteJS = site.assets.vendor.js.concat(site.assets.custom.js);
var siteCSS = site.assets.vendor.css.concat(site.assets.custom.css);
/*
* Tasks
*/
gulp.task('prepare', function(callback) {
mkdirp(site.destination, callback);
});
gulp.task('clean', function() {
gulp.src(site.destination, {read: false})
.pipe(clean());
})
gulp.task('sass', function() {
gulp.src(site.assets.custom.scss)
.pipe(sass({
includePaths: require('eggshell').includePaths
}))
.pipe(gulp.dest('./assets/css'));
});
gulp.task('concat-js', function() {
gulp.src(siteJS)
.pipe(concat('scripts.js'))
.pipe(gulp.dest('./public/assets'));
});
gulp.task('concat-css', function() {
gulp.src(siteCSS)
.pipe(concat('styles.css'))
.pipe(gulp.dest('./public/assets'));
});
gulp.task('favicons', function() {
gulp.src(site.assets.custom.favicons)
.pipe(gulp.dest('./public/assets/favicons'));
})
gulp.task('fonts', function() {
gulp.src(site.assets.custom.fonts)
.pipe(gulp.dest('./public/assets/fonts'));
})
gulp.task('images', function() {
gulp.src(site.assets.custom.images)
.pipe(gulp.dest('./public/assets/images'));
})
//
gulp.task('metalsmith', function(callback) {
var metalsmith = new Metalsmith(process.cwd());
var plugins = site.metalsmith || {};
metalsmith.source(site.source);
metalsmith.destination(site.destination);
metalsmith.metadata(site.metadata);
Object.keys(plugins).forEach(function(key) {
var plugin;
var opts = plugins[key];
plugin = require(key);
metalsmith.use(plugin(opts));
});
metalsmith.build(function(err){
if (err) return callback(err);
callback();
});
});
//
gulp.task('server', ['prepare', 'watch'], function(callback) {
var devApp, devServer, devAddress, devHost, url;
devApp = connect()
.use(connect.logger('dev'))
.use(connect.static(site.destination));
// change port and hostname to something static if you prefer
devServer = http.createServer(devApp).listen(gutil.env.port || 0 /*, hostname*/);
devServer.on('error', function(error) {
log(colors.underline(colors.red('ERROR'))+' Unable to start server!');
callback(error); // we couldn't start the server, so report it and quit gulp
});
devServer.on('listening', function() {
devAddress = devServer.address();
devHost = devAddress.address === '0.0.0.0' ? 'localhost' : devAddress.address;
url = ('http://' + devHost + ':' + devAddress.port);
log('Started dev server at '+colors.magenta(url));
if(gutil.env.open) {
log('Opening dev server URL in browser');
openBrowser(url);
} else {
log(colors.gray('(Run with --open to automatically open URL on startup)'));
}
log('Done');
callback(); // we're done with this task for now
});
});
// Rerun the task when a file changes
gulp.task('watch', function() {
gulp.watch(site.assets.custom.scss, ['sass']);
gulp.watch(siteJS, ['concat-js']);
gulp.watch(siteCSS, ['concat-css']);
gulp.watch(['./public/**/*', './assets/**/*.{png}', './templates/**/*', './source/**/*'], ['metalsmith']);
});
// The default task (called when you run `gulp` from cli)
gulp.task('default', ['sass', 'concat-js', 'concat-css', 'favicons', 'fonts', 'images', 'metalsmith']);
gulp.task('development', ['sass', 'concat-js', 'concat-css', 'favicons', 'fonts', 'images', 'metalsmith', 'server']);