dev-install.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <npm_name|git_hub_url>
  12. *
  13. * It performs follwing steps:
  14. * 1. Get GitHub URL from NPM if module name is provided.
  15. * 2. Checks if repository is cloned already. If not - clones it.
  16. * 3. Checks out plugin repository to provided branch (`master` if no branch is specified).
  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. let urlInfo = git.parseRepositoryUrl( name );
  28. const workspaceAbsolutePath = path.join( ckeditor5Path, workspaceRoot );
  29. let repositoryPath;
  30. if ( !urlInfo ) {
  31. writeln( `Not a GitHub URL. Trying to get GitHub URL from npm package...` );
  32. const url = tools.getGitUrlFromNpm( name );
  33. if ( url ) {
  34. urlInfo = git.parseRepositoryUrl( url );
  35. }
  36. }
  37. if ( urlInfo ) {
  38. repositoryPath = path.join( workspaceAbsolutePath, urlInfo.name );
  39. if ( tools.isDirectory( repositoryPath ) ) {
  40. writeln( `Directory ${ repositoryPath } already exists.` );
  41. } else {
  42. writeln( `Cloning ${ urlInfo.name } into ${ repositoryPath }... ` );
  43. git.cloneRepository( urlInfo, workspaceAbsolutePath );
  44. }
  45. writeln( `Checking ${ urlInfo.name } to ${ urlInfo.branch }...` );
  46. git.checkout( repositoryPath, urlInfo.branch );
  47. const linkPath = path.join( ckeditor5Path, 'node_modules', urlInfo.name );
  48. writeln( `Linking ${ linkPath } to ${ repositoryPath }...` );
  49. tools.linkDirectories( repositoryPath, linkPath );
  50. writeln( `Adding ${ urlInfo.name } dependency to CKEditor5 package.json... ` );
  51. tools.updateJSONFile( path.join( ckeditor5Path, 'package.json' ), ( json ) => {
  52. json.dependencies = json.dependencies || {};
  53. json.dependencies[ urlInfo.name ] = repositoryPath;
  54. return json;
  55. } );
  56. writeln( 'Running "npm install" in CKEditor5 repository...' );
  57. tools.npmInstall( ckeditor5Path );
  58. } else {
  59. throw new Error( 'Please provide valid GitHub URL or npm module name.' );
  60. }
  61. };