UNPKG

cmpl

Version:

no deps, no operations, no vowels - just a tiny compiler

3 lines (2 loc) 5.72 kB
var k=Object.defineProperty,I=Object.defineProperties;var q=Object.getOwnPropertyDescriptors;var C=Object.getOwnPropertySymbols;var L=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var E=(t,e,r)=>e in t?k(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r,R=(t,e)=>{for(var r in e||(e={}))L.call(e,r)&&E(t,r,e[r]);if(C)for(var r of C(e))S.call(e,r)&&E(t,r,e[r]);return t},B=(t,e)=>I(t,q(e));const G=(t=8,e=import("node:crypto"),r=import("node:path"))=>async(m,d)=>{const{createHash:o}=await e,{basename:p,extname:a}=await r;return`${p(m,a(m))}-${o("sha256").update(d).digest("hex").substring(0,t).toUpperCase()}${a(m)}`},U=(t=import("node:crypto"))=>{const e=new Map;return async(r,m,d)=>{const{createHash:o}=await t,p=await d?.()||null;if(p===null||m)return!0;const a=o("sha256").update(p).digest("hex");return!e.has(r)||e.get(r)!==a?(e.set(r,a),!0):!1}};async function T(t,{entry:e,fs:r=import("node:fs/promises"),path:m=import("node:path")},d){const{relative:o,dirname:p,join:a}=await m,{readFile:x,mkdir:F,writeFile:y}=await r,g=x(t),h=o(e,t);return Promise.all(d.map(async s=>{if(!s)return s;const c=(await m).basename(h);!W(s)&&typeof s.rename=="function"&&console.warn("WARING: unexpected rename method on transform processor, will be ignored!","Return new name from transform method via { content: Buffer, name: string }");const n=W(s)?{content:await g,name:await s.rename?.(h,await g)||c}:await s.transform(await g,h);if(n===null)return null;const f=a(s.outDir,o(e,p(t))),l=Array.isArray(n)?n:n instanceof Buffer?[{content:n,name:c}]:[n];if(l.length===0)return null;const w=await Promise.all(l.map(async({content:v,name:P})=>{const u=a(f,P);return await F(p(u),{recursive:!0}),await y(u,v),o(s.outDir,u)}));return{[h]:Array.isArray(n)?w:w[0]}}))}async function j({entry:t,processors:e,fs:r=import("node:fs/promises"),path:m=import("node:path")}){const d=Array.from({length:e.length}).map(()=>({})),{relative:o,join:p,dirname:a}=await m,{readdir:x,stat:F,readFile:y}=await r;let g;const h=async(c,n,f)=>{const l=p(n,c),w=(await F(l)).isDirectory();if(g||(g=w?t:a(t)),w){let v=!1;const P=await Promise.all(f.map(async u=>{const i=u&&(c===t||u.recursive!==!1&&(!u.include||await u.include(o(g,l),!0)));return i&&(v=!0),i?u:null}));v&&await s(l,P)}if(!w){let v=!1;const P=await Promise.all(f.map(async u=>{const i=u&&(!u.include||await u.include(o(g,l),!1,()=>y(l)))?u:null;return i&&(v=!0),i}));if(!v)return;(await T(l,{fs:r,path:m,entry:t===c?g:t},P)).forEach((u,i)=>{u!==null&&Object.assign(d[i],u)})}},s=async(c,n)=>{await Promise.all((await x(c)).map(f=>h(f,c,n)))};return await h(t,"",await Promise.all(e)),e.length===1?d[0]:d}async function*$({signal:t,entry:e,processors:r,poll:m=V(),onError:d=process.env.CI?a=>{throw a}:a=>console.log(a instanceof Error?a.message:a),fs:o=import("node:fs/promises"),path:p=import("node:path")}){const a={entry:e,fs:o,processors:r,path:p},{join:x,dirname:F}=await p,{watch:y,stat:g,readFile:h}=await o;let s=null;const c=()=>s.length===1?Object.assign({},s[0]):s.map(i=>Object.assign({},i));try{const i=await j(a);s=Array.isArray(i)?i:[i],yield c()}catch(i){d(i)}const n=(await g(e)).isDirectory(),f=n?e:F(e),l=n?a:B(R({},a),{entry:f}),w=(await Promise.all(r)).some(({recursive:i})=>i!==!1),v=m?M({fs:o,path:p,interval:typeof m=="number"?m:void 0}):y,P=new H;t?.addEventListener("abort",()=>{P.done(!0)});async function u(){for await(const i of v(e,{recursive:n?w:!1,signal:t}))P.push(i);P.done()}u();for await(const i of P)try{if(s)switch(i.eventType){case"rename":if(s.some(O=>O[i.filename])){s.forEach(O=>{O[i.filename]&&delete O[i.filename]}),yield c();break}case"change":{let b=!1;const O=await Promise.all((await Promise.all(r)).map(async A=>{const D=A&&(!A.include||await A.include(i.filename,!1,async()=>{try{return await h(x(f,i.filename))}catch(N){if(N?.code==="ENOENT")return null;throw N}}))?A:null;return D&&(b=!0),D}));b&&((await T(x(f,i.filename),l,O)).forEach((A,D)=>{A!==null&&Object.assign(s[D],A)}),yield c());break}}else{const b=await j(a);s=Array.isArray(b)?b:[b],yield c()}}catch(b){d(b)}}function M({path:t,fs:e,interval:r=300}){return async function*(d,o){const{join:p,relative:a}=await t,{readdir:x,stat:F}=await e,y=async(h,s={})=>(await Promise.all((await x(h)).map(async c=>{const n=p(h,c),f=await F(n);f.isDirectory()&&o.recursive?await y(n,s):s[a(d,n)]=f.mtimeMs})),s);let g=await y(d);for(;!o.signal?.aborted&&(await new Promise(n=>setTimeout(n,r)),!o.signal?.aborted);){const h=await y(d);if(o.signal?.aborted)break;const s=Object.entries(g);for(let n=0,f=s.length;n<f;n++){const[l,w]=s[n];h[l]?h[l]!==w&&(yield{eventType:"change",filename:l}):yield{eventType:"rename",filename:l}}const c=Object.entries(g);for(let n=0,f=c.length;n<f;n++){const[l]=c[n];g[l]||(yield{eventType:"rename",filename:l})}g=h}}}function V(){if(!process.env.CMPL_USE_POLLING)return!1;const t=parseInt(process.env.CMPL_USE_POLLING);return isNaN(t)?!0:t}function W(t){return typeof t.transform!="function"}class H{constructor(){this.queue=[];this.resolveNext=null;this.isDone=!1}done(e){this.isDone=e?"abort":!0,this.onAbort?.("done")}push(e){if(this.isDone)throw new Error("Can not push to done queue");this.queue.push(e),this.resolveNext&&this.resolveNext()}async pop(){if(this.queue.length===0){if(this.isDone)throw"done";await new Promise((r,m)=>{this.onAbort=m,this.resolveNext=r}),this.onAbort=void 0}return this.queue.shift()}[Symbol.asyncIterator](){return{next:async()=>{try{if(this.isDone==="abort")throw"done";return{value:await this.pop(),done:!1}}catch(e){if(e==="done")return{done:!0,value:void 0};throw e}}}}}export{j as cmpl,U as cntntChngd,G as cntntHsh,T as prcss,$ as wtch}; //# sourceMappingURL=index.mjs.map