UNPKG

4.45 kBJavaScriptView Raw
1import{stringify as e}from"javascript-stringify";import t from"node:path";import r from"typescript";function i(e,t){for(let r=0,i=0;r<t.length;r++){const o=t[r];if(o&&"string"!=typeof o){switch(o[0]){case e:i++;break;case"/"+e:i--}if(0===i)return r}}throw new Error(`Command "${e}" is not closed.`)}function o(e){for(let t=0,r=0;t<e.length;t++){const i=e[t];if(i&&"string"!=typeof i)switch(i[0]){case"if":r++;break;case"else":if(1===r)return t;break;case"/if":r--}}}function n(e,t){const r=e.split(".");for(let i=0;i<r.length;i++){const o=r[i];if("@"===o)throw new Error("Cannot use \"@\" as a key, have you forget to wrap it in a ${['each']}?");if(!("object"==typeof t&&t?o in t:t[o]))throw new Error(`Key "${o}" ${e.startsWith(o)?"":`in "${e}" `}does not exist inside data`);t=t[o]}return t}function s(e,t,r,i="@"){const o=Array(e.length);for(let n=0;n<e.length;n++){const s=e[n];let l="string"==typeof s?s:s[1];l&&l.startsWith(`${t}.${i}`)?(l=`${t}.${r}`+l.slice(t.length+i.length+1),o[n]="string"==typeof s?l:[s[0],l]):o[n]=s}return o}function l(t){return"function"==typeof t||"object"==typeof t?e(t):String(t)}function h(e,t,r,i,o){let n="",s=e;for(;s<=t;){const e=c(r,i,o,s);if(void 0!==e.code&&(n+=e.code),!e.nextIndex)break;s=e.nextIndex}return n}function c(e,t,r,c){let u=e[c];if(void 0===u)return{};const a=r[c];if(void 0===a)return{code:u,nextIndex:c+1};if("string"==typeof a)return{code:u+l(n(a,t)),nextIndex:c+1};if("/"===a[0][0]||"else"===a[0])return{code:u,nextIndex:c+1};if("$"===a[0][0]){const e=a[0].slice(1);if(!t.helpers)throw new Error(`Helper "${e}" not found`);let r;for(const i of t.helpers)if(i[0]===e){r=i[1];break}if(!r)throw new Error(`Helper "${e}" not found`);return{code:u+l(r(a[1],t)),nextIndex:c+1}}if("each"===a[0]){const o=a[1],l=n(o,t);if(!Array.isArray(l))throw new Error(`Key "${o}" is not an array`);const f=c+i("each",r.slice(c));for(let i=0;i<l.length;i++)u+=h(c+1,f,e,t,s(r,o,i));return{code:u,nextIndex:f+1}}if("if"===a[0]){const s=!!n(a[1],t),l=r.slice(c),f=c+i("if",l),p=o(l.slice(0,f));return(s||p)&&(u+=h(s?c+1:c+p+1,s&&p?c+p:f,e,t,r)),{nextIndex:f+1,code:u}}throw new Error(`Invalid command: ${l(a)}`)}function u(e,t,...r){const i=e.slice();{const e=i.shift();if(null!=e&&e.trim().length)throw new Error(`Cannot have text before data "${e}"`)}return i[0]&&(i[0]=i[0].trimStart()),i[i.length-1]&&(i[i.length-1]=i[i.length-1].trimEnd()),h(0,i.length,i,t,r)}function a(e){let i=r.readConfigFile(e,e=>r.sys.readFile(e));if(i.error)throw new Error("Failed to read tsconfig file.");if(i=r.parseJsonConfigFileContent(i.config,r.sys,t.dirname(e),void 0,e),i.errors.length)throw new Error("Failed to parse tsconfig file.");return i.options}class f{constructor(e,i){var o,n,s,l,h;this.helpers=void 0,this.sources={},this.outputs={},this.host=void 0,this.compilerOptions=void 0,this.helpers=i,this.compilerOptions="string"==typeof e?a(e):e,null!=(o=this.compilerOptions).outDir||(o.outDir="/"),null!=(n=this.compilerOptions).declaration||(n.declaration=!0),null!=(s=this.compilerOptions).moduleResolution||(s.moduleResolution=r.ModuleResolutionKind.NodeJs),null!=(l=this.compilerOptions).module||(l.module=r.ModuleKind.CommonJS),null!=(h=this.compilerOptions).target||(h.target=r.ScriptTarget.ES2022),this.host=r.createCompilerHost(this.compilerOptions,!1),this.host.readFile=e=>{var t;return null==(t=this.sources[e])?void 0:t.join("")},this.host.writeFile=(e,r)=>this.outputs[this.compilerOptions.outDir?t.relative(this.compilerOptions.outDir,e):e]=r}transpile(){return this.outputs={},r.createProgram(Object.keys(this.sources),this.compilerOptions,this.host).emit(),this.outputs}write(e,t,...r){var i,o,n;null!=t.helpers||(t.helpers=this.helpers),(null!=(n=(i=this.sources)[o=t.filename])?n:i[o]=[]).push(u(e,t,...r))}head(e,t,...r){var i,o,n;null!=t.helpers||(t.helpers=this.helpers),(null!=(n=(i=this.sources)[o=t.filename])?n:i[o]=[]).unshift(u(e,t,...r))}writeUnique(e,t,...r){var i,o,n;null!=t.helpers||(t.helpers=this.helpers);const s=u(e,t,...r),l=null!=(n=(i=this.sources)[o=t.filename])?n:i[o]=[];l.includes(s)||l.push(s)}headUnique(e,t,...r){var i,o,n;null!=t.helpers||(t.helpers=this.helpers);const s=u(e,t,...r),l=null!=(n=(i=this.sources)[o=t.filename])?n:i[o]=[];l.includes(s)||l.unshift(s)}}export{f as TsWriter,h as consume,c as consumeStep,i as findCommandEnd,o as findElseClause,n as indexObject,a as readCompilerOptions,s as replaceArrayIndexes,u as t};
2//# sourceMappingURL=index.mjs.map