releasing.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*eslint
  2. no-console: 0
  3. */
  4. var gulp = require('gulp');
  5. var bump = require('gulp-bump');
  6. var git = require('gulp-git');
  7. var publishRelease = require('publish-release');
  8. var gzipSize = require('gzip-size');
  9. var runSequence = require('gulp4-run-sequence');
  10. var fs = require('fs');
  11. var settings = require('./settings.json');
  12. var shared = require('./shared.js');
  13. gulp.task('git-release', gulp.series(['compress'], function (cb) {
  14. var pkg = require(settings.baseDir + 'package.json');
  15. var v = 'v' + pkg.version;
  16. var message = ':package: Release ' + v;
  17. updateReadmeFileSizes();
  18. gulp.src(['./package.json', './bower.json', './README.md', './bin/', './dist'])
  19. .pipe(git.add({ args: '--all --force' }))
  20. .pipe(git.commit(message));
  21. git.push('origin', 'master', function () {
  22. git.tag(v, message, function () {
  23. git.push('origin', 'master', { args: '--tags' }, cb);
  24. });
  25. });
  26. }));
  27. // Bump (increase) the version number
  28. gulp.task('bump-patch', function () {
  29. return gulp.src(['./package.json', './bower.json'])
  30. .pipe(bump({ type: 'patch' }))
  31. .pipe(gulp.dest('./'));
  32. });
  33. gulp.task('bump-minor', function () {
  34. return gulp.src(['./package.json', './bower.json'])
  35. .pipe(bump({ type: 'minor' }))
  36. .pipe(gulp.dest('./'));
  37. });
  38. gulp.task('bump-major', function () {
  39. return gulp.src(['./package.json', './bower.json'])
  40. .pipe(bump({ type: 'major' }))
  41. .pipe(gulp.dest('./'));
  42. });
  43. gulp.task('npm', function (done) {
  44. require('child_process').spawn('npm', ['publish'], { stdio: 'inherit' })
  45. .on('close', done);
  46. });
  47. gulp.task('github-release', function (done) {
  48. var pkg = require(settings.baseDir + './package.json');
  49. var v = 'v' + pkg.version;
  50. var name = "JsBarcode " + v;
  51. publishRelease({
  52. token: process.env.GITHUB_TOKEN,
  53. owner: "lindell",
  54. repo: "JsBarcode",
  55. tag: v,
  56. name: name,
  57. assets: [__dirname + "/" + settings.baseDir + "/dist/JsBarcode.all.min.js", __dirname + "/" + settings.baseDir + "/dist/JsBarcode.all.js"]
  58. }, done);
  59. });
  60. gulp.task('release', gulp.series(function (callback) {
  61. runSequence(
  62. 'git-release',
  63. 'github-release',
  64. 'npm',
  65. callback
  66. );
  67. }));
  68. gulp.task('patch', function () {
  69. runSequence(
  70. 'bump-patch',
  71. 'release',
  72. releaseDone
  73. );
  74. });
  75. gulp.task('minor', function () {
  76. runSequence(
  77. 'bump-minor',
  78. 'release',
  79. releaseDone
  80. );
  81. });
  82. gulp.task('major', function () {
  83. runSequence(
  84. 'bump-major',
  85. 'release',
  86. releaseDone
  87. );
  88. });
  89. function releaseDone(error) {
  90. if (error) {
  91. console.log(error.message);
  92. }
  93. else {
  94. console.log('Successful!');
  95. }
  96. }
  97. function updateReadmeFileSizes() {
  98. var files = require('./barcode-building.json');
  99. var readme = fs.readFileSync('README.md', "utf-8");
  100. // Update .all files
  101. var allData = fs.readFileSync('dist/JsBarcode.all.min.js');
  102. var allFilesize = gzipSize.sync(allData);
  103. var allRegexp = new RegExp('\\|[^\\|]*\\|([ \\t\\*]*\\[JsBarcode\\.all\\.min\\.js\\])');
  104. readme = readme.replace(allRegexp, "| *" + formatSize(allFilesize) + "* |$1");
  105. // Update all barcodes files
  106. for (var i in files) {
  107. var filename = shared.minifiedFilename(files[i].name);
  108. var fileData = fs.readFileSync('dist/barcodes/' + filename);
  109. var fileFilesize = gzipSize.sync(fileData);
  110. var fileRegexp = new RegExp('\\|[^\\|]*\\|([ \\t]*\\[' + RegExp.escape(filename) + '\\])');
  111. readme = readme.replace(fileRegexp, "| " + formatSize(fileFilesize) + " |$1");
  112. }
  113. fs.writeFileSync('README.md', readme, 'utf8');
  114. }
  115. // Util functions
  116. RegExp.escape = function (s) {
  117. return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  118. };
  119. function formatSize(bytes) {
  120. var kilobytes = Math.round(bytes / 1024 * 10) / 10;
  121. return kilobytes + " kB";
  122. }