From fcea252e52b6078eff6caf5531bd32c53fccb59f Mon Sep 17 00:00:00 2001 From: Nathan Friedly Date: Tue, 1 Sep 2020 21:43:54 -0400 Subject: [PATCH] make expandSources a standalone function --- lib/bestzip.js | 97 ++++++++++++++++++++++++++--------------------- package-lock.json | 12 +++--- package.json | 2 +- 3 files changed, 60 insertions(+), 51 deletions(-) diff --git a/lib/bestzip.js b/lib/bestzip.js index 26f58c1..959b286 100644 --- a/lib/bestzip.js +++ b/lib/bestzip.js @@ -16,6 +16,53 @@ function hasNativeZip() { return Boolean(which.sync("zip", { nothrow: true })); } +function expandSources(cwd, source, done) { + // options to behave more like the native zip's glob support + const globOpts = { + cwd, + dot: false, // ignore .dotfiles + noglobstar: true, // treat ** as * + noext: true, // no (a|b) + nobrace: true // no {a,b} + }; + // first handle arrays + if (Array.isArray(source)) { + return async.concat( + source, + (_source, next) => expandSources(cwd, _source, next), + done + ); + } + // then expand magic + if (typeof source !== "string") { + throw new Error(`source is (${typeof source}) `); + } + if (glob.hasMagic(source, globOpts)) { + // archiver uses this library but somehow ends up with different results on windows: + // archiver.glob('*') will include subdirectories, but omit their contents on windows + // so we'll use glob directly, and add all of the files it finds + glob(source, globOpts, done); + } else { + // or just trigger the callback with the source string if there is no magic + process.nextTick(() => { + // always return an array + done(null, [source]); + }); + } +} + +function walkDir(fullPath) { + const files = fs.readdirSync(fullPath).map(f => { + const filePath = path.join(fullPath, f); + const stats = fs.statSync(filePath); + if (stats.isDirectory()) { + return walkDir(filePath); + } + return filePath; + }); + return files.reduce((acc, cur) => acc.concat(cur), []); +} + const nativeZip = options => new Promise((resolve, reject) => { const sources = Array.isArray(options.source) @@ -57,43 +104,6 @@ const nodeZip = options => archive.pipe(output); - const globOpts = { - cwd: cwd, - // options to behave more like the native zip's glob support - dot: false, // ignore .dotfiles - noglobstar: true, // treat ** as * - noext: true, // no (a|b) - nobrace: true // no {a,b} - }; - - function findSource(source, next) { - if (glob.hasMagic(source, globOpts)) { - // archiver uses this library but somehow ends up with different results on windows: - // archiver.glob('*') will include subdirectories, but omit their contents on windows - // so we'll use glob directly, and add all of the files it finds - glob(source, globOpts, function(err, files) { - if (err) { - return next(err); - } - async.forEach(files, addSource, next); - }); - } else { - addSource(source, next); - } - } - - function walkDir(fullPath) { - const files = fs.readdirSync(fullPath).map(f => { - const filePath = path.join(fullPath, f); - const stats = fs.statSync(filePath); - if (stats.isDirectory()) { - return walkDir(filePath); - } - return filePath; - }); - return files.reduce((acc, cur) => acc.concat(cur), []); - } - function addSource(source, next) { const fullPath = path.resolve(cwd, source); const destPath = source; @@ -117,15 +127,16 @@ const nodeZip = options => }); } - const sources = Array.isArray(options.source) - ? options.source - : [options.source]; - - async.forEach(sources, findSource, function(err) { + expandSources(cwd, options.source, (err, expandedSources) => { if (err) { return reject(err); } - archive.finalize(); + async.forEach(expandedSources, addSource, err => { + if (err) { + return reject(err); + } + archive.finalize(); + }); }); }); diff --git a/package-lock.json b/package-lock.json index 174031b..62c7a79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -878,12 +878,9 @@ "dev": true }, "async": { - "version": "2.6.1", - "resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz", - "integrity": "sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==", - "requires": { - "lodash": "^4.17.10" - } + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.0.tgz", + "integrity": "sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw==" }, "async-limiter": { "version": "1.0.1", @@ -3868,7 +3865,8 @@ "lodash": { "version": "4.17.19", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz", - "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==" + "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==", + "dev": true }, "lodash.defaults": { "version": "4.2.0", diff --git a/package.json b/package.json index 02a3323..5f7873d 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ ], "dependencies": { "archiver": "^4.0.2", - "async": "^2.6.1", + "async": "^3.2.0", "glob": "^7.1.3", "which": "^1.3.1", "yargs": "^13.2.4"