update.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 path = require( 'path' );
  7. const { tools, workspace, git, log } = require( 'ckeditor5-dev-utils' );
  8. /**
  9. * 1. Fetch all branches from each origin in main CKEditor 5 repository.
  10. * 2. Get CKEditor 5 dependencies from package.json in main CKEditor 5 repository.
  11. * 3. If dependency's repository is already cloned in workspace:
  12. * 3.1. Fetch all branches from each origin.
  13. * 3.2. Checkout to specified branch.
  14. * 3.3. Pull changes to that branch.
  15. * 3.4. if --npm-update was specified run npm update --dev in that repository.
  16. * 3.5. Recreate symbolic link between repo and main node_modules.
  17. * 4. If dependency's repository is not cloned yet - run gulp install on this dependency.
  18. * 5. Remove symbolic links to dependencies that are not used in current package.json configuration.
  19. * 6. if --npm-update was specified run npm update --dev in main CKEditor 5 repository.
  20. *
  21. * @param {Function} installTask Install task to use on each dependency that is missing from workspace.
  22. * @param {String} ckeditor5Path Path to main CKEditor5 repository.
  23. * @param {Object} packageJSON Parsed package.json file from CKEditor5 repository.
  24. * @param {String} workspaceRoot Relative path to workspace root.
  25. * @param {Boolean} runNpmUpdate When set to true `npm update` will be executed inside each plugin repository
  26. * and inside CKEditor 5 repository.
  27. */
  28. module.exports = ( installTask, ckeditor5Path, packageJSON, workspaceRoot, runNpmUpdate ) => {
  29. const workspaceAbsolutePath = path.join( ckeditor5Path, workspaceRoot );
  30. // Fetch main repository
  31. log.out( `Fetching branches from ${ packageJSON.name }...` );
  32. git.fetchAll( ckeditor5Path );
  33. // Get all CKEditor dependencies from package.json.
  34. const dependencies = workspace.getDependencies( packageJSON.dependencies );
  35. if ( dependencies ) {
  36. const directories = workspace.getDirectories( workspaceAbsolutePath );
  37. for ( let dependency in dependencies ) {
  38. const repositoryURL = dependencies[ dependency ];
  39. const urlInfo = git.parseRepositoryUrl( repositoryURL );
  40. const repositoryAbsolutePath = path.join( workspaceAbsolutePath, dependency );
  41. // Check if repository's directory already exists.
  42. if ( directories.indexOf( urlInfo.name ) > -1 ) {
  43. log.out( `Fetching branches from ${ urlInfo.name }...` );
  44. git.fetchAll( repositoryAbsolutePath );
  45. log.out( `Checking out ${ urlInfo.name } to ${ urlInfo.branch }...` );
  46. git.checkout( repositoryAbsolutePath, urlInfo.branch );
  47. log.out( `Pulling changes to ${ urlInfo.name }...` );
  48. git.pull( repositoryAbsolutePath, urlInfo.branch );
  49. if ( runNpmUpdate ) {
  50. log.out( `Running "npm update" in ${ urlInfo.name }...` );
  51. tools.npmUpdate( repositoryAbsolutePath );
  52. }
  53. try {
  54. log.out( `Linking ${ repositoryURL }...` );
  55. tools.linkDirectories( repositoryAbsolutePath, path.join( ckeditor5Path, 'node_modules', dependency ) );
  56. } catch ( error ) {
  57. log.err( error );
  58. }
  59. } else {
  60. // Directory does not exits in workspace - install it.
  61. installTask( ckeditor5Path, workspaceRoot, repositoryURL );
  62. }
  63. }
  64. if ( runNpmUpdate ) {
  65. log.out( `Running "npm update" in CKEditor5 repository...` );
  66. tools.npmUpdate( ckeditor5Path );
  67. }
  68. } else {
  69. log.out( 'No CKEditor5 dependencies found in package.json file.' );
  70. }
  71. // Remove symlinks not used in this configuration.
  72. const nodeModulesPath = path.join( ckeditor5Path, 'node_modules' );
  73. const symlinks = workspace.getSymlinks( nodeModulesPath );
  74. symlinks
  75. .filter( dir => typeof dependencies[ dir ] == 'undefined' )
  76. .forEach( dir => {
  77. log.out( `Removing symbolic link to ${ dir }.` );
  78. tools.removeSymlink( path.join( nodeModulesPath, dir ) );
  79. } );
  80. };