|
1 | 1 | /* eslint-disable no-async-promise-executor */
|
2 | 2 | import path from 'node:path'
|
3 | 3 | import type { Client, FileEntryWithStats, SFTPWrapper, Stats } from 'ssh2'
|
| 4 | +import type { Ora } from 'ora' |
4 | 5 | import { getLoaclDirStat, readLoaclDir } from './local-fs'
|
5 | 6 |
|
6 | 7 | export function getSftp(client: Client) {
|
@@ -97,33 +98,37 @@ export function deleteWWWDirAllConetents(sftp: SFTPWrapper, wwwPath: string, fil
|
97 | 98 | })
|
98 | 99 | }
|
99 | 100 |
|
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 | + } |
124 | 109 |
|
125 |
| - Promise.all(uploadPromises) |
126 |
| - .then(resolve) |
127 |
| - .catch(reject) |
| 110 | + resolve() |
| 111 | + }) |
128 | 112 | })
|
129 | 113 | }
|
| 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