tools.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* jshint node: true */
  2. 'use strict';
  3. var dirtyFiles,
  4. ignoreList;
  5. module.exports = {
  6. /**
  7. * Check if a task (including its optional target) is in the queue of tasks to be executed by grunt.
  8. * @param grunt {Object} The grunt object.
  9. * @param task {String} The task name. May optionally include the target (e.g. 'task:target').
  10. * @returns {Boolean} "true" if the task is in the queue.
  11. */
  12. checkTaskInQueue: function( grunt, task ) {
  13. var cliTasks = grunt.cli.tasks;
  14. // Check if the task has been called directly.
  15. var isDirectCall = ( cliTasks.indexOf( task ) > -1 );
  16. // Check if this is a "default" call and that the task is inside "default".
  17. var isDefaultTask = ( cliTasks.indexOf( 'default' ) > -1 ) || !cliTasks.length,
  18. // Hacking grunt hard.
  19. isTaskInDefault = isDefaultTask && ( grunt.task._tasks.default.info.indexOf( '"' + task + '"' ) > -1 );
  20. return isDirectCall || isTaskInDefault;
  21. },
  22. /**
  23. * Configures a multi-task and defines targets that are queued to be run by grunt.
  24. * @param grunt {Object} The grunt object.
  25. * @param options {Object} A list of options for the method. See the jscs and jshint tasks for example.
  26. */
  27. setupMultitaskConfig: function( grunt, options ) {
  28. var task = options.task,
  29. taskConfig = {},
  30. config = taskConfig[ task ] = {
  31. options: options.defaultOptions
  32. };
  33. // "all" is the default target to be used if others are not to be run.
  34. var all = options.targets.all,
  35. isAll = true;
  36. delete options.targets.all;
  37. Object.getOwnPropertyNames( options.targets ).forEach( function( target ) {
  38. if ( this.checkTaskInQueue( grunt, task + ':' + target ) ) {
  39. config[ target ] = options.targets[ target ]();
  40. isAll = false;
  41. }
  42. }, this );
  43. if ( isAll ) {
  44. config.all = all();
  45. }
  46. // Merge over configurations set in gruntfile.js.
  47. grunt.config.merge( taskConfig );
  48. },
  49. /**
  50. * Gets the list of ignores from .gitignore.
  51. * @param grunt {Object} The grunt object.
  52. * @returns {Array} The list of ignores.
  53. */
  54. getGitIgnore: function( grunt ) {
  55. if ( !ignoreList ) {
  56. ignoreList = grunt.file.read( '.gitignore' );
  57. ignoreList = ignoreList
  58. // Remove comment lines.
  59. .replace( /^#.*$/gm, '' )
  60. // Transform into array.
  61. .split( /\n+/ )
  62. // Remove empty entries.
  63. .filter( function( path ) {
  64. return !!path;
  65. } );
  66. }
  67. return ignoreList;
  68. },
  69. /**
  70. * Gets the list of files that are supposed to be included in the next git commit.
  71. * @returns {Array} A list of file paths.
  72. */
  73. getGitDirtyFiles: function() {
  74. // Cache it, so it is executed only once when running multiple tasks.
  75. if ( !dirtyFiles ) {
  76. dirtyFiles = this
  77. // Compare the state of index with HEAD.
  78. .shExec( 'git diff-index --name-only HEAD' )
  79. // Remove trailing /n, to avoid empty entry.
  80. .replace( /\s*$/, '' )
  81. // Transform into array.
  82. .split( '\n' );
  83. // If nothing is returned, the array will one one empty string only.
  84. if ( dirtyFiles.length == 1 && !dirtyFiles[ 0 ] ) {
  85. dirtyFiles = [];
  86. }
  87. }
  88. return dirtyFiles;
  89. },
  90. /**
  91. * Executes a shell command.
  92. * @param command {String} The command to be executed.
  93. * @returns {String} The command output.
  94. */
  95. shExec: function( command ) {
  96. var sh = require( 'shelljs' );
  97. sh.config.silent = true;
  98. var ret = sh.exec( command );
  99. if ( ret.code ) {
  100. throw new Error(
  101. 'Error while executing `' + command + '`:\n\n' +
  102. ret.output
  103. );
  104. }
  105. return ret.output;
  106. }
  107. };