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