get-tsconfig
Version:
Find and parse the tsconfig.json file from a directory path
4 lines (3 loc) • 8.13 kB
JavaScript
import d from"path";import B from"fs";import{createRequire as z}from"module";function _(r){const o=/^\\\\\?\\/.test(r),a=/[^\u0000-\u0080]+/.test(r);return o||a?r:r.replace(/\\/g,"/")}function H(r,o){for(;;){const a=d.join(r,o);if(B.existsSync(a))return _(a);const e=d.dirname(r);if(e===r)return;r=e}}var U=z(import.meta.url);function J(r,o){o===void 0&&(o=!1);var a=r.length,e=0,n="",s=0,i=16,C=0,c=0,v=0,O=0,l=0;function A(t,p){for(var m=0,b=0;m<t||!p;){var g=r.charCodeAt(e);if(g>=48&&g<=57)b=b*16+g-48;else if(g>=65&&g<=70)b=b*16+g-65+10;else if(g>=97&&g<=102)b=b*16+g-97+10;else break;e++,m++}return m<t&&(b=-1),b}function N(t){e=t,n="",s=0,i=16,l=0}function D(){var t=e;if(r.charCodeAt(e)===48)e++;else for(e++;e<r.length&&w(r.charCodeAt(e));)e++;if(e<r.length&&r.charCodeAt(e)===46)if(e++,e<r.length&&w(r.charCodeAt(e)))for(e++;e<r.length&&w(r.charCodeAt(e));)e++;else return l=3,r.substring(t,e);var p=e;if(e<r.length&&(r.charCodeAt(e)===69||r.charCodeAt(e)===101))if(e++,(e<r.length&&r.charCodeAt(e)===43||r.charCodeAt(e)===45)&&e++,e<r.length&&w(r.charCodeAt(e))){for(e++;e<r.length&&w(r.charCodeAt(e));)e++;p=e}else l=3;return r.substring(t,p)}function j(){for(var t="",p=e;;){if(e>=a){t+=r.substring(p,e),l=2;break}var m=r.charCodeAt(e);if(m===34){t+=r.substring(p,e),e++;break}if(m===92){if(t+=r.substring(p,e),e++,e>=a){l=2;break}var b=r.charCodeAt(e++);switch(b){case 34:t+='"';break;case 92:t+="\\";break;case 47:t+="/";break;case 98:t+="\b";break;case 102:t+="\f";break;case 110:t+=`
`;break;case 114:t+="\r";break;case 116:t+=" ";break;case 117:var g=A(4,!0);g>=0?t+=String.fromCharCode(g):l=4;break;default:l=5}p=e;continue}if(m>=0&&m<=31)if(S(m)){t+=r.substring(p,e),l=2;break}else l=6;e++}return t}function y(){if(n="",l=0,s=e,c=C,O=v,e>=a)return s=a,i=17;var t=r.charCodeAt(e);if(F(t)){do e++,n+=String.fromCharCode(t),t=r.charCodeAt(e);while(F(t));return i=15}if(S(t))return e++,n+=String.fromCharCode(t),t===13&&r.charCodeAt(e)===10&&(e++,n+=`
`),C++,v=e,i=14;switch(t){case 123:return e++,i=1;case 125:return e++,i=2;case 91:return e++,i=3;case 93:return e++,i=4;case 58:return e++,i=6;case 44:return e++,i=5;case 34:return e++,n=j(),i=10;case 47:var p=e-1;if(r.charCodeAt(e+1)===47){for(e+=2;e<a&&!S(r.charCodeAt(e));)e++;return n=r.substring(p,e),i=12}if(r.charCodeAt(e+1)===42){e+=2;for(var m=a-1,b=!1;e<m;){var g=r.charCodeAt(e);if(g===42&&r.charCodeAt(e+1)===47){e+=2,b=!0;break}e++,S(g)&&(g===13&&r.charCodeAt(e)===10&&e++,C++,v=e)}return b||(e++,l=1),n=r.substring(p,e),i=13}return n+=String.fromCharCode(t),e++,i=16;case 45:if(n+=String.fromCharCode(t),e++,e===a||!w(r.charCodeAt(e)))return i=16;case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return n+=D(),i=11;default:for(;e<a&&k(t);)e++,t=r.charCodeAt(e);if(s!==e){switch(n=r.substring(s,e),n){case"true":return i=8;case"false":return i=9;case"null":return i=7}return i=16}return n+=String.fromCharCode(t),e++,i=16}}function k(t){if(F(t)||S(t))return!1;switch(t){case 125:case 93:case 123:case 91:case 34:case 58:case 44:case 47:return!1}return!0}function f(){var t;do t=y();while(t>=12&&t<=15);return t}return{setPosition:N,getPosition:function(){return e},scan:o?f:y,getToken:function(){return i},getTokenValue:function(){return n},getTokenOffset:function(){return s},getTokenLength:function(){return e-s},getTokenStartLine:function(){return c},getTokenStartCharacter:function(){return s-O},getTokenError:function(){return l}}}function F(r){return r===32||r===9||r===11||r===12||r===160||r===5760||r>=8192&&r<=8203||r===8239||r===8287||r===12288||r===65279}function S(r){return r===10||r===13||r===8232||r===8233}function w(r){return r>=48&&r<=57}var L;(function(r){r.DEFAULT={allowTrailingComma:!1}})(L||(L={}));function M(r,o,a){o===void 0&&(o=[]),a===void 0&&(a=L.DEFAULT);var e=null,n=[],s=[];function i(c){Array.isArray(n)?n.push(c):e!==null&&(n[e]=c)}var C={onObjectBegin:function(){var c={};i(c),s.push(n),n=c,e=null},onObjectProperty:function(c){e=c},onObjectEnd:function(){n=s.pop()},onArrayBegin:function(){var c=[];i(c),s.push(n),n=c,e=null},onArrayEnd:function(){n=s.pop()},onLiteralValue:i,onError:function(c,v,O){o.push({error:c,offset:v,length:O})}};return W(r,C,a),n[0]}function W(r,o,a){a===void 0&&(a=L.DEFAULT);var e=J(r,!1);function n(u){return u?function(){return u(e.getTokenOffset(),e.getTokenLength(),e.getTokenStartLine(),e.getTokenStartCharacter())}:function(){return!0}}function s(u){return u?function(h){return u(h,e.getTokenOffset(),e.getTokenLength(),e.getTokenStartLine(),e.getTokenStartCharacter())}:function(){return!0}}var i=n(o.onObjectBegin),C=s(o.onObjectProperty),c=n(o.onObjectEnd),v=n(o.onArrayBegin),O=n(o.onArrayEnd),l=s(o.onLiteralValue),A=s(o.onSeparator),N=n(o.onComment),D=s(o.onError),j=a&&a.disallowComments,y=a&&a.allowTrailingComma;function k(){for(;;){var u=e.scan();switch(e.getTokenError()){case 4:f(14);break;case 5:f(15);break;case 3:f(13);break;case 1:j||f(11);break;case 2:f(12);break;case 6:f(16);break}switch(u){case 12:case 13:j?f(10):N();break;case 16:f(1);break;case 15:case 14:break;default:return u}}}function f(u,h,E){if(h===void 0&&(h=[]),E===void 0&&(E=[]),D(u),h.length+E.length>0)for(var P=e.getToken();P!==17;){if(h.indexOf(P)!==-1){k();break}else if(E.indexOf(P)!==-1)break;P=k()}}function t(u){var h=e.getTokenValue();return u?l(h):C(h),k(),!0}function p(){switch(e.getToken()){case 11:var u=e.getTokenValue(),h=Number(u);isNaN(h)&&(f(2),h=0),l(h);break;case 7:l(null);break;case 8:l(!0);break;case 9:l(!1);break;default:return!1}return k(),!0}function m(){return e.getToken()!==10?(f(3,[],[2,5]),!1):(t(!1),e.getToken()===6?(A(":"),k(),V()||f(4,[],[2,5])):f(5,[],[2,5]),!0)}function b(){i(),k();for(var u=!1;e.getToken()!==2&&e.getToken()!==17;){if(e.getToken()===5){if(u||f(4,[],[]),A(","),k(),e.getToken()===2&&y)break}else u&&f(6,[],[]);m()||f(4,[],[2,5]),u=!0}return c(),e.getToken()!==2?f(7,[2],[]):k(),!0}function g(){v(),k();for(var u=!1;e.getToken()!==4&&e.getToken()!==17;){if(e.getToken()===5){if(u||f(4,[],[]),A(","),k(),e.getToken()===4&&y)break}else u&&f(6,[],[]);V()||f(4,[],[4,5]),u=!0}return O(),e.getToken()!==4?f(8,[4],[]):k(),!0}function V(){switch(e.getToken()){case 3:return g();case 1:return b();case 10:return t(!0);default:return p()}}return k(),e.getToken()===17?a.allowEmptyContent?!0:(f(4,[],[]),!1):V()?(e.getToken()!==17&&f(9,[],[]),!0):(f(4,[],[]),!1)}var G=M;const $=r=>_(/^[./]/.test(r)?r:`./${r}`);var K=Object.defineProperty,Q=Object.defineProperties,X=Object.getOwnPropertyDescriptors,q=Object.getOwnPropertySymbols,Y=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable,I=(r,o,a)=>o in r?K(r,o,{enumerable:!0,configurable:!0,writable:!0,value:a}):r[o]=a,T=(r,o)=>{for(var a in o||(o={}))Y.call(o,a)&&I(r,a,o[a]);if(q)for(var a of q(o))Z.call(o,a)&&I(r,a,o[a]);return r},x=(r,o)=>Q(r,X(o));function R(r){const o=B.realpathSync(r),a=d.dirname(o),e=B.readFileSync(r,"utf8").trim();let n={};if(e&&(n=G(e),!n||typeof n!="object"))throw new SyntaxError(`Failed to parse JSON: ${r}`);if(n.extends){let s=n.extends;try{s=U.resolve(s,{paths:[d.dirname(r)]})}catch(c){if(c.code==="MODULE_NOT_FOUND")try{s=U.resolve(d.join(s,"tsconfig.json"),{paths:[d.dirname(r)]})}catch{}}const i=R(s);if(delete i.references,i.compilerOptions?.baseUrl){const{compilerOptions:c}=i;c.baseUrl=d.relative(a,d.join(d.dirname(s),c.baseUrl))}i.files&&(i.files=i.files.map(c=>d.relative(a,d.join(d.dirname(s),c)))),i.include&&(i.include=i.include.map(c=>d.relative(a,d.join(d.dirname(s),c)))),delete n.extends;const C=x(T(T({},i),n),{compilerOptions:T(T({},i.compilerOptions),n.compilerOptions)});i.watchOptions&&(C.watchOptions=T(T({},i.watchOptions),n.watchOptions)),n=C}if(n.compilerOptions?.baseUrl){const{compilerOptions:s}=n;s.baseUrl=$(s.baseUrl)}if(n.files&&(n.files=n.files.map($)),n.include&&(n.include=n.include.map(_)),n.watchOptions){const{watchOptions:s}=n;s.excludeDirectories&&(s.excludeDirectories=s.excludeDirectories.map(i=>_(d.resolve(a,i))))}return n}function ee(r=process.cwd(),o="tsconfig.json"){const a=H(r,o);if(!a)return null;const e=R(a);return{path:a,config:e}}export{ee as default};