2
0

helpers.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /**
  9. * Tasks Helpers
  10. * @module tasks/helpers
  11. */
  12. const gulp = require('gulp');
  13. const notify = require('gulp-notify');
  14. const path = require('path');
  15. const fs = require('fs');
  16. const getCommandArguments = () => {
  17. return require('yargs')
  18. .boolean(['force', 'production', 'debug', 'skip-unit-tests'])
  19. .alias('force', 'f')
  20. .describe('force', 'Prevents tasks to exit on errors')
  21. .alias('skip-linting', 'sl')
  22. .describe('skip-linting', 'Doesn\'t run the linter agains js and html files')
  23. .alias('skip-unit-tests', 'sut')
  24. .describe('skip-unit-tests', 'Doesn\'t run the unit tests')
  25. .alias('production', 'prod')
  26. .describe('production', 'To minify, uglify and many other things for production')
  27. .describe('debug', 'Useful to debug production code or gulp tasks')
  28. .choices('type', ['patch', 'minor', 'major', 'prerelease'])
  29. .alias('type', 't')
  30. .describe('type', 'Used to indicate types of version increment. To be used only with release task otherwise will have no effect.')
  31. .help()
  32. .argv;
  33. };
  34. /**
  35. * Used to create the notification options to be used by gulp-notify
  36. *
  37. * @param {String} title The title of the notification
  38. * @param {String} message The message of the notification
  39. * @param {Boolean} onLast If the notification should only happen on the last file of the stream
  40. * @returns {Object} Notification options
  41. */
  42. const notification = (title, message, isError, onLast) => {
  43. if (!message) {
  44. message = title;
  45. title = 'Notification';
  46. }
  47. return {
  48. title,
  49. message,
  50. sound : 'Frog',
  51. icon : path.join(__dirname, `images/project-logo${isError ? '-error' : ''}.png`),
  52. onLast,
  53. };
  54. };
  55. /**
  56. * Notifies and ends the stream automatically on error
  57. *
  58. * @param {String} title The title of the notification
  59. * @param {String} message The message of the notification
  60. *
  61. * @example gulp.pipe(...).on('error', notificationOnError('Title', '<%= error.message %>'));
  62. */
  63. const notificationOnError = (title, message) => {
  64. return notify.onError(
  65. notification(title, message, true)
  66. );
  67. };
  68. const gulpNotify = (title, message) => {
  69. gulp.src('./')
  70. .pipe(
  71. notify(notification(title, message))
  72. );
  73. };
  74. /**
  75. * A fake task just to notify on demand whenever needed in a series of tasks that are being ran
  76. *
  77. * @param {String} taskName The title of the notification
  78. * @param {String} title The title of the notification
  79. * @param {String} message The message of the notification
  80. * @returns {String} taskName
  81. *
  82. * @example runSequence(
  83. * 'someTask',
  84. * notificationTask('taskName', 'Title', 'Message')
  85. * );
  86. */
  87. const notificationTask = (taskName, title, message) => {
  88. gulp.task(taskName, () => {
  89. gulpNotify(title, message);
  90. });
  91. return taskName;
  92. };
  93. /**
  94. * Used to handle failed task better. To be used as last parameter for runSequence
  95. *
  96. * @param {Function} done Gulp done callback
  97. */
  98. const handleTaskSequenceError = (done) => {
  99. // return done;
  100. return (err) => {
  101. if (err && !getCommandArguments().debug) {
  102. // return process.exit(2);
  103. return done();
  104. }
  105. return done(err);
  106. };
  107. };
  108. /**
  109. * Returns the app's version found in package.json
  110. *
  111. * @returns {String}
  112. */
  113. const getPackageVersion = () => {
  114. return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
  115. };
  116. module.exports = {
  117. getCommandArguments,
  118. getPackageVersion,
  119. notificationTask,
  120. gulpNotify,
  121. notificationOnError,
  122. handleTaskSequenceError,
  123. };