dev-plugin-install.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const inquiries = require( './inquiries' );
  7. const git = require( './git' );
  8. const tools = require( './tools' );
  9. const path = require( 'path' );
  10. /**
  11. * 1. Ask for plugin name.
  12. * 2. Ask for GitHub URL.
  13. * 3. Clone repository from provided GitHub URL.
  14. * 4. Checkout repository to provided branch (or master if no branch is provided).
  15. * 5. Update package.json file in CKEditor5 repository.
  16. * 6. Link new plugin.
  17. * 7. Call `npm install` in plugin repository.
  18. * 8. Install Git hooks in plugin repository.
  19. *
  20. * @param {String} ckeditor5Path Path to main CKEditor5 repository.
  21. * @param {Object} options grunt options.
  22. * @param {Function} writeln Function for log output.
  23. * @param {Function} writeError Function of error output
  24. * @returns {Promise} Returns promise fulfilled after task is done.
  25. */
  26. module.exports = ( ckeditor5Path, options, writeln, writeError ) => {
  27. return new Promise( ( resolve, reject ) => {
  28. const workspaceAbsolutePath = path.join( ckeditor5Path, options.workspaceRoot );
  29. let pluginName;
  30. let repositoryPath;
  31. let gitHubUrl;
  32. inquiries.getPluginName()
  33. .then( result => {
  34. pluginName = result;
  35. repositoryPath = path.join( workspaceAbsolutePath, pluginName );
  36. return inquiries.getPluginGitHubUrl( pluginName );
  37. } )
  38. .then( result => {
  39. gitHubUrl = result;
  40. let urlInfo = git.parseRepositoryUrl( gitHubUrl );
  41. try {
  42. writeln( `Clonning ${ gitHubUrl }...` );
  43. git.cloneRepository( urlInfo, workspaceAbsolutePath );
  44. writeln( `Checking out ${ gitHubUrl } to ${ urlInfo.branch }...` );
  45. git.checkout( repositoryPath, urlInfo.branch );
  46. writeln( `Updating package.json files...` );
  47. tools.updateJSONFile( path.join( ckeditor5Path, 'package.json' ), ( json ) => {
  48. if ( !json.dependencies ) {
  49. json.dependencies = {};
  50. }
  51. json.dependencies[ pluginName ] = gitHubUrl;
  52. return json;
  53. } );
  54. writeln( `Linking ${ pluginName } to node_modules...` );
  55. tools.linkDirectories( repositoryPath, path.join( ckeditor5Path, 'node_modules', pluginName ) );
  56. writeln( `Running npm install in ${ pluginName }.` );
  57. tools.npmInstall( repositoryPath );
  58. writeln( `Installing GIT hooks in ${ pluginName }.` );
  59. tools.installGitHooks( repositoryPath );
  60. } catch ( error ) {
  61. writeError( error );
  62. }
  63. } )
  64. .catch( reject );
  65. } );
  66. };