dev-install.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 git = require( './git' );
  7. const tools = require( './tools' );
  8. const path = require( 'path' );
  9. /**
  10. * This tasks install specified module in development mode. It can be executed by typing:
  11. * grunt dev-install --plugin <git_hub_url|npm_name|path_on_disk>
  12. *
  13. *
  14. * It performs following steps:
  15. * 1. If GitHub URL is provided - clones the repository.
  16. * 2. If NPM module name is provided - gets GitHub URL from NPM and clones the repository.
  17. * 3. If path on disk is provided - it is used directly.
  18. * 4. Runs `npm install` in plugin repository.
  19. * 5. If plugin exists in `ckeditor5/node_modules/` - runs `npm uninstall plugin_name`.
  20. * 6. Links plugin directory into `ckeditor5/node_modules/`.
  21. * 7. Adds dependency to `ckeditor5/package.json`.
  22. *
  23. * @param {String} ckeditor5Path Absolute path to `ckeditor5` repository.
  24. * @param {String} workspaceRoot Relative path to workspace root directory.
  25. * @param {String} name Name of the NPM module or GitHub URL.
  26. * @param {Function} writeln Function used to report progress to the console.
  27. */
  28. module.exports = ( ckeditor5Path, workspaceRoot, name, writeln ) => {
  29. const workspaceAbsolutePath = path.join( ckeditor5Path, workspaceRoot );
  30. let repositoryPath;
  31. let dependency;
  32. let urlInfo;
  33. // First check if name is local path to repository.
  34. repositoryPath = path.isAbsolute( name ) ? name : path.resolve( name );
  35. if ( tools.isDirectory( repositoryPath ) ) {
  36. const packageName = tools.readPackageName( repositoryPath );
  37. if ( packageName ) {
  38. writeln( `Plugin located at ${ repositoryPath }.` );
  39. urlInfo = {
  40. name: packageName
  41. };
  42. dependency = repositoryPath;
  43. }
  44. }
  45. // Check if name is repository URL.
  46. if ( !urlInfo ) {
  47. urlInfo = git.parseRepositoryUrl( name );
  48. dependency = name;
  49. }
  50. // Check if name is NPM package.
  51. if ( !urlInfo ) {
  52. writeln( `Not a GitHub URL. Trying to get GitHub URL from NPM package...` );
  53. const url = tools.getGitUrlFromNpm( name );
  54. if ( url ) {
  55. urlInfo = git.parseRepositoryUrl( url );
  56. dependency = url;
  57. }
  58. }
  59. if ( urlInfo ) {
  60. repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
  61. if ( tools.isDirectory( repositoryPath ) ) {
  62. writeln( `Directory ${ repositoryPath } already exists.` );
  63. } else {
  64. writeln( `Cloning ${ urlInfo.name } into ${ repositoryPath }...` );
  65. git.cloneRepository( urlInfo, workspaceAbsolutePath );
  66. }
  67. // Checkout to specified branch if one is provided.
  68. if ( urlInfo.branch ) {
  69. writeln( `Checking ${ urlInfo.name } to ${ urlInfo.branch }...` );
  70. git.checkout( repositoryPath, urlInfo.branch );
  71. }
  72. // Run `npm install` in new repository.
  73. writeln( `Running "npm install" in ${ urlInfo.name }...` );
  74. tools.npmInstall( repositoryPath );
  75. const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
  76. if ( tools.isDirectory( linkPath ) ) {
  77. writeln( `Uninstalling ${ urlInfo.name } from CKEditor5 node_modules...` );
  78. tools.npmUninstall( ckeditor5Path, urlInfo.name );
  79. }
  80. writeln( `Linking ${ linkPath } to ${ repositoryPath }...` );
  81. tools.linkDirectories( repositoryPath, linkPath );
  82. writeln( `Adding ${ urlInfo.name } dependency to CKEditor5 package.json... ` );
  83. tools.updateJSONFile( path.join( ckeditor5Path, 'package.json' ), ( json ) => {
  84. json.dependencies = json.dependencies || {};
  85. json.dependencies[ urlInfo.name ] = dependency;
  86. return json;
  87. } );
  88. writeln( `Installing Git hooks in ${ urlInfo.name }...` );
  89. tools.installGitHooks( repositoryPath );
  90. } else {
  91. throw new Error( 'Please provide valid GitHub URL, NPM module name or path.' );
  92. }
  93. };