11import simpleGit , { SimpleGit } from "simple-git" ;
22import chain from "@softwareventures/chain" ;
33import { concat } from "@softwareventures/array" ;
4+ import { mapFn as mapAsyncFn } from "../collections/async-iterable" ;
45import { FsStage , InsertResult } from "../fs-stage/fs-stage" ;
56import {
67 bindAsyncResultFn ,
@@ -15,7 +16,6 @@ import {
1516import { emptyDirectory } from "../fs-stage/directory" ;
1617import { updateCopyrightYear } from "../license/update-copyright-year" ;
1718import { commit , CommitFailureReason } from "../fs-stage/commit" ;
18- import { excludeNull , mapFn } from "../collections/async-iterable" ;
1919import { addMissingLicense } from "../license/add-missing-license" ;
2020import { YarnFixFailureReason } from "../yarn/fix" ;
2121import { applyCodeStyle } from "../yarn/apply-code-style" ;
@@ -46,14 +46,8 @@ export type UpdateStepFailureReason = YarnFixFailureReason | PrettierFixFailureR
4646export async function updateProject ( project : Project ) : Promise < UpdateResult > {
4747 const git = simpleGit ( project . path ) ;
4848
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 ) ) )
5751 . map ( combineAsyncResults ) . value ;
5852}
5953
@@ -66,40 +60,59 @@ export function gitNotClean(path: string): GitNotClean {
6660 return { type : "git-not-clean" , path} ;
6761}
6862
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 > {
7067 return async update =>
7168 git
7269 . status ( )
7370 . 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 ) )
9090 )
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 ) )
9797 )
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+ }
105118}
0 commit comments