bestzip 2.1.6 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cli.js CHANGED
@@ -8,13 +8,23 @@ var argv = require("yargs")
8
8
  .usage("\nUsage: bestzip destination.zip sources/")
9
9
  .option("force", {
10
10
  describe: "Force use of node.js or native zip methods",
11
- choices: ["node", "native"]
11
+ choices: ["node", "native"],
12
+ })
13
+ .option("level", {
14
+ describe: "Level of compression",
15
+ type: "number",
16
+ default: -1,
12
17
  })
13
18
  .demand(2).argv;
14
19
 
15
20
  var destination = argv._.shift();
16
21
  var source = argv._;
17
22
 
23
+ if (argv.level < -1 || argv.level > 9) {
24
+ console.error("Invalid compression level, must be >= 0 and <= 9");
25
+ process.exit(1);
26
+ }
27
+
18
28
  console.log("Writing %s to %s...", source.join(", "), destination);
19
29
 
20
30
  if (argv.force === "node") {
@@ -26,12 +36,13 @@ if (argv.force === "node") {
26
36
  zip({
27
37
  source: source,
28
38
  destination: destination,
29
- verbose: argv.verbose
39
+ verbose: !!argv.verbose,
40
+ level: argv.level,
30
41
  })
31
- .then(function() {
42
+ .then(function () {
32
43
  console.log("zipped!");
33
44
  })
34
- .catch(function(err) {
45
+ .catch(function (err) {
35
46
  console.error(err);
36
47
  process.exit(1);
37
48
  });
package/lib/bestzip.js CHANGED
@@ -16,98 +16,121 @@ function hasNativeZip() {
16
16
  return Boolean(which.sync("zip", { nothrow: true }));
17
17
  }
18
18
 
19
- const nativeZip = options =>
20
- new Promise((resolve, reject) => {
21
- const sources = Array.isArray(options.source)
22
- ? options.source.join(" ")
23
- : options.source;
24
- const command = `zip --quiet --recurse-paths ${
25
- options.destination
26
- } ${sources}`;
27
- const zipProcess = cp.exec(command, {
28
- stdio: "inherit",
29
- cwd: options.cwd
19
+ function expandSources(cwd, source, done) {
20
+ // options to behave more like the native zip's glob support
21
+ const globOpts = {
22
+ cwd,
23
+ dot: false, // ignore .dotfiles
24
+ noglobstar: true, // treat ** as *
25
+ noext: true, // no (a|b)
26
+ nobrace: true, // no {a,b}
27
+ };
28
+ // first handle arrays
29
+ if (Array.isArray(source)) {
30
+ return async.concat(
31
+ source,
32
+ (_source, next) => expandSources(cwd, _source, next),
33
+ done
34
+ );
35
+ }
36
+ // then expand magic
37
+ if (typeof source !== "string") {
38
+ throw new Error(`source is (${typeof source}) `);
39
+ }
40
+ if (glob.hasMagic(source, globOpts)) {
41
+ // archiver uses this library but somehow ends up with different results on windows:
42
+ // archiver.glob('*') will include subdirectories, but omit their contents on windows
43
+ // so we'll use glob directly, and add all of the files it finds
44
+ glob(source, globOpts, done);
45
+ } else {
46
+ // or just trigger the callback with the source string if there is no magic
47
+ process.nextTick(() => {
48
+ // always return an array
49
+ done(null, [source]);
30
50
  });
31
- zipProcess.on("error", reject);
32
- zipProcess.on("close", exitCode => {
33
- if (exitCode === 0) {
34
- resolve();
35
- } else {
36
- // exit code 12 means "nothing to do" right?
37
- //console.log('rejecting', zipProcess)
38
- reject(
39
- new Error(
40
- `Unexpected exit code from native zip command: ${exitCode}\n executed command '${command}'\n executed inin directory '${options.cwd ||
41
- process.cwd()}'`
42
- )
43
- );
51
+ }
52
+ }
53
+
54
+ function walkDir(fullPath) {
55
+ const files = fs.readdirSync(fullPath).map((f) => {
56
+ const filePath = path.join(fullPath, f);
57
+ const stats = fs.statSync(filePath);
58
+ if (stats.isDirectory()) {
59
+ return walkDir(filePath);
60
+ }
61
+ return filePath;
62
+ });
63
+ return files.reduce((acc, cur) => acc.concat(cur), []);
64
+ }
65
+
66
+ const nativeZip = (options) =>
67
+ new Promise((resolve, reject) => {
68
+ const cwd = options.cwd || process.cwd();
69
+ const command = "zip";
70
+ expandSources(cwd, options.source, (err, sources) => {
71
+ const args = ["--quiet", "--recurse-paths", options.destination].concat(
72
+ sources
73
+ );
74
+ if (
75
+ typeof options.level == "number" &&
76
+ !isNaN(options.level) &&
77
+ options.level >= 0 &&
78
+ options.level <= 9
79
+ ) {
80
+ args.splice(0, 0, "-" + options.level.toString());
44
81
  }
82
+
83
+ const zipProcess = cp.spawn(command, args, {
84
+ stdio: "inherit",
85
+ cwd,
86
+ });
87
+ zipProcess.on("error", reject);
88
+ zipProcess.on("close", (exitCode) => {
89
+ if (exitCode === 0) {
90
+ resolve();
91
+ } else {
92
+ // exit code 12 means "nothing to do" right?
93
+ //console.log('rejecting', zipProcess)
94
+ reject(
95
+ new Error(
96
+ `Unexpected exit code from native zip: ${exitCode}\n executed command '${command} ${args.join(
97
+ " "
98
+ )}'\n executed in directory '${cwd}'`
99
+ )
100
+ );
101
+ }
102
+ });
45
103
  });
46
104
  });
47
105
 
48
106
  // based on http://stackoverflow.com/questions/15641243/need-to-zip-an-entire-directory-using-node-js/18775083#18775083
49
- const nodeZip = options =>
107
+ const nodeZip = (options) =>
50
108
  new Promise((resolve, reject) => {
51
109
  const cwd = options.cwd || process.cwd();
52
110
  const output = fs.createWriteStream(path.resolve(cwd, options.destination));
53
- const archive = archiver("zip");
111
+ const archive = archiver("zip", {
112
+ zlib: { level: options.level },
113
+ });
54
114
 
55
115
  output.on("close", resolve);
56
116
  archive.on("error", reject);
57
117
 
58
118
  archive.pipe(output);
59
119
 
60
- const globOpts = {
61
- cwd: cwd,
62
- // options to behave more like the native zip's glob support
63
- dot: false, // ignore .dotfiles
64
- noglobstar: true, // treat ** as *
65
- noext: true, // no (a|b)
66
- nobrace: true // no {a,b}
67
- };
68
-
69
- function findSource(source, next) {
70
- if (glob.hasMagic(source, globOpts)) {
71
- // archiver uses this library but somehow ends up with different results on windows:
72
- // archiver.glob('*') will include subdirectories, but omit their contents on windows
73
- // so we'll use glob directly, and add all of the files it finds
74
- glob(source, globOpts, function(err, files) {
75
- if (err) {
76
- return next(err);
77
- }
78
- async.forEach(files, addSource, next);
79
- });
80
- } else {
81
- addSource(source, next);
82
- }
83
- }
84
-
85
- function walkDir(fullPath) {
86
- const files = fs.readdirSync(fullPath).map(f => {
87
- const filePath = path.join(fullPath, f);
88
- const stats = fs.statSync(filePath);
89
- if (stats.isDirectory()) {
90
- return walkDir(filePath);
91
- }
92
- return filePath;
93
- });
94
- return files.reduce((acc, cur) => acc.concat(cur), []);
95
- }
96
-
97
120
  function addSource(source, next) {
98
121
  const fullPath = path.resolve(cwd, source);
99
122
  const destPath = source;
100
- fs.stat(fullPath, function(err, stats) {
123
+ fs.stat(fullPath, function (err, stats) {
101
124
  if (err) {
102
125
  return next(err);
103
126
  }
104
127
  if (stats.isDirectory()) {
105
128
  // Walk directory. Works on directories and directory symlinks.
106
129
  const files = walkDir(fullPath);
107
- files.forEach(f => {
130
+ files.forEach((f) => {
108
131
  const subPath = f.substring(fullPath.length);
109
132
  archive.file(f, {
110
- name: destPath + subPath
133
+ name: destPath + subPath,
111
134
  });
112
135
  });
113
136
  } else if (stats.isFile()) {
@@ -117,15 +140,16 @@ const nodeZip = options =>
117
140
  });
118
141
  }
119
142
 
120
- const sources = Array.isArray(options.source)
121
- ? options.source
122
- : [options.source];
123
-
124
- async.forEach(sources, findSource, function(err) {
143
+ expandSources(cwd, options.source, (err, expandedSources) => {
125
144
  if (err) {
126
145
  return reject(err);
127
146
  }
128
- archive.finalize();
147
+ async.forEach(expandedSources, addSource, (err) => {
148
+ if (err) {
149
+ return reject(err);
150
+ }
151
+ archive.finalize();
152
+ });
129
153
  });
130
154
  });
131
155
 
@@ -134,7 +158,7 @@ function zip(options) {
134
158
  if (compatMode) {
135
159
  options = {
136
160
  source: arguments[1],
137
- destination: arguments[0]
161
+ destination: arguments[0],
138
162
  };
139
163
  }
140
164
 
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "bestzip",
3
- "version": "2.1.6",
3
+ "version": "2.2.1",
4
4
  "description": "Uses OS zip command if avaliable (for better performance and speed) or node.js version if there is no system command avaliable. Can be called via node or command line.",
5
5
  "main": "lib/bestzip.js",
6
6
  "bin": {
7
7
  "bestzip": "bin/cli.js"
8
8
  },
9
+ "engines": {
10
+ "node": ">=10"
11
+ },
9
12
  "scripts": {
10
13
  "lint": "eslint .",
11
14
  "lint-fix": "eslint --fix .",
@@ -21,24 +24,23 @@
21
24
  "lib/"
22
25
  ],
23
26
  "dependencies": {
24
- "archiver": "^4.0.2",
25
- "async": "^2.6.1",
26
- "glob": "^7.1.3",
27
- "which": "^1.3.1",
28
- "yargs": "^13.2.4"
27
+ "archiver": "^5.3.0",
28
+ "async": "^3.2.0",
29
+ "glob": "^7.1.6",
30
+ "which": "^2.0.2",
31
+ "yargs": "^16.2.0"
29
32
  },
30
33
  "devDependencies": {
31
- "chai": "^4.2.0",
34
+ "chai": "^4.3.4",
32
35
  "command-line-test": "^1.0.10",
33
- "eslint": "^5.6.1",
34
- "eslint-config-prettier": "^4.2.0",
35
- "eslint-plugin-prettier": "^3.1.0",
36
- "fs": "0.0.1-security",
37
- "jest": "^24.8.0",
36
+ "eslint": "^7.22.0",
37
+ "eslint-config-prettier": "^8.1.0",
38
+ "eslint-plugin-prettier": "^3.3.1",
39
+ "jest": "^26.6.3",
38
40
  "klaw-sync": "^6.0.0",
39
- "prettier": "^1.14.3",
40
- "regenerator-runtime": "^0.13.2",
41
- "rimraf": "^2.6.2",
42
- "unzip-stream": "^0.3.0"
41
+ "prettier": "^2.2.1",
42
+ "regenerator-runtime": "^0.13.7",
43
+ "rimraf": "^3.0.2",
44
+ "unzip-stream": "^0.3.1"
43
45
  }
44
46
  }
package/readme.md CHANGED
@@ -1,15 +1,18 @@
1
1
  # bestzip
2
2
 
3
3
  [![Build status](https://travis-ci.org/nfriedly/node-bestzip.svg?branch=master)](https://travis-ci.org/nfriedly/node-bestzip)
4
+ [![npm version](https://badge.fury.io/js/bestzip.svg)](https://www.npmjs.com/package/bestzip)
5
+ [![npm downloads](https://img.shields.io/npm/dm/bestzip)](https://www.npmjs.com/package/bestzip)
4
6
 
5
7
  This module provides a `bestzip` command that calls the native `zip` command if available and otherwise falls back to a
6
8
  Node.js implementation.
7
9
 
8
- The native `zip` method on macOS is significantly faster and more as efficient than the Node.js version, but Windows has no
9
- native `zip` command. This module provides the best of both worlds.
10
-
11
10
  The `--recurse-directories` (`-r`) option is automatically enabled.
12
11
 
12
+ ## Why?
13
+
14
+ The native `zip` command on GNU/Linux and macOS is significantly faster and creates moderately smaller .zip files than the Node.js version included here, but Windows has no built-in `zip` command. This module provides the best of both worlds, and allows for easier cross-platform scripting.
15
+
13
16
  ## Global command line usage
14
17
 
15
18
  npm install -g bestzip
@@ -59,9 +62,7 @@ zip({
59
62
 
60
63
  ## How to control the directory structure
61
64
 
62
- The directory structure in the .zip is going to match your input files. To have greater or fewer levels of directories.
63
-
64
- For example:
65
+ The directory structure in the .zip is going to match your input files, but the exact details depend on how the command is called. For example:
65
66
 
66
67
  `bestzip build.zip build/*`
67
68
 
@@ -73,11 +74,13 @@ Alternatively:
73
74
 
74
75
  This will not include the build/ folder, it's contents will be top-level.
75
76
 
77
+ *Note: some tools, including the Archive Utility built into macOS, will automatically create a top-level folder to group everything together when extracting a .zip archive that contains multiple top-level files.*
78
+
76
79
  When using the programmatic API, the same effect may be achieved by passing in the `cwd` option.
77
80
 
78
81
  ## .dotfiles
79
82
 
80
- Wildcards ('*') ignore dotfiles.
83
+ Wildcards (`*`) ignore dotfiles.
81
84
 
82
85
  * To include a dotfile, either include the directory it's in (`folder/`) or include it by name (`folder/.dotfile)`
83
86
  * To omit dotfiles, either use a wildcard (`folder/*`) or explicitly list the desired files (`folder/file1.txt folder/file2.txt`)