-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
517 lines (349 loc) · 11.5 KB
/
gulpfile.js
File metadata and controls
517 lines (349 loc) · 11.5 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
var gulp = require('gulp');
var conventionalChangelog = require('gulp-conventional-changelog');//update changelog
var conventionalGithubReleaser = require('conventional-github-releaser');
var bump = require('gulp-bump');//increase version number
var gutil = require('gulp-util');
var git = require('gulp-git');
var fs = require('fs');
var argv = require('minimist')(process.argv);//https://www.npmjs.com/package/minimist
var marked = require('marked');
var fs = require("fs");
var liquid = require("gulp-liquid");
var rename = require("gulp-rename");
var config;//
var ghparse = require('parse-github-repo-url')
//Set Configuration Variables
setConfig();
gulp.task('check_options', (done)=> {
//check version type
var allowed_types = ["major", "minor", "patch"];
var type = allowed_types.indexOf(argv.t);
if (type == -1) {
throw new Error("Invalid version type. Please use the -t option and specify 'major','minor', or 'patch'");
}
done()
});
gulp.task('config-package', function () {
var this_packagejson;
var tokenjson = {};
this_packagejson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
var config = argv.c;
var key_value = config.split("=", 2);
if (key_value[0] === "token") {
tokenjson[key_value[0]] = key_value[1];
fs.writeFileSync('token.json', JSON.stringify(tokenjson, null, 2));
} else {
this_packagejson['config'][key_value[0]] = key_value[1];
fs.writeFileSync('package.json', JSON.stringify(this_packagejson, null, 2));
}
});
gulp.task('changelog', function () {
if (fs.existsSync('./package.json')) {
config = JSON.parse(fs.readFileSync('./package.json')).config;
}
return gulp.src(config.changelog, {
buffer: false,
allowEmpty:true
})
.pipe(conventionalChangelog({
preset: 'angular',
releaseCount: 0 // Or to any other commit message convention you use.
}))
.pipe(gulp.dest('./'));
});
gulp.task('convert-readme-to-html', function () {
if (fs.existsSync("../" + config.readme)) {
var fileContent = fs.readFileSync("../" + config.readme, "utf8");
} else {
throw new Error('Cannot find the ' + config.readme + ". To fix this, set the package.json `readme` setting to the name of your readme file.")
}
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false
});
fileContent = marked(fileContent);
return gulp.src('./gh-pages/_layouts/index.html')
.pipe(liquid({
locals: {
CONTENT: fileContent.toString(),
GITHUB_REPO: getGitHubRepoName(),
GITHUB_USERNAME: getGitHubUserName(),
VERSION_NUMBER: getPackageJsonVersion()
}
}))
.pipe(rename('readme.html'))
.on('end', function () {
//console.log('replaced liquid tags')
})
.pipe(gulp.dest('./gh-pages/cache'));
});
/**
* Set Configuration
*
*Updates the global config variable
*
* @param void
* @return void
*/
function setConfig() {
if (fs.existsSync('./package.json')) {
config = JSON.parse(fs.readFileSync('./package.json')).config;
} else {
config = {};
}
}
/**
* Get the Package Configuration
*
* Returns the package.json global object
* @param void
* @return void
*/
function getPackageJson() {
if (fs.existsSync('../package.json')) {
return JSON.parse(fs.readFileSync('../package.json', 'utf8'));
} else {
throw new Error("package.json cannot be found");
}
}
/**
* Get GitHub Username
*
* Parses the project's repository url to get its username
* ref: https://www.npmjs.com/package/parse-github-repo-url
*
* @param void
* @return string The GitHub username
*/
function getGitHubUserName() {
try {
return ghparse(getPackageJson().repository.url)['0'];
} catch (e) {
return "";
}
}
/**
* Get GitHub Repo Name
*
* Parses the project's repository url to get its repo name
* ref: https://www.npmjs.com/package/parse-github-repo-url
* @param void
* @return string The GitHub repo
*/
function getGitHubRepoName() {
try {
return ghparse(getPackageJson().repository.url)['1'];
} catch (e) {
return "";
}
}
/**
* Get Package Json Version
*
* @param void
* @return string The version in the package file
*/
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;
}
/**
* Get GitHub Token
*
* @param void
* @return string The github token
*/
function getGitHubToken() {
if (!fs.existsSync('./token.json')) {
throw new Error("GitHub API Token not set. Run gulp config -c token=GITHUB_TOKEN to set API token.");
return;
}
// 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('./token.json', 'utf8')).token;
}
gulp.task('github-release', function (done) {
conventionalGithubReleaser({
type: "oauth",
token: getGitHubToken // To set token, do this : gulp config -c token=YOUR_TOKEN
}, {
preset: 'angular' // Or to any other commit message convention you use.
}, done);
});
gulp.task('bump-version', function (cb) {
// argv.t will supply what kind of version change type we are releasing 'patch' 'major' or 'minor'
return gulp.src(['../bower.json', '../package.json'],{allowEmpty:true})
.pipe(bump({type: argv.t}).on('error', gutil.log))
.pipe(gulp.dest('../'));
});
gulp.task('commit-changes', function (cb) {
var q = require('bluebird'); //need promises since commit doesnt normally return stream. see https://github.com/stevelacy/gulp-git/issues/49
return new q(function (resolve, reject) {
//copy index.html to gh-pages
gulp.src('../')
.pipe(git.add())
.pipe(stream = git.commit(config.release_message.replace('{VERSION_NUMBER}', getPackageJsonVersion()))).on('error', function (e) {
console.log('No changes to commit');
})
.on('error', resolve) //resolve to allow the commit to resume even when there are not changes.
.on('end', resolve);
stream.resume(); //needed since commmit doesnt return stream by design.
;
});
});
gulp.task('push-master', function (cb) {
git.push('origin', 'master', cb);
});
gulp.task('push-gh-pages', function (cb) {
git.push('origin', 'gh-pages', cb);
});
gulp.task('commit-gh-pages', function (cb) {
var q = require('bluebird'); //need promises since commit doesnt normally return stream. see https://github.com/stevelacy/gulp-git/issues/49
return new q(function (resolve, reject) {
//copy index.html to gh-pages
gulp.src('../index.html')
.pipe(stream = git.commit('updated gh-pages readme ')).on('error', function (e) {
console.log('No changes to commit');
})
.on('error', resolve) //resolve to allow the commit to resume even when there are not changes.
.on('end', resolve);
stream.resume(); //needed since commmit doesnt return stream by design.
;
});
});
gulp.task('copy-converted-readme-to-gh-pages-branch', function (cb) {
//copy index.html to gh-pages
return gulp.src('./gh-pages/cache/readme.html')
.pipe(rename("index.html"))
.pipe(gulp.dest('../'), function (err) {
if (err)
throw err;
})
.pipe(git.add(), function (err) {
if (err)
throw err;
});
});
gulp.task('checkout-ghpages', function (cb) {
return git.checkout('gh-pages', function (err) {
if (err)
throw err;
cb();
});
});
gulp.task('checkout-master', function (cb) {
return git.checkout('master', function (err) {
if (err)
throw err;
cb();
});
});
gulp.task('fetch', function (cb) {
git.fetch('origin', '', {}, function (err) {
if (err)
throw err;
cb();
});
});
gulp.task('push-gh-pages-changes', function (cb) {
git.push('origin', 'gh-pages', cb);
});
gulp.task('create-new-tag', function (cb) {
var version = getPackageJsonVersion();
git.tag(version, 'Created Tag for version: ' + version, function (error) {
if (error) {
return cb(error);
}
git.push('origin', 'master', {args: '--tags'}, cb);
}
);
});
gulp.task('sandbox',
gulp.series(
'checkout-master',
'convert-readme-to-html',
'fetch',
'checkout-ghpages',
'copy-converted-readme-to-gh-pages-branch',
'commit-gh-pages',
'push-gh-pages',
'checkout-master',
function (error) {
if (error) {
console.log(error.message);
} else {
// console.log('RELEASE ' + getPackageJsonVersion() + ' FINISHED SUCCESSFULLY');
}
callback(error);
})
)
gulp.task('updateGhPages', gulp.series(
'checkout-master',
'convert-readme-to-html',
'fetch',
'checkout-ghpages',
'copy-converted-readme-to-gh-pages-branch',
'commit-gh-pages',
'push-gh-pages',
'checkout-master',
function (error) {
if (error) {
console.log(error.message);
} else {
console.log('GitHub Pages updated successfully.');
}
callback(error);
})
)
gulp.task('_release',
gulp.series(
'checkout-master',
'check_options',
'bump-version',
'changelog',
'commit-changes',
'push-master',
'create-new-tag',
'github-release',
function (error,responses) {
if (error){
console.log(error.message)
callback(error);
}
else{
console.log('conventional release completed successfully ' + responses)
}
})
)
gulp.task('release',
gulp.series(
'_release',
'updateGhPages',
function (error) {
if (error) {
console.log(error.message);
} else {
console.log('RELEASE ' + getPackageJsonVersion() + ' completed successfully.');
}
callback(error);
})
)
gulp.task('config',
gulp.series(
'config-package',
function (error) {
if (error) {
console.log(error.message);
} else {
console.log('Package configuration updated.' + JSON.stringify(JSON.parse(fs.readFileSync('package.json', 'utf8')).config));
}
callback(error);
})
)