Skip to content

Commit

Permalink
Change: auto read and write file type for sftp
Browse files Browse the repository at this point in the history
  • Loading branch information
NobleMajo committed May 14, 2024
1 parent 5ef189e commit 98c254e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
42 changes: 37 additions & 5 deletions src/essentials/SftpPromiseWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,54 @@ export interface SFTPPromiseInterface {
* (Client-only)
* Reads a file in memory and returns its contents
*/
readFile(
readFile<
O extends ReadFileOptions | BufferEncoding
>(
remotePath: string,
options?: ReadFileOptions | BufferEncoding,
): Promise<Buffer>
options?: O,
): Promise<
O extends BufferEncoding ?
string :
O extends { encodeing: BufferEncoding } ?
string :
Buffer
>

/**
* (Client-only)
* Writes data to a file
*/
writeFile(remotePath: string, data: string | Buffer, options?: WriteFileOptions | BufferEncoding): Promise<void>
writeFile<
O extends WriteFileOptions | BufferEncoding,
>(
remotePath: string,
data: (
O extends BufferEncoding ?
string :
O extends { encodeing: BufferEncoding } ?
string :
Buffer
),
options?: WriteFileOptions | BufferEncoding
): Promise<void>

/**
* (Client-only)
* Appends data to a file
*/
appendFile(remotePath: string, data: string | Buffer, options?: WriteFileOptions): Promise<void>
appendFile<
O extends WriteFileOptions | BufferEncoding,
>(
remotePath: string,
data: (
O extends BufferEncoding ?
string :
O extends { encodeing: BufferEncoding } ?
string :
Buffer
),
options?: WriteFileOptions
): Promise<void>

/**
* (Client-only)
Expand Down
20 changes: 19 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,22 @@ export * from "./utils/base.js"

import { SshHost } from "./SshHost.js"

export default SshHost
export default SshHost

// or import SshHost from "hivelib" (SshHost is also the default export)

//connect
const myHost = await SshHost.connect({
host: "127.0.0.1",
//port: 22, (default 22)
//user: "root", (default root)

//password: "123456789",
// or
//privateKey: "..."
// or
//privateKeyPath:"/home/user/.ssh/id_rsa",
//passphrase: "123456789"
})

myHost.sftp.readFile("/etc/example/config.yml", "utf8")

0 comments on commit 98c254e

Please sign in to comment.