dev-init.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 tools = require( './tools' );
  7. const git = require( './git' );
  8. const path = require( 'path' );
  9. /**
  10. * 1. Get CKEditor5 dependencies from package.json file.
  11. * 2. Check if any of the repositories are already present in the workspace.
  12. * 2.1. If repository is present in the workspace, check it out to desired branch if one is provided.
  13. * 2.2. If repository is not present in the workspace, clone it and checkout to desired branch if one is provided.
  14. * 3. Pull changes from remote branch.
  15. * 4. Link each new repository to node_modules. (do not use npm link, use standard linking instead)
  16. * 5. Run `npm install` in each repository.
  17. * 6. Install Git hooks in each repository.
  18. *
  19. * @param {String} ckeditor5Path Path to main CKEditor5 repository.
  20. * @param {Object} packageJSON Parsed package.json file from CKEditor5 repository.
  21. * @param {Object} options grunt options.
  22. * @param {Function} writeln Function for log output.
  23. * @param {Function} writeError Function of error output
  24. */
  25. module.exports = ( ckeditor5Path, packageJSON, options, writeln, writeError ) => {
  26. const workspaceAbsolutePath = path.join( ckeditor5Path, options.workspaceRoot );
  27. // Get all CKEditor dependencies from package.json.
  28. const dependencies = tools.getCKEditorDependencies( packageJSON.dependencies );
  29. if ( dependencies ) {
  30. const directories = tools.getCKE5Directories( workspaceAbsolutePath );
  31. for ( let dependency in dependencies ) {
  32. const repositoryURL = dependencies[ dependency ];
  33. const urlInfo = git.parseRepositoryUrl( repositoryURL );
  34. const repositoryAbsolutePath = path.join( workspaceAbsolutePath, dependency );
  35. // Check if repository's directory already exists.
  36. try {
  37. if ( directories.indexOf( dependency ) === -1 ) {
  38. writeln( `Clonning ${ repositoryURL }...` );
  39. git.cloneRepository( urlInfo, workspaceAbsolutePath );
  40. }
  41. // Check out proper branch.
  42. writeln( `Checking out ${ repositoryURL } to ${ urlInfo.branch }...` );
  43. git.checkout( repositoryAbsolutePath, urlInfo.branch );
  44. writeln( `Pulling changes to ${ repositoryURL } ${ urlInfo.branch }...` );
  45. git.pull( repositoryAbsolutePath, urlInfo.branch );
  46. writeln( `Linking ${ repositoryURL }...` );
  47. tools.linkDirectories( repositoryAbsolutePath, path.join( ckeditor5Path, 'node_modules' , dependency ) );
  48. writeln( `Running npm install in ${ repositoryURL }.` );
  49. tools.npmInstall( repositoryAbsolutePath );
  50. writeln( `Installing Git hooks in ${ repositoryURL }.` );
  51. tools.installGitHooks( repositoryAbsolutePath );
  52. } catch ( error ) {
  53. writeError( error );
  54. }
  55. }
  56. } else {
  57. writeln( 'No CKEditor5 dependencies found in package.json file.' );
  58. }
  59. };