8
0

common.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. var tools = require( '../utils/tools' );
  3. var path = require( 'path' );
  4. var fs = require( 'fs' );
  5. var INITIAL_COMMIT_MESSAGE = 'Initial plugin commit.';
  6. module.exports = {
  7. linkPlugin: function( data ) {
  8. // Don't use sudo on windows when executing npm link.
  9. var isWin = process.platform == 'win32';
  10. data.grunt.log.writeln( 'Linking plugin with npm link...' );
  11. return new Promise( function( resolve ) {
  12. var linkCommands = [
  13. 'cd ' + data.repositoryLocation,
  14. ( !isWin ? 'sudo ' : '' ) + 'npm link',
  15. 'cd ' + data.cwd,
  16. 'npm link ' + data.pluginName
  17. ];
  18. tools.shExec( linkCommands.join( ' && ' ) );
  19. resolve( data );
  20. } );
  21. },
  22. cloneRepository: function( data ) {
  23. var repo = 'git@github.com:' + data.gitHubUrl;
  24. data.grunt.log.writeln( 'Cloning repository ' + repo + '...' );
  25. return new Promise( function( resolve ) {
  26. var cloneCommands = [
  27. 'cd ' + path.join( data.repositoryLocation, '..' ),
  28. 'git clone ' + repo
  29. ];
  30. tools.shExec( cloneCommands.join( ' && ' ) );
  31. resolve( data );
  32. } );
  33. },
  34. /**
  35. * Creates initial git commit for new plugin. New plugin is created by merging CKEditor5 boilerplate and modifying
  36. * package.json. After that initial commit is needed.
  37. * @param data
  38. * @param {String} data.repositoryLocation New plugin location on disk.
  39. */
  40. commitNewPlugin: function( data ) {
  41. return new Promise( function( resolve ) {
  42. var commitCommands = [
  43. 'cd ' + data.repositoryLocation,
  44. 'git add package.json',
  45. 'git commit -m "' + INITIAL_COMMIT_MESSAGE + '"'
  46. ];
  47. tools.shExec( commitCommands.join( ' && ' ) );
  48. resolve( data );
  49. } );
  50. },
  51. /**
  52. * Updates CKEditor5 package.json file by adding new plugin dependency. Returns promise resolved when update is
  53. * finished.
  54. * @param {Object} data
  55. * @param {String} data.cwd Grunt starting directory - full path to CKEditor5 git repository on disk.
  56. * @param {String} data.pluginName New plugin name.
  57. * @param {String} data.gitHubUrl New plugin GitHub url.
  58. * @returns {Promise}
  59. */
  60. updatePackageJson: function( data ) {
  61. data.grunt.log.writeln( 'Updating CKEditor5 package.json...' );
  62. return new Promise( function( resolve, reject ) {
  63. module.exports.updateJSONFile( path.join( data.cwd, 'package.json' ), function( json ) {
  64. if ( !json.dependencies ) {
  65. json.dependencies = {};
  66. }
  67. json.dependencies[ data.pluginName ] = data.gitHubUrl;
  68. return json;
  69. } ).then( function() {
  70. resolve( data );
  71. }, function( error ) {
  72. reject( error );
  73. } );
  74. } );
  75. },
  76. // Changes given package.json.
  77. updateJSONFile: function( path, changeFn ) {
  78. return new Promise( function( resolve, reject ) {
  79. fs.readFile( path, 'utf-8', function( err, file ) {
  80. var json;
  81. if ( err ) {
  82. return reject( err );
  83. }
  84. // Update package.json file.
  85. json = JSON.parse( file );
  86. json = changeFn( json );
  87. fs.writeFile( path, JSON.stringify( json, null, 2 ), 'utf-8', function( err ) {
  88. if ( err ) {
  89. return reject( err );
  90. }
  91. resolve( );
  92. } );
  93. } );
  94. } );
  95. }
  96. };