tools.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. 'use strict';
  2. let dirtyFiles,
  3. ignoreList;
  4. const dependencyRegExp = /^ckeditor5-/;
  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( grunt, task ) {
  14. const cliTasks = grunt.cli.tasks;
  15. // Check if the task has been called directly.
  16. const isDirectCall = ( cliTasks.indexOf( task ) > -1 );
  17. // Check if this is a "default" call and that the task is inside "default".
  18. const isDefaultTask = ( cliTasks.indexOf( 'default' ) > -1 ) || !cliTasks.length;
  19. // Hacking Grunt hard.
  20. const 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( grunt, options ) {
  30. const task = options.task;
  31. const taskConfig = {};
  32. const 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. const all = options.targets.all;
  37. let 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. let ignoreProp = task + '.options.' + options.addGitIgnore;
  51. let 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( 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() {
  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( command ) {
  108. const sh = require( 'shelljs' );
  109. sh.config.silent = true;
  110. const 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. /**
  120. * Links directory located in source path to directory located in destination path.
  121. * @param {String} source
  122. * @param {String} destination
  123. */
  124. linkDirectories( source, destination ) {
  125. const fs = require( 'fs' );
  126. // Remove destination directory if exists.
  127. if ( this.isDirectory( destination ) ) {
  128. this.shExec( `rm -rf ${ destination }` );
  129. }
  130. fs.symlinkSync( source, destination, 'dir' );
  131. },
  132. /**
  133. * Returns dependencies that starts with ckeditor5-, and have valid, short GitHub url. Returns null if no
  134. * dependencies are found.
  135. *
  136. * @param {Object} dependencies Dependencies object loaded from package.json file.
  137. * @returns {Object|null}
  138. */
  139. getCKEditorDependencies( dependencies ) {
  140. let result = null;
  141. if ( dependencies ) {
  142. Object.keys( dependencies ).forEach( function( key ) {
  143. if ( dependencyRegExp.test( key ) ) {
  144. if ( result === null ) {
  145. result = {};
  146. }
  147. result[ key ] = dependencies[ key ];
  148. }
  149. } );
  150. }
  151. return result;
  152. },
  153. /**
  154. * Returns array with all directories under specified path.
  155. *
  156. * @param {String} path
  157. * @returns {Array}
  158. */
  159. getDirectories( path ) {
  160. const fs = require( 'fs' );
  161. const pth = require( 'path' );
  162. return fs.readdirSync( path ).filter( item => {
  163. return this.isDirectory( pth.join( path, item ) );
  164. } );
  165. },
  166. /**
  167. * Returns true if path points to existing directory.
  168. * @param {String} path
  169. * @returns {Boolean}
  170. */
  171. isDirectory( path ) {
  172. const fs = require( 'fs' );
  173. try {
  174. return fs.statSync( path ).isDirectory();
  175. } catch ( e ) {}
  176. return false;
  177. },
  178. /**
  179. * Returns all directories under specified path that match 'ckeditor5' pattern.
  180. *
  181. * @param {String} path
  182. * @returns {Array}
  183. */
  184. getCKE5Directories( path ) {
  185. return this.getDirectories( path ).filter( dir => {
  186. return dependencyRegExp.test( dir );
  187. } );
  188. },
  189. /**
  190. * Updates JSON file under specified path.
  191. * @param {String} path Path to file on disk.
  192. * @param {Function} updateFunction Function that will be called with parsed JSON object. It should return
  193. * modified JSON object to save.
  194. */
  195. updateJSONFile( path, updateFunction ) {
  196. const fs = require( 'fs' );
  197. const contents = fs.readFileSync( path, 'utf-8' );
  198. let json = JSON.parse( contents );
  199. json = updateFunction( json );
  200. fs.writeFileSync( path, JSON.stringify( json, null, 2 ), 'utf-8' );
  201. },
  202. /**
  203. * Calls `npm install` command in specified path.
  204. *
  205. * @param {String} path
  206. */
  207. npmInstall( path ) {
  208. this.shExec( `cd ${ path } && npm install` );
  209. },
  210. /**
  211. * Installs Git hooks in specified repository.
  212. *
  213. * @param {String} path
  214. */
  215. installGitHooks( path ) {
  216. this.shExec( `cd ${ path } && grunt githooks` );
  217. }
  218. };