madp-cli1
Version:
A simple CLI for scaffolding madp projects, we provide madp-template to quickly build small and medium sized app.
53 lines (51 loc) • 2.11 kB
JavaScript
let fs = require("fs");//获取文件系统模块,负责读写文件
let path = require("path");//工具模块,处理文件路径的小工具
let JSZIP = require("jszip");
// 排除部分文件或文件夹
const exclude = name =>!['.git', 'node_modules','.madp-cli.common'].includes(name)
module.exports = {
// zip 递归读取文件夹下的文件流
readDir(zip, nowPath) {
// 读取目录中的所有文件及文件夹(同步操作)
let files = fs.readdirSync(nowPath)
//遍历检测目录中的文件
files.filter(exclude).forEach((fileName, index) => {
// 打印当前读取的文件名
// console.log(fileName, index)
// 当前文件的全路径
let fillPath = path.join(nowPath, fileName)
// 获取一个文件的属性
let file = fs.statSync(fillPath)
// 如果是目录的话,继续查询
if (file.isDirectory()) {
// 压缩对象中生成该目录
let dirlist = zip.folder(fileName)
// (递归)重新检索目录文件
this.readDir(dirlist, fillPath)
} else {
// 压缩目录添加文件
zip.file(fileName, fs.readFileSync(fillPath))
}
})
},
// 开始压缩文件
zipFolder( target , output ,callback) {
// 创建 zip 实例
const zip = new JSZIP()
// zip 递归读取文件夹下的文件流
this.readDir(zip, target)
// 设置压缩格式,开始打包
zip.generateAsync({
// nodejs 专用
type: 'nodebuffer',
// 压缩算法
compression: 'DEFLATE',
// 压缩级别
compressionOptions: { level: 9, },
}).then(content => {
// 将打包的内容写入 当前目录下的 result.zip中
fs.writeFileSync(output, content, 'utf-8')
typeof callback === "function" && callback();
})
}
}