@wrote/read-dir-structure
Version:
Reads directory structure.
10 lines (8 loc) • 2.82 kB
JavaScript
const fs = require('fs');
const path = require('path');
const os = require('os'); const l=fs.lstat,m=fs.readdir;const n=(a,b=0,d=!1)=>{if(0===b&&!d)return a;a=a.split("\n",d?b+1:void 0);return d?a[a.length-1]:a.slice(b).join("\n")},p=(a,b=!1)=>n(a,2+(b?1:0)),q=a=>{({callee:{caller:a}}=a);return a};const r=os.homedir;const t=/\s+at.*(?:\(|\s)(.*)\)?/,u=/^(?:(?:(?:node|(?:internal\/[\w/]*|.*node_modules\/(?:IGNORED_MODULES)\/.*)?\w+)\.js:\d+:\d+)|native)/,v=r(),w=a=>{const {pretty:b=!1,ignoredModules:d=["pirates"]}={},e=d.join("|"),f=new RegExp(u.source.replace("IGNORED_MODULES",e));return a.replace(/\\/g,"/").split("\n").filter(c=>{c=c.match(t);if(null===c||!c[1])return!0;c=c[1];return c.includes(".app/Contents/Resources/electron.asar")||c.includes(".app/Contents/Resources/default_app.asar")?!1:!f.test(c)}).filter(c=>
c.trim()).map(c=>b?c.replace(t,(g,h)=>g.replace(h,h.replace(v,"~"))):c).join("\n")};function x(a,b,d=!1){return function(e){var f=q(arguments),{stack:c}=Error();const g=n(c,2,!0),h=(c=e instanceof Error)?e.message:e;f=[`Error: ${h}`,...null!==f&&a===f||d?[b]:[g,b]].join("\n");f=w(f);return Object.assign(c?e:Error(),{message:h,stack:f})}};function y(a){var {stack:b}=Error();const d=q(arguments);b=p(b,a);return x(d,b,a)};async function z(a,b,d){const e=y(!0);if("function"!=typeof a)throw Error("Function must be passed.");if(!a.length)throw Error(`Function${a.name?` ${a.name}`:""} does not accept any arguments.`);return await new Promise((f,c)=>{const g=(k,D)=>k?(k=e(k),c(k)):f(d||D);let h=[g];Array.isArray(b)?h=[...b,g]:1<Array.from(arguments).length&&(h=[b,g]);a(...h)})};const A=path.join,B=path.relative;async function C(a,b){b=b.map(async d=>{const e=A(a,d);return{lstat:await z(l,e),path:e,relativePath:d}});return await Promise.all(b)}const E=a=>a.lstat.isDirectory(),F=a=>!a.lstat.isDirectory();
async function G(a,b={}){if(!a)throw Error("Please specify a path to the directory");const {ignore:d=[]}=b;if(!(await z(l,a)).isDirectory())throw b=Error("Path is not a directory"),b.code="ENOTDIR",b;b=await z(m,a);var e=await C(a,b);b=e.filter(E);e=e.filter(F).reduce((f,c)=>{var g=c.lstat.isDirectory()?"Directory":c.lstat.isFile()?"File":c.lstat.isSymbolicLink()?"SymbolicLink":void 0;return{...f,[c.relativePath]:{type:g}}},{});b=await b.reduce(async(f,{path:c,relativePath:g})=>{const h=B(a,c);if(d.includes(h))return f;
f=await f;c=await G(c);return{...f,[g]:c}},{});return{content:{...e,...b},type:"Directory"}}const H=(a,b)=>{let d=[],e=[];Object.keys(a).forEach(c=>{const {type:g}=a[c];"File"==g?d.push(A(b,c)):"Directory"==g&&e.push(c)});const f=e.reduce((c,g)=>{const {content:h}=a[g];g=H(h,A(b,g));return[...c,...g]},[]);return[...d,...f]};module.exports={_readDirStructure:G,_getFiles:H};
//# sourceMappingURL=depack.js.map