-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.js
163 lines (141 loc) · 4.29 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var gulp = require('gulp')
, glob = require("glob")
, webserver = require('gulp-webserver')
, Constants = require('./constants.json')
, minimist = require('minimist')
, _ = require('underscore')
, underscore_string = require('underscore.string')
, watch = require('gulp-watch')
, clean = require('gulp-clean')
, path = require('path')
, aglio = require('gulp-aglio')
, sass = require('gulp-sass')
, concat = require('gulp-concat')
, gulpif = require('gulp-if')
, uglify = require('gulp-uglify')
, JadeHelpers = require('./redbooth-theme/helpers/base.js')
, env
, options;
_.mixin(underscore_string.exports());
env = getEnviroment();
options = {
docs: 'parts'
, dest: 'dest'
, stylesheets: 'redbooth-theme/assets/stylesheets/**/*.scss'
, javascripts: 'redbooth-theme/assets/javascripts/**/*.js'
, jades: 'redbooth-theme/**/*.jade'
, images: Constants[env].root_url + 'assets/images'
, src_images: 'redbooth-theme/assets/images/**/*'
, redbooth_theme: path.resolve(__dirname, 'redbooth-theme', 'redbooth.jade')
};
/**
* Return the context in wich gulp is executed
*
* @return {String}
*/
function getEnviroment () {
var args = getEnviromentVariables()
, available_envs = ['development', 'production'];
if (args.env && _.indexOf(available_envs, args.env) != -1 ) {
return args.env;
}
return 'development';
}
/**
* Check parametres in the gulp call
*
* @return {Object}
*/
function getEnviromentVariables () {
return minimist(process.argv.slice(2));
}
/**
* From parts folder get the file names with .html extension
*
* @return {Array}
*/
function getDocSections (folder) {
return _.compact(_.map(glob.sync(folder), function (file) {
var raw_name = file.replace(/parts\/([^\.]*)\.(md)/i, "$1");
raw_name = raw_name.replace(/_/i, ' ');
if (raw_name === 'index') {
return;
};
return {
url: file.replace(/parts\/([^\.]*)\.(md)/i, "\/$1\.html")
, raw_name: raw_name
, name: _.capitalize(raw_name)
};
}));
}
/**
* Get theme local variables by enviroment
*
* @param {String} enviroment
* @return {Object}
*/
function getThemeLocals (enviroment) {
var doc_sections = getDocSections(options.docs + '/*.md');
return _.extend(Constants[enviroment], {
doc_sections: doc_sections
, _: _
, env: enviroment
, custom_helpers: JadeHelpers
});
}
gulp.task('clean', function () {
return gulp.src([options.dest], {read: false})
.pipe(clean({force: true}));
});
gulp.task('sass', ['clean'], function () {
return gulp.src(options.stylesheets)
.pipe(watch(options.stylesheets))
.pipe(sass({
imagePath: options.images
}))
.pipe(gulp.dest(options.dest + '/assets/css'));
});
gulp.task('copy_images', ['clean'], function () {
return gulp.src(options.src_images)
.pipe(watch(options.src_images))
.pipe(gulp.dest(options.dest + '/assets/images'));
});
gulp.task('javascripts', ['clean'], function () {
var is_production = getEnviroment() === 'production' ? true : false;
return gulp.src(options.javascripts)
.pipe(gulpif(is_production, concat('main.js')))
.pipe(gulpif(is_production, uglify()))
.pipe(gulp.dest(options.dest + '/assets/javascripts'));
});
gulp.task('generate_docs', ['clean'], function () {
var wached_files = [options.docs + '/*.md']
, env = getEnviroment();
// NOTE: If you see some error like this:
// (CarbonCore.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-21)
// is because the watcher is checking too much files. It could be you have another
// project with a watcher running. Try to stop the other project.
return gulp.src(wached_files)
.pipe(watch(wached_files))
.pipe(aglio({
template: options.redbooth_theme
, locals: getThemeLocals(env)
}))
.pipe(gulp.dest(options.dest));
});
gulp.task('webserver', function() {
return gulp.src(options.dest)
.pipe(webserver({
livereload: true
, directoryListing: false
, open: false
, port: 8080
, fallback: 'index.html'
}));
});
// Shared tasks bettwen production and development
gulp.task('common', ['copy_images', 'javascripts', 'sass', 'generate_docs']);
// Development
gulp.task('default', ['common', 'webserver']);
// Compile for production
// FIXME: Assets fingerprint pending
gulp.task('build-production', ['common']);