UNPKG

find-remove

Version:

recursively finds files and/or directories by filter options from a start directory onwards and deletes these according to plenty of options you can configure. useful if you want to clean up stuff within a directory in your node.js app.

3 lines (2 loc) 2.31 kB
import e from"fs";import t from"path";import{rimrafSync as n}from"rimraf";function i(){return i=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var i in n)({}).hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},i.apply(null,arguments)}let r,o;function l(t,n){if(!r)return!1;const i=e.statSync(t).mtime.getTime();return r>i+1e3*n}function a(e={}){return void 0!==e.totalRemoved?e.totalRemoved:-2}function c(e={}){return a(e)>=function(e={}){return void 0!==e.limit?e.limit:-1}(e)}function f(e={}){return void 0!==e.maxLevel?e.maxLevel:-1}function s(e={}){var t;return e.age&&null!=(t=e.age.seconds)?t:null}const u=function u(d,m={},v){let x={};if(c(m))return x;let y=!1;const g=e.existsSync(d),p=function(t){try{return e.lstatSync(t),!0}catch(e){return!1}}(d);if(g&&!p)y=!0;else if(g){const n=f(m);void 0!==m.limit&&(m.totalRemoved=void 0!==m.totalRemoved?a(m):0),void 0===v?v=0:v++,v<1?(r=(new Date).getTime(),o=m.test):y=function(e,n,i={}){let r=!1;const o=i.dir;if(o){const a=s(i),u=t.basename(e);Array.isArray(o)?r=-1!==o.indexOf("*")||-1!==o.indexOf(u):(i.regex&&u.match(new RegExp(o))||u===o||"*"===o)&&(r=!0),r&&void 0!==i.limit&&(r=!c(i)),r&&void 0!==i.maxLevel&&n>0&&(r=n<=f(i)),a&&r&&(r=l(e,a))}return r}(d,v,m),(-1===n||v<n)&&e.readdirSync(d).forEach(function(n){var r;const a=t.join(d,n);let f,y=!1;try{f=e.statSync(a)}catch(e){y=!0}if(y);else if(null!=(r=f)&&r.isDirectory()){const e=u(a,m,v);x=i({},x,e),void 0!==m.totalRemoved&&(m.totalRemoved+=Object.keys(e).length)}else if(function(e,n={}){var i;let r=!1;const o=n.extensions?n.extensions:null,a=n.files?n.files:null,f=n.prefix?n.prefix:null,u=null!=(i=n.ignore)?i:null,d=t.basename(e);if(a&&(r=Array.isArray(a)?-1!==a.indexOf("*.*")||-1!==a.indexOf(d):!!(n.regex&&d.match(new RegExp(a))||"*.*"===a)||d===a),!r&&o){const n=t.extname(e);r=Array.isArray(o)?-1!==o.indexOf(n):n===o}if(!r&&f&&(r=0===d.indexOf(f)),r&&void 0!==n.limit&&(r=!c(n)),r&&u&&(r=Array.isArray(u)?!(-1!==u.indexOf(d)):!(d===u)),r){const t=s(n);t&&(r=l(e,t))}return r}(a,m)){let t;if(o)t=!0;else try{e.unlinkSync(a),t=!0}catch(e){}t&&(x[a]=!0,void 0!==m.totalRemoved&&m.totalRemoved++)}})}return y&&(o||n(d),void 0===m.totalRemoved&&(x[d]=!0)),x};export{u as default}; //# sourceMappingURL=find-remove.modern.mjs.map