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