UNPKG

@dovenv/examples

Version:

Tools for Examples files/directories for dovenv

14 lines (9 loc) 4.38 kB
import{validate as l,ensureDir as C,getDirName as d,writeFile as T,getStringType as x,getBaseName as $,getExtName as m,getStringFrom as O,joinPath as j,deepmergeCustom as E,getObjectFrom as S,getPaths as J,relativePath as M}from"@dovenv/core/utils";import N from"jsdoc-api";const v={CONFIG:"config",PATH:"path",JSDOC:"jsdoc",MULTIPLE:"multiple",CUSTOM:"custom"},D={__proto__:null,TYPE:v},F=l.object({input:l.string(),title:l.string().optional(),type:l.string().optional(),desc:l.string().optional(),outro:l.string().optional()}),L=l.object({title:l.string().optional(),desc:l.string().optional(),outro:l.string().optional(),data:l.record(l.string(),F)}),I=l.record(l.string(),L),U="https://dovenv.pigeonposse.com/guide/plugin/examples";class f{const=D;utils;opts;constructor(s){this.utils=s.utils,this.opts=s.opts,this.utils.helpURL=U,this.utils.title="examples"}async#t(s,t){t&&(await C(d(t)),await T(t,s))}#s(s){const{title:t="Examples",desc:n}=s;let e="";return t&&(e+=`# ${t} `),n&&(e+=`${n} `),e}async#i(s){const{data:t,header:n=2,path:e=this.utils.process.cwd(),step:i=` `}=s,r="#".repeat(n)+" ";let c="";for(const[p,o]of Object.entries(t)){const a=s.contentType||x(o.input),h=o.desc?o.desc+i:"",w=o.outro?i+o.outro:"",y=(o.title||(a==="path"?$(o.input):p))+i,b=o.type||(a==="path"?m(o.input).replace(".",""):"markdown"),P=await O(a==="path"?j(e,o.input):o.input);c+=`${r}${y}${h}\`\`\`${b} ${P} \`\`\`${w}${i}`}return c}async#e(s,t,n){const e=` `;let i=this.#s(n||{});for(const[r,c]of Object.entries(s)){const p=(c.title||r)+e,o=c.desc?c.desc+e:"",a=c.outro?e+c.outro:"",h=c.data?await this.#i({data:c.data,header:3,path:t}):"";i+=`## ${p}${o}${h}${a}${e}`}return i}async#o(s){const t=await this.utils.getOptsKeys({input:this.opts,pattern:s});if(!t||!this.opts)return;const n={},{style:e}=this.utils;for(const i of t){const r=this.opts[i];console.log(e.info.h(`Example for ${e.badge(i)} key`)),n[i]=await this.get(r),console.log(` `,e.success.msg(" \u2728 Successful!"),` `)}return n}async fromConfig(s){const{input:t,config:n,title:e,desc:i,output:r}=s,c=E({}),p=typeof t=="string"?await S(t):t,o=c(p,n||{}),a=typeof t=="string"?d(t):this.utils.process.cwd();await this.utils.validateSchema(I,o,{showValid:!0});const h=await this.#e(o,a,{title:e,desc:i});return await this.#t(h,r),h}async fromPath(s){const{output:t,title:n,desc:e,input:i,opts:r}=s,c=await J(i,r),p={};for(const a of c)p[a]={input:a};let o=this.#s({title:n,desc:e});return o+=await this.#i({data:p,path:r?.cwd?.toString(),contentType:"path"}),await this.#t(o,t),o}async fromJsdoc(s){const{output:t,input:n,title:e,desc:i}=s;console.debug({data:s});const r=await N.explain({files:n.map(a=>M(this.utils.process.cwd(),a)),cache:!1});console.debug({jsdocData:r});const c=r.filter(a=>a.examples).map(a=>({title:a.name||a.meta.filename,input:a.examples,desc:a.description,type:m(a.meta.path).replace(".","")||"ts"})).reduce((a,h)=>(a[h.title]=h,a),{}),p=await this.#i({data:c});if(!p||p.trim()=="")return console.log(),console.warn(this.utils.style.warn.msg("No JSODC examples retrived")),"";const o=this.#s({title:e,desc:i})+p;return await this.#t(o,t),o}async fromMultiple(s){const{output:t,title:n,desc:e,...i}=s;let r=this.#s({title:n,desc:e});if(!i)throw new Error("No input found for any type.");return i.config&&(r+=await this.fromConfig({title:!1,...i.config})),i.jsdoc&&(r+=await this.fromJsdoc({title:!1,...i.jsdoc})),i.path&&(r+=await this.fromPath({title:!1,...i.path})),await this.#t(r,t),r}async fromCustom(s){const t=this.const.TYPE;return await s.fn({config:this.utils.config||{},run:{[t.CONFIG]:this.fromConfig.bind(this),[t.PATH]:this.fromPath.bind(this),[t.JSDOC]:this.fromJsdoc.bind(this),[t.MULTIPLE]:this.fromMultiple.bind(this)}})}async get(s){const t=this.const.TYPE,{type:n,...e}=s,i={[t.CONFIG]:this.fromConfig.bind(this),[t.PATH]:this.fromPath.bind(this),[t.JSDOC]:this.fromJsdoc.bind(this),[t.MULTIPLE]:this.fromMultiple.bind(this),[t.CUSTOM]:this.fromCustom.bind(this)};if(i[n])return await i[n](e);throw new Error("You needd to set one type: "+Object.values(t).join(", "))}async run(s){return await this.utils.catchFn(this.#o(s))}}const g=u=>({custom:{examples:{desc:"Toolkit for managing example paths",opts:{key:{alias:"k",desc:"Key pattern",type:"array"}},fn:async({utils:s})=>{await new f({opts:u,utils:s}).run()}}}});export{f as Examples,g as default,g as examplesPlugin};