bestzip 2.2.2 → 2.2.4

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/lib/bestzip.js CHANGED
@@ -8,15 +8,14 @@ const fs = require("fs");
8
8
  const path = require("path");
9
9
 
10
10
  const archiver = require("archiver");
11
- const async = require("async");
12
- const glob = require("glob");
11
+ const { glob, hasMagic } = require("glob");
13
12
  const which = require("which");
14
13
 
15
14
  function hasNativeZip() {
16
15
  return Boolean(which.sync("zip", { nothrow: true }));
17
16
  }
18
17
 
19
- function expandSources(cwd, source, done) {
18
+ async function expandSources(cwd, source) {
20
19
  // options to behave more like the native zip's glob support
21
20
  const globOpts = {
22
21
  cwd,
@@ -25,29 +24,27 @@ function expandSources(cwd, source, done) {
25
24
  noext: true, // no (a|b)
26
25
  nobrace: true, // no {a,b}
27
26
  };
27
+
28
28
  // first handle arrays
29
29
  if (Array.isArray(source)) {
30
- return async.concat(
31
- source,
32
- (_source, next) => expandSources(cwd, _source, next),
33
- done
34
- );
30
+ const results = await Promise.all(source.map((s) => expandSources(cwd, s)));
31
+ return results.flat();
35
32
  }
33
+
36
34
  // then expand magic
37
35
  if (typeof source !== "string") {
38
36
  throw new Error(`source is (${typeof source}) `);
39
37
  }
40
- if (glob.hasMagic(source, globOpts)) {
38
+
39
+ if (hasMagic(source, globOpts)) {
41
40
  // archiver uses this library but somehow ends up with different results on windows:
42
41
  // archiver.glob('*') will include subdirectories, but omit their contents on windows
43
42
  // so we'll use glob directly, and add all of the files it finds
44
- glob(source, globOpts, done);
43
+ return await glob(source, globOpts);
45
44
  } else {
46
45
  // 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]);
50
- });
46
+ // always return an array
47
+ return [source];
51
48
  }
52
49
  }
53
50
 
@@ -63,95 +60,96 @@ function walkDir(fullPath) {
63
60
  return files.reduce((acc, cur) => acc.concat(cur), []);
64
61
  }
65
62
 
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());
81
- }
63
+ const nativeZip = async (options) => {
64
+ const cwd = options.cwd || process.cwd();
65
+ const command = "zip";
66
+ const sources = await expandSources(cwd, options.source);
67
+
68
+ const args = ["--quiet", "--recurse-paths", options.destination].concat(
69
+ sources
70
+ );
71
+ if (
72
+ typeof options.level == "number" &&
73
+ !isNaN(options.level) &&
74
+ options.level >= 0 &&
75
+ options.level <= 9
76
+ ) {
77
+ args.splice(0, 0, "-" + options.level.toString());
78
+ }
82
79
 
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
- });
80
+ return new Promise((resolve, reject) => {
81
+ const zipProcess = cp.spawn(command, args, {
82
+ stdio: "inherit",
83
+ cwd,
84
+ });
85
+ zipProcess.on("error", reject);
86
+ zipProcess.on("close", (exitCode) => {
87
+ if (exitCode === 0) {
88
+ resolve();
89
+ } else {
90
+ reject(
91
+ new Error(
92
+ `Unexpected exit code from native zip: ${exitCode}\n executed command '${command} ${args.join(
93
+ " "
94
+ )}'\n executed in directory '${cwd}'`
95
+ )
96
+ );
97
+ }
103
98
  });
104
99
  });
100
+ };
105
101
 
106
102
  // based on http://stackoverflow.com/questions/15641243/need-to-zip-an-entire-directory-using-node-js/18775083#18775083
107
- const nodeZip = (options) =>
108
- new Promise((resolve, reject) => {
109
- const cwd = options.cwd || process.cwd();
110
- const output = fs.createWriteStream(path.resolve(cwd, options.destination));
111
- const archive = archiver("zip", {
112
- zlib: { level: options.level },
113
- });
103
+ const nodeZip = async (options) => {
104
+ const cwd = options.cwd || process.cwd();
105
+ const output = fs.createWriteStream(path.resolve(cwd, options.destination));
106
+ const archive = archiver("zip", {
107
+ zlib: { level: options.level },
108
+ });
109
+
110
+ output.on("close", () => resolvePromise());
111
+ archive.on("error", (err) => rejectPromise(err));
114
112
 
115
- output.on("close", resolve);
116
- archive.on("error", reject);
117
-
118
- archive.pipe(output);
119
-
120
- function addSource(source, next) {
121
- const fullPath = path.resolve(cwd, source);
122
- const destPath = source;
123
- fs.stat(fullPath, function (err, stats) {
124
- if (err) {
125
- return next(err);
126
- }
127
- if (stats.isDirectory()) {
128
- // Walk directory. Works on directories and directory symlinks.
129
- const files = walkDir(fullPath);
130
- files.forEach((f) => {
131
- const subPath = f.substring(fullPath.length);
132
- archive.file(f, {
133
- name: destPath + subPath,
134
- });
135
- });
136
- } else if (stats.isFile()) {
137
- archive.file(fullPath, { stats: stats, name: destPath });
138
- }
139
- next();
113
+ archive.pipe(output);
114
+
115
+ let resolvePromise;
116
+ let rejectPromise;
117
+ const promise = new Promise((resolve, reject) => {
118
+ resolvePromise = resolve;
119
+ rejectPromise = reject;
120
+ });
121
+
122
+ async function addSource(source) {
123
+ const fullPath = path.resolve(cwd, source);
124
+ const destPath = source;
125
+
126
+ const stats = await fs.promises.stat(fullPath);
127
+ if (stats.isDirectory()) {
128
+ // Walk directory. Works on directories and directory symlinks.
129
+ const files = walkDir(fullPath);
130
+ files.forEach((f) => {
131
+ const subPath = f.substring(fullPath.length);
132
+ archive.file(f, {
133
+ name: destPath + subPath,
134
+ });
140
135
  });
136
+ } else if (stats.isFile()) {
137
+ archive.file(fullPath, { stats: stats, name: destPath });
141
138
  }
139
+ }
142
140
 
143
- expandSources(cwd, options.source, (err, expandedSources) => {
144
- if (err) {
145
- return reject(err);
146
- }
147
- async.forEach(expandedSources, addSource, (err) => {
148
- if (err) {
149
- return reject(err);
150
- }
151
- archive.finalize();
152
- });
153
- });
154
- });
141
+ try {
142
+ const expandedSources = await expandSources(cwd, options.source);
143
+ for (const source of expandedSources) {
144
+ await addSource(source);
145
+ }
146
+ archive.finalize();
147
+ } catch (err) {
148
+ rejectPromise(err);
149
+ }
150
+
151
+ return promise;
152
+ };
155
153
 
156
154
  function zip(options) {
157
155
  const compatMode = typeof options === "string";
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "bestzip",
3
- "version": "2.2.2",
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.",
3
+ "version": "2.2.4",
4
+ "description": "Uses OS zip command if available (for better performance and speed) or node.js version if there is no system command available. 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
9
  "engines": {
10
- "node": ">=10"
10
+ "node": ">=20"
11
11
  },
12
12
  "scripts": {
13
13
  "lint": "eslint .",
@@ -27,16 +27,15 @@
27
27
  "lib/"
28
28
  ],
29
29
  "dependencies": {
30
- "archiver": "^5.3.0",
31
- "async": "^3.2.0",
32
- "glob": "^7.1.6",
33
- "which": "^2.0.2",
30
+ "archiver": "^7.0.1",
31
+ "glob": "^13.0.6",
32
+ "which": "^6.0.1",
34
33
  "yargs": "^16.2.0"
35
34
  },
36
35
  "devDependencies": {
37
36
  "chai": "^4.3.4",
38
37
  "command-line-test": "^1.0.10",
39
- "eslint": "^7.22.0",
38
+ "eslint": "^8.57.1",
40
39
  "eslint-config-prettier": "^8.1.0",
41
40
  "eslint-plugin-prettier": "^3.3.1",
42
41
  "jest": "^29.7.0",
package/readme.md CHANGED
@@ -83,7 +83,7 @@ When using the programmatic API, the same effect may be achieved by passing in t
83
83
 
84
84
  Wildcards (`*`) ignore dotfiles.
85
85
 
86
- * To include a dotfile, either include the directory it's in (`folder/`) or include it by name (`folder/.dotfile)`
86
+ * To include a dotfile, either include the directory it's in (`folder/`) or include it by name (`folder/.dotfile`)
87
87
  * To omit dotfiles, either use a wildcard (`folder/*`) or explicitly list the desired files (`folder/file1.txt folder/file2.txt`)
88
88
 
89
89
  ## Breaking changes for v2