8
0

dev-install.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 urlInfo;
  30. // First check if name is local path to repository.
  31. repositoryPath = path.isAbsolute( name ) ? name : path.resolve( name );
  32. if ( tools.isDirectory( repositoryPath ) ) {
  33. const packageName = tools.readPackageName( repositoryPath );
  34. if ( packageName ) {
  35. writeln( `Plugin located at ${ repositoryPath }.` );
  36. urlInfo = {
  37. name: packageName
  38. };
  39. }
  40. }
  41. // Check if name is repository URL.
  42. if ( !urlInfo ) {
  43. urlInfo = git.parseRepositoryUrl( name );
  44. }
  45. // Check if name is NPM package.
  46. if ( !urlInfo ) {
  47. writeln( `Not a GitHub URL. Trying to get GitHub URL from NPM package...` );
  48. const url = tools.getGitUrlFromNpm( name );
  49. if ( url ) {
  50. urlInfo = git.parseRepositoryUrl( url );
  51. }
  52. }
  53. if ( urlInfo ) {
  54. repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
  55. if ( tools.isDirectory( repositoryPath ) ) {
  56. writeln( `Directory ${ repositoryPath } already exists.` );
  57. } else {
  58. writeln( `Cloning ${ urlInfo.name } into ${ repositoryPath }...` );
  59. git.cloneRepository( urlInfo, workspaceAbsolutePath );
  60. }
  61. // Checkout to specified branch if one is provided.
  62. if ( urlInfo.branch ) {
  63. writeln( `Checking ${ urlInfo.name } to ${ urlInfo.branch }...` );
  64. git.checkout( repositoryPath, urlInfo.branch );
  65. }
  66. const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
  67. writeln( `Linking ${ linkPath } to ${ repositoryPath }...` );
  68. tools.linkDirectories( repositoryPath, linkPath );
  69. writeln( `Adding ${ urlInfo.name } dependency to CKEditor5 package.json... ` );
  70. tools.updateJSONFile( path.join( ckeditor5Path, 'package.json' ), ( json ) => {
  71. json.dependencies = json.dependencies || {};
  72. json.dependencies[ urlInfo.name ] = repositoryPath;
  73. return json;
  74. } );
  75. writeln( 'Running "npm install" in CKEditor5 repository...' );
  76. tools.npmInstall( ckeditor5Path );
  77. } else {
  78. throw new Error( 'Please provide valid GitHub URL or npm module name.' );
  79. }
  80. };