dev-plugin-create.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. *
  21. * @param {String} ckeditor5Path Path to main CKEditor5 repository.
  22. * @param {Object} options grunt options.
  23. * @param {Function} writeln Function for log output.
  24. * @param {Function} writeError Function of error output
  25. * @returns {Promise} Returns promise fulfilled after task is done.
  26. */
  27. module.exports = ( ckeditor5Path, options, writeln, writeError ) => {
  28. return new Promise( ( resolve, reject ) => {
  29. const workspaceAbsolutePath = path.join( ckeditor5Path, options.workspaceRoot );
  30. let pluginName;
  31. let repositoryPath;
  32. let pluginVersion;
  33. let gitHubUrl;
  34. inquiries.getPluginName()
  35. .then( result => {
  36. pluginName = result;
  37. repositoryPath = path.join( workspaceAbsolutePath, pluginName );
  38. return inquiries.getPluginVersion();
  39. } )
  40. .then( result => {
  41. pluginVersion = result;
  42. return inquiries.getPluginGitHubUrl( pluginName );
  43. } )
  44. .then( result => {
  45. try {
  46. gitHubUrl = result;
  47. writeln( `Initializing repository ${ repositoryPath }...` );
  48. git.initializeRepository( repositoryPath );
  49. writeln( `Updating package.json files...` );
  50. tools.updateJSONFile( path.join( repositoryPath, 'package.json' ), ( json ) => {
  51. json.name = pluginName;
  52. json.version = pluginVersion;
  53. return json;
  54. } );
  55. tools.updateJSONFile( path.join( ckeditor5Path, 'package.json' ), ( json ) => {
  56. if ( !json.dependencies ) {
  57. json.dependencies = {};
  58. }
  59. json.dependencies[ pluginName ] = gitHubUrl;
  60. return json;
  61. } );
  62. writeln( `Linking ${ pluginName } to node_modules...` );
  63. tools.linkDirectories( repositoryPath, path.join( ckeditor5Path, 'node_modules', pluginName ) );
  64. } catch ( error ) {
  65. writeError( error );
  66. }
  67. } )
  68. .catch( reject );
  69. } );
  70. };