@@ -37,27 +37,21 @@ function userHome() {
37
37
}
38
38
39
39
function mkFullPath ( pathToCreate ) {
40
- let joinedDirs = [ ] ;
41
- let pathToKnownHostsParts = pathToCreate . split ( path . sep )
42
- pathToKnownHostsParts = pathToKnownHostsParts . map ( ( part ) => {
43
- if ( ! part . length ) return path . sep
44
- return part ;
45
- } ) ;
46
- pathToKnownHostsParts . forEach ( ( dir ) => {
47
- joinedDirs . push ( dir ) ;
48
- let newPath = path . join . apply ( null , joinedDirs )
49
- try {
50
- fs . accessSync ( newPath ) ;
40
+ try {
41
+ fs . mkdirSync ( pathToCreate ) ;
42
+ } catch ( e ) {
43
+ if ( e . code === 'ENOENT' ) {
44
+ // Create parent dir
45
+ mkFullPath ( path . dirname ( pathToCreate ) ) ;
46
+ // And now try again
47
+ mkFullPath ( pathToCreate ) ;
51
48
return ;
52
49
}
53
- catch ( _ ) {
54
- try {
55
- fs . mkdirSync ( newPath ) ;
56
- } catch ( e ) {
57
- if ( e . code != 'EEXIST' ) throw e ;
58
- }
50
+ if ( e . code === 'EEXIST' ) {
51
+ return ;
59
52
}
60
- } ) ;
53
+ throw e ;
54
+ }
61
55
}
62
56
63
57
function loadFingerprint ( serverId , knownHostsPath , cb ) {
@@ -86,8 +80,7 @@ function storeFingerprint(serverId, knownHostsPath, fingerprint) {
86
80
try {
87
81
fs . accessSync ( knownHostsPath ) ;
88
82
} catch ( _ ) {
89
- let pathWithoutFile = knownHostsPath . split ( path . sep ) . slice ( 0 , - 1 ) . join ( path . sep ) ;
90
- mkFullPath ( pathWithoutFile ) ;
83
+ mkFullPath ( path . dirname ( knownHostsPath ) ) ;
91
84
}
92
85
fs . appendFile ( knownHostsPath , serverId + " " + fingerprint + EOL , "utf8" , ( err ) => {
93
86
if ( err ) {
0 commit comments