tools.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* jshint node: true */
  2. 'use strict';
  3. var dirtyFiles;
  4. module.exports = {
  5. checkTaskInQueue: function( grunt, task ) {
  6. var cliTasks = grunt.cli.tasks;
  7. // Check if the task has been called directly.
  8. var isDirectCall = ( cliTasks.indexOf( task ) > -1 );
  9. // Check if this is a "default" call and that the task is inside "default".
  10. var isDefaultTask = ( cliTasks.indexOf( 'default' ) > -1 ) || !cliTasks.length,
  11. // Hacking grunt hard.
  12. isTaskInDefault = isDefaultTask && ( grunt.task._tasks.default.info.indexOf( '"' + task + '"' ) > -1 );
  13. return isDirectCall || isDefaultTask;
  14. },
  15. setupMultitaskConfig: function( grunt, options ) {
  16. var that = this,
  17. task = options.task,
  18. taskConfig = {},
  19. config = taskConfig[ task ] = {
  20. options: options.defaultOptions
  21. };
  22. // "all" is the default target to be used if others are not to be run.
  23. var all = options.targets.all,
  24. isAll = true;
  25. delete options.targets.all;
  26. Object.getOwnPropertyNames( options.targets ).forEach( function( target ) {
  27. if ( that.checkTaskInQueue( grunt, task + ':' + target ) ) {
  28. config[ target ] = options.targets[ target ]();
  29. isAll = false;
  30. }
  31. } );
  32. if ( isAll ) {
  33. config.all = all();
  34. }
  35. // Merge over configurations set in gruntfile.js.
  36. grunt.config.merge( taskConfig );
  37. },
  38. getGitDirtyFiles: function() {
  39. // Cache it, so it is executed only once when running multiple tasks.
  40. if ( !dirtyFiles ) {
  41. dirtyFiles = this.shExec( 'git diff-index --name-only HEAD' ).replace( /\s*$/, '' ).split( '\n' );
  42. if ( dirtyFiles.length == 1 && !dirtyFiles[ 0 ] ) {
  43. dirtyFiles = [];
  44. }
  45. }
  46. return dirtyFiles;
  47. },
  48. shExec: function( command ) {
  49. var sh = require( 'shelljs' );
  50. sh.config.silent = true;
  51. var ret = sh.exec( command );
  52. if ( ret.code ) {
  53. throw new Error(
  54. 'Error while executing `' + command + '`:\n\n' +
  55. ret.output
  56. );
  57. }
  58. return ret.output;
  59. }
  60. };