tools.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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( '"jshint:git"' ) > -1 );
  13. return isDirectCall || isDefaultTask;
  14. },
  15. getGitDirtyFiles: function() {
  16. // Cache it, so it is executed only once when running multiple tasks.
  17. if ( !dirtyFiles ) {
  18. dirtyFiles = this.shExec( 'git diff-index --name-only HEAD' ).replace( /\s*$/, '' ).split( '\n' );
  19. if ( dirtyFiles.length == 1 && !dirtyFiles[ 0 ] ) {
  20. dirtyFiles = [];
  21. }
  22. }
  23. return dirtyFiles;
  24. },
  25. shExec: function( command ) {
  26. var sh = require( 'shelljs' );
  27. sh.config.silent = true;
  28. var ret = sh.exec( command );
  29. if ( ret.code ) {
  30. throw new Error(
  31. 'Error while executing `' + command + '`:\n\n' +
  32. ret.output
  33. );
  34. }
  35. return ret.output;
  36. }
  37. };