持续popputeer的文件操作内容。
删除文件
fs只支持无子文件的删除操作,对于整体的删除要采用递归的方式。
async deleteFile(path) {
const fs = require('fs')
var files = [];
if (fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.statSync(curPath).isDirectory()) { // recurse
deleteFile(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
创建文件
创建文件,基本引用的都是fs库。
async createFile(path) {
const fs = require('fs'); // 引入文件系统模块
fs.mkdirSync(path);
}
等待文件出现
等待文件出现,若文件出现,打印对应信息。
async waitForFile(path) {
const fs = require('fs')
return new Promise(function (resolve, reject) {
const watcher = fs.watch(path, function (event, fileName) {
if (event === "rename") {
watcher.close();
console.log(`文件 ${fileName} 已出现11.`);
resolve(`文件 ${fileName} 已出现.`);
}
});
})
}