8
0

git.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 BOILERPLATE_REPOSITORY = 'git@github.com:ckeditor/ckeditor-boilerplate.git';
  8. const BOILERPLATE_BRANCH = 'ckeditor5';
  9. module.exports = {
  10. /**
  11. * Parses GitHub URL. Extracts used server, repository and branch.
  12. *
  13. * @param {String} url GitHub URL from package.json file.
  14. * @returns {Object} urlInfo
  15. * @returns {String} urlInfo.server
  16. * @returns {String} urlInfo.repository
  17. * @returns {String} urlInfo.branch
  18. */
  19. parseRepositoryUrl( url ) {
  20. const regexp = /^(git@github\.com:|https?:\/\/github.com\/)?([^#]+)(?:#)?(.*)$/;
  21. const match = url.match( regexp );
  22. let server;
  23. let repository;
  24. let branch;
  25. if ( !match ) {
  26. return null;
  27. }
  28. server = match[ 1 ] || 'https://github.com/';
  29. repository = match[ 2 ] || '';
  30. branch = match[ 3 ] || 'master';
  31. if ( !repository ) {
  32. return null;
  33. }
  34. return {
  35. server: server,
  36. repository: repository,
  37. branch: branch
  38. };
  39. },
  40. /**
  41. * Clones repository to workspace.
  42. *
  43. * @param {Object} urlInfo Parsed URL object from {@link #parseRepositoryUrl}.
  44. * @param {String} workspacePath Path to the workspace location where repository will be cloned.
  45. */
  46. cloneRepository( urlInfo, workspacePath ) {
  47. const cloneCommands = [
  48. `cd ${ workspacePath }`,
  49. `git clone ${ urlInfo.server + urlInfo.repository }`
  50. ];
  51. tools.shExec( cloneCommands.join( ' && ' ) );
  52. },
  53. /**
  54. * Checks out branch on selected repository.
  55. *
  56. * @param {String} repositoryLocation Absolute path to repository.
  57. * @param {String} branchName Name of the branch to checkout.
  58. */
  59. checkout( repositoryLocation, branchName ) {
  60. const checkoutCommands = [
  61. `cd ${ repositoryLocation }`,
  62. `git checkout ${ branchName }`
  63. ];
  64. tools.shExec( checkoutCommands.join( ' && ' ) );
  65. },
  66. pull( repositoryLocation, branchName ) {
  67. const checkoutCommands = [
  68. `cd ${ repositoryLocation }`,
  69. `git pull origin ${ branchName }`
  70. ];
  71. tools.shExec( checkoutCommands.join( ' && ' ) );
  72. },
  73. initializeRepository( repositoryPath ) {
  74. const initializeCommands = [
  75. `git init ${ repositoryPath }`,
  76. `cd ${ repositoryPath }`,
  77. `git remote add boilerplate ${ BOILERPLATE_REPOSITORY }`,
  78. `git fetch boilerplate ${ BOILERPLATE_BRANCH }`,
  79. `git merge boilerplate/${ BOILERPLATE_BRANCH }`
  80. ];
  81. tools.shExec( initializeCommands.join( ' && ' ) );
  82. },
  83. getStatus( repositoryPath ) {
  84. return tools.shExec( `cd ${ repositoryPath } && git status --porcelain -sb` ).trim();
  85. }
  86. };