Skip to content

Commit

Permalink
feat: add download for remote dir
Browse files Browse the repository at this point in the history
feat: add list all files in remote dir
feat: add convert relate path to absolute path on remote server
docs: update docs
  • Loading branch information
maitrungduc1410 committed Dec 13, 2020
1 parent 0fc1742 commit e9ea319
Show file tree
Hide file tree
Showing 7 changed files with 1,006 additions and 17 deletions.
157 changes: 152 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ And other new features:
* [Scp file from local to remote server](#scp-file-from-local-to-remote-server)
* [Scp file from remote server to local](#Scp-file-from-remote-server-to-local)
* [Scp a directory from local to remote server](#Scp-a-directory-from-local-to-remote-server)
* [Scp a directory from remote server to local](#Scp-a-directory-from-remote-server-to-local)
* [Create a directory on remote server](#Create-a-directory-on-remote-server)
* [Check if a path exists on remote server](#Check-if-a-path-exists-on-remote-server)
* [Get stats of a path on remote server](#Get-stats-of-a-path-on-remote-server)
* [List all files in remote directory](#List-all-files-in-remote-directory)
* [Convert relative path to absolute path on remote server](#Convert-relative-path-to-absolute-path-on-remote-server)
* [Connection options](#Connection-options)

# Installation
Expand Down Expand Up @@ -163,6 +166,51 @@ async funtion test () {
test()
```

## Scp a directory from remote server to local
Using `Promise`
```js
const scp = require('node-scp')

scp({
host: 'your host',
port: 22,
username: 'username',
password: 'password',
// privateKey: fs.readFileSync('./key.pem'),
// passphrase: 'your key passphrase',
}).then(client => {
client.downloadDir('/server/path', 'local/path')
.then(response => {
client.close() // remember to close connection after you finish
})
.catch(error => {})
}).catch(e => console.log(e))
```

Using `async/await`:
```js
const scp = require('node-scp')

async funtion test () {
try {
const client = await scp({
host: 'your host',
port: 22,
username: 'username',
password: 'password',
// privateKey: fs.readFileSync('./key.pem'),
// passphrase: 'your key passphrase',
})
await client.downloadDir('./local/dir', '/server/path')
client.close() // remember to close connection after you finish
} catch (e) {
console.log(e)
}
}

test()
```

## Create a directory on remote server
Using `Promise`
```js
Expand Down Expand Up @@ -221,8 +269,9 @@ scp({
// privateKey: fs.readFileSync('./key.pem'),
// passphrase: 'your key passphrase',
}).then(client => {
client.exists('/server/path')
.then(response => {
const result = client.exists('/server/path')
.then(result => {
console.log(result)
client.close() // remember to close connection after you finish
})
.catch(error => {})
Expand All @@ -242,7 +291,8 @@ async function test() {
// privateKey: fs.readFileSync('./key.pem'),
// passphrase: 'your key passphrase',
})
await client.exists('/server/path')
const result = await client.exists('/server/path')
console.log(result)
client.close() // remember to close connection after you finish
} catch (e) {
console.log(e)
Expand All @@ -266,7 +316,8 @@ scp({
// passphrase: 'your key passphrase',
}).then(client => {
client.stat('/server/path')
.then(response => {
.then(result => {
console.log(result)
client.close() // remember to close connection after you finish
})
.catch(error => {})
Expand All @@ -287,7 +338,8 @@ async function test() {
// privateKey: fs.readFileSync('./key.pem'),
// passphrase: 'your key passphrase',
})
await client.stat('/server/path')
cosnt result = await client.stat('/server/path')
console.log(result)
client.close() // remember to close connection after you finish
} catch (e) {
console.log(e)
Expand All @@ -296,6 +348,101 @@ async function test() {

test()
```

## List all files in remote directory
Using `Promise`
```js
const scp = require('node-scp')

scp({
host: 'your host',
port: 22,
username: 'username',
password: 'password',
// privateKey: fs.readFileSync('./key.pem'),
// passphrase: 'your key passphrase',
}).then(client => {
client.list('/server/path')
.then(result => {
console.log(result)
client.close() // remember to close connection after you finish
})
.catch(error => {})
}).catch(e => console.log(e))
```

Using `async/await`:
```js
const scp = require('node-scp')

async function test() {
try {
const client = await scp({
host: 'your host',
port: 22,
username: 'username',
password: 'password',
// privateKey: fs.readFileSync('./key.pem'),
// passphrase: 'your key passphrase',
})
cosnt result = await client.list('/server/path')
console.log(result)
client.close() // remember to close connection after you finish
} catch (e) {
console.log(e)
}
}

test()
```

## Convert relative path to absolute path on remote server
Using `Promise`
```js
const scp = require('node-scp')

scp({
host: 'your host',
port: 22,
username: 'username',
password: 'password',
// privateKey: fs.readFileSync('./key.pem'),
// passphrase: 'your key passphrase',
}).then(client => {
client.realpath('/server/path')
.then(result => {
console.log(result)
client.close() // remember to close connection after you finish
})
.catch(error => {})
}).catch(e => console.log(e))
```

Using `async/await`:
```js
const scp = require('node-scp')

async function test() {
try {
const client = await scp({
host: 'your host',
port: 22,
username: 'username',
password: 'password',
// privateKey: fs.readFileSync('./key.pem'),
// passphrase: 'your key passphrase',
})
cosnt result = await client.realpath('/server/path')
console.log(result)
client.close() // remember to close connection after you finish
} catch (e) {
console.log(e)
}
}

test()
```

## Connection options
Below are available options you can pass when connecting to server:
- **host**: - *string* - Hostname or IP address of the server. **Default**: `localhost`
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "node-scp",
"title": "SCP module for NodeJS",
"description": "Lightweight, fast and secure SCP function for NodeJS",
"version": "0.0.8",
"version": "0.0.9",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"license": "MIT",
Expand Down Expand Up @@ -31,7 +31,7 @@
],
"scripts": {
"watch": "tsc -w",
"prepublish": "npm --no-git-tag-version version patch",
"prepublish": "npm --no-git-tag-version version patch && git add .",
"build": "tsc"
},
"dependencies": {
Expand Down
17 changes: 17 additions & 0 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export enum errorCode {
generic = 'ERR_GENERIC_CLIENT',
connect = 'ERR_NOT_CONNECTED',
badPath = 'ERR_BAD_PATH',
permission = 'EACCES',
notexist = 'ENOENT',
notdir = 'ENOTDIR'
}

export enum targetType {
writeFile = 1,
readFile = 2,
writeDir = 3,
readDir = 4,
readObj = 5,
writeObj = 6
}

0 comments on commit e9ea319

Please sign in to comment.