forked from janpaepke/ScrollMagic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
250 lines (222 loc) · 7.23 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env node
"use strict";
/* ########################################## */
/* ########### load requirements ############ */
/* ########################################## */
var
gulp = require('gulp'),
jshint = require('gulp-jshint'),
include = require('gulp-file-include'),
rename = require('gulp-rename'),
replace = require('gulp-replace-task'),
concat = require('gulp-concat-util'),
uglify = require('gulp-uglify'),
gulpFilter = require('gulp-filter'),
gutil = require('gulp-util'),
jeditor = require('gulp-json-editor'),
// jsdoc = require('gulp-jsdoc'),
clone = require('gulp-clone'),
addsrc = require('gulp-add-src'),
fs = require('fs'),
del = require('del'),
semver = require('semver'),
exec = require('child_process').exec,
config = require('./dev/config.json'), // config
pkg = require('./package.json'); // package
var args = require('yargs')
.alias("d", "docs") .default('d', false)
.alias("o", "out") .default('o', './' + config.dirs.defaultOutput)
.alias("b", "ver") .default('b', config.version)
.argv; // TODO: document parameters
/* ########################################## */
/* ################ settings ################ */
/* ########################################## */
var options = {
version: args.ver,
dodocs: !!args.docs,
folderOut: args.out,
folderDocsOut: args.docs.split ? args.docs : './' + config.dirs.defaultDocsOutput
};
var now = config.version === options.version ? new Date(config.lastupdate) : new Date();
var replaceVars = {
variables: {
"%VERSION%": options.version,
"%YEAR%": now.getFullYear(),
"%MONTH%": ("0"+(now.getMonth() + 1)).slice(-2),
"%DAY%": ("0"+now.getDate()).slice(-2),
"%DESCRIPTION%": config.info.description,
},
usePrefix: false
};
var banner = {
regular: fs.readFileSync("dev/src/banner.regular.js", 'utf-8'),
min: fs.readFileSync("dev/src/banner.min.js", 'utf-8')
};
// log helper
var log = {
exit : function () {
gutil.log.apply(gutil, Array.prototype.concat.apply([gutil.colors.red("ERROR:")], arguments));
process.exit(1);
},
warn : function () {
gutil.log.apply(gutil, Array.prototype.concat.apply([gutil.colors.yellow("WARNING:")], arguments));
},
info : function () {
gutil.log.apply(gutil, Array.prototype.concat.apply([gutil.colors.blue("INFO:")], arguments));
},
};
/* ########################################## */
/* ############### MAIN TASKS ############### */
/* ########################################## */
gulp.task('default', ['validateoptions', 'lintsource', 'updatejsonfiles', 'updatereadme', 'build'], function () {
if (options.version != pkg.version) {
log.info("Updated to version", options.version);
}
});
gulp.task('validateoptions', function() {
// version
if (!semver.valid(options.version)) {
log.exit("Invalid version number supplied");
} else if (semver.lt(options.version, pkg.version)) {
log.exit("Supplied version (" + options.version + ") is older than current (" + pkg.version + "), defined in package.json");
}
// output
if (!fs.existsSync(options.folderOut)) {
log.exit("Supplied output path not found.");
}
// docs output
if (options.dodocs && !fs.existsSync(options.folderDocsOut)) {
log.exit("Supplied output path for docs not found.");
}
});
gulp.task('clean', function(callback) {
var toclear = [options.folderOut];
if (options.dodocs) {
toclear.push(options.folderDocsOut);
}
del(toclear, callback);
});
gulp.task('build', ['clean'], function(callback) {
var filterMainFile = gulpFilter('core.js');
var uncompressed = gulp.src(config.files, { base: config.dirs.source })
.pipe(include("// ")) // do file inclusions
.pipe(filterMainFile)
.pipe(rename("ScrollMagic.js"))
.pipe(filterMainFile.restore());
var minified = uncompressed.pipe(clone())
.pipe(rename({suffix: ".min"}))
.pipe(replace({
patterns: [
{ // remove log messages
match: /\s*log\([0-3],.+;.*$/gm,
replacement: ""
},
{ // remove unnecessary stuff in minify
match: /\/\/ \(BUILD\) - REMOVE IN MINIFY - START[^]*?\/\/ \(BUILD\) - REMOVE IN MINIFY - END/gm,
replacement: ""
}
]
}))
.pipe(uglify())
.pipe(concat.header(banner.min + "\n"))
.pipe(replace(replaceVars))
.pipe(gulp.dest(options.folderOut + "/minified"));
uncompressed.pipe(replace({
patterns: [
{ // remove build notes
match: /[\t ]*\/\/ \(BUILD\).*$\r?\n?/gm,
replacement: ''
}
]
}))
// .pipe(rename(function(path){console.log(path);}))
.pipe(concat.header(banner.regular + "\n")) // have header vars already replaced
.pipe(replace(replaceVars))
.pipe(gulp.dest(options.folderOut + "/uncompressed"));
if (options.dodocs) {
log.info("Generating new docs");
// gulp-jsdoc only works with jsdoc-alpha5 which sucks.
// var jsdocconf = require('./dev/docs/jsdoc.conf.json');
// jsdocconf.templates.path = 'dev/docs/template';
// uncompressed
// .pipe(addsrc('./README.md'))
// .pipe(jsdoc(
// options.folderDocsOut,
// jsdocconf.templates,
// {
// plugins: jsdocconf.plugins
// }
// ));
// gulp jsdoc doesnt work properly, so do it manually
var
bin = '"' + 'node_modules/.bin/jsdoc' + '"',
docIn = '"' + 'README.md' + '"',
docOut = '-d "' + options.folderDocsOut + '"',
conf = '-c "' + './dev/docs/jsdoc.conf.json' + '"',
template = '-t "' + './dev/docs/template' + '"';
uncompressed
.pipe(gutil.buffer(function(err, files){
files.forEach(function (file) {
docIn += ' "' + file.path + '"';
});
}));
setTimeout(function(){ // etwas dirty mit timeout, aber wenigstens funktionierts...
// console.log(docIn);
var cmd = [bin, docIn, conf, template, docOut].join(" ");
// console.log(cmd);
exec(cmd,
function (error, stdout, stderr) {
// log.info('stdout: ' + stdout);
// log.info('stderr: ' + stderr);
if (error !== null) {
log.exit('exec error: ' + error);
}
}
)
.on("close", callback);
}, 500);
} else {
callback();
}
});
gulp.task('lintsource', function() {
var x = gulp.src(config.dirs.source + "/**/*.js")
.pipe(jshint())
.pipe(jshint.reporter('default')); // TODO: custom reporter
});
gulp.task('updatejsonfiles', function() {
gulp.src(["./package.json", "./bower.json", "./ScrollMagic.jquery.json"])
.pipe(jeditor(config.info, {keep_array_indentation: true}))
.pipe(jeditor({version: options.version}, {keep_array_indentation: true}))
.pipe(gulp.dest("./"));
if (config.version !== options.version) {
gulp.src("./dev/config.json")
.pipe(jeditor(
{
version: options.version,
lastupdate: now.getFullYear() + "-" + ("0"+(now.getMonth() + 1)).slice(-2) + "-" + ("0"+now.getDate()).slice(-2)
},
{
keep_array_indentation: true
}
))
.pipe(gulp.dest("./dev"));
}
});
gulp.task('updatereadme', function() {
gulp.src("./README.md")
.pipe(replace({
patterns: [
{
match: /(<a .*class='version'.*>v)\d+\.\d+\.\d+(<\/a>)/gi,
replacement: "$1" + options.version + "$2"
}
]
}))
.pipe(gulp.dest("./"));
});
gulp.task('openindex', function() { // just open the index file
var open = require("gulp-open");
gulp.src("./index.html")
.pipe(open("<%file.path%>"));
});