diff --git a/bin/cli.js b/bin/cli.js index e03bbcd..0e077fa 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -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") { @@ -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!"); diff --git a/lib/bestzip.js b/lib/bestzip.js index 6d112ca..745d714 100644 --- a/lib/bestzip.js +++ b/lib/bestzip.js @@ -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, @@ -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);