-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathgulpfile.js
175 lines (158 loc) · 5.08 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
'use strict';
// Default gulp modules
const gulp = require('gulp');
const tap = require('gulp-tap');
const babel = require('gulp-babel');
const minify = require('gulp-minify');
const rename = require('gulp-rename'); // Helps rename files or changing extensions
const babelMinify = require("gulp-babel-minify"); // Converts to ES5 before minifying
const gutil = require('gulp-util'); // Helps with debugging
const eslint = require('gulp-eslint');
// Additional release gulp modules
const env = require('gulp-env'); // For accessing environment variables
const runSequence = require('run-sequence'); // Runs a sequence of gulp tasks
const conventionalChangelog = require('gulp-conventional-changelog'); // Generates a changelog from git metadata
const args = require('yargs').argv; // Add additional arguments to the commands
const conventionalGithubReleaser = require('conventional-github-releaser'); // Make a new release from github metadata
const bump = require('gulp-bump'); // Increases the version number
const git = require('gulp-git'); // Run git functions with gulp
const fs = require('fs'); // For working with the local file system
// Define the location of our build directory
const destination = 'dist/';
const source = 'src/ajaxinate.js';
var type = 'patch';
var version = null;
function getPackageJsonVersion () {
// We parse the json file instead of using require because require caches
// multiple calls so the version number won't be updated
return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
};
// The default task, run it using `gulp`
// Converts source to ES5 and creates a minified version and builds to dist
gulp.task('build', function() {
return gulp.src(source)
// First convert it to ecma2015
.pipe(babel({
presets: ['env']
}))
// Add a non minified version to the dist
.pipe(gulp.dest(destination))
.pipe(minify())
// If there is an error during minification this will pretty print to the console
.on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); })
// Then we can adjust the extension include min
.pipe(rename({ extname: '.min.js' }))
// Then we output to the destination
.pipe(gulp.dest(destination));
});
// Add the new version to the changelog
gulp.task('changelog', function () {
return gulp.src('CHANGELOG.md', {
buffer: false
})
.pipe(conventionalChangelog({
preset: 'atom'
}))
.pipe(gulp.dest('./'));
});
// Ensure you duplicated the .env-sample and set your own GitHub token and renamed it .env
// Create a convention github release
gulp.task('github-release', function(done) {
env({file: ".env.json"});
gutil.log(gutil.colors.blue('[github]'), 'Pushing to github using authtoken: '+process.env.GITHUB_AUTH_KEY);
conventionalGithubReleaser({
type: "oauth",
url: "https://api.github.com/",
token: process.env.GITHUB_AUTH_KEY
}, {
preset: 'atom'
}, done);
});
gulp.task('bump-version', function () {
// We hardcode the version change type to 'patch' but it may be a good idea to
// use minimist (https://www.npmjs.com/package/minimist) to determine with a
// command argument whether you are doing a 'major', 'minor' or a 'patch' change.
return gulp.src(['./bower.json', './package.json'])
.pipe(bump({type: type}).on('error', gutil.log))
.pipe(gulp.dest('./'))
.pipe(tap(function(file){
var json = JSON.parse(String(file.contents));
version = json.version;
}));
});
gulp.task('commit-changes', function () {
return gulp.src('.')
.pipe(git.add())
.pipe(git.commit(`:bookmark: version: ${version} [${type}] `));
});
gulp.task('push-changes', function (cb) {
git.push('origin', 'master', cb);
});
gulp.task('create-new-tag', function (cb) {
git.tag(version, 'Created Tag for version: ' + version, function (error) {
if (error) {
return cb(error);
}
git.push('origin', 'master', {args: '--tags'}, cb);
});
});
gulp.task('lint', function() {
return gulp.src(source)
.pipe(eslint({
envs: ['node', 'browser']
}))
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('license', function() {
gulp.src('./LICENSE')
.pipe(bump())
.pipe(gulp.dest('./'));
gulp.src('./src/ajaxinate.js')
.pipe(bump())
.pipe(gulp.dest('./src/'));
});
gulp.task('release', function (callback) {
if (
args.type === 'minor' ||
args.type === 'major' ||
args.type === 'prerelease'
) {
type = args.type;
}
runSequence(
'lint',
'bump-version',
'license',
'changelog',
'build',
'commit-changes',
'push-changes',
'create-new-tag',
'github-release',
function (error) {
if (error) {
console.log(error.message);
} else {
console.log('RELEASE FINISHED SUCCESSFULLY');
}
callback(error);
});
});
gulp.task('testrelease', function (callback) {
runSequence(
'lint',
'log'
)
});
gulp.task('log', function (callback) {
if(
args.type === 'minor' ||
args.type === 'major' ||
args.type === 'prerelease'
) {
type = args.type;
}
gutil.log(args.type);
gutil.log(type);
});