UNPKG

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