0cf0f3557296f9cc4c98837552fa297e8b938d53eb256a4d8919c57b0edb9fe8cabf3784ca8ee0df58d842a6ba00afcdc59b9bcb0d97cca2b68681c52a1ba1 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict'
  2. const { GIT_ERROR, TASK_ERROR } = require('./messages')
  3. const {
  4. ApplyEmptyCommitError,
  5. TaskError,
  6. RestoreOriginalStateError,
  7. GitError,
  8. RestoreUnstagedChangesError,
  9. } = require('./symbols')
  10. const getInitialState = ({ quiet = false } = {}) => ({
  11. hasPartiallyStagedFiles: null,
  12. shouldBackup: null,
  13. errors: new Set([]),
  14. output: [],
  15. quiet,
  16. })
  17. const hasPartiallyStagedFiles = (ctx) => ctx.hasPartiallyStagedFiles
  18. const applyModificationsSkipped = (ctx) => {
  19. // Always apply back unstaged modifications when skipping backup
  20. if (!ctx.shouldBackup) return false
  21. // Should be skipped in case of git errors
  22. if (ctx.errors.has(GitError)) {
  23. return GIT_ERROR
  24. }
  25. // Should be skipped when tasks fail
  26. if (ctx.errors.has(TaskError)) {
  27. return TASK_ERROR
  28. }
  29. }
  30. const restoreUnstagedChangesSkipped = (ctx) => {
  31. // Should be skipped in case of git errors
  32. if (ctx.errors.has(GitError)) {
  33. return GIT_ERROR
  34. }
  35. // Should be skipped when tasks fail
  36. if (ctx.errors.has(TaskError)) {
  37. return TASK_ERROR
  38. }
  39. }
  40. const restoreOriginalStateEnabled = (ctx) =>
  41. ctx.shouldBackup &&
  42. (ctx.errors.has(TaskError) ||
  43. ctx.errors.has(ApplyEmptyCommitError) ||
  44. ctx.errors.has(RestoreUnstagedChangesError))
  45. const restoreOriginalStateSkipped = (ctx) => {
  46. // Should be skipped in case of unknown git errors
  47. if (
  48. ctx.errors.has(GitError) &&
  49. !ctx.errors.has(ApplyEmptyCommitError) &&
  50. !ctx.errors.has(RestoreUnstagedChangesError)
  51. ) {
  52. return GIT_ERROR
  53. }
  54. }
  55. const cleanupEnabled = (ctx) => ctx.shouldBackup
  56. const cleanupSkipped = (ctx) => {
  57. // Should be skipped in case of unknown git errors
  58. if (
  59. ctx.errors.has(GitError) &&
  60. !ctx.errors.has(ApplyEmptyCommitError) &&
  61. !ctx.errors.has(RestoreUnstagedChangesError)
  62. ) {
  63. return GIT_ERROR
  64. }
  65. // Should be skipped when reverting to original state fails
  66. if (ctx.errors.has(RestoreOriginalStateError)) {
  67. return GIT_ERROR
  68. }
  69. }
  70. module.exports = {
  71. getInitialState,
  72. hasPartiallyStagedFiles,
  73. applyModificationsSkipped,
  74. restoreUnstagedChangesSkipped,
  75. restoreOriginalStateEnabled,
  76. restoreOriginalStateSkipped,
  77. cleanupEnabled,
  78. cleanupSkipped,
  79. }