UNPKG

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