1
1
import simpleGit , { SimpleGit } from "simple-git" ;
2
2
import chain from "@softwareventures/chain" ;
3
3
import { concat } from "@softwareventures/array" ;
4
+ import { mapFn as mapAsyncFn } from "../collections/async-iterable" ;
4
5
import { FsStage , InsertResult } from "../fs-stage/fs-stage" ;
5
6
import {
6
7
bindAsyncResultFn ,
@@ -15,7 +16,6 @@ import {
15
16
import { emptyDirectory } from "../fs-stage/directory" ;
16
17
import { updateCopyrightYear } from "../license/update-copyright-year" ;
17
18
import { commit , CommitFailureReason } from "../fs-stage/commit" ;
18
- import { excludeNull , mapFn } from "../collections/async-iterable" ;
19
19
import { addMissingLicense } from "../license/add-missing-license" ;
20
20
import { YarnFixFailureReason } from "../yarn/fix" ;
21
21
import { applyCodeStyle } from "../yarn/apply-code-style" ;
@@ -46,14 +46,8 @@ export type UpdateStepFailureReason = YarnFixFailureReason | PrettierFixFailureR
46
46
export async function updateProject ( project : Project ) : Promise < UpdateResult > {
47
47
const git = simpleGit ( project . path ) ;
48
48
49
- return chain ( [
50
- updateFixScript ( project ) ,
51
- applyCodeStyle ( project ) ,
52
- updateCopyrightYear ( project ) ,
53
- addMissingLicense ( project )
54
- ] )
55
- . map ( excludeNull )
56
- . map ( mapFn ( step ( project , git ) ) )
49
+ return chain ( [ updateFixScript , applyCodeStyle , updateCopyrightYear , addMissingLicense ] )
50
+ . map ( mapAsyncFn ( step ( project , git ) ) )
57
51
. map ( combineAsyncResults ) . value ;
58
52
}
59
53
@@ -66,40 +60,59 @@ export function gitNotClean(path: string): GitNotClean {
66
60
return { type : "git-not-clean" , path} ;
67
61
}
68
62
69
- function step ( project : Project , git : SimpleGit ) : ( update : Update ) => Promise < UpdateResult > {
63
+ function step (
64
+ project : Project ,
65
+ git : SimpleGit
66
+ ) : ( update : ( project : Project ) => Promise < Update | null > ) => Promise < UpdateResult > {
70
67
return async update =>
71
68
git
72
69
. status ( )
73
70
. then ( status => ( status . isClean ( ) ? success ( ) : failure ( [ gitNotClean ( project . path ) ] ) ) )
74
- . then (
75
- bindAsyncResultFn < GitNotClean , UpdateFailureReason > ( async ( ) =>
76
- update . type === "fs-stage-update"
77
- ? update
78
- . apply ( { root : emptyDirectory , overwrite : true } )
79
- . then ( throwFailureFn ( "Internal error creating update file stage" ) )
80
- . then ( async stage => commit ( project . path , stage ) )
81
- : update . apply ( )
82
- )
83
- )
84
- . then ( mapAsyncResultFn ( async ( ) => git . status ( ) ) )
85
- . then ( mapResultFn ( status => concat ( [ status . modified , status . not_added ] ) ) )
86
- . then (
87
- bindAsyncResultFn ( async files =>
88
- prettierFixFilesIfAvailable ( project , files ) . then ( mapResultFn ( ( ) => files ) )
89
- )
71
+ . then ( mapAsyncResultFn ( async ( ) => update ( project ) ) )
72
+ . then ( bindAsyncResultFn ( async update => commitUpdate ( project , git , update ) ) ) ;
73
+ }
74
+
75
+ async function commitUpdate (
76
+ project : Project ,
77
+ git : SimpleGit ,
78
+ update : Update | null
79
+ ) : Promise < UpdateResult > {
80
+ if ( update == null ) {
81
+ return success ( ) ;
82
+ }
83
+
84
+ return writeUpdate ( project , update )
85
+ . then ( mapAsyncResultFn ( async ( ) => git . status ( ) ) )
86
+ . then ( mapResultFn ( status => concat ( [ status . modified , status . not_added ] ) ) )
87
+ . then (
88
+ bindAsyncResultFn ( async files =>
89
+ prettierFixFilesIfAvailable ( project , files ) . then ( mapResultFn ( ( ) => files ) )
90
90
)
91
- . then (
92
- mapAsyncResultFn ( async files =>
93
- files . length === 0
94
- ? undefined
95
- : git . add ( files ) . then ( async ( ) => git . commit ( update . log ) )
96
- )
91
+ )
92
+ . then (
93
+ mapAsyncResultFn ( async files =>
94
+ files . length === 0
95
+ ? undefined
96
+ : git . add ( files ) . then ( async ( ) => git . commit ( update . log ) )
97
97
)
98
- . then (
99
- mapResultFn ( commitResult => {
100
- if ( commitResult != null ) {
101
- console . log ( `Applied update: ${ update . log } ` ) ;
102
- }
103
- } )
104
- ) ;
98
+ )
99
+ . then (
100
+ mapResultFn ( commitResult => {
101
+ if ( commitResult != null ) {
102
+ console . log ( `Applied update: ${ update . log } ` ) ;
103
+ }
104
+ } )
105
+ ) ;
106
+ }
107
+
108
+ async function writeUpdate ( project : Project , update : Update ) : Promise < UpdateResult > {
109
+ switch ( update . type ) {
110
+ case "fs-stage-update" :
111
+ return update
112
+ . apply ( { root : emptyDirectory , overwrite : true } )
113
+ . then ( throwFailureFn ( "Internal error creating update file stage" ) )
114
+ . then ( async stage => commit ( project . path , stage ) ) ;
115
+ case "direct-update" :
116
+ return update . apply ( ) ;
117
+ }
105
118
}
0 commit comments