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