@richardo2016/rcli
Version:
Richard's cli
36 lines (35 loc) • 1.42 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
const zip = require("zip");
const fsUtils = require("../utils/fs");
function handler(srcfile, target, ctx = {}) {
const { cwd = process.cwd() } = ctx || {};
let { password = '' } = ctx;
srcfile = fsUtils.normalizeToAbsolute(srcfile, cwd);
if (!fs.exists(srcfile))
throw `non-existed source: ${srcfile}`;
if (!target) {
const tareget_dirname = path.dirname(srcfile);
target = path.join(tareget_dirname, path.basename(srcfile) + '.zip');
}
console.notice('target', target);
target = fsUtils.normalizeToAbsolute(target, cwd);
const target_dir = path.dirname(target);
fsUtils.ensureDirectoryExisted(target_dir);
const zipfile = zip.open(target, fs.exists(target) ? 'a' : 'w');
fsUtils.walkFileList(srcfile, (rsrcpath, ctx) => {
const srcpath = path.join(ctx.basedir, rsrcpath);
// exclude self
if (path.resolve(srcpath) === path.resolve(target))
return;
const f = fs.openFile(srcpath, 'r');
zipfile.write(f, rsrcpath, password);
console.notice(`[zip] write file success {@password: ${password}}!`);
console.notice(`source -- ${srcpath}`);
console.notice(`target -- ${rsrcpath}`);
console.log();
});
zipfile.close();
}
exports.default = handler;