@@ -2,7 +2,7 @@ import { spawn } from 'child_process';
2
2
3
3
import path from 'path' ;
4
4
5
- import { write , open } from 'fs' ;
5
+ import { writeFileSync , openSync , existsSync , closeSync } from 'fs' ;
6
6
7
7
import dedent from 'dedent' ;
8
8
@@ -51,18 +51,44 @@ function commit (sh, repoPath, message, options, done) {
51
51
} ) ;
52
52
} else {
53
53
const commitFilePath = path . join ( repoPath , '/.git/COMMIT_EDITMSG' ) ;
54
- open ( commitFilePath , 'w' , function ( err , fd ) {
55
- if ( err ) {
56
- if ( called ) return ;
57
- called = true ;
58
- done ( err ) ;
59
- } else {
60
- write ( fd , dedent ( message ) , 0 , function ( err ) {
61
- if ( called ) return ;
62
- called = true ;
63
- done ( err ) ;
64
- } ) ;
54
+ const commitFileExists = existsSync ( commitFilePath ) ;
55
+ const readWriteFlag = commitFileExists ? 'r+' : 'w' ;
56
+ console . log ( 'mode:' , readWriteFlag ) ;
57
+ try {
58
+ console . log ( 'opening in w mode' ) ;
59
+ const fd = openSync ( commitFilePath , 'w' ) ;
60
+ try {
61
+ writeFileSync ( fd , dedent ( message ) ) ;
62
+ console . log ( 'successfully write to the commit file!' ) ;
63
+ done ( null ) ;
64
+ } catch ( e ) {
65
+ console . log ( 'error on writeSync:' , e ) ;
66
+ done ( e ) ;
67
+ } finally {
68
+ closeSync ( fd ) ;
65
69
}
66
- } ) ;
70
+ } catch ( e ) {
71
+ // windows doesn't allow opening existing hidden files
72
+ // in 'w' mode... but it does let you do 'r+'! (maybe)
73
+ console . log ( 'error on w mode open:' , e ) ;
74
+ if ( e ) {
75
+ try {
76
+ console . log ( 'opening in r+ mode' ) ;
77
+ const fd = openSync ( commitFilePath , 'r+' ) ;
78
+ try {
79
+ writeFileSync ( fd , dedent ( message ) ) ;
80
+ console . log ( 'successfully wrote to the commit file!' ) ;
81
+ done ( null ) ;
82
+ } catch ( e ) {
83
+ console . log ( 'error on writeSync:' , e ) ;
84
+ done ( e ) ;
85
+ } finally {
86
+ closeSync ( fd ) ;
87
+ }
88
+ } catch ( e ) {
89
+ console . log ( 'all attempts to write to .git have failed. error on r+ mode open:' , e ) ;
90
+ }
91
+ }
92
+ }
67
93
}
68
94
}
0 commit comments