UNPKG

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