8
0

dev.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. var tools = require( './utils/tools' );
  7. var git = require( './utils/git' );
  8. var path = require( 'path' );
  9. var ckeditor5Path = process.cwd();
  10. module.exports = grunt => {
  11. const packageJSON = grunt.config.data.pkg;
  12. /**
  13. * 1. Get CKEditor5 dependencies from package.json file.
  14. * 2. Check if any of the repositories are already present in the workspace.
  15. * 2.1. If repository is present in the workspace, check it out to desired branch if one is provided.
  16. * 2.2. If repository is not present in the workspace, clone it and checkout to desired branch if one is provided.
  17. * 3. Link new repository to node_modules. (do not use npm link, use standard linking instead)
  18. */
  19. grunt.registerTask( 'dev-init', function() {
  20. // Get workspace root relative path from configuration and convert it to absolute path.
  21. let options = {
  22. workspaceRoot: '..'
  23. };
  24. options = this.options( options );
  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. grunt.log.writeln( `Clonning ${ repositoryURL }...` );
  38. git.cloneRepository( urlInfo, workspaceAbsolutePath );
  39. } catch ( error ) {
  40. grunt.log.error( error );
  41. }
  42. }
  43. // Check out proper branch.
  44. try {
  45. grunt.log.writeln( `Checking out ${ repositoryURL } to ${ urlInfo.branch }...` );
  46. git.checkout( repositoryAbsolutePath, urlInfo.branch );
  47. } catch ( error ) {
  48. grunt.log.error( error );
  49. }
  50. // Link plugin.
  51. try {
  52. grunt.log.writeln( `Linking ${ repositoryURL }...` );
  53. tools.linkDirectories( repositoryAbsolutePath, path.join( ckeditor5Path, 'node_modules' , dependency ) );
  54. } catch ( error ) {
  55. grunt.log.error( error );
  56. }
  57. }
  58. } else {
  59. grunt.log.writeln( 'No CKEditor5 dependencies found in package.json file.' );
  60. }
  61. } );
  62. };