dev-plugin-create.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 new plugin name.
  12. * 2. Ask for initial version.
  13. * 3. Ask for GitHub URL.
  14. * 4. Initialize repository
  15. * 4.1. Initialize GIT repository.
  16. * 4.2. Fetch and merge boilerplate project.
  17. * 5. Update package.json file in new plugin's repository.
  18. * 6. Update package.json file in CKEditor5 repository.
  19. * 7. Link new plugin.
  20. * 8. Call `npm install` in plugin repository.
  21. * 9. Install Git hooks in plugin repository.
  22. *
  23. * @param {String} ckeditor5Path Path to main CKEditor5 repository.
  24. * @param {Object} options grunt options.
  25. * @param {Function} writeln Function for log output.
  26. * @param {Function} writeError Function of error output
  27. * @returns {Promise} Returns promise fulfilled after task is done.
  28. */
  29. module.exports = ( ckeditor5Path, options, writeln, writeError ) => {
  30. return new Promise( ( resolve, reject ) => {
  31. const workspaceAbsolutePath = path.join( ckeditor5Path, options.workspaceRoot );
  32. let pluginName;
  33. let repositoryPath;
  34. let pluginVersion;
  35. let gitHubUrl;
  36. inquiries.getPluginName()
  37. .then( result => {
  38. pluginName = result;
  39. repositoryPath = path.join( workspaceAbsolutePath, pluginName );
  40. return inquiries.getPluginVersion();
  41. } )
  42. .then( result => {
  43. pluginVersion = result;
  44. return inquiries.getPluginGitHubUrl( pluginName );
  45. } )
  46. .then( result => {
  47. try {
  48. gitHubUrl = result;
  49. writeln( `Initializing repository ${ repositoryPath }...` );
  50. git.initializeRepository( repositoryPath );
  51. writeln( `Updating package.json files...` );
  52. tools.updateJSONFile( path.join( repositoryPath, 'package.json' ), ( json ) => {
  53. json.name = pluginName;
  54. json.version = pluginVersion;
  55. return json;
  56. } );
  57. tools.updateJSONFile( path.join( ckeditor5Path, 'package.json' ), ( json ) => {
  58. if ( !json.dependencies ) {
  59. json.dependencies = {};
  60. }
  61. json.dependencies[ pluginName ] = gitHubUrl;
  62. return json;
  63. } );
  64. writeln( `Linking ${ pluginName } to node_modules...` );
  65. tools.linkDirectories( repositoryPath, path.join( ckeditor5Path, 'node_modules', pluginName ) );
  66. writeln( `Running npm install in ${ pluginName }.` );
  67. tools.npmInstall( repositoryPath );
  68. writeln( `Installing GIT hooks in ${ pluginName }.` );
  69. tools.installGitHooks( repositoryPath );
  70. } catch ( error ) {
  71. writeError( error );
  72. }
  73. } )
  74. .catch( reject );
  75. } );
  76. };