forked from ErikGartner/dTree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
225 lines (197 loc) · 5.99 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
// Load Gulp and all of our Gulp plugins
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
// Load other npm modules
const del = require('del');
const glob = require('glob');
const path = require('path');
const isparta = require('isparta');
const babelify = require('babelify');
const watchify = require('watchify');
const buffer = require('vinyl-buffer');
const browserify = require('browserify');
const runSequence = require('run-sequence');
const source = require('vinyl-source-stream');
const rollup = require( 'rollup' );
const argv = require('minimist')(process.argv.slice(2));
const fs = require('fs');
const conventionalGithubReleaser = require('conventional-github-releaser');
// Gather the library data from `package.json`
const manifest = require('./package.json');
const config = manifest.babelBoilerplateOptions;
const mainFile = manifest.main;
const demoFolder = manifest.demo;
const destinationFolder = path.dirname(mainFile);
const exportFileName = path.basename(mainFile, path.extname(mainFile));
// Remove the built files
gulp.task('clean', function(cb) {
del([destinationFolder], cb);
});
// Remove our temporary files
gulp.task('clean-tmp', function(cb) {
del(['tmp'], cb);
});
function createLintTask(taskName, files) {
gulp.task(taskName, function() {
return gulp.src(files)
.pipe($.plumber())
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failOnError());
});
}
// Lint our source code
createLintTask('lint-src', ['src/**/*.js']);
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;
}
// Build two versions of the library
gulp.task('build', ['lint-src', 'clean'], function(done) {
var version = getPackageJsonVersion();
rollup.rollup({
entry: 'src/' + config.entryFileName,
}).then(function(bundle) {
var res = bundle.generate({
// use this instead of `toUmd`
format: 'umd',
// this is equivalent to `strict: true` -
// optional, will be auto-detected
//exports: 'named',
// `name` -> `moduleName`
moduleName: config.mainVarName,
});
$.file(exportFileName + '.js', res.code, { src: true })
.pipe($.preprocess({context: {DTREE_VERSION: version}}))
.pipe($.plumber())
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.babel())
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(destinationFolder))
.pipe($.filter(['*', '!**/*.js.map']))
.pipe($.rename(exportFileName + '.min.js'))
.pipe($.sourcemaps.init({ loadMaps: true }))
.pipe($.uglify())
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(destinationFolder))
.pipe(gulp.dest(demoFolder, {overwrite: true}))
.on('end', done);
})
.catch(done);
});
function bundle(bundler) {
return bundler.bundle()
.on('error', function(err) {
console.log(err.message);
this.emit('end');
})
.pipe($.plumber())
.pipe(source('./tmp/__spec-build.js'))
.pipe(buffer())
.pipe(gulp.dest(''))
.pipe($.livereload());
}
// These are JS files that should be watched by Gulp. When running tests in the browser,
// watchify is used instead, so these aren't included.
const jsWatchFiles = ['src/**/*'];
// These are files other than JS files which are to be watched. They are always watched.
const otherWatchFiles = ['package.json', '**/.eslintrc'];
// Run the headless unit tests as you make changes.
gulp.task('watch', function() {
const watchFiles = jsWatchFiles.concat(otherWatchFiles);
gulp.watch(watchFiles, ['build']);
});
gulp.task('bump', function() {
return gulp.src('./package.json')
.pipe($.bump({key: 'version', type: argv.bump}))
.pipe(gulp.dest('./'));
});
gulp.task('changelog', function () {
return gulp.src('./CHANGELOG.md')
.pipe($.conventionalChangelog({
preset: 'angular',
}))
.pipe(gulp.dest('./'));
});
gulp.task('update-cdn', function() {
gulp.src(['./README.md'])
.pipe($.replace(/(\d\.\d\.\d)\/dist\/dTree.min.js/g, getPackageJsonVersion() + '/dist/dTree.min.js'))
.pipe(gulp.dest('./'));
});
gulp.task('tag-release', function (cb) {
var version = getPackageJsonVersion();
$.git.tag(version, 'Created Tag for version: ' + version, cb);
});
gulp.task('commit-changes', function () {
return gulp.src(['./README.md', './CHANGELOG.md', './dist/*'])
.pipe($.git.add())
.pipe($.git.commit('chore: Bump version number'));
});
gulp.task('prepare-release', function (callback) {
runSequence(
'bump',
'changelog',
'build',
'update-cdn',
'commit-changes',
'tag-release',
function (error) {
if (error) {
console.log(error.message);
} else {
console.log('Updated workspace for release!');
}
callback(error);
});
});
gulp.task('push-changes', function (cb) {
$.git.push('origin', 'master', cb);
});
gulp.task('push-tags', function (cb) {
$.git.push('origin', 'master', {args: '--tags'}, cb);
});
gulp.task('github-release', function (done) {
conventionalGithubReleaser({
type: 'oauth',
token: process.env.GITHUB_TOKEN
}, {
preset: 'angular'
}, done);
});
gulp.task('npm-publish', $.shell.task([
'npm publish'
]))
gulp.task('push-release', function (callback) {
runSequence(
'push-changes',
'push-tags',
'npm-publish',
'github-release',
function (error) {
if (error) {
console.log(error.message);
} else {
console.log('Release Uploaded!');
}
callback(error);
});
});
gulp.task('release', function (callback) {
runSequence(
'prepare-release',
'push-release',
function (error) {
if (error) {
console.log(error.message);
} else {
console.log('Release complete!');
}
callback(error);
});
});
gulp.task('demo', ['build'], $.shell.task([
'node test/demo/demo.js'
]))
// An alias of build
gulp.task('default', ['build']);