dev-install.js 3.3 KB

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