|
1 | 1 | var usage = "node " + __filename.replace(/.*[\/\\]/, "") + " " +
|
2 |
| - "[FILE | --compress | --no-compress]... -o OUTPUT.zip"; |
| 2 | + "[FILE | --compress | --no-compress | --buffer | --no-buffer]... -o OUTPUT.zip" + "\n" + |
| 3 | + "\n" + |
| 4 | + "all arguments and switches are processed in order. for example:" + "\n" + |
| 5 | + " node zip.js --compress a.txt --no-compress b.txt -o out.zip" + "\n" + |
| 6 | + "would result in compression for a.txt, but not for b.txt."; |
3 | 7 | var yazl = require("../");
|
4 | 8 | var fs = require("fs");
|
5 | 9 |
|
6 | 10 | var zipfile = new yazl.ZipFile();
|
7 | 11 | var options = {compress: false};
|
| 12 | +var use_buffer = false; |
8 | 13 |
|
9 | 14 | var args = process.argv.slice(2);
|
10 |
| -if (Math.max(args.indexOf("-h"), args.indexOf("--help")) !== -1) throw new Error("usage: " + usage); |
11 |
| -var outputFileIndex = args.indexOf("-o"); |
12 |
| -if (outputFileIndex === -1) throw new Error("missing -o"); |
13 |
| -zipfile.outputStream.pipe(fs.createWriteStream(args[outputFileIndex + 1])); |
14 |
| -args.splice(outputFileIndex, 2); |
| 15 | +if (Math.max(args.indexOf("-h"), args.indexOf("--help")) !== -1) { |
| 16 | + console.log("usage: " + usage); |
| 17 | + process.exit(1); |
| 18 | +} |
| 19 | +// this one's important |
| 20 | +if (args.indexOf("-o") === -1) throw new Error("missing -o"); |
| 21 | +if (args.indexOf("-o") + 1 >= args.length) throw new Error("missing argument after -o"); |
| 22 | + |
| 23 | +var its_the_dash_o = false; |
15 | 24 | args.forEach(function(arg) {
|
16 |
| - if (/--compress/.test(arg)) { |
| 25 | + if (its_the_dash_o) { |
| 26 | + its_the_dash_o = false; |
| 27 | + zipfile.outputStream.pipe(fs.createWriteStream(arg)); |
| 28 | + } else if (arg === "--compress") { |
17 | 29 | options.compress = true;
|
18 |
| - } else if (/--no-compress/.test(arg)) { |
| 30 | + } else if (arg === "--no-compress") { |
19 | 31 | options.compress = false;
|
| 32 | + } else if (arg === "--buffer") { |
| 33 | + use_buffer = true; |
| 34 | + } else if (arg === "--no-buffer") { |
| 35 | + use_buffer = false; |
| 36 | + } else if (arg === "-o") { |
| 37 | + its_the_dash_o = true; |
20 | 38 | } else {
|
21 | 39 | // file thing
|
22 | 40 | var stats = fs.statSync(arg);
|
23 | 41 | if (stats.isFile()) {
|
24 |
| - zipfile.addFile(arg, arg, options); |
| 42 | + if (use_buffer) { |
| 43 | + zipfile.addBuffer(fs.readFileSync(arg), arg, options); |
| 44 | + } else { |
| 45 | + zipfile.addFile(arg, arg, options); |
| 46 | + } |
25 | 47 | } else if (stats.isDirectory()) {
|
26 | 48 | zipfile.addEmptyDirectory(arg);
|
27 | 49 | } else {
|
|
0 commit comments