git.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 tools = require( './tools' );
  7. module.exports = {
  8. /**
  9. * Parses GitHub URL. Extracts used server, repository and branch.
  10. *
  11. * @param {String} url GitHub URL from package.json file.
  12. * @returns {Object} urlInfo
  13. * @returns {String} urlInfo.server
  14. * @returns {String} urlInfo.repository
  15. * @returns {String} urlInfo.user
  16. * @returns {String} urlInfo.name
  17. * @returns {String} urlInfo.branch
  18. */
  19. parseRepositoryUrl( url ) {
  20. const regexp = /^((?:git@|(?:http[s]?|git):\/\/)github\.com(?:\/|:))?(([\w-]+)\/([\w-]+(?:\.git)?))(?:#([\w-\/]+))?$/;
  21. const match = url.match( regexp );
  22. let server;
  23. let repository;
  24. let branch;
  25. let name;
  26. let user;
  27. if ( !match ) {
  28. return null;
  29. }
  30. server = match[ 1 ] || 'git@github.com:';
  31. repository = match[ 2 ];
  32. user = match[ 3 ];
  33. name = match[ 4 ];
  34. branch = match[ 5 ] || 'master';
  35. name = /\.git$/.test( name ) ? name.slice( 0, -4 ) : name;
  36. return {
  37. server: server,
  38. repository: repository,
  39. branch: branch,
  40. user: user,
  41. name: name
  42. };
  43. },
  44. /**
  45. * Clones repository to workspace.
  46. *
  47. * @param {Object} urlInfo Parsed URL object from {@link #parseRepositoryUrl}.
  48. * @param {String} workspacePath Path to the workspace location where repository will be cloned.
  49. */
  50. cloneRepository( urlInfo, workspacePath ) {
  51. const cloneCommands = [
  52. `cd ${ workspacePath }`,
  53. `git clone ${ urlInfo.server + urlInfo.repository }`
  54. ];
  55. tools.shExec( cloneCommands.join( ' && ' ) );
  56. },
  57. /**
  58. * Checks out branch on selected repository.
  59. *
  60. * @param {String} repositoryLocation Absolute path to repository.
  61. * @param {String} branchName Name of the branch to checkout.
  62. */
  63. checkout( repositoryLocation, branchName ) {
  64. const checkoutCommands = [
  65. `cd ${ repositoryLocation }`,
  66. `git checkout ${ branchName }`
  67. ];
  68. tools.shExec( checkoutCommands.join( ' && ' ) );
  69. },
  70. /**
  71. * Pulls specified branch from origin.
  72. *
  73. * @param {String} repositoryLocation Absolute path to repository.
  74. * @param {String} branchName Branch name to pull.
  75. */
  76. pull( repositoryLocation, branchName ) {
  77. const pullCommands = [
  78. `cd ${ repositoryLocation }`,
  79. `git pull origin ${ branchName }`
  80. ];
  81. tools.shExec( pullCommands.join( ' && ' ) );
  82. },
  83. /**
  84. * Fetch all branches from each origin on selected repository.
  85. *
  86. * @param {String} repositoryLocation
  87. */
  88. fetchAll( repositoryLocation ) {
  89. const fetchCommands = [
  90. `cd ${ repositoryLocation }`,
  91. `git fetch --all`
  92. ];
  93. tools.shExec( fetchCommands.join( ' && ' ) );
  94. },
  95. /**
  96. * Initializes new repository, adds and merges CKEditor5 boilerplate project.
  97. *
  98. * @param {String} repositoryPath Absolute path where repository should be created.
  99. */
  100. initializeRepository( repositoryPath ) {
  101. tools.shExec( `git init ${ repositoryPath }` );
  102. },
  103. /**
  104. * Returns Git status of repository stored under specified path. It runs `git status --porcelain -sb` command.
  105. *
  106. * @param {String} repositoryPath Absolute path to repository.
  107. * @returns {String} Executed command's result.
  108. */
  109. getStatus( repositoryPath ) {
  110. return tools.shExec( `cd ${ repositoryPath } && git status --porcelain -sb`, false );
  111. },
  112. /**
  113. * Creates initial commit on repository under specified path.
  114. *
  115. * @param {String} pluginName
  116. * @param {String} repositoryPath
  117. */
  118. initialCommit( pluginName, repositoryPath ) {
  119. const commitCommands = [
  120. `cd ${ repositoryPath }`,
  121. `git add .`,
  122. `git commit -m "Initial commit for ${ pluginName }."`
  123. ];
  124. tools.shExec( commitCommands.join( ' && ' ) );
  125. }
  126. };