@@ -36,10 +36,35 @@ function userHome() {
36
36
return process . env [ ( process . platform == 'win32' ) ? 'USERPROFILE' : 'HOME' ] ;
37
37
}
38
38
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 ) ;
51
+ return ;
52
+ }
53
+ catch ( _ ) {
54
+ try {
55
+ fs . mkdirSync ( newPath ) ;
56
+ } catch ( e ) {
57
+ if ( e . code != 'EEXIST' ) throw e ;
58
+ }
59
+ }
60
+ } ) ;
61
+ }
62
+
39
63
function loadFingerprint ( serverId , knownHostsPath , cb ) {
40
- if ( ! fs . existsSync ( knownHostsPath ) ) {
41
- cb ( null ) ;
42
- return ;
64
+ try {
65
+ fs . accessSync ( knownHostsPath ) ;
66
+ } catch ( e ) {
67
+ return cb ( null )
43
68
}
44
69
let found = false ;
45
70
require ( 'readline' ) . createInterface ( {
@@ -57,6 +82,13 @@ function loadFingerprint( serverId, knownHostsPath, cb ) {
57
82
}
58
83
59
84
function storeFingerprint ( serverId , knownHostsPath , fingerprint ) {
85
+ // If file doesn't exist, create full path to it
86
+ try {
87
+ fs . accessSync ( knownHostsPath ) ;
88
+ } catch ( _ ) {
89
+ let pathWithoutFile = [ ] . concat ( knownHostsPath . split ( path . sep ) ) . slice ( 0 , - 1 ) . join ( path . sep ) ;
90
+ mkFullPath ( pathWithoutFile ) ;
91
+ }
60
92
fs . appendFile ( knownHostsPath , serverId + " " + fingerprint + EOL , "utf8" , ( err ) => {
61
93
if ( err ) {
62
94
console . log ( err ) ;
0 commit comments