@avdl/folder-walker
Version:
A powerful, fluent, and configurable directory traversal utility for Node.js with TypeScript support
3 lines (2 loc) • 13.5 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("fs"),e=require("path");function n(t,e){this.v=t,this.k=e}function r(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=Array(e);n<e;n++)r[n]=t[n];return r}function i(t){var e={},r=!1;function i(e,i){return r=!0,{done:!1,value:new n(i=new Promise((function(n){n(t[e](i))})),1)}}return e["undefined"!=typeof Symbol&&Symbol.iterator||"@@iterator"]=function(){return this},e.next=function(t){return r?(r=!1,t):i("next",t)},"function"==typeof t.throw&&(e.throw=function(t){if(r)throw r=!1,t;return i("throw",t)}),"function"==typeof t.return&&(e.return=function(t){return r?(r=!1,t):i("return",t)}),e}function o(t){var e,n,r,i=2;for("undefined"!=typeof Symbol&&(n=Symbol.asyncIterator,r=Symbol.iterator);i--;){if(n&&null!=(e=t[n]))return e.call(t);if(r&&null!=(e=t[r]))return new s(e.call(t));n="@@asyncIterator",r="@@iterator"}throw new TypeError("Object is not async iterable")}function s(t){function e(t){if(Object(t)!==t)return Promise.reject(new TypeError(t+" is not an object."));var e=t.done;return Promise.resolve(t.value).then((function(t){return{value:t,done:e}}))}return(s=function(t){this.s=t,this.n=t.next}).prototype={s:null,n:null,next:function(){return e(this.n.apply(this.s,arguments))},return:function(t){var n=this.s.return;return void 0===n?Promise.resolve({value:t,done:!0}):e(n.apply(this.s,arguments))},throw:function(t){var n=this.s.return;return void 0===n?Promise.reject(t):e(n.apply(this.s,arguments))}},new s(t)}function a(t,e,n,r,i,o,s){try{var a=t[o](s),u=a.value}catch(t){return void n(t)}a.done?e(u):Promise.resolve(u).then(r,i)}function u(t){return function(){var e=this,n=arguments;return new Promise((function(r,i){var o=t.apply(e,n);function s(t){a(o,r,i,s,u,"next",t)}function u(t){a(o,r,i,s,u,"throw",t)}s(void 0)}))}}function c(t){return new n(t,0)}function l(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return r(t,e);var n={}.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var i=0;return function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function f(){var t,e,n="function"==typeof Symbol?Symbol:{},r=n.iterator||"@@iterator",i=n.toStringTag||"@@toStringTag";function o(n,r,i,o){var u=Object.create((r&&r.prototype instanceof a?r:a).prototype);return h(u,"_invoke",function(n,r,i){var o,a,u,c=0,l=i||[],f=!1,h={p:0,n:0,v:t,a:p,f:p.bind(t,4),d:function(e,n){return o=e,a=0,u=t,h.n=n,s}};function p(n,r){for(a=n,u=r,e=0;!f&&c&&!i&&e<l.length;e++){var i,o=l[e],p=h.p,d=o[2];n>3?(i=d===r)&&(u=o[(a=o[4])?5:(a=3,3)],o[4]=o[5]=t):o[0]<=p&&((i=n<2&&p<o[1])?(a=0,h.v=r,h.n=o[1]):p<d&&(i=n<3||o[0]>r||r>d)&&(o[4]=n,o[5]=r,h.n=d,a=0))}if(i||n>1)return s;throw f=!0,r}return function(i,l,d){if(c>1)throw TypeError("Generator is already running");for(f&&1===l&&p(l,d),a=l,u=d;(e=a<2?t:u)||!f;){o||(a?a<3?(a>1&&(h.n=-1),p(a,u)):h.n=u:h.v=u);try{if(c=2,o){if(a||(i="next"),e=o[i]){if(!(e=e.call(o,u)))throw TypeError("iterator result is not an object");if(!e.done)return e;u=e.value,a<2&&(a=0)}else 1===a&&(e=o.return)&&e.call(o),a<2&&(u=TypeError("The iterator does not provide a '"+i+"' method"),a=1);o=t}else if((e=(f=h.n<0)?u:n.call(r,h))!==s)break}catch(e){o=t,a=1,u=e}finally{c=1}}return{value:e,done:f}}}(n,i,o),!0),u}var s={};function a(){}function u(){}function c(){}e=Object.getPrototypeOf;var l=[][r]?e(e([][r]())):(h(e={},r,(function(){return this})),e),p=c.prototype=a.prototype=Object.create(l);function d(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,c):(t.__proto__=c,h(t,i,"GeneratorFunction")),t.prototype=Object.create(p),t}return u.prototype=c,h(p,"constructor",c),h(c,"constructor",u),u.displayName="GeneratorFunction",h(c,i,"GeneratorFunction"),h(p),h(p,i,"Generator"),h(p,r,(function(){return this})),h(p,"toString",(function(){return"[object Generator]"})),(f=function(){return{w:o,m:d}})()}function h(t,e,n,r){var i=Object.defineProperty;try{i({},"",{})}catch(t){i=0}(h=function(t,e,n,r){if(e)i?i(t,e,{value:n,enumerable:!r,configurable:!r,writable:!r}):t[e]=n;else{function o(e,n){h(t,e,(function(t){return this._invoke(e,n,t)}))}o("next",0),o("throw",1),o("return",2)}})(t,e,n,r)}function p(t){if(null!=t){var e=t["function"==typeof Symbol&&Symbol.iterator||"@@iterator"],n=0;if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length))return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}}}throw new TypeError(typeof t+" is not iterable")}function d(t){return function(){return new v(t.apply(this,arguments))}}function v(t){var e,r;function i(e,r){try{var s=t[e](r),a=s.value,u=a instanceof n;Promise.resolve(u?a.v:a).then((function(n){if(u){var r="return"===e?"return":"next";if(!a.k||n.done)return i(r,n);n=t[r](n).value}o(s.done?"return":"normal",n)}),(function(t){i("throw",t)}))}catch(t){o("throw",t)}}function o(t,n){switch(t){case"return":e.resolve({value:n,done:!0});break;case"throw":e.reject(n);break;default:e.resolve({value:n,done:!1})}(e=e.next)?i(e.key,e.arg):r=null}this._invoke=function(t,n){return new Promise((function(o,s){var a={key:t,arg:n,resolve:o,reject:s,next:null};r?r=r.next=a:(e=r=a,i(t,n))}))},"function"!=typeof t.return&&(this.return=void 0)}v.prototype["function"==typeof Symbol&&Symbol.asyncIterator||"@@asyncIterator"]=function(){return this},v.prototype.next=function(t){return this._invoke("next",t)},v.prototype.throw=function(t){return this._invoke("throw",t)},v.prototype.return=function(t){return this._invoke("return",t)},exports.FolderWalker=function(){function n(){this.options={includeFiles:!0,includeFolders:!0,followSymlinks:!1,includeHidden:!1,readContents:!1,customFilters:[]},this.collectingFiles=!1,this.collectingFolders=!1}var r=n.prototype;return r.withDepth=function(t){return this.options.maxDepth=t,this},r.withFilesOnly=function(){return this.options.includeFiles=!0,this.options.includeFolders=!1,this},r.withFoldersOnly=function(){return this.options.includeFiles=!1,this.options.includeFolders=!0,this},r.withFilesAndFolders=function(){return this.options.includeFiles=!0,this.options.includeFolders=!0,this},r.withFileContents=function(t){return void 0===t&&(t="utf-8"),this.options.readContents=!0,this.options.encoding=t,this},r.withExtensions=function(t){return this.options.extensions=t.map((function(t){return t.startsWith(".")?t:"."+t})),this},r.withPattern=function(t){return this.options.pattern=t,this},r.exclude=function(t){return this.options.exclude=t,this},r.include=function(t){return this.options.include=t,this},r.withHidden=function(){return this.options.includeHidden=!0,this},r.withSymlinks=function(){return this.options.followSymlinks=!0,this},r.withMaxFileSize=function(t){return this.options.maxFileSize=t,this},r.withFilter=function(t){return this.options.customFilters||(this.options.customFilters=[]),this.options.customFilters.push(t),this},r.onFile=function(t){return this.fileHandler=t,this},r.onFolder=function(t){return this.folderHandler=t,this},r.onError=function(t){return this.errorHandler=t,this},r.withFileCollector=function(t){return this.collectingFiles=!0,this.fileCollector=t,this},r.withFolderCollector=function(t){return this.collectingFolders=!0,this.folderCollector=t,this},r.execute=function(){var t=u(f().m((function t(e){var n,r,i,o;return f().w((function(t){for(;;)switch(t.n){case 0:return this.rootPath=e,n=[],r=[],i=this.fileHandler,o=this.folderHandler,this.collectingFiles&&this.fileHandler&&(this.fileHandler=function(){var t=u(f().m((function t(e){var r;return f().w((function(t){for(;;)switch(t.n){case 0:return t.n=1,i(e);case 1:return void 0!==(r=t.v)&&n.push(r),t.a(2,r)}}),t)})));return function(e){return t.apply(this,arguments)}}()),this.collectingFolders&&this.folderHandler&&(this.folderHandler=function(){var t=u(f().m((function t(e){var n;return f().w((function(t){for(;;)switch(t.n){case 0:return t.n=1,o(e);case 1:return void 0!==(n=t.v)&&r.push(n),t.a(2,n)}}),t)})));return function(e){return t.apply(this,arguments)}}()),t.n=1,this.walk(e,0);case 1:if(this.fileHandler=i,this.folderHandler=o,!this.collectingFiles){t.n=4;break}if(!this.fileCollector){t.n=3;break}return t.n=2,this.fileCollector(n);case 2:return t.a(2,t.v);case 3:return t.a(2,n);case 4:if(!this.collectingFolders){t.n=7;break}if(!this.folderCollector){t.n=6;break}return t.n=5,this.folderCollector(r);case 5:return t.a(2,t.v);case 6:return t.a(2,r);case 7:return t.a(2,void 0)}}),t,this)})));return function(e){return t.apply(this,arguments)}}(),r.files=function(t){return t&&(this.rootPath=t),this.createAsyncIterable("file")},r.folders=function(t){return t&&(this.rootPath=t),this.createAsyncIterable("folder")},r.entries=function(t){return t&&(this.rootPath=t),this.createAsyncIterable("all")},r.createAsyncIterable=function(t){var e=this;return d(f().m((function n(){return f().w((function(n){for(;;)switch(n.n){case 0:if(e.rootPath){n.n=1;break}throw new Error("Root path not set. Call execute() or use iterator with path.");case 1:return n.d(p(i(o(e.walkIterator(e.rootPath,0,t)))),2);case 2:return n.a(2)}}),n)})))()},r.walkIterator=function(n,r,s){var a=this;return d(f().m((function u(){var h,d,v,y,b,w,m,k;return f().w((function(u){for(;;)switch(u.n){case 0:if(!(void 0!==a.options.maxDepth&&r>a.options.maxDepth)){u.n=1;break}return u.a(2);case 1:return u.p=1,u.n=2,c(t.promises.readdir(n));case 2:h=l(u.v);case 3:if((d=h()).done){u.n=18;break}if(y=e.join(n,v=d.value),a.shouldProcess(v,y)){u.n=4;break}return u.a(3,17);case 4:return u.p=4,u.n=5,c(t.promises.stat(y));case 5:if(!(b=u.v).isDirectory()){u.n=10;break}if("folder"!==s&&"all"!==s||!a.options.includeFolders){u.n=8;break}return w={path:y,relativePath:e.relative(a.rootPath,y),name:v,stats:b,depth:r,isFile:!1,isDirectory:!0},u.n=6,c(a.passesCustomFilters(w));case 6:if(!u.v){u.n=8;break}if(!a.folderHandler){u.n=7;break}return u.n=7,c(a.folderHandler(w));case 7:return u.n=8,w;case 8:return u.d(p(i(o(a.walkIterator(y,r+1,s)))),9);case 9:u.n=15;break;case 10:if(!b.isFile()){u.n=15;break}if("file"!==s&&"all"!==s||!a.options.includeFiles){u.n=15;break}return u.n=11,c(a.createFileEntry(y,v,b,r));case 11:if(!(k=m=u.v)){u.n=13;break}return u.n=12,c(a.passesCustomFilters(m));case 12:k=u.v;case 13:if(!k){u.n=15;break}if(!a.fileHandler){u.n=14;break}return u.n=14,c(a.fileHandler(m));case 14:return u.n=15,m;case 15:u.n=17;break;case 16:u.p=16,a.handleError(u.v,y);case 17:u.n=3;break;case 18:u.n=20;break;case 19:u.p=19,a.handleError(u.v,n);case 20:return u.a(2)}}),u,null,[[4,16],[1,19]])})))()},r.createFileEntry=function(){var n=u(f().m((function n(r,i,o,s){var a,u;return f().w((function(n){for(;;)switch(n.n){case 0:if(!(this.options.maxFileSize&&o.size>this.options.maxFileSize)){n.n=1;break}return n.a(2,null);case 1:if(a=e.extname(i),!this.options.extensions||this.options.extensions.includes(a)){n.n=2;break}return n.a(2,null);case 2:if(!this.options.pattern||this.options.pattern.test(i)){n.n=3;break}return n.a(2,null);case 3:if(u={path:r,relativePath:e.relative(this.rootPath,r),name:i,ext:a,stats:o,depth:s,isFile:!0,isDirectory:!1},!this.options.readContents){n.n=7;break}return n.p=4,n.n=5,t.promises.readFile(r,this.options.encoding);case 5:u.content=n.v,n.n=7;break;case 6:n.p=6,this.handleError(n.v,r);case 7:return n.a(2,u)}}),n,this,[[4,6]])})));return function(t,e,r,i){return n.apply(this,arguments)}}(),r.walk=function(){var t=u(f().m((function t(e,n){var r,i,s,a;return f().w((function(t){for(;;)switch(t.n){case 0:r=!1,i=!1,t.p=1,a=o(this.walkIterator(e,n,"all"));case 2:return t.n=3,a.next();case 3:if(!(r=!t.v.done)){t.n=5;break}case 4:r=!1,t.n=2;break;case 5:t.n=7;break;case 6:t.p=6,i=!0,s=t.v;case 7:if(t.p=7,t.p=8,!r||null==a.return){t.n=9;break}return t.n=9,a.return();case 9:if(t.p=9,!i){t.n=10;break}throw s;case 10:return t.f(9);case 11:return t.f(7);case 12:return t.a(2)}}),t,this,[[8,,9,11],[1,6,7,12]])})));return function(e,n){return t.apply(this,arguments)}}(),r.shouldProcess=function(t,e){if(!this.options.includeHidden&&t.startsWith("."))return!1;if(this.options.exclude)for(var n,r=l(this.options.exclude);!(n=r()).done;){var i=n.value;if(t===i||e.includes(i))return!1}if(this.options.include){for(var o,s=!1,a=l(this.options.include);!(o=a()).done;){var u=o.value;if(t===u||e.includes(u)){s=!0;break}}if(!s)return!1}return!0},r.passesCustomFilters=function(){var t=u(f().m((function t(e){var n,r,i;return f().w((function(t){for(;;)switch(t.n){case 0:if(this.options.customFilters&&0!==this.options.customFilters.length){t.n=1;break}return t.a(2,!0);case 1:n=l(this.options.customFilters);case 2:if((r=n()).done){t.n=8;break}return i=r.value,t.p=3,t.n=4,i(e);case 4:if(t.v){t.n=5;break}return t.a(2,!1);case 5:t.n=7;break;case 6:return t.p=6,this.handleError(t.v,e.path),t.a(2,!1);case 7:t.n=2;break;case 8:return t.a(2,!0)}}),t,this,[[3,6]])})));return function(e){return t.apply(this,arguments)}}(),r.handleError=function(t,e){this.errorHandler&&this.errorHandler(t,e)},n}();
//# sourceMappingURL=folder-walker.cjs.production.min.js.map