Skip to content

Commit 6ef5147

Browse files
committed
增加文件夹删除
1 parent e0ce953 commit 6ef5147

84 files changed

Lines changed: 234 additions & 10458 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

index.js

Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@ module.exports = (api, projectOptions) => {
1313
console.log("未设置ftp地址或项目路径")
1414
return;
1515
}
16+
if (!projectOptions.pluginOptions.ftp.delArr) {
17+
console.log("未设置删除文件夹");
18+
}
1619
// ...
1720
// const ora = require("ora");
1821
const ftp = new Client();
1922
const connectionProperties = {
20-
host: projectOptions.pluginOptions.ftp.host
23+
host: projectOptions.pluginOptions.ftp.host // '192.168.1.101' //
2124
};
22-
const remoteFtpPath = projectOptions.pluginOptions.ftp.remoteFtpPath;
25+
const DelArrPath = projectOptions.pluginOptions.ftp.delArr || false;
26+
const remoteFtpPath = projectOptions.pluginOptions.ftp.remoteFtpPath // '/F/ftpserve-home' //
2327
const dirPath = `${process.cwd()}/dist/`;
2428
// const spinner = ora("正在和服务器同步")
2529
// eslint-disable-next-line no-unused-vars
2630
let pb = null;
31+
let db = null;
2732
const ACTIONS = []
2833
/** 递归写入文件 */
2934
function readFiles(filepath) {
@@ -70,7 +75,6 @@ module.exports = (api, projectOptions) => {
7075
const action = () => {
7176
return new Promise(resolve => {
7277
const element = fileList[index];
73-
7478
ftp.mkdir(element.dir, false, (() => {
7579
ftp.put(element.data, element.dirName, (err2) => {
7680
if (err) {
@@ -95,16 +99,134 @@ module.exports = (api, projectOptions) => {
9599
});
96100
});
97101
}
98-
// 递归读取文件
99-
ftp.connect(connectionProperties);
100102

101-
ftp.on("ready", () => {
103+
/** 删除文件 */
104+
const deleteFiles = (filePath)=> {
105+
return new Promise((res, rej) => {
106+
const actions = []; // 读取server文件action
107+
const fileList = []; // 需要处理的files
108+
const actionDel = []; // 执行操作action
109+
ftp.list(filePath, (err, files) => {
110+
if (err) rej(err);
111+
if ( files && files.length > 0) {
112+
for (let i=0; i<files.length; i++) {
113+
const fileIndex = files[i];
114+
const action = () => {
115+
return new Promise(resolve => {
116+
( (file) => {
117+
if (file.type === '-') {
118+
// this is file
119+
const delPath = `${filePath}/${file.name}`
120+
fileList.push({...file, delPath})
121+
resolve()
122+
} else if (file.type === 'd') {
123+
// this is directory
124+
const delPath = `${filePath}/${file.name}`
125+
deleteFiles(delPath)
126+
resolve()
127+
} else {
128+
// i dont know, symlink?
129+
const delPath = `${filePath}/${file.name}`
130+
fileList.push({...file, delPath})
131+
resolve()
132+
}
133+
})(fileIndex)
134+
})
135+
}
136+
actions.push(action())
137+
}
138+
// 或得到了所有要删除的文件,开始删除
139+
Promise.all(actions).then(()=>{
140+
for(let i=0; i<fileList.length; i++) {
141+
const actionD = () => {
142+
return new Promise(resolve => {
143+
const f = fileList[i];
144+
ftp.delete(f.delPath, (de)=>{
145+
if (de) console.log(de)
146+
})
147+
// ftp.end();
148+
db.render({completed: i, total: fileList.length})
149+
resolve()
150+
})
151+
}
152+
actionDel.push(actionD())
153+
}
154+
Promise.all(actionDel).then(()=>{
155+
db.render({completed: fileList.length, total: fileList.length})
156+
res(`${filePath}删除完了`)
157+
console.log(`--${filePath}删除完毕`);
158+
})
159+
})
160+
} else {
161+
res(`${filePath}无内容`)
162+
}
163+
})
164+
})
165+
}
166+
/** 清空文件(夹) */
167+
async function removeFile(removePath, removeArr = ['js', 'css']) {
168+
return new Promise( (res, rej) => {
169+
const actionList = []
170+
const actionDirectory = []
171+
172+
let arr = [];
173+
ftp.list(removePath, (rErr, rList)=>{
174+
if (rErr) rej(err);
175+
if (rList && rList.length > 0) {
176+
arr = rList.filter(rf=>{
177+
return removeArr.includes(rf.name) && rf.type === 'd'
178+
}).map(rm=>{
179+
return rm.name
180+
})
181+
if (arr && arr.length > 0) {
182+
for (let i=0; i<arr.length; i++) {
183+
const path = `${remoteFtpPath}${arr[i]}`
184+
// console.log(`${path}---path`)
185+
actionList.push(deleteFiles(path))
186+
// const actionDir = () => {
187+
// return new Promise(resD => {
188+
// ftp.rmdir(path, true, (errD)=>{
189+
// if (errD) console.log(errD)
190+
// resD()
191+
// })
192+
// })
193+
// }
194+
// actionDirectory.push(actionDir())
195+
}
196+
} else {
197+
rej('请检测参数')
198+
}
199+
Promise.all(actionList).then(()=>{
200+
console.log('文件删除完毕,开始上传...')
201+
res('删除完毕')
202+
// Promise.all(actionDirectory).then(()=>{
203+
// res('文件夹也删除完了')
204+
// })
205+
}).catch(err=>{
206+
console.log('this is removeFile catch————————\n',err)
207+
rej(err)
208+
})
209+
} else {
210+
res('无匹配文件夹')
211+
}
212+
})
213+
214+
})
215+
}
216+
ftp.connect(connectionProperties);
102217

218+
ftp.on("ready", async () => {
219+
try {
220+
if (DelArrPath) {
221+
db = new ProgressBar('正在删除...', 0)
222+
await removeFile(remoteFtpPath, DelArrPath)
223+
}
224+
} catch (delErr) {
225+
console.log('删除方法有异常\n', delErr)
226+
}
103227
fs.readdir(dirPath, { withFileTypes: true }, (err, files) => {
104-
console.log("正在和服务器同步");
105228
pb = new ProgressBar("正在上传...", 0);
106229
readFiles(dirPath)
107-
108230
})
109231
});
110232

node_modules/ansi-regex/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

node_modules/ansi-regex/license

Lines changed: 0 additions & 21 deletions
This file was deleted.

node_modules/ansi-regex/package.json

Lines changed: 0 additions & 64 deletions
This file was deleted.

node_modules/ansi-regex/readme.md

Lines changed: 0 additions & 39 deletions
This file was deleted.

node_modules/code-point-at/index.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

node_modules/code-point-at/license

Lines changed: 0 additions & 21 deletions
This file was deleted.

node_modules/code-point-at/package.json

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)