dev-install.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @license Copyright (c) 2003-2016, 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. * 8. Installs Git hooks.
  23. *
  24. * @param {String} ckeditor5Path Absolute path to `ckeditor5` repository.
  25. * @param {String} workspaceRoot Relative path to workspace root directory.
  26. * @param {String} name Name of the NPM module or GitHub URL.
  27. * @param {Function} writeln Function used to report progress to the console.
  28. */
  29. module.exports = ( ckeditor5Path, workspaceRoot, name, writeln ) => {
  30. const workspaceAbsolutePath = path.join( ckeditor5Path, workspaceRoot );
  31. let repositoryPath;
  32. let dependency;
  33. let urlInfo;
  34. // First check if name is local path to repository.
  35. repositoryPath = path.isAbsolute( name ) ? name : path.resolve( name );
  36. if ( tools.isDirectory( repositoryPath ) ) {
  37. const packageName = tools.readPackageName( repositoryPath );
  38. if ( packageName ) {
  39. writeln( `Plugin located at ${ repositoryPath }.` );
  40. urlInfo = {
  41. name: packageName
  42. };
  43. dependency = repositoryPath;
  44. }
  45. }
  46. // Check if name is repository URL.
  47. if ( !urlInfo ) {
  48. urlInfo = git.parseRepositoryUrl( name );
  49. dependency = name;
  50. }
  51. // Check if name is NPM package.
  52. if ( !urlInfo ) {
  53. writeln( `Not a GitHub URL. Trying to get GitHub URL from NPM package...` );
  54. const url = tools.getGitUrlFromNpm( name );
  55. if ( url ) {
  56. urlInfo = git.parseRepositoryUrl( url );
  57. dependency = url;
  58. }
  59. }
  60. if ( urlInfo ) {
  61. repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
  62. if ( tools.isDirectory( repositoryPath ) ) {
  63. writeln( `Directory ${ repositoryPath } already exists.` );
  64. } else {
  65. writeln( `Cloning ${ urlInfo.name } into ${ repositoryPath }...` );
  66. git.cloneRepository( urlInfo, workspaceAbsolutePath );
  67. }
  68. // Checkout to specified branch if one is provided.
  69. if ( urlInfo.branch ) {
  70. writeln( `Checking ${ urlInfo.name } to ${ urlInfo.branch }...` );
  71. git.checkout( repositoryPath, urlInfo.branch );
  72. }
  73. // Run `npm install` in new repository.
  74. writeln( `Running "npm install" in ${ urlInfo.name }...` );
  75. tools.npmInstall( repositoryPath );
  76. const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
  77. if ( tools.isDirectory( linkPath ) ) {
  78. writeln( `Uninstalling ${ urlInfo.name } from CKEditor5 node_modules...` );
  79. tools.npmUninstall( ckeditor5Path, urlInfo.name );
  80. }
  81. writeln( `Linking ${ linkPath } to ${ repositoryPath }...` );
  82. tools.linkDirectories( repositoryPath, linkPath );
  83. writeln( `Adding ${ urlInfo.name } dependency to CKEditor5 package.json...` );
  84. tools.updateJSONFile( path.join( ckeditor5Path, 'package.json' ), ( json ) => {
  85. json.dependencies = json.dependencies || {};
  86. json.dependencies[ urlInfo.name ] = dependency;
  87. return json;
  88. } );
  89. writeln( `Installing Git hooks in ${ urlInfo.name }...` );
  90. tools.installGitHooks( repositoryPath );
  91. } else {
  92. throw new Error( 'Please provide valid GitHub URL, NPM module name or path.' );
  93. }
  94. };