securityos/node_modules/get-tsconfig/dist/index.mjs

4 lines
15 KiB
JavaScript
Executable File

import p from"path";import B from"fs";import cn from"module";function L(n){return/^\\\\\?\\/.test(n)?n:n.replace(/\\/g,"/")}function S(n,s){for(;;){const t=p.posix.join(n,s);if(B.existsSync(t))return t;const e=p.dirname(n);if(e===n)return;n=e}}const W=/^\.{1,2}(\/.*)?$/,M=n=>L(W.test(n)?n:`./${n}`);function un(n,s=!1){const t=n.length;let e=0,i="",l=0,c=16,a=0,g=0,v=0,k=0,r=0;function F(o,f){let u=0,j=0;for(;u<o||!f;){let T=n.charCodeAt(e);if(T>=48&&T<=57)j=j*16+T-48;else if(T>=65&&T<=70)j=j*16+T-65+10;else if(T>=97&&T<=102)j=j*16+T-97+10;else break;e++,u++}return u<o&&(j=-1),j}function U(o){e=o,i="",l=0,c=16,r=0}function A(){let o=e;if(n.charCodeAt(e)===48)e++;else for(e++;e<n.length&&_(n.charCodeAt(e));)e++;if(e<n.length&&n.charCodeAt(e)===46)if(e++,e<n.length&&_(n.charCodeAt(e)))for(e++;e<n.length&&_(n.charCodeAt(e));)e++;else return r=3,n.substring(o,e);let f=e;if(e<n.length&&(n.charCodeAt(e)===69||n.charCodeAt(e)===101))if(e++,(e<n.length&&n.charCodeAt(e)===43||n.charCodeAt(e)===45)&&e++,e<n.length&&_(n.charCodeAt(e))){for(e++;e<n.length&&_(n.charCodeAt(e));)e++;f=e}else r=3;return n.substring(o,f)}function b(){let o="",f=e;for(;;){if(e>=t){o+=n.substring(f,e),r=2;break}const u=n.charCodeAt(e);if(u===34){o+=n.substring(f,e),e++;break}if(u===92){if(o+=n.substring(f,e),e++,e>=t){r=2;break}switch(n.charCodeAt(e++)){case 34:o+='"';break;case 92:o+="\\";break;case 47:o+="/";break;case 98:o+="\b";break;case 102:o+="\f";break;case 110:o+=`
`;break;case 114:o+="\r";break;case 116:o+=" ";break;case 117:const T=F(4,!0);T>=0?o+=String.fromCharCode(T):r=4;break;default:r=5}f=e;continue}if(u>=0&&u<=31)if(N(u)){o+=n.substring(f,e),r=2;break}else r=6;e++}return o}function O(){if(i="",r=0,l=e,g=a,k=v,e>=t)return l=t,c=17;let o=n.charCodeAt(e);if(R(o)){do e++,i+=String.fromCharCode(o),o=n.charCodeAt(e);while(R(o));return c=15}if(N(o))return e++,i+=String.fromCharCode(o),o===13&&n.charCodeAt(e)===10&&(e++,i+=`
`),a++,v=e,c=14;switch(o){case 123:return e++,c=1;case 125:return e++,c=2;case 91:return e++,c=3;case 93:return e++,c=4;case 58:return e++,c=6;case 44:return e++,c=5;case 34:return e++,i=b(),c=10;case 47:const f=e-1;if(n.charCodeAt(e+1)===47){for(e+=2;e<t&&!N(n.charCodeAt(e));)e++;return i=n.substring(f,e),c=12}if(n.charCodeAt(e+1)===42){e+=2;const u=t-1;let j=!1;for(;e<u;){const T=n.charCodeAt(e);if(T===42&&n.charCodeAt(e+1)===47){e+=2,j=!0;break}e++,N(T)&&(T===13&&n.charCodeAt(e)===10&&e++,a++,v=e)}return j||(e++,r=1),i=n.substring(f,e),c=13}return i+=String.fromCharCode(o),e++,c=16;case 45:if(i+=String.fromCharCode(o),e++,e===t||!_(n.charCodeAt(e)))return c=16;case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return i+=A(),c=11;default:for(;e<t&&E(o);)e++,o=n.charCodeAt(e);if(l!==e){switch(i=n.substring(l,e),i){case"true":return c=8;case"false":return c=9;case"null":return c=7}return c=16}return i+=String.fromCharCode(o),e++,c=16}}function E(o){if(R(o)||N(o))return!1;switch(o){case 125:case 93:case 123:case 91:case 34:case 58:case 44:case 47:return!1}return!0}function $(){let o;do o=O();while(o>=12&&o<=15);return o}return{setPosition:U,getPosition:()=>e,scan:s?$:O,getToken:()=>c,getTokenValue:()=>i,getTokenOffset:()=>l,getTokenLength:()=>e-l,getTokenStartLine:()=>g,getTokenStartCharacter:()=>l-k,getTokenError:()=>r}}function R(n){return n===32||n===9}function N(n){return n===10||n===13}function _(n){return n>=48&&n<=57}var P;(function(n){n[n.lineFeed=10]="lineFeed",n[n.carriageReturn=13]="carriageReturn",n[n.space=32]="space",n[n._0=48]="_0",n[n._1=49]="_1",n[n._2=50]="_2",n[n._3=51]="_3",n[n._4=52]="_4",n[n._5=53]="_5",n[n._6=54]="_6",n[n._7=55]="_7",n[n._8=56]="_8",n[n._9=57]="_9",n[n.a=97]="a",n[n.b=98]="b",n[n.c=99]="c",n[n.d=100]="d",n[n.e=101]="e",n[n.f=102]="f",n[n.g=103]="g",n[n.h=104]="h",n[n.i=105]="i",n[n.j=106]="j",n[n.k=107]="k",n[n.l=108]="l",n[n.m=109]="m",n[n.n=110]="n",n[n.o=111]="o",n[n.p=112]="p",n[n.q=113]="q",n[n.r=114]="r",n[n.s=115]="s",n[n.t=116]="t",n[n.u=117]="u",n[n.v=118]="v",n[n.w=119]="w",n[n.x=120]="x",n[n.y=121]="y",n[n.z=122]="z",n[n.A=65]="A",n[n.B=66]="B",n[n.C=67]="C",n[n.D=68]="D",n[n.E=69]="E",n[n.F=70]="F",n[n.G=71]="G",n[n.H=72]="H",n[n.I=73]="I",n[n.J=74]="J",n[n.K=75]="K",n[n.L=76]="L",n[n.M=77]="M",n[n.N=78]="N",n[n.O=79]="O",n[n.P=80]="P",n[n.Q=81]="Q",n[n.R=82]="R",n[n.S=83]="S",n[n.T=84]="T",n[n.U=85]="U",n[n.V=86]="V",n[n.W=87]="W",n[n.X=88]="X",n[n.Y=89]="Y",n[n.Z=90]="Z",n[n.asterisk=42]="asterisk",n[n.backslash=92]="backslash",n[n.closeBrace=125]="closeBrace",n[n.closeBracket=93]="closeBracket",n[n.colon=58]="colon",n[n.comma=44]="comma",n[n.dot=46]="dot",n[n.doubleQuote=34]="doubleQuote",n[n.minus=45]="minus",n[n.openBrace=123]="openBrace",n[n.openBracket=91]="openBracket",n[n.plus=43]="plus",n[n.slash=47]="slash",n[n.formFeed=12]="formFeed",n[n.tab=9]="tab"})(P||(P={}));var h;(function(n){n.DEFAULT={allowTrailingComma:!1}})(h||(h={}));function fn(n,s=[],t=h.DEFAULT){let e=null,i=[];const l=[];function c(g){Array.isArray(i)?i.push(g):e!==null&&(i[e]=g)}return rn(n,{onObjectBegin:()=>{const g={};c(g),l.push(i),i=g,e=null},onObjectProperty:g=>{e=g},onObjectEnd:()=>{i=l.pop()},onArrayBegin:()=>{const g=[];c(g),l.push(i),i=g,e=null},onArrayEnd:()=>{i=l.pop()},onLiteralValue:c,onError:(g,v,k)=>{s.push({error:g,offset:v,length:k})}},t),i[0]}function rn(n,s,t=h.DEFAULT){const e=un(n,!1),i=[];function l(m){return m?()=>m(e.getTokenOffset(),e.getTokenLength(),e.getTokenStartLine(),e.getTokenStartCharacter()):()=>!0}function c(m){return m?()=>m(e.getTokenOffset(),e.getTokenLength(),e.getTokenStartLine(),e.getTokenStartCharacter(),()=>i.slice()):()=>!0}function a(m){return m?w=>m(w,e.getTokenOffset(),e.getTokenLength(),e.getTokenStartLine(),e.getTokenStartCharacter()):()=>!0}function g(m){return m?w=>m(w,e.getTokenOffset(),e.getTokenLength(),e.getTokenStartLine(),e.getTokenStartCharacter(),()=>i.slice()):()=>!0}const v=c(s.onObjectBegin),k=g(s.onObjectProperty),r=l(s.onObjectEnd),F=c(s.onArrayBegin),U=l(s.onArrayEnd),A=g(s.onLiteralValue),b=a(s.onSeparator),O=l(s.onComment),E=a(s.onError),$=t&&t.disallowComments,o=t&&t.allowTrailingComma;function f(){for(;;){const m=e.scan();switch(e.getTokenError()){case 4:u(14);break;case 5:u(15);break;case 3:u(13);break;case 1:$||u(11);break;case 2:u(12);break;case 6:u(16);break}switch(m){case 12:case 13:$?u(10):O();break;case 16:u(1);break;case 15:case 14:break;default:return m}}}function u(m,w=[],H=[]){if(E(m),w.length+H.length>0){let I=e.getToken();for(;I!==17;){if(w.indexOf(I)!==-1){f();break}else if(H.indexOf(I)!==-1)break;I=f()}}}function j(m){const w=e.getTokenValue();return m?A(w):(k(w),i.push(w)),f(),!0}function T(){switch(e.getToken()){case 11:const m=e.getTokenValue();let w=Number(m);isNaN(w)&&(u(2),w=0),A(w);break;case 7:A(null);break;case 8:A(!0);break;case 9:A(!1);break;default:return!1}return f(),!0}function sn(){return e.getToken()!==10?(u(3,[],[2,5]),!1):(j(!1),e.getToken()===6?(b(":"),f(),V()||u(4,[],[2,5])):u(5,[],[2,5]),i.pop(),!0)}function on(){v(),f();let m=!1;for(;e.getToken()!==2&&e.getToken()!==17;){if(e.getToken()===5){if(m||u(4,[],[]),b(","),f(),e.getToken()===2&&o)break}else m&&u(6,[],[]);sn()||u(4,[],[2,5]),m=!0}return r(),e.getToken()!==2?u(7,[2],[]):f(),!0}function ln(){F(),f();let m=!0,w=!1;for(;e.getToken()!==4&&e.getToken()!==17;){if(e.getToken()===5){if(w||u(4,[],[]),b(","),f(),e.getToken()===4&&o)break}else w&&u(6,[],[]);m?(i.push(0),m=!1):i[i.length-1]++,V()||u(4,[],[4,5]),w=!0}return U(),m||i.pop(),e.getToken()!==4?u(8,[4],[]):f(),!0}function V(){switch(e.getToken()){case 3:return ln();case 1:return on();case 10:return j(!0);default:return T()}}return f(),e.getToken()===17?t.allowEmptyContent?!0:(u(4,[],[]),!1):V()?(e.getToken()!==17&&u(9,[],[]),!0):(u(4,[],[]),!1)}var X;(function(n){n[n.None=0]="None",n[n.UnexpectedEndOfComment=1]="UnexpectedEndOfComment",n[n.UnexpectedEndOfString=2]="UnexpectedEndOfString",n[n.UnexpectedEndOfNumber=3]="UnexpectedEndOfNumber",n[n.InvalidUnicode=4]="InvalidUnicode",n[n.InvalidEscapeCharacter=5]="InvalidEscapeCharacter",n[n.InvalidCharacter=6]="InvalidCharacter"})(X||(X={}));var Y;(function(n){n[n.OpenBraceToken=1]="OpenBraceToken",n[n.CloseBraceToken=2]="CloseBraceToken",n[n.OpenBracketToken=3]="OpenBracketToken",n[n.CloseBracketToken=4]="CloseBracketToken",n[n.CommaToken=5]="CommaToken",n[n.ColonToken=6]="ColonToken",n[n.NullKeyword=7]="NullKeyword",n[n.TrueKeyword=8]="TrueKeyword",n[n.FalseKeyword=9]="FalseKeyword",n[n.StringLiteral=10]="StringLiteral",n[n.NumericLiteral=11]="NumericLiteral",n[n.LineCommentTrivia=12]="LineCommentTrivia",n[n.BlockCommentTrivia=13]="BlockCommentTrivia",n[n.LineBreakTrivia=14]="LineBreakTrivia",n[n.Trivia=15]="Trivia",n[n.Unknown=16]="Unknown",n[n.EOF=17]="EOF"})(Y||(Y={}));const pn=fn;var Z;(function(n){n[n.InvalidSymbol=1]="InvalidSymbol",n[n.InvalidNumberFormat=2]="InvalidNumberFormat",n[n.PropertyNameExpected=3]="PropertyNameExpected",n[n.ValueExpected=4]="ValueExpected",n[n.ColonExpected=5]="ColonExpected",n[n.CommaExpected=6]="CommaExpected",n[n.CloseBraceExpected=7]="CloseBraceExpected",n[n.CloseBracketExpected=8]="CloseBracketExpected",n[n.EndOfFileExpected=9]="EndOfFileExpected",n[n.InvalidCommentToken=10]="InvalidCommentToken",n[n.UnexpectedEndOfComment=11]="UnexpectedEndOfComment",n[n.UnexpectedEndOfString=12]="UnexpectedEndOfString",n[n.UnexpectedEndOfNumber=13]="UnexpectedEndOfNumber",n[n.InvalidUnicode=14]="InvalidUnicode",n[n.InvalidEscapeCharacter=15]="InvalidEscapeCharacter",n[n.InvalidCharacter=16]="InvalidCharacter"})(Z||(Z={}));const q=n=>pn(B.readFileSync(n,"utf8")),{existsSync:D}=B,gn=()=>{const{findPnpApi:n}=cn;return n&&n(process.cwd())};function d(n){const s=q(n);return p.join(n,"..",s&&"tsconfig"in s?s.tsconfig:"tsconfig.json")}function mn(n,s){let t=n;const e=n[0]===".";if(e||p.isAbsolute(n)){if(e&&(t===".."&&(t+="/tsconfig.json"),t=p.resolve(s,t)),D(t)&&B.statSync(t).isFile()||!t.endsWith(".json")&&(t+=".json",D(t)))return t;throw new Error(`File '${n}' not found.`)}const i=gn();if(i){const{resolveRequest:c}=i,[a,g]=n.split("/"),v=a.startsWith("@")?`${a}/${g}`:a;try{if(v===n){const k=c(p.join(v,"package.json"),s);if(k){const r=d(k);if(D(r))return r}}else{let k;try{k=c(n,s,{extensions:[".json"]})}catch{k=c(p.join(n,"tsconfig.json"),s)}if(k)return k}}catch{}}let l=S(s,p.join("node_modules",t));if(l){if(B.statSync(l).isDirectory()){const c=p.join(l,"package.json");if(D(c)?l=d(c):l=p.join(l,"tsconfig.json"),D(l))return l}else if(l.endsWith(".json"))return l}if(!t.endsWith(".json")&&(t+=".json",l=S(s,p.join("node_modules",t)),l))return l;throw new Error(`File '${n}' not found.`)}const an=(n,s)=>{var t;const e=mn(n,s),i=J(e);if(delete i.references,(t=i.compilerOptions)!=null&&t.baseUrl){const{compilerOptions:l}=i;l.baseUrl=p.relative(s,p.join(p.dirname(e),l.baseUrl))||"./"}return i.files&&(i.files=i.files.map(l=>p.relative(s,p.join(p.dirname(e),l)))),i.include&&(i.include=i.include.map(l=>p.relative(s,p.join(p.dirname(e),l)))),i},J=n=>{let s;try{s=B.realpathSync(n)}catch{throw new Error(`Cannot resolve tsconfig at path: ${n}`)}const t=p.dirname(s);let e=q(s)||{};if(typeof e!="object")throw new SyntaxError(`Failed to parse tsconfig at: ${n}`);if(e.extends){const i=Array.isArray(e.extends)?e.extends:[e.extends];delete e.extends;for(const l of i.reverse()){const c=an(l,t),a={...c,...e,compilerOptions:{...c.compilerOptions,...e.compilerOptions}};c.watchOptions&&(a.watchOptions={...c.watchOptions,...e.watchOptions}),e=a}}if(e.compilerOptions){const{compilerOptions:i}=e;i.baseUrl&&(i.baseUrl=M(i.baseUrl)),i.outDir&&(Array.isArray(e.exclude)||(e.exclude=[]),e.exclude.push(i.outDir),i.outDir=M(i.outDir))}else e.compilerOptions={};if(e.files&&(e.files=e.files.map(M)),e.include&&(e.include=e.include.map(L)),e.watchOptions){const{watchOptions:i}=e;i.excludeDirectories&&(i.excludeDirectories=i.excludeDirectories.map(l=>L(p.resolve(t,l))))}return e};function kn(n=process.cwd(),s="tsconfig.json"){const t=S(L(n),s);if(!t)return null;const e=J(t);return{path:t,config:e}}const bn=/\*/g,K=(n,s)=>{const t=n.match(bn);if(t&&t.length>1)throw new Error(s)};function Tn(n){if(n.includes("*")){const[s,t]=n.split("*");return{prefix:s,suffix:t}}return n}const wn=({prefix:n,suffix:s},t)=>t.startsWith(n)&&t.endsWith(s);function On(n,s,t){return Object.entries(n).map(([e,i])=>(K(e,`Pattern '${e}' can have at most one '*' character.`),{pattern:Tn(e),substitutions:i.map(l=>{if(K(l,`Substitution '${l}' in pattern '${e}' can have at most one '*' character.`),!s&&!W.test(l))throw new Error("Non-relative paths are not allowed when 'baseUrl' is not set. Did you forget a leading './'?");return p.join(t,l)})}))}function vn(n){if(!n.config.compilerOptions)return null;const{baseUrl:s,paths:t}=n.config.compilerOptions;if(!s&&!t)return null;const e=p.resolve(p.dirname(n.path),s||"."),i=t?On(t,s,e):[];return function(c){if(W.test(c))return[];const a=[];for(const r of i){if(r.pattern===c)return r.substitutions.map(L);typeof r.pattern!="string"&&a.push(r)}let g,v=-1;for(const r of a)wn(r.pattern,c)&&r.pattern.prefix.length>v&&(v=r.pattern.prefix.length,g=r);if(!g)return s?[L(p.join(e,c))]:[];const k=c.slice(g.pattern.prefix.length,c.length-g.pattern.suffix.length);return g.substitutions.map(r=>L(r.replace("*",k)))}}const C=n=>{let s="";for(let t=0;t<n.length;t+=1){const e=n[t],i=e.toUpperCase();s+=e===i?e.toLowerCase():i}return s},jn=65,An=97,Ln=()=>Math.floor(Math.random()*26),En=n=>Array.from({length:n},()=>String.fromCodePoint(Ln()+(Math.random()>.5?jn:An))).join(""),Bn=(n=B)=>{const s=process.execPath;if(n.existsSync(s))return!n.existsSync(C(s));const t=`/${En(10)}`;n.writeFileSync(t,"");const e=!n.existsSync(C(t));return n.unlinkSync(t),e},{join:x}=p.posix,G={ts:[".ts",".tsx",".d.ts"],cts:[".cts",".d.cts"],mts:[".mts",".d.mts"]},Un=n=>{const s=[...G.ts],t=[...G.cts],e=[...G.mts];return n!=null&&n.allowJs&&(s.push(".js",".jsx"),t.push(".cjs"),e.push(".mjs")),[...s,...t,...e]},$n=n=>{const s=[];if(!n)return s;const{outDir:t,declarationDir:e}=n;return t&&s.push(t),e&&s.push(e),s},nn=n=>n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),_n=["node_modules","bower_components","jspm_packages"],Q=`(?!(${_n.join("|")})(/|$))`,Fn=/(?:^|\/)[^.*?]+$/,en="**/*",y="[^/]",z="[^./]",tn=process.platform==="win32",Nn=({config:n,path:s},t=Bn())=>{if("extends"in n)throw new Error("tsconfig#extends must be resolved. Use getTsconfig or parseTsconfig to resolve it.");if(!p.isAbsolute(s))throw new Error("The tsconfig path must be absolute");tn&&(s=L(s));const e=p.dirname(s),{files:i,include:l,exclude:c,compilerOptions:a}=n,g=i==null?void 0:i.map(b=>x(e,b)),v=Un(a),k=t?"":"i",F=(c||$n(a)).map(b=>{const O=x(e,b),E=nn(O).replace(/\\\*\\\*\//g,"(.+/)?").replace(/\\\*/g,`${y}*`).replace(/\\\?/g,y);return new RegExp(`^${E}($|/)`,k)}),U=i||l?l:[en],A=U?U.map(b=>{let O=x(e,b);Fn.test(O)&&(O=x(O,en));const E=nn(O).replace(/\/\\\*\\\*/g,`(/${Q}${z}${y}*)*?`).replace(/(\/)?\\\*/g,($,o)=>{const f=`(${z}|(\\.(?!min\\.js$))?)*`;return o?`/${Q}${z}${f}`:f}).replace(/(\/)?\\\?/g,($,o)=>{const f=y;return o?`/${Q}${f}`:f});return new RegExp(`^${E}$`,k)}):void 0;return b=>{if(!p.isAbsolute(b))throw new Error("filePath must be absolute");if(tn&&(b=L(b)),g!=null&&g.includes(b))return n;if(!(!v.some(O=>b.endsWith(O))||F.some(O=>O.test(b)))&&A&&A.some(O=>O.test(b)))return n}};export{Nn as createFilesMatcher,vn as createPathsMatcher,kn as getTsconfig,J as parseTsconfig};