UNPKG

ancesdir

Version:

Find a specific ancestor/root directory given a starting location and a search parameter

3 lines (2 loc) 1.48 kB
import*as r from"node:path";import o from"node:path";import*as n from"node:fs";const e=new Map,t=(r,o)=>{for(const n of r)e.set(n,o)},i=()=>{e.clear()},l=(r,o)=>{throw new Error(`Could not find marker, "${o}", from given starting location "${r}"`)},m=({from:o,marker:i,includeFrom:m,force:c})=>{r.isAbsolute(o)||(r=>{throw new Error(`The starting path must be absolute, but "${r}" is relative`)})(o);const s=r=>{if(c)return;const n=(r=>e.get(r))(r);if(null!=n)return n;null===n&&l(o,i)},u=(o,n)=>{const e=o.split(r.sep).join("/");return n?`includeFrom:${e}:${i}`:`${e}:${i}`};if(m){const e=u(o,!0),l=s(e);if(null!=l)return l;if(n.existsSync(r.join(o,i)))return t([e],o),o}const f=[];let a=null,d=o;for(;null==a;){const e=u(d,!1);f.push(e);const m=s(e);if(null!=m){a=m;continue}const c=r.dirname(d);c!==d&&null!=c||(t(f,null),l(o,i));const p=r.join(c,i);n.existsSync(p)?(f.push(u(c,!0)),t(f,c),a=c):d=c}return a},c=()=>m({force:!1,from:__dirname,marker:s,includeFrom:!1}),s="package.json",u=(r,o)=>"string"==typeof r?{force:!1,from:r,includeFrom:!1,marker:o??s}:"object"==typeof r&&null!==r?{force:r.force??!1,from:r.from??c(),marker:r.marker??s,includeFrom:r.includeFrom??!1}:{force:!1,from:c(),marker:s,includeFrom:!1},f=(r,o)=>{const n=u(r,o);return m({...n,includeFrom:!1})},a=(r,n)=>{const e=u(r,n);return e.from===c()&&(e.from=o.dirname(e.from)),m({...e,includeFrom:!0})},d=f;export{f as ancesdir,i as clearCache,a as closesdir,d as default}; //# sourceMappingURL=index.js.map