UNPKG

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