8
0

tools.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. 'use strict';
  2. var dirtyFiles,
  3. ignoreList;
  4. module.exports = {
  5. /**
  6. * Check if a task (including its optional target) is in the queue of tasks to be executed by Grunt.
  7. *
  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. var 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. *
  25. * @param grunt {Object} The Grunt object.
  26. * @param options {Object} A list of options for the method. See the jscs and jshint tasks for example.
  27. */
  28. setupMultitaskConfig: function( grunt, options ) {
  29. var task = options.task;
  30. var taskConfig = {};
  31. var config = taskConfig[ task ] = {
  32. options: options.defaultOptions
  33. };
  34. // "all" is the default target to be used if others are not to be run.
  35. var all = options.targets.all;
  36. var isAll = true;
  37. delete options.targets.all;
  38. Object.getOwnPropertyNames( options.targets ).forEach( function( target ) {
  39. if ( this.checkTaskInQueue( grunt, task + ':' + target ) ) {
  40. config[ target ] = options.targets[ target ]();
  41. isAll = false;
  42. }
  43. }, this );
  44. if ( isAll ) {
  45. config.all = all();
  46. }
  47. // Append .gitignore entries to the ignore list.
  48. if ( options.addGitIgnore ) {
  49. var ignoreProp = task + '.options.' + options.addGitIgnore;
  50. var ignores = grunt.config.get( ignoreProp ) || [];
  51. ignores = ignores.concat( this.getGitIgnore( grunt ) );
  52. grunt.config.set( ignoreProp, ignores );
  53. }
  54. // Merge over configurations set in gruntfile.js.
  55. grunt.config.merge( taskConfig );
  56. },
  57. /**
  58. * Gets the list of ignores from `.gitignore`.
  59. *
  60. * @param grunt {Object} The Grunt object.
  61. * @returns {String[]} The list of ignores.
  62. */
  63. getGitIgnore: function( grunt ) {
  64. if ( !ignoreList ) {
  65. ignoreList = grunt.file.read( '.gitignore' );
  66. ignoreList = ignoreList
  67. // Remove comment lines.
  68. .replace( /^#.*$/gm, '' )
  69. // Transform into array.
  70. .split( /\n+/ )
  71. // Remove empty entries.
  72. .filter( function( path ) {
  73. return !!path;
  74. } );
  75. }
  76. return ignoreList;
  77. },
  78. /**
  79. * Gets the list of files that are supposed to be included in the next Git commit.
  80. *
  81. * @returns {String[]} A list of file paths.
  82. */
  83. getGitDirtyFiles: function() {
  84. // Cache it, so it is executed only once when running multiple tasks.
  85. if ( !dirtyFiles ) {
  86. dirtyFiles = this
  87. // Compare the state of index with HEAD.
  88. .shExec( 'git diff-index --name-only HEAD' )
  89. // Remove trailing /n to avoid an empty entry.
  90. .replace( /\s*$/, '' )
  91. // Transform into array.
  92. .split( '\n' );
  93. // If nothing is returned, the array will one one empty string only.
  94. if ( dirtyFiles.length == 1 && !dirtyFiles[ 0 ] ) {
  95. dirtyFiles = [];
  96. }
  97. }
  98. return dirtyFiles;
  99. },
  100. /**
  101. * Executes a shell command.
  102. *
  103. * @param command {String} The command to be executed.
  104. * @returns {String} The command output.
  105. */
  106. shExec: function( command ) {
  107. var sh = require( 'shelljs' );
  108. sh.config.silent = true;
  109. var ret = sh.exec( command );
  110. if ( ret.code ) {
  111. throw new Error(
  112. 'Error while executing `' + command + '`:\n\n' +
  113. ret.output
  114. );
  115. }
  116. return ret.output;
  117. },
  118. /**
  119. * Links repository located in source path to repository located in destination path. Uses npm link.
  120. * @param {String} sourcePath
  121. * @param {String} destinationPath
  122. * @param {String} pluginName
  123. */
  124. npmLink: function( sourcePath, destinationPath, pluginName ) {
  125. // Don't use sudo on windows when executing npm link.
  126. var isWin = process.platform == 'win32';
  127. var linkCommands = [
  128. 'cd ' + sourcePath,
  129. ( !isWin ? 'sudo ' : '' ) + 'npm link',
  130. 'cd ' + destinationPath,
  131. 'npm link ' + pluginName
  132. ];
  133. module.exports.shExec( linkCommands.join( ' && ' ) );
  134. },
  135. /**
  136. * Clones repository from provided GitHub URL.
  137. * @param {String} gitHubUrl GitHub url to repository.
  138. * @param {String} location Destination path.
  139. */
  140. cloneRepository: function( gitHubUrl, location ) {
  141. var cloneCommands = [
  142. 'cd ' + location,
  143. 'git clone git@github.com:' + gitHubUrl
  144. ];
  145. module.exports.shExec( cloneCommands.join( ' && ' ) );
  146. },
  147. /**
  148. * Returns dependencies that starts with ckeditor5- or null if no dependencies are found.
  149. * @param {Object} dependencies Dependencies object loaded from package.json file.
  150. * @returns {Object|null}
  151. */
  152. getCKEditorDependencies: function( dependencies ) {
  153. var result = null;
  154. var regexp = /^ckeditor5-/;
  155. if ( dependencies ) {
  156. Object.keys( dependencies ).forEach( function( key ) {
  157. if ( regexp.test( key ) ) {
  158. if ( result === null ) {
  159. result = {};
  160. }
  161. result[ key ] = dependencies[ key ];
  162. }
  163. } );
  164. }
  165. return result;
  166. }
  167. };