tools.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 using `ln -s` command.
  121. * @param {String} source
  122. * @param {String} destination
  123. */
  124. linkDirectories( source, destination ) {
  125. // Remove destination directory if exists.
  126. if ( this.isDirectory( destination ) ) {
  127. this.shExec( `rm -rf ${ destination }` );
  128. }
  129. this.shExec( `ln -s ${ source } ${ destination }` );
  130. },
  131. /**
  132. * Returns dependencies that starts with ckeditor5-, and have valid, short GitHub url. Returns null if no
  133. * dependencies are found.
  134. *
  135. * @param {Object} dependencies Dependencies object loaded from package.json file.
  136. * @returns {Object|null}
  137. */
  138. getCKEditorDependencies( dependencies ) {
  139. let result = null;
  140. if ( dependencies ) {
  141. Object.keys( dependencies ).forEach( function( key ) {
  142. if ( dependencyRegExp.test( key ) ) {
  143. if ( result === null ) {
  144. result = {};
  145. }
  146. result[ key ] = dependencies[ key ];
  147. }
  148. } );
  149. }
  150. return result;
  151. },
  152. /**
  153. * Returns array with all directories under specified path.
  154. *
  155. * @param {String} path
  156. * @returns {Array}
  157. */
  158. getDirectories( path ) {
  159. const fs = require( 'fs' );
  160. const pth = require( 'path' );
  161. return fs.readdirSync( path ).filter( item => {
  162. return this.isDirectory( pth.join( path, item ) );
  163. } );
  164. },
  165. /**
  166. * Returns true if path points to existing directory.
  167. * @param {String} path
  168. * @returns {Boolean}
  169. */
  170. isDirectory( path ) {
  171. const fs = require( 'fs' );
  172. try {
  173. return fs.statSync( path ).isDirectory();
  174. } catch ( e ) {}
  175. return false;
  176. },
  177. /**
  178. * Returns all directories under specified path that match 'ckeditor5' pattern.
  179. *
  180. * @param {String} path
  181. * @returns {Array}
  182. */
  183. getCKE5Directories( path ) {
  184. return this.getDirectories( path ).filter( dir => {
  185. return dependencyRegExp.test( dir );
  186. } );
  187. },
  188. /**
  189. * Updates JSON file under specified path.
  190. * @param {String} path Path to file on disk.
  191. * @param {Function} updateFunction Function that will be called with parsed JSON object. It should return
  192. * modified JSON object to save.
  193. */
  194. updateJSONFile( path, updateFunction ) {
  195. const fs = require( 'fs' );
  196. const contents = fs.readFileSync( path, 'utf-8' );
  197. let json = JSON.parse( contents );
  198. json = updateFunction( json );
  199. fs.writeFileSync( path, JSON.stringify( json, null, 2 ), 'utf-8' );
  200. },
  201. /**
  202. * Calls `npm install` command in specified path.
  203. *
  204. * @param {String} path
  205. */
  206. npmInstall( path ) {
  207. this.shExec( `cd ${ path } && npm install` );
  208. },
  209. /**
  210. * Installs Git hooks in specified repository.
  211. *
  212. * @param {String} path
  213. */
  214. installGitHooks( path ) {
  215. this.shExec( `cd ${ path } && grunt githooks` );
  216. }
  217. };