UNPKG

@datawheel/bespoke-cms-warmup

Version:

Warmup utility for Bespoke CMS pages

14 lines (11 loc) 4.78 kB
'use strict'; var u=(s=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(s,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):s)(function(s){if(typeof require<"u")return require.apply(this,arguments);throw new Error('Dynamic require of "'+s+'" is not supported')});var F=(s,e)=>()=>(s&&(e=s(s=0)),e);var $=(s,e)=>()=>(e||s((e={exports:{}}).exports,e),e.exports);var l=F(()=>{});var _=$((N,x)=>{l();var v=u("path"),q=__dirname.endsWith("models")?__dirname:v.resolve(__dirname,"models");x.exports={modelPaths:{report:v.resolve(q,"report.js"),dimension:v.resolve(q,"dimension.js"),variant:v.resolve(q,"variant.js"),search:v.resolve(q,"search.js")}};});var U=$((G,P)=>{l();var{Sequelize:O,DataTypes:b}=u("sequelize");P.exports={hydrateModels:T};async function T(s,e){let t=O.Op,r=s.db||"postgresql://".concat(s["db-user"],":",s["db-pass"],"@",s["db-host"],"/",s["db-name"]);e.print(`Database Connection: ${r}`);let n;n=e.step("Creating new sequelize instance");let o=new O(r,{logging:!1,pool:{max:10,min:1,acquire:5*1e3,idle:10*1e3,evict:1*1e3}});n.resolve(),n=e.step("Testing connection"),await o.authenticate().then(n.resolve,n.reject),n=e.step("Retrieving database models");let{modelPaths:d}=_(),c=u(d.report)(o,b),f=u(d.dimension)(o,b),p=u(d.variant)(o,b),h=u(d.search)(o,b);return c.associate&&c.associate({dimension:f}),f.associate&&f.associate({report:c,variant:p}),p.associate&&p.associate({dimension:f}),h.associate&&h.associate({dimension:f,report:c,variant:p}),n.resolve(),c.findAllIn=function(i=[],a=[]){let g=e.step("Requesting configured profiles");return c.findAll({where:{id:i&&i.length>0?{[t.in]:i}:{[t.ne]:0},visible:!0},include:[{association:"dimensions",separate:!0,include:[{association:"variants",separate:!0,where:{id:a&&Array.isArray(a)&&a.length>0?{[t.in]:a}:{[t.ne]:0}}}],order:[["ordering","ASC"]]}]}).then(g.resolve,g.reject).then(z=>(i.length>0&&e.print(`User requested for profiles: ${i.join(", ")}`),a.length>0&&e.print(`User requested for variants: ${a.join(", ")}`),z.sort((w,m)=>i.indexOf(w.id)-i.indexOf(m.id)).map(w=>(e.print(`Profile found: ${w.id} ${w.name}`),w.dimensions.forEach(m=>{e.print(` Dimension found: ${m.id} ${m.name}`),m.variants.forEach(A=>{e.print(` Variant found: ${A.id} ${A.name}`);});}),w))))},h.findAllFromProfile=function(i,a){let g={};return a&&Array.isArray(a)&&a.length>0&&(g.variant_id={[t.in]:a}),h.findAll({where:{report_id:i.id,visible:!0,...g},order:[["zvalue","DESC"]]})},{Report:c,Search:h}}});var M=$((Y,I)=>{l();var C=u("fs"),j=u("path"),y=class{errorCount;failures;outputPath;successCount;writer;constructor(e){this.resetCounters(),e&&this.setOutput(e);}setOutput(e){let t=j.resolve(process.cwd(),e);C.existsSync(t)||C.mkdirSync(t),process.stdout.write(`Reports will be saved on: ${t} `),this.outputPath=t;let r=j.resolve(t,"results.json");this.writer=C.createWriteStream(r,{flags:"w"});}log(e){this.writer.write(`${e} `);}writeReport(e){let t=E(this.failures),r=j.resolve(this.outputPath,`profile_${e}.json`);return C.promises.writeFile(r,t)}print(e,t=!0){process.stdout.write(`${e}${t?` `:""}`);}printLine(e="-"){let t=process.stdout.columns*1||80,r=new Array(t).fill(e).join("").slice(0,t);process.stdout.write(`${r} `);}countResult(e){e.status==="ERROR"&&this.errorCount++,e.status==="SUCCESS"&&this.successCount++,e.status==="FAILURE"&&this.failures.push(e);}resetCounters(){this.errorCount=0,this.failures=[],this.successCount=0;}printCount(e){let t="";this.errorCount>0&&(t=`with ${this.errorCount} errors`),this.failures.length>0&&(t=`${t?", and":"with"} ${this.failures.length} failures`),this.print(` Profile "${e}" completed ${t||"without errors"}.`);}step(e){return process.stdout.write(`WAIT: ${e}...`),{reject(t){throw process.stdout.write("\r\x1B[K"),process.stdout.write(`ERR : ${e} `),t},resolve(t){return process.stdout.write("\r\x1B[K"),process.stdout.write(`OK : ${e} `),t}}}};function E(s){let e=new Map([["",[""].slice(0,0)]]);e.clear();let t=r=>{let n=e.get(r);if(n!==void 0)return n;let o=[];return e.set(r,o),o};for(let r of s){let{test_na:n}=r.result;if(n&&n.length>0){let{url:o}=r.job;for(let d of n)t(d).push(o);}}return JSON.stringify([...e.entries()],null,2)}function W(s){return [].concat(s).reduce((e,t)=>{let r=t.indexOf(":"),n=t.substr(0,r).trim();return e[n]=t.substr(r+1).trim(),e},{})}I.exports={Reporter:y,parseHeaders:W,transposeResults:E};});var k=$((V,D)=>{l();var{hydrateModels:K}=U(),{Reporter:L}=M(),R=new L;D.exports=async function(s){R.setOutput(s.output),R.printLine();let e=s.base||"",{ProfileMeta:t,Search:r}=await K(s,R),n=await t.findAllIn([],[]);for(let o of n){let d=await r.findAllFromProfile(o,[]);for(let c of d){let f={page:c.slug,profile:o.slug},p=e.replace(/:(\w+)\b/g,(h,i)=>f[i]);R.log(p);}}};});var list = k(); module.exports = list;