tools.js 3.7 KB

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