11import * as fse from 'fs-extra'
2+ // import * as fs from 'fs'
23import path from 'path'
34import SftpClient from 'ssh2-sftp-client'
45import NodeSsh from 'node-ssh'
56import normalizePath from 'normalize-path'
67import Model from '../../model'
78
9+ type sftpConnectConfig = {
10+ host : string ;
11+ port : number ;
12+ type ?: string ;
13+ username : string ;
14+ password ?: string ;
15+ privateKey ?: string | Buffer ;
16+ }
17+
818export default class SftpDeploy extends Model {
919 // connect: SftpClient
1020 constructor ( appInstance : any ) {
@@ -22,11 +32,24 @@ export default class SftpDeploy extends Model {
2232 const client = new SftpClient ( )
2333
2434 const { setting } = this . db
25- const connectConfig = {
35+
36+ const connectConfig : sftpConnectConfig = {
2637 host : setting . server ,
2738 port : Number ( setting . port ) ,
2839 username : setting . username ,
29- password : setting . password ,
40+ }
41+
42+ if ( setting . privateKey ) {
43+ try {
44+ connectConfig . privateKey = fse . readFileSync ( setting . privateKey )
45+ } catch ( e ) {
46+ console . error ( 'SFTP Test Remote Error: ' , e . message )
47+ result . success = false
48+ result . message = e . message
49+ return result
50+ }
51+ } else {
52+ connectConfig . password = setting . password
3053 }
3154
3255 const testFilename = 'gridea.txt'
@@ -58,7 +81,7 @@ export default class SftpDeploy extends Model {
5881 } finally {
5982 await client . end ( )
6083 }
61-
84+
6285 return result
6386 }
6487
@@ -71,12 +94,19 @@ export default class SftpDeploy extends Model {
7194 const client = new NodeSsh ( )
7295
7396 const { setting } = this . db
74- const connectConfig = {
97+
98+ const connectConfig : sftpConnectConfig = {
7599 host : setting . server ,
76100 port : Number ( setting . port ) ,
77- username : setting . username ,
78- password : setting . password ,
79101 type : 'sftp' ,
102+ username : setting . username ,
103+ }
104+
105+ // node-ssh: privateKey is path string.
106+ if ( setting . privateKey ) {
107+ connectConfig . privateKey = setting . privateKey
108+ } else {
109+ connectConfig . password = setting . password
80110 }
81111
82112 const localPath = normalizePath ( path . join ( this . appDir , 'output' ) )
0 commit comments