Skip to content

Commit 56f836b

Browse files
authored
Merge pull request #3 from CurryPaste/master
fix:删除文件夹修复
2 parents afb8fdf + 4a43611 commit 56f836b

1 file changed

Lines changed: 34 additions & 21 deletions

File tree

index.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module.exports = (api, projectOptions) => {
6262
// 目录
6363
const child_filepath = `${filepath}${file.name}/`;
6464
readFiles(child_filepath)
65-
// resolve();
65+
resolve();
6666
}
6767
})(file1);
6868
});
@@ -79,7 +79,7 @@ module.exports = (api, projectOptions) => {
7979
if (err) {
8080
console.log(err);
8181
}
82-
ftp.end();
82+
// ftp.end();
8383
pb.render({ completed: index, total: fileList.length });
8484
resolve();
8585
});
@@ -121,8 +121,9 @@ module.exports = (api, projectOptions) => {
121121
} else if (file.type === 'd') {
122122
// this is directory
123123
const delPath = `${filePath}/${file.name}`
124-
deleteFiles(delPath)
125-
resolve()
124+
deleteFiles(delPath).then(()=>{
125+
resolve()
126+
})
126127
} else {
127128
// i dont know, symlink?
128129
const delPath = `${filePath}/${file.name}`
@@ -170,7 +171,10 @@ module.exports = (api, projectOptions) => {
170171

171172
let arr = [];
172173
ftp.list(removePath, (rErr, rList)=>{
173-
if (rErr) rej(err);
174+
if (rErr) {
175+
console.log(rErr, '--------------')
176+
rej(err);
177+
}
174178
if (rList && rList.length > 0) {
175179
arr = rList.filter(rf=>{
176180
return removeArr.includes(rf.name) && rf.type === 'd'
@@ -180,27 +184,33 @@ module.exports = (api, projectOptions) => {
180184
if (arr && arr.length > 0) {
181185
for (let i=0; i<arr.length; i++) {
182186
const path = `${remoteFtpPath}${arr[i]}`
183-
// console.log(`${path}---path`)
184187
actionList.push(deleteFiles(path))
185-
// const actionDir = () => {
186-
// return new Promise(resD => {
187-
// ftp.rmdir(path, true, (errD)=>{
188-
// if (errD) console.log(errD)
189-
// resD()
190-
// })
191-
// })
192-
// }
193-
// actionDirectory.push(actionDir())
194188
}
195189
} else {
196190
rej('请检测参数')
197191
}
198192
Promise.all(actionList).then(()=>{
199-
console.log('文件删除完毕,开始上传...')
200-
res('删除完毕')
201-
// Promise.all(actionDirectory).then(()=>{
202-
// res('文件夹也删除完了')
203-
// })
193+
console.log('文件删除完毕,开始删除文件夹...')
194+
/** 不能在上面的for里push删除dir的promise,因为那个时候文件还没删除完
195+
* 虽然promise是pending状态,但其实那个时候的promise保存的是有文件的删除方法(rmdir一定会报错)
196+
* 所以在删除所有文件后,再去定义新的promise-删除文件夹
197+
*/
198+
for (let i=0; i<arr.length; i++) {
199+
const path = `${remoteFtpPath}${arr[i]}`
200+
const actionDir = () => {
201+
return new Promise(resD => {
202+
ftp.rmdir(path, true, (errD)=>{
203+
if (errD) console.log(errD,'??????')
204+
console.log(`${path}文件夹删除完毕`)
205+
resD()
206+
})
207+
})
208+
}
209+
actionDirectory.push(actionDir())
210+
}
211+
Promise.all(actionDirectory).then(()=>{
212+
res('文件夹删除完毕')
213+
})
204214
}).catch(err=>{
205215
console.log('this is removeFile catch————————\n',err)
206216
rej(err)
@@ -219,13 +229,16 @@ module.exports = (api, projectOptions) => {
219229
if (DelArrPath) {
220230
db = new ProgressBar('正在删除...', 0)
221231
await removeFile(remoteFtpPath, DelArrPath)
232+
console.log('删除完毕,开始上传...')
222233
}
223234
} catch (delErr) {
224235
console.log('删除方法有异常\n', delErr)
225236
}
226237
fs.readdir(dirPath, { withFileTypes: true }, (err, files) => {
227238
pb = new ProgressBar("正在上传...", 0);
228-
readFiles(dirPath)
239+
readFiles(dirPath).finally(()=>{
240+
ftp.end();
241+
})
229242
})
230243
});
231244

0 commit comments

Comments
 (0)