Skip to content

Commit ddd48b6

Browse files
committed
fix(ssh): must queue upload
1 parent d4dbb1f commit ddd48b6

File tree

2 files changed

+33
-28
lines changed

2 files changed

+33
-28
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ client.connect({
6767
spinner.start(`Upload files`)
6868

6969
await utilAwaitTime(800)
70-
await uploadFiles(sftp, localDir, config.wwwPath)
70+
await uploadFiles(spinner, sftp, localDir, config.wwwPath)
7171

7272
spinner.succeed('Upload success all')
7373
client.end()

src/www-fs.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-async-promise-executor */
22
import path from 'node:path'
33
import type { Client, FileEntryWithStats, SFTPWrapper, Stats } from 'ssh2'
4+
import type { Ora } from 'ora'
45
import { getLoaclDirStat, readLoaclDir } from './local-fs'
56

67
export function getSftp(client: Client) {
@@ -97,33 +98,37 @@ export function deleteWWWDirAllConetents(sftp: SFTPWrapper, wwwPath: string, fil
9798
})
9899
}
99100

100-
export function uploadFiles(sftp: SFTPWrapper, localDir: string, wwwPath: string) {
101-
return new Promise(async (resolve, reject) => {
102-
const files = await readLoaclDir(localDir)
103-
104-
const uploadPromises = files.map((file) => {
105-
const localFilePath = path.join(localDir, file)
106-
const remoteFilePath = path.posix.join(wwwPath, file)
107-
108-
return new Promise<void>(async (resolve, reject) => {
109-
const stats = await getLoaclDirStat(localFilePath)
110-
111-
if (stats.isDirectory()) {
112-
await createWWWDir(sftp, remoteFilePath)
113-
uploadFiles(sftp, localFilePath, remoteFilePath).then(() => resolve()).catch(reject)
114-
}
115-
else {
116-
sftp.fastPut(localFilePath, remoteFilePath, (err) => {
117-
if (err)
118-
throw err
119-
resolve()
120-
})
121-
}
122-
})
123-
})
101+
export function sendLocaWWWfile(sftp: SFTPWrapper, localFilePath: string, remoteFilePath: string) {
102+
return new Promise<void>((resolve) => {
103+
sftp.fastPut(localFilePath, remoteFilePath, async (err) => {
104+
if (err) {
105+
console.log(localFilePath)
106+
console.log(remoteFilePath)
107+
throw err
108+
}
124109

125-
Promise.all(uploadPromises)
126-
.then(resolve)
127-
.catch(reject)
110+
resolve()
111+
})
128112
})
129113
}
114+
115+
export async function uploadFiles(spinner: Ora, sftp: SFTPWrapper, localDir: string, wwwPath: string) {
116+
const files = await readLoaclDir(localDir)
117+
118+
for (const [index, file] of files.entries()) {
119+
const localFilePath = path.join(localDir, file)
120+
const remoteFilePath = path.posix.join(wwwPath, file)
121+
122+
const stats = await getLoaclDirStat(localFilePath)
123+
124+
if (stats.isDirectory()) {
125+
await createWWWDir(sftp, remoteFilePath)
126+
spinner.start(`Create folder ${localFilePath}`)
127+
await uploadFiles(spinner, sftp, localFilePath, remoteFilePath)
128+
}
129+
else {
130+
await sendLocaWWWfile(sftp, localFilePath, remoteFilePath)
131+
spinner.start(`Upload files [${index + 1}/${files.length}] ${localFilePath}`)
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)