8
0

tools.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* jshint node: true */
  2. 'use strict';
  3. var dirtyFiles,
  4. ignoreList;
  5. module.exports = {
  6. checkTaskInQueue: function( grunt, task ) {
  7. var cliTasks = grunt.cli.tasks;
  8. // Check if the task has been called directly.
  9. var isDirectCall = ( cliTasks.indexOf( task ) > -1 );
  10. // Check if this is a "default" call and that the task is inside "default".
  11. var isDefaultTask = ( cliTasks.indexOf( 'default' ) > -1 ) || !cliTasks.length,
  12. // Hacking grunt hard.
  13. isTaskInDefault = isDefaultTask && ( grunt.task._tasks.default.info.indexOf( '"' + task + '"' ) > -1 );
  14. return isDirectCall || isTaskInDefault;
  15. },
  16. setupMultitaskConfig: function( grunt, options ) {
  17. var 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 ( this.checkTaskInQueue( grunt, task + ':' + target ) ) {
  28. config[ target ] = options.targets[ target ]();
  29. isAll = false;
  30. }
  31. }, this );
  32. if ( isAll ) {
  33. config.all = all();
  34. }
  35. // Merge over configurations set in gruntfile.js.
  36. grunt.config.merge( taskConfig );
  37. },
  38. getGitIgnore: function( grunt ) {
  39. if ( !ignoreList ) {
  40. ignoreList = grunt.file.read( '.gitignore' );
  41. ignoreList = ignoreList
  42. // Remove comment lines.
  43. .replace( /^#.*$/gm, '' )
  44. // Transform into array.
  45. .split( /\n+/ )
  46. // Remove empty entries.
  47. .filter( function( path ) {
  48. return !!path;
  49. } );
  50. }
  51. return ignoreList;
  52. },
  53. getGitDirtyFiles: function() {
  54. // Cache it, so it is executed only once when running multiple tasks.
  55. if ( !dirtyFiles ) {
  56. dirtyFiles = this.shExec( 'git diff-index --name-only HEAD' ).replace( /\s*$/, '' ).split( '\n' );
  57. if ( dirtyFiles.length == 1 && !dirtyFiles[ 0 ] ) {
  58. dirtyFiles = [];
  59. }
  60. }
  61. return dirtyFiles;
  62. },
  63. shExec: function( command ) {
  64. var sh = require( 'shelljs' );
  65. sh.config.silent = true;
  66. var ret = sh.exec( command );
  67. if ( ret.code ) {
  68. throw new Error(
  69. 'Error while executing `' + command + '`:\n\n' +
  70. ret.output
  71. );
  72. }
  73. return ret.output;
  74. }
  75. };