Skip to content

Commit

Permalink
make expandSources a standalone function
Browse files Browse the repository at this point in the history
nfriedly committed Sep 2, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d195517 commit fcea252
Showing 3 changed files with 60 additions and 51 deletions.
97 changes: 54 additions & 43 deletions lib/bestzip.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

12 changes: 5 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit fcea252

Please sign in to comment.