-
Notifications
You must be signed in to change notification settings - Fork 27
/
gulpfile.js
119 lines (105 loc) · 3.45 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
#!/usr/bin/env node
"use strict";
/* ########################################## */
/* ########### load requirements ############ */
/* ########################################## */
var
// node modules
fs = require('fs'),
semver = require('semver'),
// gulp & plugins
gulp = require('gulp'),
plumber = require('gulp-plumber'),
jshint = require('gulp-jshint'),
rename = require('gulp-rename'),
replace = require('gulp-replace-task'),
concat = require('gulp-concat-util'),
uglify = require('gulp-uglify'),
gutil = require('gulp-util'),
jeditor = require('gulp-json-editor'),
size = require('gulp-size'),
// package info
info = require('./package.json');
// command line options (use gulp -h to for details)
var args = require('yargs')
.usage('Usage: gulp [options]')
.describe('b', 'Bumps version number. Accepts \'patch\', \'minor\' and \'major\'.')
.alias('b', 'bump')
.help('h')
.alias('h', '?')
// examples
.example("$0 --bump=patch", 'build and update version number from to 1.1.1 to 1.1.2')
.example("$0 -b", 'same as above, as \'patch\' is default')
.argv;
/* ########################################## */
/* ########### validate parameters ########## */
/* ########################################## */
// bump
if (args.bump) {
var validBumps = ["patch", "minor", "major"];
if (args.bump === true) {
args.b = args.bump = validBumps[0];
} else if (validBumps.indexOf(args.bump) === -1) {
gutil.log("Supplied option for bump ('" + args.bump + "') is invalid. Allowed values are: '" + validBumps.join("', '") + "'");
process.exit(1);
}
}
/* ########################################## */
/* ################# options ################ */
/* ########################################## */
var options = {
version: args.bump ? semver.inc(info.version, args.bump) : info.version,
banner: {
uncompressed: fs.readFileSync('src/banner.regular.txt', 'utf-8') + "\n",
minified: fs.readFileSync('src/banner.min.txt', 'utf-8') + "\n"
}
};
/* ########################################## */
/* ############### MAIN TASKS ############### */
/* ########################################## */
gulp.task('default', ['sync-json-files', 'build'], function () {
if (args.bump) {
gutil.log("Bumped version number from v" + info.version + " to v" + options.version);
}
});
gulp.task('sync-json-files', function() {
var syncInfo = {
version: options.version
};
["main", "description", "keywords", "homepage", "license", "repository"].forEach(function(key) {
syncInfo[key] = info[key];
});
return gulp.src(["./package.json", "./bower.json"])
.pipe(jeditor(syncInfo, {keep_array_indentation: true}))
.pipe(gulp.dest("./"));
});
gulp.task('lint', function() {
return gulp.src("src/*.js")
.pipe(jshint({lookup: false}))
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
});
gulp.task('build', ['lint'], function() {
var replacePatterns = {
patterns: [
{
match: /%VERSION%/gm,
replacement: options.version
}
]
};
return gulp.src("src/*.js")
// make uncompressed
.pipe(plumber())
.pipe(concat.header(options.banner.uncompressed))
.pipe(replace(replacePatterns))
.pipe(size({showFiles: true}))
.pipe(gulp.dest('./'))
// make minified
.pipe(uglify())
.pipe(concat.header(options.banner.minified))
.pipe(replace(replacePatterns))
.pipe(rename({suffix: ".min"}))
.pipe(size({showFiles: true}))
.pipe(gulp.dest('./'));
});