UNPKG

rafale

Version:

Deploy every services, everywhere, in just a few seconds

1 lines 4.38 kB
var fs=require("fs"),pth=require("path");fs.existsSync=fs.existsSync||pth.existsSync;var ZipEntry=require("./zipEntry"),ZipFile=require("./zipFile"),Utils=require("./util");module.exports=function(t){function e(t){if(t&&r){var e;if("string"==typeof t&&(e=r.getEntry(t)),"object"==typeof t&&void 0!=t.entryName&&void 0!=t.header&&(e=r.getEntry(t.entryName)),e)return e}return null}var r=void 0,n="";if(t&&"string"==typeof t){if(!fs.existsSync(t))throw Utils.Errors.INVALID_FILENAME;n=t,r=new ZipFile(t,Utils.Constants.FILE)}else r=t&&Buffer.isBuffer(t)?new ZipFile(t,Utils.Constants.BUFFER):new ZipFile(null,Utils.Constants.NONE);return{readFile:function(t){var r=e(t);return r&&r.getData()||null},readFileAsync:function(t,r){var n=e(t);n?n.getDataAsync(r):r(null,"getEntry failed for:"+t)},readAsText:function(t,r){var n=e(t);if(n){var i=n.getData();if(i&&i.length)return i.toString(r||"utf8")}return""},readAsTextAsync:function(t,r,n){var i=e(t);i?i.getDataAsync(function(t){r(t&&t.length?t.toString(n||"utf8"):"")}):r("")},deleteFile:function(t){var n=e(t);n&&r.deleteEntry(n.entryName)},addZipComment:function(t){r.comment=t},getZipComment:function(){return r.comment||""},addZipEntryComment:function(t,r){var n=e(t);n&&(n.comment=r)},getZipEntryComment:function(t){var r=e(t);return r?r.comment||"":""},updateFile:function(t,r){var n=e(t);n&&n.setData(r)},addLocalFile:function(t,e,r){if(!fs.existsSync(t))throw Utils.Errors.FILE_NOT_FOUND.replace("%s",t);e?(e=e.split("\\").join("/"),"/"!=e.charAt(e.length-1)&&(e+="/")):e="";var n=t.split("\\").join("/").split("/").pop();r?this.addFile(e+r,fs.readFileSync(t),"",0):this.addFile(e+n,fs.readFileSync(t),"",0)},addLocalFolder:function(t,e,r){void 0===r?r=function(){return!0}:r instanceof RegExp&&(r=function(t){return function(e){return t.test(e)}}(r)),e?(e=e.split("\\").join("/"),"/"!=e.charAt(e.length-1)&&(e+="/")):e="",t=t.split("\\").join("/"),t=pth.normalize(t),"/"!=t.charAt(t.length-1)&&(t+="/");var n=t;if("win32"==require("os").platform()&&(n=n.replace(/\.\./g,"...")),!fs.existsSync(t))throw Utils.Errors.FILE_NOT_FOUND.replace("%s",t);var i=Utils.findFiles(t),o=this;i.length&&i.forEach(function(t){var n=pth.normalize(t);r(n)&&(n.charAt(n.length-1)!==pth.sep?(n=n.replace(/\\/g,"/"),o.addFile(e+n,fs.readFileSync(t),"",0)):(n=n.replace(/\\/g,"/"),o.addFile(e+n,new Buffer(0),"",0)))})},addFile:function(t,e,n,i){var o=new ZipEntry;o.entryName=t,o.comment=n||"",o.attr=i||438,o.isDirectory&&e.length,o.setData(e),r.setEntry(o)},getEntries:function(){return r?r.entries:[]},getEntry:function(t){return e(t)},extractEntryTo:function(t,n,i,o){o=o||!1,i="undefined"==typeof i?!0:i;var s=e(t);if(!s)throw Utils.Errors.NO_ENTRY;var a=pth.resolve(n,i?s.entryName:pth.basename(s.entryName));if(s.isDirectory){a=pth.resolve(a,"..");var l=r.getEntryChildren(s);return l.forEach(function(t){if(!t.isDirectory){var e=t.getData();if(!e)throw Utils.Errors.CANT_EXTRACT_FILE;Utils.writeFileTo(pth.resolve(n,i?t.entryName:t.entryName.substr(s.entryName.length)),e,o)}}),!0}var f=s.getData();if(!f)throw Utils.Errors.CANT_EXTRACT_FILE;if(fs.existsSync(a)&&!o)throw Utils.Errors.CANT_OVERRIDE;return Utils.writeFileTo(a,f,o),!0},extractAllTo:function(t,e){if(e=e||!1,!r)throw Utils.Errors.NO_ZIP;r.entries.forEach(function(r){if(r.isDirectory)return void Utils.makeDir(pth.resolve(t,r.entryName.toString()));var n=r.getData();if(!n)throw Utils.Errors.CANT_EXTRACT_FILE+"2";Utils.writeFileTo(pth.resolve(t,r.entryName.toString()),n,e)})},extractAllToAsync:function(t,e,n){if(e=e||!1,!r)return void n(new Error(Utils.Errors.NO_ZIP));var i=r.entries,o=i.length;i.forEach(function(r){return 0>=o?void 0:r.isDirectory?(Utils.makeDir(pth.resolve(t,r.entryName.toString())),void(0==--o&&n(void 0))):void r.getDataAsync(function(i){return 0>=o?void 0:i?void Utils.writeFileToAsync(pth.resolve(t,r.entryName.toString()),i,e,function(t){return 0>=o?void 0:t?void(0==--o&&n(void 0)):(o=0,void n(new Error("Unable to write")))}):(o=0,void n(new Error(Utils.Errors.CANT_EXTRACT_FILE+"2")))})})},writeZip:function(t,e){if(1==arguments.length&&"function"==typeof t&&(e=t,t=""),!t&&n&&(t=n),t){var i=r.compressToBuffer();if(i){var o=Utils.writeFileTo(t,i,!0);"function"==typeof e&&e(o?null:new Error("failed"),"")}}},toBuffer:function(t,e,n,i){return this.valueOf=2,"function"==typeof t?(r.toAsyncBuffer(t,e,n,i),null):r.compressToBuffer()}}};