Bump the npm group with 4 updates (#2419)
* Bump the npm group with 4 updates Bumps the npm group with 4 updates: [adm-zip](https://github.com/cthackers/adm-zip), [@eslint/js](https://github.com/eslint/eslint/tree/HEAD/packages/js), [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/eslint-plugin) and [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint/tree/HEAD/packages/parser). Updates `adm-zip` from 0.5.14 to 0.5.15 - [Release notes](https://github.com/cthackers/adm-zip/releases) - [Changelog](https://github.com/cthackers/adm-zip/blob/master/history.md) - [Commits](https://github.com/cthackers/adm-zip/compare/v0.5.14...v0.5.15) Updates `@eslint/js` from 9.8.0 to 9.9.0 - [Release notes](https://github.com/eslint/eslint/releases) - [Changelog](https://github.com/eslint/eslint/blob/main/CHANGELOG.md) - [Commits](https://github.com/eslint/eslint/commits/v9.9.0/packages/js) Updates `@typescript-eslint/eslint-plugin` from 8.0.1 to 8.1.0 - [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases) - [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/CHANGELOG.md) - [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v8.1.0/packages/eslint-plugin) Updates `@typescript-eslint/parser` from 8.0.1 to 8.1.0 - [Release notes](https://github.com/typescript-eslint/typescript-eslint/releases) - [Changelog](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/parser/CHANGELOG.md) - [Commits](https://github.com/typescript-eslint/typescript-eslint/commits/v8.1.0/packages/parser) --- updated-dependencies: - dependency-name: adm-zip dependency-type: direct:production update-type: version-update:semver-patch dependency-group: npm - dependency-name: "@eslint/js" dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm - dependency-name: "@typescript-eslint/eslint-plugin" dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm - dependency-name: "@typescript-eslint/parser" dependency-type: direct:development update-type: version-update:semver-minor dependency-group: npm ... Signed-off-by: dependabot[bot] <support@github.com> * Update checked-in dependencies --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
parent
25ad3c8e40
commit
d620faa0b4
103 changed files with 1631 additions and 740 deletions
490
node_modules/adm-zip/adm-zip.js
generated
vendored
490
node_modules/adm-zip/adm-zip.js
generated
vendored
|
|
@ -3,8 +3,9 @@ const pth = require("path");
|
|||
const ZipEntry = require("./zipEntry");
|
||||
const ZipFile = require("./zipFile");
|
||||
|
||||
const get_Bool = (val, def) => (typeof val === "boolean" ? val : def);
|
||||
const get_Str = (val, def) => (typeof val === "string" ? val : def);
|
||||
const get_Bool = (...val) => Utils.findLast(val, (c) => typeof c === "boolean");
|
||||
const get_Str = (...val) => Utils.findLast(val, (c) => typeof c === "string");
|
||||
const get_Fun = (...val) => Utils.findLast(val, (c) => typeof c === "function");
|
||||
|
||||
const defaultOptions = {
|
||||
// option "noSort" : if true it disables files sorting
|
||||
|
|
@ -46,6 +47,10 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
// instanciate utils filesystem
|
||||
const filetools = new Utils(opts);
|
||||
|
||||
if (typeof opts.decoder !== "object" || typeof opts.decoder.encode !== "function" || typeof opts.decoder.decode !== "function") {
|
||||
opts.decoder = Utils.decoder;
|
||||
}
|
||||
|
||||
// if input is file name we retrieve its content
|
||||
if (input && "string" === typeof input) {
|
||||
// load zip file
|
||||
|
|
@ -54,20 +59,20 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
opts.filename = input;
|
||||
inBuffer = filetools.fs.readFileSync(input);
|
||||
} else {
|
||||
throw new Error(Utils.Errors.INVALID_FILENAME);
|
||||
throw Utils.Errors.INVALID_FILENAME();
|
||||
}
|
||||
}
|
||||
|
||||
// create variable
|
||||
const _zip = new ZipFile(inBuffer, opts);
|
||||
|
||||
const { canonical, sanitize } = Utils;
|
||||
const { canonical, sanitize, zipnamefix } = Utils;
|
||||
|
||||
function getEntry(/**Object*/ entry) {
|
||||
if (entry && _zip) {
|
||||
var item;
|
||||
// If entry was given as a file name
|
||||
if (typeof entry === "string") item = _zip.getEntry(entry);
|
||||
if (typeof entry === "string") item = _zip.getEntry(pth.posix.normalize(entry));
|
||||
// if entry was given as a ZipEntry object
|
||||
if (typeof entry === "object" && typeof entry.entryName !== "undefined" && typeof entry.header !== "undefined") item = _zip.getEntry(entry.entryName);
|
||||
|
||||
|
|
@ -84,26 +89,60 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
return join(".", normalize(sep + zipPath.split("\\").join(sep) + sep));
|
||||
}
|
||||
|
||||
function filenameFilter(filterfn) {
|
||||
if (filterfn instanceof RegExp) {
|
||||
// if filter is RegExp wrap it
|
||||
return (function (rx) {
|
||||
return function (filename) {
|
||||
return rx.test(filename);
|
||||
};
|
||||
})(filterfn);
|
||||
} else if ("function" !== typeof filterfn) {
|
||||
// if filter is not function we will replace it
|
||||
return () => true;
|
||||
}
|
||||
return filterfn;
|
||||
}
|
||||
|
||||
// keep last character on folders
|
||||
const relativePath = (local, entry) => {
|
||||
let lastChar = entry.slice(-1);
|
||||
lastChar = lastChar === filetools.sep ? filetools.sep : "";
|
||||
return pth.relative(local, entry) + lastChar;
|
||||
};
|
||||
|
||||
return {
|
||||
/**
|
||||
* Extracts the given entry from the archive and returns the content as a Buffer object
|
||||
* @param entry ZipEntry object or String with the full path of the entry
|
||||
*
|
||||
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
|
||||
* @param {Buffer|string} [pass] - password
|
||||
* @return Buffer or Null in case of error
|
||||
*/
|
||||
readFile: function (/**Object*/ entry, /*String, Buffer*/ pass) {
|
||||
readFile: function (entry, pass) {
|
||||
var item = getEntry(entry);
|
||||
return (item && item.getData(pass)) || null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns how many child elements has on entry (directories) on files it is always 0
|
||||
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
|
||||
* @returns {integer}
|
||||
*/
|
||||
childCount: function (entry) {
|
||||
const item = getEntry(entry);
|
||||
if (item) {
|
||||
return _zip.getChildCount(item);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Asynchronous readFile
|
||||
* @param entry ZipEntry object or String with the full path of the entry
|
||||
* @param callback
|
||||
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
|
||||
* @param {callback} callback
|
||||
*
|
||||
* @return Buffer or Null in case of error
|
||||
*/
|
||||
readFileAsync: function (/**Object*/ entry, /**Function*/ callback) {
|
||||
readFileAsync: function (entry, callback) {
|
||||
var item = getEntry(entry);
|
||||
if (item) {
|
||||
item.getDataAsync(callback);
|
||||
|
|
@ -114,12 +153,12 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
|
||||
/**
|
||||
* Extracts the given entry from the archive and returns the content as plain text in the given encoding
|
||||
* @param entry ZipEntry object or String with the full path of the entry
|
||||
* @param encoding Optional. If no encoding is specified utf8 is used
|
||||
* @param {ZipEntry|string} entry - ZipEntry object or String with the full path of the entry
|
||||
* @param {string} encoding - Optional. If no encoding is specified utf8 is used
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
readAsText: function (/**Object*/ entry, /**String=*/ encoding) {
|
||||
readAsText: function (entry, encoding) {
|
||||
var item = getEntry(entry);
|
||||
if (item) {
|
||||
var data = item.getData();
|
||||
|
|
@ -132,13 +171,13 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
|
||||
/**
|
||||
* Asynchronous readAsText
|
||||
* @param entry ZipEntry object or String with the full path of the entry
|
||||
* @param callback
|
||||
* @param encoding Optional. If no encoding is specified utf8 is used
|
||||
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
|
||||
* @param {callback} callback
|
||||
* @param {string} [encoding] - Optional. If no encoding is specified utf8 is used
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
readAsTextAsync: function (/**Object*/ entry, /**Function*/ callback, /**String=*/ encoding) {
|
||||
readAsTextAsync: function (entry, callback, encoding) {
|
||||
var item = getEntry(entry);
|
||||
if (item) {
|
||||
item.getDataAsync(function (data, err) {
|
||||
|
|
@ -161,11 +200,26 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Remove the entry from the file or the entry and all it's nested directories and files if the given entry is a directory
|
||||
*
|
||||
* @param entry
|
||||
* @param {ZipEntry|string} entry
|
||||
* @returns {void}
|
||||
*/
|
||||
deleteFile: function (/**Object*/ entry) {
|
||||
deleteFile: function (entry, withsubfolders = true) {
|
||||
// @TODO: test deleteFile
|
||||
var item = getEntry(entry);
|
||||
if (item) {
|
||||
_zip.deleteFile(item.entryName, withsubfolders);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove the entry from the file or directory without affecting any nested entries
|
||||
*
|
||||
* @param {ZipEntry|string} entry
|
||||
* @returns {void}
|
||||
*/
|
||||
deleteEntry: function (entry) {
|
||||
// @TODO: test deleteEntry
|
||||
var item = getEntry(entry);
|
||||
if (item) {
|
||||
_zip.deleteEntry(item.entryName);
|
||||
}
|
||||
|
|
@ -174,9 +228,9 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Adds a comment to the zip. The zip must be rewritten after adding the comment.
|
||||
*
|
||||
* @param comment
|
||||
* @param {string} comment
|
||||
*/
|
||||
addZipComment: function (/**String*/ comment) {
|
||||
addZipComment: function (comment) {
|
||||
// @TODO: test addZipComment
|
||||
_zip.comment = comment;
|
||||
},
|
||||
|
|
@ -194,10 +248,10 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
* Adds a comment to a specified zipEntry. The zip must be rewritten after adding the comment
|
||||
* The comment cannot exceed 65535 characters in length
|
||||
*
|
||||
* @param entry
|
||||
* @param comment
|
||||
* @param {ZipEntry} entry
|
||||
* @param {string} comment
|
||||
*/
|
||||
addZipEntryComment: function (/**Object*/ entry, /**String*/ comment) {
|
||||
addZipEntryComment: function (entry, comment) {
|
||||
var item = getEntry(entry);
|
||||
if (item) {
|
||||
item.comment = comment;
|
||||
|
|
@ -207,10 +261,10 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Returns the comment of the specified entry
|
||||
*
|
||||
* @param entry
|
||||
* @param {ZipEntry} entry
|
||||
* @return String
|
||||
*/
|
||||
getZipEntryComment: function (/**Object*/ entry) {
|
||||
getZipEntryComment: function (entry) {
|
||||
var item = getEntry(entry);
|
||||
if (item) {
|
||||
return item.comment || "";
|
||||
|
|
@ -221,10 +275,10 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Updates the content of an existing entry inside the archive. The zip must be rewritten after updating the content
|
||||
*
|
||||
* @param entry
|
||||
* @param content
|
||||
* @param {ZipEntry} entry
|
||||
* @param {Buffer} content
|
||||
*/
|
||||
updateFile: function (/**Object*/ entry, /**Buffer*/ content) {
|
||||
updateFile: function (entry, content) {
|
||||
var item = getEntry(entry);
|
||||
if (item) {
|
||||
item.setData(content);
|
||||
|
|
@ -234,17 +288,18 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Adds a file from the disk to the archive
|
||||
*
|
||||
* @param localPath File to add to zip
|
||||
* @param zipPath Optional path inside the zip
|
||||
* @param zipName Optional name for the file
|
||||
* @param {string} localPath File to add to zip
|
||||
* @param {string} [zipPath] Optional path inside the zip
|
||||
* @param {string} [zipName] Optional name for the file
|
||||
* @param {string} [comment] Optional file comment
|
||||
*/
|
||||
addLocalFile: function (/**String*/ localPath, /**String=*/ zipPath, /**String=*/ zipName, /**String*/ comment) {
|
||||
addLocalFile: function (localPath, zipPath, zipName, comment) {
|
||||
if (filetools.fs.existsSync(localPath)) {
|
||||
// fix ZipPath
|
||||
zipPath = zipPath ? fixPath(zipPath) : "";
|
||||
|
||||
// p - local file name
|
||||
var p = localPath.split("\\").join("/").split("/").pop();
|
||||
const p = pth.win32.basename(pth.win32.normalize(localPath));
|
||||
|
||||
// add file name into zippath
|
||||
zipPath += zipName ? zipName : p;
|
||||
|
|
@ -252,37 +307,77 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
// read file attributes
|
||||
const _attr = filetools.fs.statSync(localPath);
|
||||
|
||||
// get file content
|
||||
const data = _attr.isFile() ? filetools.fs.readFileSync(localPath) : Buffer.alloc(0);
|
||||
|
||||
// if folder
|
||||
if (_attr.isDirectory()) zipPath += filetools.sep;
|
||||
|
||||
// add file into zip file
|
||||
this.addFile(zipPath, filetools.fs.readFileSync(localPath), comment, _attr);
|
||||
this.addFile(zipPath, data, comment, _attr);
|
||||
} else {
|
||||
throw new Error(Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath));
|
||||
throw Utils.Errors.FILE_NOT_FOUND(localPath);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Callback for showing if everything was done.
|
||||
*
|
||||
* @callback doneCallback
|
||||
* @param {Error} err - Error object
|
||||
* @param {boolean} done - was request fully completed
|
||||
*/
|
||||
|
||||
/**
|
||||
* Adds a file from the disk to the archive
|
||||
*
|
||||
* @param {(object|string)} options - options object, if it is string it us used as localPath.
|
||||
* @param {string} options.localPath - Local path to the file.
|
||||
* @param {string} [options.comment] - Optional file comment.
|
||||
* @param {string} [options.zipPath] - Optional path inside the zip
|
||||
* @param {string} [options.zipName] - Optional name for the file
|
||||
* @param {doneCallback} callback - The callback that handles the response.
|
||||
*/
|
||||
addLocalFileAsync: function (options, callback) {
|
||||
options = typeof options === "object" ? options : { localPath: options };
|
||||
const localPath = pth.resolve(options.localPath);
|
||||
const { comment } = options;
|
||||
let { zipPath, zipName } = options;
|
||||
const self = this;
|
||||
|
||||
filetools.fs.stat(localPath, function (err, stats) {
|
||||
if (err) return callback(err, false);
|
||||
// fix ZipPath
|
||||
zipPath = zipPath ? fixPath(zipPath) : "";
|
||||
// p - local file name
|
||||
const p = pth.win32.basename(pth.win32.normalize(localPath));
|
||||
// add file name into zippath
|
||||
zipPath += zipName ? zipName : p;
|
||||
|
||||
if (stats.isFile()) {
|
||||
filetools.fs.readFile(localPath, function (err, data) {
|
||||
if (err) return callback(err, false);
|
||||
self.addFile(zipPath, data, comment, stats);
|
||||
return setImmediate(callback, undefined, true);
|
||||
});
|
||||
} else if (stats.isDirectory()) {
|
||||
zipPath += filetools.sep;
|
||||
self.addFile(zipPath, Buffer.alloc(0), comment, stats);
|
||||
return setImmediate(callback, undefined, true);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Adds a local directory and all its nested files and directories to the archive
|
||||
*
|
||||
* @param localPath
|
||||
* @param zipPath optional path inside zip
|
||||
* @param filter optional RegExp or Function if files match will
|
||||
* be included.
|
||||
* @param {number | object} attr - number as unix file permissions, object as filesystem Stats object
|
||||
* @param {string} localPath - local path to the folder
|
||||
* @param {string} [zipPath] - optional path inside zip
|
||||
* @param {(RegExp|function)} [filter] - optional RegExp or Function if files match will be included.
|
||||
*/
|
||||
addLocalFolder: function (/**String*/ localPath, /**String=*/ zipPath, /**=RegExp|Function*/ filter, /**=number|object*/ attr) {
|
||||
addLocalFolder: function (localPath, zipPath, filter) {
|
||||
// Prepare filter
|
||||
if (filter instanceof RegExp) {
|
||||
// if filter is RegExp wrap it
|
||||
filter = (function (rx) {
|
||||
return function (filename) {
|
||||
return rx.test(filename);
|
||||
};
|
||||
})(filter);
|
||||
} else if ("function" !== typeof filter) {
|
||||
// if filter is not function we will replace it
|
||||
filter = function () {
|
||||
return true;
|
||||
};
|
||||
}
|
||||
filter = filenameFilter(filter);
|
||||
|
||||
// fix ZipPath
|
||||
zipPath = zipPath ? fixPath(zipPath) : "";
|
||||
|
|
@ -295,43 +390,29 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
const self = this;
|
||||
|
||||
if (items.length) {
|
||||
items.forEach(function (filepath) {
|
||||
var p = pth.relative(localPath, filepath).split("\\").join("/"); //windows fix
|
||||
for (const filepath of items) {
|
||||
const p = pth.join(zipPath, relativePath(localPath, filepath));
|
||||
if (filter(p)) {
|
||||
var stats = filetools.fs.statSync(filepath);
|
||||
if (stats.isFile()) {
|
||||
self.addFile(zipPath + p, filetools.fs.readFileSync(filepath), "", attr ? attr : stats);
|
||||
} else {
|
||||
self.addFile(zipPath + p + "/", Buffer.alloc(0), "", attr ? attr : stats);
|
||||
}
|
||||
self.addLocalFile(filepath, pth.dirname(p));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error(Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath));
|
||||
throw Utils.Errors.FILE_NOT_FOUND(localPath);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Asynchronous addLocalFile
|
||||
* @param localPath
|
||||
* @param callback
|
||||
* @param zipPath optional path inside zip
|
||||
* @param filter optional RegExp or Function if files match will
|
||||
* Asynchronous addLocalFolder
|
||||
* @param {string} localPath
|
||||
* @param {callback} callback
|
||||
* @param {string} [zipPath] optional path inside zip
|
||||
* @param {RegExp|function} [filter] optional RegExp or Function if files match will
|
||||
* be included.
|
||||
*/
|
||||
addLocalFolderAsync: function (/*String*/ localPath, /*Function*/ callback, /*String*/ zipPath, /*RegExp|Function*/ filter) {
|
||||
if (filter instanceof RegExp) {
|
||||
filter = (function (rx) {
|
||||
return function (filename) {
|
||||
return rx.test(filename);
|
||||
};
|
||||
})(filter);
|
||||
} else if ("function" !== typeof filter) {
|
||||
filter = function () {
|
||||
return true;
|
||||
};
|
||||
}
|
||||
addLocalFolderAsync: function (localPath, callback, zipPath, filter) {
|
||||
// Prepare filter
|
||||
filter = filenameFilter(filter);
|
||||
|
||||
// fix ZipPath
|
||||
zipPath = zipPath ? fixPath(zipPath) : "";
|
||||
|
|
@ -342,7 +423,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
var self = this;
|
||||
filetools.fs.open(localPath, "r", function (err) {
|
||||
if (err && err.code === "ENOENT") {
|
||||
callback(undefined, Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath));
|
||||
callback(undefined, Utils.Errors.FILE_NOT_FOUND(localPath));
|
||||
} else if (err) {
|
||||
callback(undefined, err);
|
||||
} else {
|
||||
|
|
@ -353,7 +434,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
i += 1;
|
||||
if (i < items.length) {
|
||||
var filepath = items[i];
|
||||
var p = pth.relative(localPath, filepath).split("\\").join("/"); //windows fix
|
||||
var p = relativePath(localPath, filepath).split("\\").join("/"); //windows fix
|
||||
p = p
|
||||
.normalize("NFD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
|
|
@ -391,24 +472,99 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
},
|
||||
|
||||
/**
|
||||
* Adds a local directory and all its nested files and directories to the archive
|
||||
*
|
||||
* @param {object | string} options - options object, if it is string it us used as localPath.
|
||||
* @param {string} options.localPath - Local path to the folder.
|
||||
* @param {string} [options.zipPath] - optional path inside zip.
|
||||
* @param {RegExp|function} [options.filter] - optional RegExp or Function if files match will be included.
|
||||
* @param {function|string} [options.namefix] - optional function to help fix filename
|
||||
* @param {doneCallback} callback - The callback that handles the response.
|
||||
*
|
||||
*/
|
||||
addLocalFolderAsync2: function (options, callback) {
|
||||
const self = this;
|
||||
options = typeof options === "object" ? options : { localPath: options };
|
||||
localPath = pth.resolve(fixPath(options.localPath));
|
||||
let { zipPath, filter, namefix } = options;
|
||||
|
||||
if (filter instanceof RegExp) {
|
||||
filter = (function (rx) {
|
||||
return function (filename) {
|
||||
return rx.test(filename);
|
||||
};
|
||||
})(filter);
|
||||
} else if ("function" !== typeof filter) {
|
||||
filter = function () {
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
// fix ZipPath
|
||||
zipPath = zipPath ? fixPath(zipPath) : "";
|
||||
|
||||
// Check Namefix function
|
||||
if (namefix == "latin1") {
|
||||
namefix = (str) =>
|
||||
str
|
||||
.normalize("NFD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
.replace(/[^\x20-\x7E]/g, ""); // accent fix (latin1 characers only)
|
||||
}
|
||||
|
||||
if (typeof namefix !== "function") namefix = (str) => str;
|
||||
|
||||
// internal, create relative path + fix the name
|
||||
const relPathFix = (entry) => pth.join(zipPath, namefix(relativePath(localPath, entry)));
|
||||
const fileNameFix = (entry) => pth.win32.basename(pth.win32.normalize(namefix(entry)));
|
||||
|
||||
filetools.fs.open(localPath, "r", function (err) {
|
||||
if (err && err.code === "ENOENT") {
|
||||
callback(undefined, Utils.Errors.FILE_NOT_FOUND(localPath));
|
||||
} else if (err) {
|
||||
callback(undefined, err);
|
||||
} else {
|
||||
filetools.findFilesAsync(localPath, function (err, fileEntries) {
|
||||
if (err) return callback(err);
|
||||
fileEntries = fileEntries.filter((dir) => filter(relPathFix(dir)));
|
||||
if (!fileEntries.length) callback(undefined, false);
|
||||
|
||||
setImmediate(
|
||||
fileEntries.reverse().reduce(function (next, entry) {
|
||||
return function (err, done) {
|
||||
if (err || done === false) return setImmediate(next, err, false);
|
||||
|
||||
self.addLocalFileAsync(
|
||||
{
|
||||
localPath: entry,
|
||||
zipPath: pth.dirname(relPathFix(entry)),
|
||||
zipName: fileNameFix(entry)
|
||||
},
|
||||
next
|
||||
);
|
||||
};
|
||||
}, callback)
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Adds a local directory and all its nested files and directories to the archive
|
||||
*
|
||||
* @param {string} localPath - path where files will be extracted
|
||||
* @param {object} props - optional properties
|
||||
* @param {string} props.zipPath - optional path inside zip
|
||||
* @param {regexp, function} props.filter - RegExp or Function if files match will be included.
|
||||
* @param {string} [props.zipPath] - optional path inside zip
|
||||
* @param {RegExp|function} [props.filter] - optional RegExp or Function if files match will be included.
|
||||
* @param {function|string} [props.namefix] - optional function to help fix filename
|
||||
*/
|
||||
addLocalFolderPromise: function (/*String*/ localPath, /* object */ props) {
|
||||
addLocalFolderPromise: function (localPath, props) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { filter, zipPath } = Object.assign({}, props);
|
||||
this.addLocalFolderAsync(
|
||||
localPath,
|
||||
(done, err) => {
|
||||
if (err) reject(err);
|
||||
if (done) resolve(this);
|
||||
},
|
||||
zipPath,
|
||||
filter
|
||||
);
|
||||
this.addLocalFolderAsync2(Object.assign({ localPath }, props), (err, done) => {
|
||||
if (err) reject(err);
|
||||
if (done) resolve(this);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
|
|
@ -419,17 +575,18 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
*
|
||||
* @param {string} entryName
|
||||
* @param {Buffer | string} content - file content as buffer or utf8 coded string
|
||||
* @param {string} comment - file comment
|
||||
* @param {number | object} attr - number as unix file permissions, object as filesystem Stats object
|
||||
* @param {string} [comment] - file comment
|
||||
* @param {number | object} [attr] - number as unix file permissions, object as filesystem Stats object
|
||||
*/
|
||||
addFile: function (/**String*/ entryName, /**Buffer*/ content, /**String*/ comment, /**Number*/ attr) {
|
||||
addFile: function (entryName, content, comment, attr) {
|
||||
entryName = zipnamefix(entryName);
|
||||
let entry = getEntry(entryName);
|
||||
const update = entry != null;
|
||||
|
||||
// prepare new entry
|
||||
if (!update) {
|
||||
entry = new ZipEntry();
|
||||
entry.entryName = Utils.canonical(entryName);
|
||||
entry = new ZipEntry(opts);
|
||||
entry.entryName = entryName;
|
||||
}
|
||||
entry.comment = comment || "";
|
||||
|
||||
|
|
@ -471,9 +628,10 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Returns an array of ZipEntry objects representing the files and folders inside the archive
|
||||
*
|
||||
* @return Array
|
||||
* @param {string} [password]
|
||||
* @returns Array
|
||||
*/
|
||||
getEntries: function (/**String*/ password) {
|
||||
getEntries: function (password) {
|
||||
_zip.password = password;
|
||||
return _zip ? _zip.entries : [];
|
||||
},
|
||||
|
|
@ -481,7 +639,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Returns a ZipEntry object representing the file or folder specified by ``name``.
|
||||
*
|
||||
* @param name
|
||||
* @param {string} name
|
||||
* @return ZipEntry
|
||||
*/
|
||||
getEntry: function (/**String*/ name) {
|
||||
|
|
@ -500,34 +658,24 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
* Extracts the given entry to the given targetPath
|
||||
* If the entry is a directory inside the archive, the entire directory and it's subdirectories will be extracted
|
||||
*
|
||||
* @param entry ZipEntry object or String with the full path of the entry
|
||||
* @param targetPath Target folder where to write the file
|
||||
* @param maintainEntryPath If maintainEntryPath is true and the entry is inside a folder, the entry folder
|
||||
* will be created in targetPath as well. Default is TRUE
|
||||
* @param overwrite If the file already exists at the target path, the file will be overwriten if this is true.
|
||||
* Default is FALSE
|
||||
* @param keepOriginalPermission The file will be set as the permission from the entry if this is true.
|
||||
* Default is FALSE
|
||||
* @param outFileName String If set will override the filename of the extracted file (Only works if the entry is a file)
|
||||
* @param {string|ZipEntry} entry - ZipEntry object or String with the full path of the entry
|
||||
* @param {string} targetPath - Target folder where to write the file
|
||||
* @param {boolean} [maintainEntryPath=true] - If maintainEntryPath is true and the entry is inside a folder, the entry folder will be created in targetPath as well. Default is TRUE
|
||||
* @param {boolean} [overwrite=false] - If the file already exists at the target path, the file will be overwriten if this is true.
|
||||
* @param {boolean} [keepOriginalPermission=false] - The file will be set as the permission from the entry if this is true.
|
||||
* @param {string} [outFileName] - String If set will override the filename of the extracted file (Only works if the entry is a file)
|
||||
*
|
||||
* @return Boolean
|
||||
*/
|
||||
extractEntryTo: function (
|
||||
/**Object*/ entry,
|
||||
/**String*/ targetPath,
|
||||
/**Boolean*/ maintainEntryPath,
|
||||
/**Boolean*/ overwrite,
|
||||
/**Boolean*/ keepOriginalPermission,
|
||||
/**String**/ outFileName
|
||||
) {
|
||||
overwrite = get_Bool(overwrite, false);
|
||||
keepOriginalPermission = get_Bool(keepOriginalPermission, false);
|
||||
maintainEntryPath = get_Bool(maintainEntryPath, true);
|
||||
outFileName = get_Str(outFileName, get_Str(keepOriginalPermission, undefined));
|
||||
extractEntryTo: function (entry, targetPath, maintainEntryPath, overwrite, keepOriginalPermission, outFileName) {
|
||||
overwrite = get_Bool(false, overwrite);
|
||||
keepOriginalPermission = get_Bool(false, keepOriginalPermission);
|
||||
maintainEntryPath = get_Bool(true, maintainEntryPath);
|
||||
outFileName = get_Str(keepOriginalPermission, outFileName);
|
||||
|
||||
var item = getEntry(entry);
|
||||
if (!item) {
|
||||
throw new Error(Utils.Errors.NO_ENTRY);
|
||||
throw Utils.Errors.NO_ENTRY();
|
||||
}
|
||||
|
||||
var entryName = canonical(item.entryName);
|
||||
|
|
@ -540,7 +688,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
if (child.isDirectory) return;
|
||||
var content = child.getData();
|
||||
if (!content) {
|
||||
throw new Error(Utils.Errors.CANT_EXTRACT_FILE);
|
||||
throw Utils.Errors.CANT_EXTRACT_FILE();
|
||||
}
|
||||
var name = canonical(child.entryName);
|
||||
var childName = sanitize(targetPath, maintainEntryPath ? name : pth.basename(name));
|
||||
|
|
@ -552,10 +700,10 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
}
|
||||
|
||||
var content = item.getData(_zip.password);
|
||||
if (!content) throw new Error(Utils.Errors.CANT_EXTRACT_FILE);
|
||||
if (!content) throw Utils.Errors.CANT_EXTRACT_FILE();
|
||||
|
||||
if (filetools.fs.existsSync(target) && !overwrite) {
|
||||
throw new Error(Utils.Errors.CANT_OVERRIDE);
|
||||
throw Utils.Errors.CANT_OVERRIDE();
|
||||
}
|
||||
// The reverse operation for attr depend on method addFile()
|
||||
const fileAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
|
||||
|
|
@ -566,7 +714,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
|
||||
/**
|
||||
* Test the archive
|
||||
*
|
||||
* @param {string} [pass]
|
||||
*/
|
||||
test: function (pass) {
|
||||
if (!_zip) {
|
||||
|
|
@ -592,28 +740,28 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Extracts the entire archive to the given location
|
||||
*
|
||||
* @param targetPath Target location
|
||||
* @param overwrite If the file already exists at the target path, the file will be overwriten if this is true.
|
||||
* @param {string} targetPath Target location
|
||||
* @param {boolean} [overwrite=false] If the file already exists at the target path, the file will be overwriten if this is true.
|
||||
* Default is FALSE
|
||||
* @param keepOriginalPermission The file will be set as the permission from the entry if this is true.
|
||||
* @param {boolean} [keepOriginalPermission=false] The file will be set as the permission from the entry if this is true.
|
||||
* Default is FALSE
|
||||
* @param {string|Buffer} [pass] password
|
||||
*/
|
||||
extractAllTo: function (/**String*/ targetPath, /**Boolean*/ overwrite, /**Boolean*/ keepOriginalPermission, /*String, Buffer*/ pass) {
|
||||
overwrite = get_Bool(overwrite, false);
|
||||
extractAllTo: function (targetPath, overwrite, keepOriginalPermission, pass) {
|
||||
keepOriginalPermission = get_Bool(false, keepOriginalPermission);
|
||||
pass = get_Str(keepOriginalPermission, pass);
|
||||
keepOriginalPermission = get_Bool(keepOriginalPermission, false);
|
||||
if (!_zip) {
|
||||
throw new Error(Utils.Errors.NO_ZIP);
|
||||
}
|
||||
overwrite = get_Bool(false, overwrite);
|
||||
if (!_zip) throw Utils.Errors.NO_ZIP();
|
||||
|
||||
_zip.entries.forEach(function (entry) {
|
||||
var entryName = sanitize(targetPath, canonical(entry.entryName.toString()));
|
||||
var entryName = sanitize(targetPath, canonical(entry.entryName));
|
||||
if (entry.isDirectory) {
|
||||
filetools.makeDir(entryName);
|
||||
return;
|
||||
}
|
||||
var content = entry.getData(pass);
|
||||
if (!content) {
|
||||
throw new Error(Utils.Errors.CANT_EXTRACT_FILE);
|
||||
throw Utils.Errors.CANT_EXTRACT_FILE();
|
||||
}
|
||||
// The reverse operation for attr depend on method addFile()
|
||||
const fileAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
|
||||
|
|
@ -621,7 +769,7 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
try {
|
||||
filetools.fs.utimesSync(entryName, entry.header.time, entry.header.time);
|
||||
} catch (err) {
|
||||
throw new Error(Utils.Errors.CANT_EXTRACT_FILE);
|
||||
throw Utils.Errors.CANT_EXTRACT_FILE();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
|
@ -629,18 +777,17 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Asynchronous extractAllTo
|
||||
*
|
||||
* @param targetPath Target location
|
||||
* @param overwrite If the file already exists at the target path, the file will be overwriten if this is true.
|
||||
* @param {string} targetPath Target location
|
||||
* @param {boolean} [overwrite=false] If the file already exists at the target path, the file will be overwriten if this is true.
|
||||
* Default is FALSE
|
||||
* @param keepOriginalPermission The file will be set as the permission from the entry if this is true.
|
||||
* @param {boolean} [keepOriginalPermission=false] The file will be set as the permission from the entry if this is true.
|
||||
* Default is FALSE
|
||||
* @param callback The callback will be executed when all entries are extracted successfully or any error is thrown.
|
||||
* @param {function} callback The callback will be executed when all entries are extracted successfully or any error is thrown.
|
||||
*/
|
||||
extractAllToAsync: function (/**String*/ targetPath, /**Boolean*/ overwrite, /**Boolean*/ keepOriginalPermission, /**Function*/ callback) {
|
||||
if (typeof overwrite === "function" && !callback) callback = overwrite;
|
||||
overwrite = get_Bool(overwrite, false);
|
||||
if (typeof keepOriginalPermission === "function" && !callback) callback = keepOriginalPermission;
|
||||
keepOriginalPermission = get_Bool(keepOriginalPermission, false);
|
||||
extractAllToAsync: function (targetPath, overwrite, keepOriginalPermission, callback) {
|
||||
callback = get_Fun(overwrite, keepOriginalPermission, callback);
|
||||
keepOriginalPermission = get_Bool(false, keepOriginalPermission);
|
||||
overwrite = get_Bool(false, overwrite);
|
||||
if (!callback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.extractAllToAsync(targetPath, overwrite, keepOriginalPermission, function (err) {
|
||||
|
|
@ -653,13 +800,13 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
});
|
||||
}
|
||||
if (!_zip) {
|
||||
callback(new Error(Utils.Errors.NO_ZIP));
|
||||
callback(Utils.Errors.NO_ZIP());
|
||||
return;
|
||||
}
|
||||
|
||||
targetPath = pth.resolve(targetPath);
|
||||
// convert entryName to
|
||||
const getPath = (entry) => sanitize(targetPath, pth.normalize(canonical(entry.entryName.toString())));
|
||||
const getPath = (entry) => sanitize(targetPath, pth.normalize(canonical(entry.entryName)));
|
||||
const getError = (msg, file) => new Error(msg + ': "' + file + '"');
|
||||
|
||||
// separate directories from files
|
||||
|
|
@ -694,13 +841,13 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
const entryName = pth.normalize(canonical(entry.entryName.toString()));
|
||||
const entryName = pth.normalize(canonical(entry.entryName));
|
||||
const filePath = sanitize(targetPath, entryName);
|
||||
entry.getDataAsync(function (content, err_1) {
|
||||
if (err_1) {
|
||||
next(new Error(err_1));
|
||||
next(err_1);
|
||||
} else if (!content) {
|
||||
next(new Error(Utils.Errors.CANT_EXTRACT_FILE));
|
||||
next(Utils.Errors.CANT_EXTRACT_FILE());
|
||||
} else {
|
||||
// The reverse operation for attr depend on method addFile()
|
||||
const fileAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
|
||||
|
|
@ -726,10 +873,10 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Writes the newly created zip file to disk at the specified location or if a zip was opened and no ``targetFileName`` is provided, it will overwrite the opened zip
|
||||
*
|
||||
* @param targetFileName
|
||||
* @param callback
|
||||
* @param {string} targetFileName
|
||||
* @param {function} callback
|
||||
*/
|
||||
writeZip: function (/**String*/ targetFileName, /**Function*/ callback) {
|
||||
writeZip: function (targetFileName, callback) {
|
||||
if (arguments.length === 1) {
|
||||
if (typeof targetFileName === "function") {
|
||||
callback = targetFileName;
|
||||
|
|
@ -749,6 +896,15 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
}
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} targetFileName
|
||||
* @param {object} [props]
|
||||
* @param {boolean} [props.overwrite=true] If the file already exists at the target path, the file will be overwriten if this is true.
|
||||
* @param {boolean} [props.perm] The file will be set as the permission from the entry if this is true.
|
||||
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
writeZipPromise: function (/**String*/ targetFileName, /* object */ props) {
|
||||
const { overwrite, perm } = Object.assign({ overwrite: true }, props);
|
||||
|
||||
|
|
@ -764,6 +920,9 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @returns {Promise<Buffer>} A promise to the Buffer.
|
||||
*/
|
||||
toBufferPromise: function () {
|
||||
return new Promise((resolve, reject) => {
|
||||
_zip.toAsyncBuffer(resolve, reject);
|
||||
|
|
@ -773,10 +932,13 @@ module.exports = function (/**String*/ input, /** object */ options) {
|
|||
/**
|
||||
* Returns the content of the entire zip file as a Buffer object
|
||||
*
|
||||
* @return Buffer
|
||||
* @prop {function} [onSuccess]
|
||||
* @prop {function} [onFail]
|
||||
* @prop {function} [onItemStart]
|
||||
* @prop {function} [onItemEnd]
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
toBuffer: function (/**Function=*/ onSuccess, /**Function=*/ onFail, /**Function=*/ onItemStart, /**Function=*/ onItemEnd) {
|
||||
this.valueOf = 2;
|
||||
toBuffer: function (onSuccess, onFail, onItemStart, onItemEnd) {
|
||||
if (typeof onSuccess === "function") {
|
||||
_zip.toAsyncBuffer(onSuccess, onFail, onItemStart, onItemEnd);
|
||||
return null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue