-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
161 lines (124 loc) · 4.13 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
/**
* Grunt build tasks
*/
/*eslint-env node, mocha */
/*eslint one-var: 0, no-new: 0, func-names: 0, strict: 0 */
'use strict';
var fs = require('fs'),
path = require('path'),
gulp = require('gulp'),
runSequence = require('run-sequence'),
_ = require('lodash'),
$ = require('gulp-load-plugins')(),
argv = require('yargs').argv || {},
pkg = require('./package.json'),
taskPath = path.join(process.cwd(), 'build', 'gulp-tasks'),
optionsPath = path.join(process.cwd(), 'build', 'gulp-config'),
options = {},
banners = {};
pkg.year = new Date().getFullYear();
banners.application = "/*! <%= pkg.description %> v<%= pkg.version %> - <%= pkg.author.name %> - Copyright <%= pkg.year %> <%= pkg.author.company %> */\n";
banners.vendors = "/*! <%= pkg.description %> v<%= pkg.version %> - <%= pkg.author.name %> - Vendor package */\n";
//load configuration from yaml files
fs.readdirSync(optionsPath).filter(function (optFile) {
//accept just js files
return path.extname(optFile) === '.js' && optFile.indexOf('.conf.') === -1;
}).forEach(function (optFile) {
var key = _.camelCase(path.basename(optFile, path.extname(optFile)));
if (key === 'properties') {
//properties are merged directly in options, so they are overriddable by cli
_.assign(options, require(optionsPath + '/' + optFile));
} else {
options[key] = require(optionsPath + '/' + optFile);
}
});
_.forOwn({
production: false,
command: null,
remotehost: null //be explicit!
}, function (value, key) {
options[key] = argv.hasOwnProperty(key) ? argv[key] : value;
});
//force production env
if (options.production) {
process.env.NODE_ENV = 'production';
}
options.pkg = pkg;
options.banners = banners;
//unique build identifier
options.buildHash = 'buildhash' + (Date.now());
options.assetsPath = function (type, match) {
var parts = type.split('.'),
paths = options.paths,
folderPath;
if (parts.length > 1) {
folderPath = path.join(paths[parts[0]].assets, paths[parts[1]]);
} else {
folderPath = path.normalize(paths[parts[0]].assets);
}
if (match) {
folderPath = path.join(folderPath, match);
}
return folderPath;
};
fs.readdirSync(taskPath).filter(function (taskFile) {
//accept just js files
return path.extname(taskFile) === '.js';
}).forEach(function (taskFile) {
require(taskPath + '/' + taskFile)(gulp, $, options);
});
gulp.task('default', ['clean'], function (done) {
var tasks = [
'images',
['fonts', 'media', 'styles', 'scripts'],
'modernizr',
'views'
];
if (options.styleguideDriven) {
tasks.push('styleguide');
}
if (options.production) {
//rev production files and cleanup temp files
tasks.push('rev', 'clean:tmp');
}
tasks.push(done);
runSequence.apply(null, tasks);
});
gulp.task('dev', ['default']);
gulp.task('dist', function () {
$.util.log($.util.colors.red('`dist` task has been removed. Please run `gulp --production`'));
// emit the end event, to properly end the task
this.emit('end');
});
if (options.buildOnly) {
gulp.task('build', function (done) {
var testHash = require('crypto').createHash('md5').update(fs.readFileSync(__filename, {encoding: 'utf8'})).digest('hex');
if (!argv.grunthash) {
$.util.log($.util.colors.red('Cannot run this task directly'));
this.emit('end');
return;
}
if (argv.grunthash !== testHash) {
$.util.log($.util.colors.red('Safety hash check not passed'));
this.emit('end');
return;
}
runSequence('default', done);
});
} else {
gulp.task('deploy', function (done) {
var tasks = ['default'];
switch (options.deployStrategy) {
case 'rsync':
//force backup
options.command = 'backup';
tasks.push('remote', 'rsync');
break;
default:
tasks.push(options.deployStrategy);
break;
}
tasks.push(done);
runSequence.apply(null, tasks);
});
}