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