hot-update/packages/update-manager/js/FileUtils.js
andrewlu ef915a121e 增加版本信息获取工具类.
可以在项目代码中直接使用 import um = require('UpdateManager') 然后调用相关接口.
2021-02-03 21:34:58 +08:00

64 lines
1.8 KiB
JavaScript

const fs = require('fs')
const path = require('path')
class FileUtil {
/*
* 获取window上的文件目录以及文件列表信息
* @param { String } localDir 本地路径
* @return { files, dirs }
* */
listDirsAndFiles(localDir) {
const dirs = []
const files = []
const dir = fs.readdirSync(localDir)
for (let i = 0; i < dir.length; i++) {
const p = path.join(localDir, dir[i])
const stat = fs.statSync(p)
if (stat.isDirectory()) {
dirs.push(p)
const children = this.listDirsAndFiles(p)
dirs.push(...children.dirs)
files.push(...children.files)
} else {
files.push(p)
}
}
return {
files,
dirs
}
}
/**
* 拷贝文件或目录。
* @param src
* @param dst
*/
async copy(src, dst) {
const st = fs.statSync(src);
if (st.isFile()) {
const readable = fs.createReadStream(src);//创建读取流
const writable = fs.createWriteStream(dst);//创建写入流
readable.pipe(writable);
return;
}
if (!fs.existsSync(dst)) {
fs.mkdirSync(dst, {recursive: true});
}
//读取目录
const paths = fs.readdirSync(src);
for (let path of paths) {
this.copy(src + '/' + path, dst + '/' + path);
}
};
write(filePath, content) {
const pathInfo = path.parse(filePath);
if (!fs.existsSync(pathInfo.dir)) {
fs.mkdirSync(pathInfo.dir, {recursive: true});
}
fs.writeFileSync(filePath, content, 'utf-8');
}
}
module.exports = new FileUtil();