UNPKG

@wenbo/fis3-deploy-zip

Version:
87 lines (69 loc) 2.15 kB
var archiver = require('archiver'); var concatStream = require('concat-stream'); var _ = require('lodash'); var path = require('path'); module.exports = function(options, modified, total, callback) { var list = options.modified ? modified : total; if (!list.length) { return false; } var root = fis.project.getProjectPath(); var zipfile = archiver.create('zip'); zipfile.pipe(concatStream(function(data) { var file = fis.file(root, options.filename || 'all.zip'); file.setContent(data); if (!options.keep) { modified.splice(0, modified.length); total.splice(0, total.length); } modified.push(file); total.push(file); callback(); })); list.forEach(function(file) { var filepath = file.getHashRelease().substring(1); zipfile.append(file.getContent(), { name: filepath }); }); function getAllFiles (root, ignoreDir) { var fs = require('fs'); var res = [], files = fs.readdirSync(root); files.forEach(function (file) { var pathname = root + '/' + file , stat = fs.lstatSync(pathname); if ((typeof(ignoreDir) === 'string' && file === ignoreDir ) || (_.isArray(ignoreDir) && ignoreDir.indexOf(file) !== -1)) { return; } if (!stat.isDirectory()) { res.push({path: pathname, dir: false}); } else { res.push({path: pathname, dir: true}); res = res.concat(getAllFiles(pathname)); } }); return res }; if(options.include&&options.include.length>0){ options.include.forEach(function(item){ getAllFiles(item.dir,['.DS_Store','.idea','.hoo_cache','.img_origin_cache','.svn']).forEach(function(fileObj){ if(!fileObj.dir){ zipfile.append(fis.util.read(fileObj.path, true), { name: path.join(item.name,path.relative(item.dir,fileObj.path)) }); } }) }); } zipfile.finalize(); }; module.exports.options = { // 是否保留原始文件。 keep: false, // 是否只打包修改过的。 modified: false, //打包额外的文件目录 include:[], // zip 文件名 filename: 'all.zip' };