Skip to content

Commit

Permalink
Add option to set level of compression (#52)
Browse files Browse the repository at this point in the history
* Add option to set level of compression

* Fix default compression level

Co-authored-by: Patrik Valkovic <[email protected]>
  • Loading branch information
PatrikValkovic and PatrikValkovic authored Sep 9, 2021
1 parent d17148a commit 01a4081
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 12 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ var argv = require("yargs")
describe: "Force use of node.js or native zip methods",
choices: ["node", "native"],
})
.option("level", {
describe: "Level of compression",
type: "number",
default: -1,
})
.demand(2).argv;

var destination = argv._.shift();
var source = argv._;

if (argv.level < -1 || argv.level > 9) {
console.error("Invalid compression level, must be >= 0 and <= 9");
process.exit(1);
}

console.log("Writing %s to %s...", source.join(", "), destination);

if (argv.force === "node") {
Expand All @@ -26,7 +36,8 @@ if (argv.force === "node") {
zip({
source: source,
destination: destination,
verbose: argv.verbose,
verbose: !!argv.verbose,
level: argv.level,
})
.then(function () {
console.log("zipped!");
Expand Down
8 changes: 7 additions & 1 deletion lib/bestzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ const nativeZip = (options) =>
const args = ["--quiet", "--recurse-paths", options.destination].concat(
sources
);
if (typeof options.level == "number" && !isNaN(options.level)) {
args.splice(0, 0, "-" + options.level.toString());
}

const zipProcess = cp.spawn(command, args, {
stdio: "inherit",
cwd,
Expand Down Expand Up @@ -99,7 +103,9 @@ const nodeZip = (options) =>
new Promise((resolve, reject) => {
const cwd = options.cwd || process.cwd();
const output = fs.createWriteStream(path.resolve(cwd, options.destination));
const archive = archiver("zip");
const archive = archiver("zip", {
zlib: { level: options.level },
});

output.on("close", resolve);
archive.on("error", reject);
Expand Down

0 comments on commit 01a4081

Please sign in to comment.