1 | import EventEmitter from 'events'
|
2 |
|
3 | import { GIT_ERROR, TASK_ERROR } from './messages.js'
|
4 | import {
|
5 | ApplyEmptyCommitError,
|
6 | TaskError,
|
7 | RestoreOriginalStateError,
|
8 | GitError,
|
9 | RestoreUnstagedChangesError,
|
10 | } from './symbols.js'
|
11 |
|
12 | export const getInitialState = ({ quiet = false } = {}) => ({
|
13 | hasPartiallyStagedFiles: null,
|
14 | shouldBackup: null,
|
15 | errors: new Set([]),
|
16 | events: new EventEmitter(),
|
17 | output: [],
|
18 | quiet,
|
19 | })
|
20 |
|
21 | export const hasPartiallyStagedFiles = (ctx) => ctx.hasPartiallyStagedFiles
|
22 |
|
23 | export const applyModificationsSkipped = (ctx) => {
|
24 |
|
25 | if (!ctx.shouldBackup) return false
|
26 |
|
27 | if (ctx.errors.has(GitError)) {
|
28 | return GIT_ERROR
|
29 | }
|
30 |
|
31 | if (ctx.errors.has(TaskError)) {
|
32 | return TASK_ERROR
|
33 | }
|
34 | }
|
35 |
|
36 | export const restoreUnstagedChangesSkipped = (ctx) => {
|
37 |
|
38 | if (ctx.errors.has(GitError)) {
|
39 | return GIT_ERROR
|
40 | }
|
41 |
|
42 | if (ctx.errors.has(TaskError)) {
|
43 | return TASK_ERROR
|
44 | }
|
45 | }
|
46 |
|
47 | export const restoreOriginalStateEnabled = (ctx) =>
|
48 | ctx.shouldBackup &&
|
49 | (ctx.errors.has(TaskError) ||
|
50 | ctx.errors.has(ApplyEmptyCommitError) ||
|
51 | ctx.errors.has(RestoreUnstagedChangesError))
|
52 |
|
53 | export const restoreOriginalStateSkipped = (ctx) => {
|
54 |
|
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 |
|
64 | export const cleanupEnabled = (ctx) => ctx.shouldBackup
|
65 |
|
66 | export const cleanupSkipped = (ctx) => {
|
67 |
|
68 | if (
|
69 | ctx.errors.has(GitError) &&
|
70 | !ctx.errors.has(ApplyEmptyCommitError) &&
|
71 | !ctx.errors.has(RestoreUnstagedChangesError)
|
72 | ) {
|
73 | return GIT_ERROR
|
74 | }
|
75 |
|
76 | if (ctx.errors.has(RestoreOriginalStateError)) {
|
77 | return GIT_ERROR
|
78 | }
|
79 | }
|