@abw/badger 1.2.2 → 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/badger.cjs.js +1 -1
- package/dist/badger.cjs.js.map +1 -1
- package/dist/badger.esm.js +1 -1
- package/dist/badger.esm.js.map +1 -1
- package/package.json +1 -1
package/dist/badger.cjs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=require("@abw/badger-utils"),t=require("@abw/badger-filesystem"),r=require("node:process"),o=require("prompts"),n=require("commander"),s=require("@abw/badger-timestamp"),i=require("dotenv"),a=require("chokidar"),l=require("node:child_process"),c=require("@abw/badger-codecs");const p={reset:0,bold:1,bright:1,dark:2,black:0,red:1,green:2,yellow:3,blue:4,magenta:5,cyan:6,grey:7,white:8,fg:30,bg:40},h=(e,t="fg")=>{let r=[],o=e.split(/ /,2);const n=o.pop(),s=(t?p[t]:0)+p[n];if(r.push(s),o.length){const e=o.length?o.shift():"dark";r.push(p[e])}return"["+r.join(";")+"m"},u=(t={})=>{const r=e.isObject(t)?t:{fg:t};let o=[];return r.bg&&o.push(h(r.bg,"bg")),r.fg&&o.push(h(r.fg,"fg")),o.join("")},g=()=>h("reset",!1),d=e=>(...t)=>u(e)+t.join("")+g(),f=e=>Object.entries(e).reduce(((e,[t,r])=>(e[t]=d(r),e)),{}),b=d("black"),m=d("red"),w=d("green"),x=d("yellow"),y=d("blue"),$=d("magenta"),v=d("cyan"),C=d("grey"),k=d("white"),j=d("bright black"),P=d("bright red"),V=d("bright green"),E=d("bright yellow"),q=d("bright blue"),D=d("bright magenta"),N=d("bright cyan"),S=d("bright grey"),L=d("bright white"),T=d("dark black"),F=d("dark red"),A=d("dark green"),O=d("dark yellow"),z=d("dark blue"),I=d("dark magenta"),W=d("dark cyan"),B=d("dark grey"),G=d("dark white");function M(t,r="",o){return t?r?(e,...t)=>console.log("%s"+r+"%s"+e,o?u(o):"",g(),...t):console.log.bind(console):e.doNothing}function R(e,t,r="",o){e.debug=M(t,r,o)}const U={b:"\b",f:"\f",n:"\n",r:"\r",t:"\t",v:"\v",'"':'"',"'":"'","\\":"\\"},Y=t=>{let r,o,n=[];for(;t.length;){let s,i;(o=t.match(/^(\/+)/))?t=t.slice(o[1].length):(t.match(/^(\?)/)&&(t=t.slice(1),s=!0),(o=t.match(/^('((\\[\\'bfnrtv]|.)*?)')/))?(t=t.slice(o[1].length),r=o[2].replace(/\\([\\'bfnrtv])/g,((e,t)=>U[t]||`\\${t}`))):(o=t.match(/^("((\\[\\"bfnrtv]|.)*?)")/))?(t=t.slice(o[1].length),r=o[2].replace(/\\([\\"bfnrtv])/g,((e,t)=>U[t]||`\\${t}`))):(o=t.match(/^([^/?]+)/))?(r=o[1],t=t.slice(r.length)):e.fail(`Can't parse data path: ${t}`),t.match(/^(\?)/)&&(t=t.slice(1),i=!0),s?n.push([r,{maybe:!0}]):i?n.push([r,{optional:!0}]):n.push(r))}return n},H=(t,r)=>{let o=t,n=Y(r),s=[];for(let t of n){const[r,n]=e.isArray(t)?t:[t,{}],i=o[r];if(s.push(r),e.noValue(i)){if(n.maybe)return o;if(n.optional)return;e.fail("No value for data at path: ",s.join("/"))}o=i}return o},Q={codec:["yaml","json"],jsExt:["js","mjs"]};class _ extends t.DirPath{constructor(t,r={}){super(t);const o={...Q,...r};this.state.codec=e.splitList(o.codec),this.state.jsExt=e.splitList(o.jsExt),R(this,r.debug,r.debugPrefix,r.debugColor)}async firstFileWithExt(t,r,o=e.doNothing){const n=await this.dirs();for(let e of n)for(let n of r){const r=t+"."+n,s=e.file(r,o(t,n));if(this.debug("looking for config file: ",s.path()),await s.exists())return this.debug("config file exists: ",s.path()),s}}async jsFile(e){return await this.firstFileWithExt(e,this.state.jsExt)}async file(e){return await this.firstFileWithExt(e,this.state.codec,((e,t)=>({codec:t})))}async config(t,r){const[o,n]=t.split("#",2);let s,i,a;if(s=await this.jsFile(o))a=await import(s.path());else if(i=await this.file(o))a=await i.read();else{if(!r)return e.fail("No configuration file for "+o);a=r}return n?H(a,n):a}}const J={jsExt:"js mjs"};class K extends t.DirPath{constructor(t,r={}){super(t);const o={...J,...r},n=e.splitList(o.jsExt).map((e=>e.replace(/^\./,"")));this.state.exts=n,R(this,r.debug,r.debugPrefix,r.debugColor),this.debug("state: ",this.state)}async library(t){const[r,o]=t.split("#",2),n=await this.dirs(),s=this.state.exts;for(let e of n)for(let t of s){const n=e.file(r+"."+t);this.debug("looking for module %s as",r,n.path());if(await n.exists()){const e=await import(n.path());return this.debug("loaded %s as",n.path()),o?H(e,o):e}}e.fail("Library not found: ",t)}}const X=e=>async function(...t){try{const r=await e(...t);return r&&console.log(V(`✓ ${r}`)),!0}catch(e){return console.log(r.env.DEBUG?e:P(`✗ ${e.message||e}`)),!1}},Z=async(t,r={})=>{const n=(r=e.isString(r)?{default:r}:r).name||"answer";return(await o([{name:n,type:r.type||"text",message:t,initial:r.default}]))[n]},ee=()=>r.argv.slice(2);function te(t,r){t?e.isArray(t)&&(r=t,t={}):t={},r||(r||=ee());const o=!!t.options&&e.splitHash(t.options),n=t.others||"error",s=t.short||{},i=t.on||{};let a=!1,l=0,c={};for(;l<r.length;){const t=r[l].match(/^-(-)?(.*)/);if("--"===r[l]&&(a=!0,r.splice(l,1)),t&&!a){const a=!("-"===t[1])&&e.hasValue(s[t[2]])?s[t[2]]:t[2];if(i[a]){if(i[a](a,r.splice(l,1)[0],r,c))continue;r.splice(l,0,t[0])}if(o&&!o[a])switch(n){case"collect":break;case"remove":r.splice(l,1)[0];case"keep":l++;continue;case"error":return e.fail(`Invalid command line flag: ${t[0]}`);default:return e.fail(`Invalid "others" option: ${n}`)}r.splice(l,1),c[a]=!0}else l++}return{flags:c,args:r}}async function re(e,t=ee()){return t.length?t.shift():e?await Z(e):void 0}function oe(e,t){t&&console.log(t),r.exit(e)}function ne(e){oe(0,e)}function se(e){oe(1,e)}const ie={verboseColor:"magenta",titleColor:"bright yellow",underlineColor:"dark yellow",infoColor:"cyan",tickColor:"green",questionColor:"bright white",answerColor:"bright green",sectionNewlines:!0},ae=async t=>{const s=d(ae.verboseColor||ie.verboseColor),i=new n.Command;t.name&&i.name(t.name),t.description&&i.description(t.description),t.version&&i.version(t.version),t.yes&&i.option("-y, --yes","Accept default answers"),t.verbose&&i.option("-v, --verbose","Verbose output"),t.quiet&&i.option("-q, --quiet","Quiet output"),t.options?.filter((t=>!(e.hasValue(t.arg)&&!t.arg)&&(t.title?void 0:e.hasValue(t.name)))).forEach((t=>{const r=t.name,o=t.about,n=t.default,s=t.handler,a=t.short,l=t.type,c=t.pattern||(e.hasValue(l)?`<${l}>`:void 0);let p=`--${r}`,h=[];e.hasValue(a)&&(p=`-${a}, ${p}`),e.hasValue(c)&&(p=`${p} ${c}`),h.push(p),e.hasValue(o)&&h.push(o),e.hasValue(s)&&h.push(s),e.hasValue(n)&&h.push(n),i.option(...h)}));let a={};t.commands?.forEach((t=>{const r=t.name,o=t.about,n=t.type,s=t.pattern||(e.hasValue(n)?`<${n}>`:void 0);let l=[],c=r,p=i.command(c);e.hasValue(s)&&p.argument(s),e.hasValue(o)&&p.description(o),e.hasValue(t.arguments)&&e.splitList(t.arguments).forEach((t=>{l.push(function(t){const r=t.match(/(\w+)/);r||e.fail("Can't parse argument name: ",t);return r[1]}(t)),p.argument(t)})),p.action(((...e)=>{a[r]=l.length?l.reduce(((t,r)=>(t[r]=e.shift(),t)),{}):e[0]}))})),i.parse();const l=i.opts(),c=t.yes&&l.yes,p=t.verbose&&l.verbose,h=t.quiet&&l.quiet;c&&(p&&!h&&r.stdout.write(s("Accepting default answers (-y option is set)\n")),o.override(l));let u=[];t.options?.forEach((t=>{const r=t.type||"text",o=t.name,n=t.prompt,s=e.hasValue(t.arg)&&!t.arg,i=t.validate||(t.required?t=>!(!e.hasValue(t)||!t.toString().length)||(ae.invalid||`You must enter a value for ${o}`):void 0);let a=s?t.default:l[o];"select"===r&&e.hasValue(a)&&(Number.isInteger(a)||(a=t.choices?.findIndex((e=>e.value===a)),a<0&&(a=0))),e.hasValue(n)?u.push({...t,type:e.hasValue(a)?()=>(c&&(h||ce({question:n,answer:a})),r):r,name:o,message:n,initial:a,validate:i}):(t.title||t.info)&&u.push({type:()=>(h||le(t),!1)})}));let g=!1;const f=await o(u,{onCancel:()=>(g=!0,!1)});return g?void 0:{...l,...f,...a}};const le=t=>{const o=t.title,n=d(t.titleColor||ie.titleColor),s=d(t.underlineColor||ie.underlineColor),i=d(t.infoColor||ie.infoColor),a=(e.hasValue(t.newlines)?t.newlines:ie.sectionNewlines)?"\n":"";if(o){const e="-".repeat(o.length);r.stdout.write(a+n(o)+"\n"+s(e)+"\n"+a)}t.info&&r.stdout.write(i(t.info)+"\n"+a)},ce=e=>{const t=d(e.tickColor||ie.tickColor),o=d(e.questionColor||ie.questionColor),n=d(e.answerColor||ie.answerColor);r.stdout.write(t("✔ ")+o(e.question)+" "+n(e.answer)+"\n")};function pe(t={},r){const o=t[r];if(e.hasValue(o))return o;e.fail("Missing value for required parameter: ",r)}function he(t={},r){return e.splitList(r).map((e=>pe(t,e)))}const ue=he;const ge={border:"┌─┐│└─┘|-",background:"+∙",fill:"*"},de={border:"green",background:"dark grey",foreground:"blue",fill:"bright yellow"},fe={charTypes:ge,source:"\n┌────────────────────────────────────────────────────────────────┐\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙ ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ∙∙ │\n│ ∙∙ ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ∙∙ │\n│ ∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙ │\n│ ∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙ │\n│ ∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙ │\n│ ∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙ │\n│ ∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙ │\n│ ∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙ ●● *◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦* ●● ∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙ ●● **◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦** ●● ∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙ ●● *****◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦***** ●● ∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● *******◦◦◦◦◦◦◦◦◦◦******* ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ****************** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ************ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ****** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● **** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● **** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● **** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦****◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦****◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦****◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦******◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦********◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦************◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦******************◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦************************◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙ │\n│ ∙∙∙∙∙ ●● ◦◦◦◦◦◦◦******************************◦◦◦◦◦◦◦ ●● ∙∙∙∙∙ │\n│ ∙∙∙∙ ●● ◦◦◦◦◦************************************◦◦◦◦◦ ●● ∙∙∙∙ │\n│ ∙∙∙ ●● ◦◦◦◦****************************************◦◦◦◦ ●● ∙∙∙ │\n│ ∙∙ ●● ◦◦◦********************************************◦◦◦ ●● ∙∙ │\n│ ∙∙ ●● ◦◦**********************************************◦◦ ●● ∙∙ │\n│ ∙∙ ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ∙∙ │\n│ ∙∙ ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n└────────────────────────────────────────────────────────────────┘\n"},be={size:100,elapsed:!1,picture:fe,colours:de};function me(e){const{source:t,charTypes:r={background:""}}=e,o=t.trim()+"\n",n=o.length,s=Object.entries(r).reduce(((e,[t,r])=>(r.split("").forEach((r=>e[r]=t)),e)),{});return{picture:o,length:n,lineLength:t.split("\n").reduce(((e,t)=>Math.max(e,t.length)),0),charType:s}}function we(e){return Object.entries(e).reduce(((e,[t,r])=>(e[t]=d(r),e)),{})}function xe(e){const{picture:t,charType:r={},colours:o={}}=e;return t.split("").map((e=>{const t=r[e]||"foreground",n=o[t];return n?n(e):e})).join("")}function ye(e){const t=e%60,r=Math.floor(e/60);return[Math.floor(e/3600),r,t].map((e=>e.toString().padStart(2,"0"))).join(":")}class $e{constructor(t={}){const r={...be,...e.isInteger(t)?{size:t}:t},{size:o,colours:n,colors:i,debug:a,elapsed:l}=r,{picture:c,length:p,lineLength:h,charType:u}=me(e.isString(r.picture)?{source:r.picture,charTypes:ge}:r.picture);this.size=o,this.picture=c,this.pixels=this.picture.split(""),this.length=p,this.lineLength=h,this.charType=u,this.colours=we(i||n),this.count=0,this.lastCount=0,this.lastPixel=0,this.startTime=s.now().epochSeconds(),this.elapsed=0,this.elapsedCol=d("dark grey"),this.showElapsed=l,this.debugging=a,R(this,a,"Progress > ","blue")}progress(e=1){this.count+=e;const t=s.now().epochSeconds()-this.startTime;this.elapsed=t;const r=Math.floor(this.length*this.count/this.size);if(r>this.lastPixel){const e=this.lastPixel;return this.lastPixel=r,this.debug(`count: ${this.count}/${this.size} pixels from ${e} to ${r}`),this.pictureSegment(e,r)}return""}remains(){this.count=this.size;const e=this.lastPixel,t=this.length;return t>e?(this.lastPixel=this.length,this.pictureSegment(e,t)):""}pictureSegment(e,t){if(this.debugging)return"";let r=xe({picture:this.picture.substring(e,t),charType:this.charType,colours:this.colours});if(this.showElapsed&&r.match("\n")){const e=r.split("\n"),t=e.pop();r=e.map((e=>e+" "+this.elapsedCol(ye(this.elapsed)))).join("\n").concat("\n",t)}return r}printProgress(e=1){r.stdout.write(this.progress(e))}printRemains(){r.stdout.write(this.remains())}}const ve={description:"Project setup script.",configFiles:["config/setup.yaml","config/setup.json","setup.yaml","setup.json",".setup.yaml",".setup.json"],dataFile:".env.yaml",envFile:".env",envComments:!0,envSections:!0,writeData:!0,writeEnv:!1,compact:!1,cancelled:"Setup cancelled",allDone:"All configuration options have been set",warning:e=>`\n#=============================================================================\n# WARNING: This file is generated automatically when the ${e.scriptName}\n# script is run. Any changes made here may be lost.\n#\n# Generated: ${s.now()}\n#=============================================================================\n\n`},Ce=X((async e=>{await ke(e)}));async function ke(o){const n={...ve,...o},s=n.rootDir?t.dir(n.rootDir):t.cwd(),a=s.file(n.envFile);i.config({path:a.path()});const l=r.env,c=s.file(n.dataFile,{codec:"auto"}),p=await c.exists()?await c.read():{},h=n.configFile?s.file(n.configFile,{codec:"auto"}):await je(s,n.configFiles),u=await h.read();u.name||=n.name||r.argv[1].replace(s.path()+"/",""),u.version||=n.version,u.description||=n.description,o.preprocess&&await o.preprocess(u);const g={root:s.path(),...n.values||{}},d={quiet:!0,verbose:!0,yes:!0};for(let t of u.options){const{name:r,save:o}=t;if(e.isBoolean(o)&&!o&&(d[r]=!0),!r)continue;t.pattern&&(t.type||="text");const n=t.envvar||=r.toUpperCase();e.hasValue(l[n])?t.default=l[n]:e.hasValue(p[r])?t.default=p[r]:e.hasValue(g[r])?t.default=g[r]:t.program&&(t.default=await Pe(t.program),t.pattern||="<path>")}const f=await ae(u);f||ne("\n👎 "+P(n.cancelled));const{debug:b,verbose:m,quiet:w}=f;return n.writeData&&(await c.write(e.extract(f,(e=>!d[e]))),m&&console.log(V(`✓ Wrote data file: ${c}`))),n.writeEnv&&(await a.write(await async function(t,r,o,n){const{compact:s,envComments:i,envSections:a}=r,l=s?"":"\n";let c=[Ve(r.warning(r))];const p="#"+"-".repeat(77),h="#"+"=".repeat(77);for(let t of o.options){const{name:r,envvar:o,title:h,about:u,save:g}=t;if(!e.isBoolean(g)||g)if(h&&a)c.push(`${l}\n${p}\n${Ve(h)}\n${p}`);else if(s||c.push(""),u&&i&&c.push(`${Ve(u)}`),r){const t=n[r];if(e.hasValue(t)){const e=t.toString().match(/[#\r\n]/)?'"$value"':t;c.push(`${o||r.toUpperCase()}=${e}`)}else c.push(Ve(`No value for ${o||r.toUpperCase()}`))}}if(r.envExtra){const o=t.file(r.envExtra);await o.exists()||e.fail(`Cannot find envExtra file: ${r.envExtra}`),a&&c.push("\n",h,Ve(` Additional configuration copied from ${r.envExtra}`),h,""),c.push(await o.read())}return c.join("\n")+"\n"}(s,n,u,f)),m&&console.log(V(`✓ Wrote .env file: ${a}`))),b&&console.log("\nGenerated configuration:",f),n.allDone&&!w&&console.log("\n👍 ",V(n.allDone),"\n"),f}async function je(t,r=[]){for(let e of r){const r=t.file(e,{codec:"auto"});if(await r.exists())return r}e.fail("Can't find a configFile: ",r.join(", "))}async function Pe(o){for(let n of e.splitList(o))for(let e of r.env.PATH.split(":")){const r=t.dir(e).file(n);if(await r.exists())return r.path()}}function Ve(t){return e.splitLines(t.trim()).map((e=>e.match(/^#/)?e:`# ${e}`)).join("\n")}const Ee={script:r.argv[1].split("/").at(-1),prefix:"watcher >",colors:{prefix:"dark grey",error:"bright red",watcher:"bright yellow",section:"bright white",program:"bright green",arg:"bright cyan",args:"bright blue",debug:"bright grey",command:"yellow",scanning:"cyan",watching:"cyan",change:"bright cyan",starting:"bright green",stopping:"bright red",stopped:"green",failed:"bright red"}};const qe=e=>{const r={...Ee,...e},o=r.colors=f({...Ee.colors,...r.colors}),n=Ne(r),s=()=>ne(n()),i=e=>se(n(e));return{config:r,colors:o,helpText:n,help:s,barf:i,cmdLine:async()=>{const{flags:e,args:r}=te({options:"watch restart verbose help",others:"keep",short:{w:"watch",r:"restart",v:"verbose",h:"help"},on:{help:s,watch:(e,t,r,o)=>(o.watch||=[],r.length||i("The -w option expects an argument!"),o.watch.push(r.shift()),!0)}}),[o,...n]=r,a=r.join(" ");return r.length<1&&i("No program specified to run!"),e.watch?.length||i("Nothing specified to watch!"),await t.file(o).exists()||i(`Program not found: ${o}`),{...e,progText:a,program:o,programArgs:n}}}},De=({verbose:t,colors:r,prefix:o,progText:n})=>({scanning:()=>t?console.log(r.prefix(o),r.scanning("scanning...")):e.doNothing,watching:n=>t?n.forEach((e=>console.log(r.prefix(o),r.watching("watching "),r.command(e)))):e.doNothing,change:(n,s)=>t?console.log(r.prefix(o),r.change(`${n}`.padEnd(9)),r.command(s)):e.doNothing,starting:()=>t?console.log(r.prefix(o),r.starting("starting "),r.command(n)):e.doNothing,stopping:()=>t?console.log(r.prefix(o),r.stopping("stopping "),r.command(n)):e.doNothing,failed:s=>t?console.log(r.prefix(o),r.failed(`failed ${s}`),r.command(n)):e.doNothing,stopped:()=>t?console.log(r.prefix(o),r.stopped("stopped "),r.command(n)):e.doNothing}),Ne=({colors:e,script:t})=>{const{watcher:r,arg:o,section:n,program:s,args:i,error:a}=e,l=r(t);return e=>`${l}\n${e?`\n${a(e)}\n`:""}\n${n("Usage")}\n\n $ ${l} [${o("options")}] ${s("program")} [${i("arg1 arg2 ...")}]\n\n${n("Description")}\n\n This program will run another program and restart it if any files\n are added, deleted or modified in one or more watched locations.\n\n Paths to watch should be specified using the ${o("-w")} (or ${o("--watch")})\n option. You can specify this multiple times for different paths.\n\n The path to the ${s("program")} should follow, along with any additional\n ${i("arguments")} to be passed to it.\n\n If the ${o("-r")} (or ${o("--restart")}) option is specified then the\n program will be restarted when it exits.\n\n${n("Options")}\n\n ${o("-w <path>")} / ${o("--watch <path>")} Watch a path for changes\n ${o("-r")} / ${o("--restart")} Restart program on exit\n ${o("-v")} / ${o("--verbose")} Verbose mode\n ${o("-h")} / ${o("--help")} This help\n\n${n("Examples")}\n\n $ ${l} ${o("-w .")} ${s("bin/foo.js")}\n $ ${l} ${o("-w lib -w config")} ${s("bin/foo.js")}\n $ ${l} ${o("-w lib -w config")} ${s("bin/foo.js")} ${i("bar baz")}\n $ ${l} ${o("-w lib -w config -v")} ${s("bin/foo.js")} ${i("bar baz")}\n $ ${l} ${o("-w lib -w config -v -r")} ${s("bin/foo.js")} ${i("bar baz")}\n`},Se={dir:"config"},Le={dir:"lib library src components"},Te={};class Fe{constructor(r,o={}){const n=t.dir(r),s=e.splitList(o.config?.dir||Se.dir).map((e=>n.dir(e))),i={...Se,...o.config||{}},a=new _(s,i),l=e.splitList(o.library?.dir||Le.dir).map((e=>n.dir(e))),c={...Le,...o.library||{}},p=new K(l,c);this.case={...Te,...o.case||{}},this.state={rootDir:n,config:a,library:p},R(this,o.debug,o.debugPrefix,o.debugColor),this.debug("root dir: ",n.path()),this.debug("config dir: ",s.map((e=>e.path()))),this.debug("libDirs: ",l),this.debug("libOpts: ",c)}dir(t,r){return this.debug("dir(%s, %o)",t,r),e.hasValue(t)?this.state.rootDir.dir(t,r):this.state.rootDir}file(e,t){return this.debug("file(%s, %o)",e,t),this.state.rootDir.file(e,t)}read(e,t){return this.debug("read(%s, %o)",e,t),this.file(e,t).read()}write(e,t,r){return this.debug("write(%s, %o, %o)",e,t,r),this.file(e,r).write(t)}configDir(t,r){return this.debug("configDir(%s, %o)",t,r),e.hasValue(t)?this.state.configDir.dir(t,r):this.state.configDir}async config(t,r){return this.debug("config(%s, %o)",t,r),e.hasValue(t)?this.state.config.config(t,r):this.state.config}async library(t){return this.debug("library(%s, %o)",t),e.hasValue(t)?this.state.library.library(t):this.state.library}async component(t,r){const[o,n]=t.split("#",2),s=this.convertCase("config",o),i=await this.config(s,{}),a=i.component?.library||this.convertCase("library",o),l=await this.library(a),c=n||i.component?.export||"default";return new(l[c]||e.fail("No '",c,"' export from component library: ",t))(this,{...i,...r})}convertCase(e,t){const r=this.case[e];return r?r(t):t}}exports.ANSIescape=u,exports.ANSIescapeCode=h,exports.ANSIreset=g,exports.Component=class{constructor(e,t={}){this.workspace=e,this.props=t,R(this,t.debug,t.debugPrefix,t.debugColor),this.initComponent(t)}initComponent(){}},exports.Config=_,exports.Debugger=M,exports.Library=K,exports.Progress=$e,exports.Workspace=Fe,exports.abort=se,exports.addDebug=R,exports.allParams=ue,exports.answer=ce,exports.anyParams=function(t,r){let o=!1;const n=e.splitList(r),s=n.map((r=>{const n=t[r];return e.hasValue(n)&&(o=!0),n}));return o?s:e.fail("Missing value for one of: ",e.joinListOr(n))},exports.appStatus=X,exports.args=function(e=r.argv){return e.slice(2)},exports.black=b,exports.blue=y,exports.brightBlack=j,exports.brightBlue=q,exports.brightCyan=N,exports.brightGreen=V,exports.brightGrey=S,exports.brightMagenta=D,exports.brightRed=P,exports.brightWhite=L,exports.brightYellow=E,exports.charTypes=ge,exports.cmdLine=ee,exports.cmdLineArg=re,exports.cmdLineArgs=async function(e,t=ee()){let r=[];if(e){for(let o of e){const e=await re(o,t);if(!e?.length)return;r.push(e)}return r}return t},exports.cmdLineFlags=te,exports.color=d,exports.colours=de,exports.config=(e,t)=>new _(e,t),exports.confirm=async(t,r={})=>(r=e.isBoolean(r)?{default:r}:r,Z(t,{...r,type:"confirm"})),exports.cyan=v,exports.darkBlack=T,exports.darkBlue=z,exports.darkCyan=W,exports.darkGreen=A,exports.darkGrey=B,exports.darkMagenta=I,exports.darkRed=F,exports.darkWhite=G,exports.darkYellow=O,exports.dataPath=H,exports.defaults=ie,exports.exit=oe,exports.findFirstFile=je,exports.findProgram=Pe,exports.formatElapsed=ye,exports.formatPicture=xe,exports.green=w,exports.grey=C,exports.library=(e,t)=>new K(e,t),exports.magenta=$,exports.matchDoubleQuotedString=e=>{const t=e.match(/^"((?:\\[\\"bfnrtv]|.)*?)"$/);return t?t[1].replace(/\\([\\"bfnrtv])/g,((e,t)=>U[t]||`\\${t}`)):null},exports.matchSingleQuotedString=e=>{const t=e.match(/^'((?:\\[\\'bfnrtv]|.)*?)'$/);return t?t[1].replace(/\\([\\'bfnrtv])/g,((e,t)=>U[t]||`\\${t}`)):null},exports.options=ae,exports.palette=f,exports.picture=fe,exports.prepareColours=we,exports.preparePicture=me,exports.progress=e=>new $e(e),exports.prompt=Z,exports.quit=ne,exports.red=m,exports.requiredParam=pe,exports.requiredParams=he,exports.runSetup=ke,exports.section=le,exports.select=async(t,r,n)=>(await o([{type:"select",name:"selected",message:t,initial:n,choices:e.isArray(r)?r:Object.entries(r).map((([e,t])=>({value:e,title:t})))}])).selected,exports.setup=Ce,exports.splitDataPath=Y,exports.watch=async function(t){const{config:r,cmdLine:o}=qe(t),n=await o(),{watch:s,restart:i,program:c,programArgs:p}=n,{scanning:h,watching:u,starting:g,stopping:d,failed:f,stopped:b,change:m}=De({...r,...n});let w,x=!1;h(),u(s);const y=()=>{w||(g(),w=l.spawn(c,p,{stdio:"inherit"}),w.on("exit",((t,r)=>{r||(t?f(t):b(),i?(w=null,e.sleep(500).then(y)):oe(t))})))},$=e.debounce((()=>{w&&(d(),w.kill(),w=null),y()}),300);a.watch(s).on("ready",(()=>{y(),x=!0})).on("all",((e,t)=>{x&&(m(e,t),$())}))},exports.watcherFunctions=qe,exports.watcherHelpText=Ne,exports.watcherStatus=De,exports.white=k,exports.workspace=(e,t)=>new Fe(e,t),exports.yellow=x,Object.keys(t).forEach((function(e){"default"===e||Object.prototype.hasOwnProperty.call(exports,e)||Object.defineProperty(exports,e,{enumerable:!0,get:function(){return t[e]}})})),Object.keys(c).forEach((function(e){"default"===e||Object.prototype.hasOwnProperty.call(exports,e)||Object.defineProperty(exports,e,{enumerable:!0,get:function(){return c[e]}})}));
|
|
1
|
+
"use strict";var e=require("@abw/badger-utils"),t=require("@abw/badger-filesystem"),r=require("node:process"),o=require("prompts"),n=require("commander"),s=require("@abw/badger-timestamp"),i=require("dotenv"),a=require("chokidar"),l=require("node:child_process"),c=require("@abw/badger-codecs");const p={reset:0,bold:1,bright:1,dark:2,black:0,red:1,green:2,yellow:3,blue:4,magenta:5,cyan:6,grey:7,white:8,fg:30,bg:40},h={fg:e=>`38;2;${e.r};${e.g};${e.b}`,bg:e=>`48;2;${e.r};${e.g};${e.b}`},u=(t,r="fg")=>{const o=(e=>{const t=e.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i);return t?{r:parseInt(t[1],16),g:parseInt(t[2],16),b:parseInt(t[3],16)}:null})(t)||(t=>{const r=e.splitList(t);return 3===r.length?{r:r[0],g:r[1],b:r[2]}:null})(t);return o?((e,t="fg")=>"["+h[t](e)+"m")(o,r):((e,t="fg")=>{let r=[],o=e.split(/ /,2);const n=o.pop(),s=(t?p[t]:0)+p[n];if(r.push(s),o.length){const e=o.length?o.shift():"dark";r.push(p[e])}return"["+r.join(";")+"m"})(t,r)},g=(t={})=>{const r=e.isObject(t)?t:{fg:t};let o=[];return r.bg&&o.push(u(r.bg,"bg")),r.fg&&o.push(u(r.fg,"fg")),o.join("")},d=u("reset",!1),f=()=>d,b=e=>(...t)=>g(e)+t.join("")+d,m=e=>Object.entries(e).reduce(((e,[t,r])=>(e[t]=b(r),e)),{}),w=b("black"),x=b("red"),y=b("green"),$=b("yellow"),v=b("blue"),C=b("magenta"),k=b("cyan"),j=b("grey"),P=b("white"),V=b("bright black"),E=b("bright red"),q=b("bright green"),N=b("bright yellow"),D=b("bright blue"),S=b("bright magenta"),L=b("bright cyan"),T=b("bright grey"),A=b("bright white"),F=b("dark black"),O=b("dark red"),I=b("dark green"),z=b("dark yellow"),W=b("dark blue"),B=b("dark magenta"),G=b("dark cyan"),M=b("dark grey"),R=b("dark white");function U(t,r="",o){return t?r?(e,...t)=>console.log("%s"+r+"%s"+e,o?g(o):"",f(),...t):console.log.bind(console):e.doNothing}function Y(e,t,r="",o){e.debug=U(t,r,o)}const H={b:"\b",f:"\f",n:"\n",r:"\r",t:"\t",v:"\v",'"':'"',"'":"'","\\":"\\"},Q=t=>{let r,o,n=[];for(;t.length;){let s,i;(o=t.match(/^(\/+)/))?t=t.slice(o[1].length):(t.match(/^(\?)/)&&(t=t.slice(1),s=!0),(o=t.match(/^('((\\[\\'bfnrtv]|.)*?)')/))?(t=t.slice(o[1].length),r=o[2].replace(/\\([\\'bfnrtv])/g,((e,t)=>H[t]||`\\${t}`))):(o=t.match(/^("((\\[\\"bfnrtv]|.)*?)")/))?(t=t.slice(o[1].length),r=o[2].replace(/\\([\\"bfnrtv])/g,((e,t)=>H[t]||`\\${t}`))):(o=t.match(/^([^/?]+)/))?(r=o[1],t=t.slice(r.length)):e.fail(`Can't parse data path: ${t}`),t.match(/^(\?)/)&&(t=t.slice(1),i=!0),s?n.push([r,{maybe:!0}]):i?n.push([r,{optional:!0}]):n.push(r))}return n},_=(t,r)=>{let o=t,n=Q(r),s=[];for(let t of n){const[r,n]=e.isArray(t)?t:[t,{}],i=o[r];if(s.push(r),e.noValue(i)){if(n.maybe)return o;if(n.optional)return;e.fail("No value for data at path: ",s.join("/"))}o=i}return o},J={codec:["yaml","json"],jsExt:["js","mjs"]};class K extends t.DirPath{constructor(t,r={}){super(t);const o={...J,...r};this.state.codec=e.splitList(o.codec),this.state.jsExt=e.splitList(o.jsExt),Y(this,r.debug,r.debugPrefix,r.debugColor)}async firstFileWithExt(t,r,o=e.doNothing){const n=await this.dirs();for(let e of n)for(let n of r){const r=t+"."+n,s=e.file(r,o(t,n));if(this.debug("looking for config file: ",s.path()),await s.exists())return this.debug("config file exists: ",s.path()),s}}async jsFile(e){return await this.firstFileWithExt(e,this.state.jsExt)}async file(e){return await this.firstFileWithExt(e,this.state.codec,((e,t)=>({codec:t})))}async config(t,r){const[o,n]=t.split("#",2);let s,i,a;if(s=await this.jsFile(o))a=await import(s.path());else if(i=await this.file(o))a=await i.read();else{if(!r)return e.fail("No configuration file for "+o);a=r}return n?_(a,n):a}}const X={jsExt:"js mjs"};class Z extends t.DirPath{constructor(t,r={}){super(t);const o={...X,...r},n=e.splitList(o.jsExt).map((e=>e.replace(/^\./,"")));this.state.exts=n,Y(this,r.debug,r.debugPrefix,r.debugColor),this.debug("state: ",this.state)}async library(t){const[r,o]=t.split("#",2),n=await this.dirs(),s=this.state.exts;for(let e of n)for(let t of s){const n=e.file(r+"."+t);this.debug("looking for module %s as",r,n.path());if(await n.exists()){const e=await import(n.path());return this.debug("loaded %s as",n.path()),o?_(e,o):e}}e.fail("Library not found: ",t)}}const ee=e=>async function(...t){try{const r=await e(...t);return r&&console.log(q(`✓ ${r}`)),!0}catch(e){return console.log(r.env.DEBUG?e:E(`✗ ${e.message||e}`)),!1}},te=async(t,r={})=>{const n=(r=e.isString(r)?{default:r}:r).name||"answer";return(await o([{name:n,type:r.type||"text",message:t,initial:r.default}]))[n]},re=()=>r.argv.slice(2);function oe(t,r){t?e.isArray(t)&&(r=t,t={}):t={},r||(r||=re());const o=!!t.options&&e.splitHash(t.options),n=t.others||"error",s=t.short||{},i=t.on||{};let a=!1,l=0,c={};for(;l<r.length;){const t=r[l].match(/^-(-)?(.*)/);if("--"===r[l]&&(a=!0,r.splice(l,1)),t&&!a){const a=!("-"===t[1])&&e.hasValue(s[t[2]])?s[t[2]]:t[2];if(i[a]){if(i[a](a,r.splice(l,1)[0],r,c))continue;r.splice(l,0,t[0])}if(o&&!o[a])switch(n){case"collect":break;case"remove":r.splice(l,1)[0];case"keep":l++;continue;case"error":return e.fail(`Invalid command line flag: ${t[0]}`);default:return e.fail(`Invalid "others" option: ${n}`)}r.splice(l,1),c[a]=!0}else l++}return{flags:c,args:r}}async function ne(e,t=re()){return t.length?t.shift():e?await te(e):void 0}function se(e,t){t&&console.log(t),r.exit(e)}function ie(e){se(0,e)}function ae(e){se(1,e)}const le={verboseColor:"magenta",titleColor:"bright yellow",underlineColor:"dark yellow",infoColor:"cyan",tickColor:"green",questionColor:"bright white",answerColor:"bright green",sectionNewlines:!0},ce=async t=>{const s=b(ce.verboseColor||le.verboseColor),i=new n.Command;t.name&&i.name(t.name),t.description&&i.description(t.description),t.version&&i.version(t.version),t.yes&&i.option("-y, --yes","Accept default answers"),t.verbose&&i.option("-v, --verbose","Verbose output"),t.quiet&&i.option("-q, --quiet","Quiet output"),t.options?.filter((t=>!(e.hasValue(t.arg)&&!t.arg)&&(t.title?void 0:e.hasValue(t.name)))).forEach((t=>{const r=t.name,o=t.about,n=t.default,s=t.handler,a=t.short,l=t.type,c=t.pattern||(e.hasValue(l)?`<${l}>`:void 0);let p=`--${r}`,h=[];e.hasValue(a)&&(p=`-${a}, ${p}`),e.hasValue(c)&&(p=`${p} ${c}`),h.push(p),e.hasValue(o)&&h.push(o),e.hasValue(s)&&h.push(s),e.hasValue(n)&&h.push(n),i.option(...h)}));let a={};t.commands?.forEach((t=>{const r=t.name,o=t.about,n=t.type,s=t.pattern||(e.hasValue(n)?`<${n}>`:void 0);let l=[],c=r,p=i.command(c);e.hasValue(s)&&p.argument(s),e.hasValue(o)&&p.description(o),e.hasValue(t.arguments)&&e.splitList(t.arguments).forEach((t=>{l.push(function(t){const r=t.match(/(\w+)/);r||e.fail("Can't parse argument name: ",t);return r[1]}(t)),p.argument(t)})),p.action(((...e)=>{a[r]=l.length?l.reduce(((t,r)=>(t[r]=e.shift(),t)),{}):e[0]}))})),i.parse();const l=i.opts(),c=t.yes&&l.yes,p=t.verbose&&l.verbose,h=t.quiet&&l.quiet;c&&(p&&!h&&r.stdout.write(s("Accepting default answers (-y option is set)\n")),o.override(l));let u=[];t.options?.forEach((t=>{const r=t.type||"text",o=t.name,n=t.prompt,s=e.hasValue(t.arg)&&!t.arg,i=t.validate||(t.required?t=>!(!e.hasValue(t)||!t.toString().length)||(ce.invalid||`You must enter a value for ${o}`):void 0);let a=s?t.default:l[o];"select"===r&&e.hasValue(a)&&(Number.isInteger(a)||(a=t.choices?.findIndex((e=>e.value===a)),a<0&&(a=0))),e.hasValue(n)?u.push({...t,type:e.hasValue(a)?()=>(c&&(h||he({question:n,answer:a})),r):r,name:o,message:n,initial:a,validate:i}):(t.title||t.info)&&u.push({type:()=>(h||pe(t),!1)})}));let g=!1;const d=await o(u,{onCancel:()=>(g=!0,!1)});return g?void 0:{...l,...d,...a}};const pe=t=>{const o=t.title,n=b(t.titleColor||le.titleColor),s=b(t.underlineColor||le.underlineColor),i=b(t.infoColor||le.infoColor),a=(e.hasValue(t.newlines)?t.newlines:le.sectionNewlines)?"\n":"";if(o){const e="-".repeat(o.length);r.stdout.write(a+n(o)+"\n"+s(e)+"\n"+a)}t.info&&r.stdout.write(i(t.info)+"\n"+a)},he=e=>{const t=b(e.tickColor||le.tickColor),o=b(e.questionColor||le.questionColor),n=b(e.answerColor||le.answerColor);r.stdout.write(t("✔ ")+o(e.question)+" "+n(e.answer)+"\n")};function ue(t={},r){const o=t[r];if(e.hasValue(o))return o;e.fail("Missing value for required parameter: ",r)}function ge(t={},r){return e.splitList(r).map((e=>ue(t,e)))}const de=ge;const fe={border:"┌─┐│└─┘|-",background:"+∙",fill:"*"},be={border:"green",background:"dark grey",foreground:"blue",fill:"bright yellow"},me={charTypes:fe,source:"\n┌────────────────────────────────────────────────────────────────┐\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙ ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ∙∙ │\n│ ∙∙ ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ∙∙ │\n│ ∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙ │\n│ ∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙ │\n│ ∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙ │\n│ ∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙ │\n│ ∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙ │\n│ ∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙ ●● *◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦* ●● ∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙ ●● **◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦** ●● ∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙ ●● *****◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦***** ●● ∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● *******◦◦◦◦◦◦◦◦◦◦******* ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ****************** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ************ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ****** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● **** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● **** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● **** ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦****◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦****◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦****◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦******◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦********◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦◦************◦◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦◦******************◦◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙∙∙ │\n│ ∙∙∙∙∙∙ ●● ◦◦◦◦◦◦◦◦◦************************◦◦◦◦◦◦◦◦◦ ●● ∙∙∙∙∙∙ │\n│ ∙∙∙∙∙ ●● ◦◦◦◦◦◦◦******************************◦◦◦◦◦◦◦ ●● ∙∙∙∙∙ │\n│ ∙∙∙∙ ●● ◦◦◦◦◦************************************◦◦◦◦◦ ●● ∙∙∙∙ │\n│ ∙∙∙ ●● ◦◦◦◦****************************************◦◦◦◦ ●● ∙∙∙ │\n│ ∙∙ ●● ◦◦◦********************************************◦◦◦ ●● ∙∙ │\n│ ∙∙ ●● ◦◦**********************************************◦◦ ●● ∙∙ │\n│ ∙∙ ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ∙∙ │\n│ ∙∙ ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● ∙∙ │\n│ ∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙∙ │\n└────────────────────────────────────────────────────────────────┘\n"},we={size:100,elapsed:!1,picture:me,colours:be};function xe(e){const{source:t,charTypes:r={background:""}}=e,o=t.trim()+"\n",n=o.length,s=Object.entries(r).reduce(((e,[t,r])=>(r.split("").forEach((r=>e[r]=t)),e)),{});return{picture:o,length:n,lineLength:t.split("\n").reduce(((e,t)=>Math.max(e,t.length)),0),charType:s}}function ye(e){return Object.entries(e).reduce(((e,[t,r])=>(e[t]=b(r),e)),{})}function $e(e){const{picture:t,charType:r={},colours:o={}}=e;return t.split("").map((e=>{const t=r[e]||"foreground",n=o[t];return n?n(e):e})).join("")}function ve(e){const t=e%60,r=Math.floor(e/60);return[Math.floor(e/3600),r,t].map((e=>e.toString().padStart(2,"0"))).join(":")}class Ce{constructor(t={}){const r={...we,...e.isInteger(t)?{size:t}:t},{size:o,colours:n,colors:i,debug:a,elapsed:l}=r,{picture:c,length:p,lineLength:h,charType:u}=xe(e.isString(r.picture)?{source:r.picture,charTypes:fe}:r.picture);this.size=o,this.picture=c,this.pixels=this.picture.split(""),this.length=p,this.lineLength=h,this.charType=u,this.colours=ye(i||n),this.count=0,this.lastCount=0,this.lastPixel=0,this.startTime=s.now().epochSeconds(),this.elapsed=0,this.elapsedCol=b("dark grey"),this.showElapsed=l,this.debugging=a,Y(this,a,"Progress > ","blue")}progress(e=1){this.count+=e;const t=s.now().epochSeconds()-this.startTime;this.elapsed=t;const r=Math.floor(this.length*this.count/this.size);if(r>this.lastPixel){const e=this.lastPixel;return this.lastPixel=r,this.debug(`count: ${this.count}/${this.size} pixels from ${e} to ${r}`),this.pictureSegment(e,r)}return""}remains(){this.count=this.size;const e=this.lastPixel,t=this.length;return t>e?(this.lastPixel=this.length,this.pictureSegment(e,t)):""}pictureSegment(e,t){if(this.debugging)return"";let r=$e({picture:this.picture.substring(e,t),charType:this.charType,colours:this.colours});if(this.showElapsed&&r.match("\n")){const e=r.split("\n"),t=e.pop();r=e.map((e=>e+" "+this.elapsedCol(ve(this.elapsed)))).join("\n").concat("\n",t)}return r}printProgress(e=1){r.stdout.write(this.progress(e))}printRemains(){r.stdout.write(this.remains())}}const ke={description:"Project setup script.",configFiles:["config/setup.yaml","config/setup.json","setup.yaml","setup.json",".setup.yaml",".setup.json"],dataFile:".env.yaml",envFile:".env",envComments:!0,envSections:!0,writeData:!0,writeEnv:!1,compact:!1,cancelled:"Setup cancelled",allDone:"All configuration options have been set",warning:e=>`\n#=============================================================================\n# WARNING: This file is generated automatically when the ${e.scriptName}\n# script is run. Any changes made here may be lost.\n#\n# Generated: ${s.now()}\n#=============================================================================\n\n`},je=ee((async e=>{await Pe(e)}));async function Pe(o){const n={...ke,...o},s=n.rootDir?t.dir(n.rootDir):t.cwd(),a=s.file(n.envFile);i.config({path:a.path()});const l=r.env,c=s.file(n.dataFile,{codec:"auto"}),p=await c.exists()?await c.read():{},h=n.configFile?s.file(n.configFile,{codec:"auto"}):await Ve(s,n.configFiles),u=await h.read();u.name||=n.name||r.argv[1].replace(s.path()+"/",""),u.version||=n.version,u.description||=n.description,o.preprocess&&await o.preprocess(u);const g={root:s.path(),...n.values||{}},d={quiet:!0,verbose:!0,yes:!0};for(let t of u.options){const{name:r,save:o}=t;if(e.isBoolean(o)&&!o&&(d[r]=!0),!r)continue;t.pattern&&(t.type||="text");const n=t.envvar||=r.toUpperCase();e.hasValue(l[n])?t.default=l[n]:e.hasValue(p[r])?t.default=p[r]:e.hasValue(g[r])?t.default=g[r]:t.program&&(t.default=await Ee(t.program),t.pattern||="<path>")}const f=await ce(u);f||ie("\n👎 "+E(n.cancelled));const{debug:b,verbose:m,quiet:w}=f;return n.writeData&&(await c.write(e.extract(f,(e=>!d[e]))),m&&console.log(q(`✓ Wrote data file: ${c}`))),n.writeEnv&&(await a.write(await async function(t,r,o,n){const{compact:s,envComments:i,envSections:a}=r,l=s?"":"\n";let c=[qe(r.warning(r))];const p="#"+"-".repeat(77),h="#"+"=".repeat(77);for(let t of o.options){const{name:r,envvar:o,title:h,about:u,save:g}=t;if(!e.isBoolean(g)||g)if(h&&a)c.push(`${l}\n${p}\n${qe(h)}\n${p}`);else if(s||c.push(""),u&&i&&c.push(`${qe(u)}`),r){const t=n[r];if(e.hasValue(t)){const e=t.toString().match(/[#\r\n]/)?'"$value"':t;c.push(`${o||r.toUpperCase()}=${e}`)}else c.push(qe(`No value for ${o||r.toUpperCase()}`))}}if(r.envExtra){const o=t.file(r.envExtra);await o.exists()||e.fail(`Cannot find envExtra file: ${r.envExtra}`),a&&c.push("\n",h,qe(` Additional configuration copied from ${r.envExtra}`),h,""),c.push(await o.read())}return c.join("\n")+"\n"}(s,n,u,f)),m&&console.log(q(`✓ Wrote .env file: ${a}`))),b&&console.log("\nGenerated configuration:",f),n.allDone&&!w&&console.log("\n👍 ",q(n.allDone),"\n"),f}async function Ve(t,r=[]){for(let e of r){const r=t.file(e,{codec:"auto"});if(await r.exists())return r}e.fail("Can't find a configFile: ",r.join(", "))}async function Ee(o){for(let n of e.splitList(o))for(let e of r.env.PATH.split(":")){const r=t.dir(e).file(n);if(await r.exists())return r.path()}}function qe(t){return e.splitLines(t.trim()).map((e=>e.match(/^#/)?e:`# ${e}`)).join("\n")}const Ne={script:r.argv[1].split("/").at(-1),prefix:"watcher >",colors:{prefix:"dark grey",error:"bright red",watcher:"bright yellow",section:"bright white",program:"bright green",arg:"bright cyan",args:"bright blue",debug:"bright grey",command:"yellow",scanning:"cyan",watching:"cyan",change:"bright cyan",starting:"bright green",stopping:"bright red",stopped:"green",failed:"bright red"}};const De=e=>{const r={...Ne,...e},o=r.colors=m({...Ne.colors,...r.colors}),n=Le(r),s=()=>ie(n()),i=e=>ae(n(e));return{config:r,colors:o,helpText:n,help:s,barf:i,cmdLine:async()=>{const{flags:e,args:r}=oe({options:"watch restart verbose help",others:"keep",short:{w:"watch",r:"restart",v:"verbose",h:"help"},on:{help:s,watch:(e,t,r,o)=>(o.watch||=[],r.length||i("The -w option expects an argument!"),o.watch.push(r.shift()),!0)}}),[o,...n]=r,a=r.join(" ");return r.length<1&&i("No program specified to run!"),e.watch?.length||i("Nothing specified to watch!"),await t.file(o).exists()||i(`Program not found: ${o}`),{...e,progText:a,program:o,programArgs:n}}}},Se=({verbose:t,colors:r,prefix:o,progText:n})=>({scanning:()=>t?console.log(r.prefix(o),r.scanning("scanning...")):e.doNothing,watching:n=>t?n.forEach((e=>console.log(r.prefix(o),r.watching("watching "),r.command(e)))):e.doNothing,change:(n,s)=>t?console.log(r.prefix(o),r.change(`${n}`.padEnd(9)),r.command(s)):e.doNothing,starting:()=>t?console.log(r.prefix(o),r.starting("starting "),r.command(n)):e.doNothing,stopping:()=>t?console.log(r.prefix(o),r.stopping("stopping "),r.command(n)):e.doNothing,failed:s=>t?console.log(r.prefix(o),r.failed(`failed ${s}`),r.command(n)):e.doNothing,stopped:()=>t?console.log(r.prefix(o),r.stopped("stopped "),r.command(n)):e.doNothing}),Le=({colors:e,script:t})=>{const{watcher:r,arg:o,section:n,program:s,args:i,error:a}=e,l=r(t);return e=>`${l}\n${e?`\n${a(e)}\n`:""}\n${n("Usage")}\n\n $ ${l} [${o("options")}] ${s("program")} [${i("arg1 arg2 ...")}]\n\n${n("Description")}\n\n This program will run another program and restart it if any files\n are added, deleted or modified in one or more watched locations.\n\n Paths to watch should be specified using the ${o("-w")} (or ${o("--watch")})\n option. You can specify this multiple times for different paths.\n\n The path to the ${s("program")} should follow, along with any additional\n ${i("arguments")} to be passed to it.\n\n If the ${o("-r")} (or ${o("--restart")}) option is specified then the\n program will be restarted when it exits.\n\n${n("Options")}\n\n ${o("-w <path>")} / ${o("--watch <path>")} Watch a path for changes\n ${o("-r")} / ${o("--restart")} Restart program on exit\n ${o("-v")} / ${o("--verbose")} Verbose mode\n ${o("-h")} / ${o("--help")} This help\n\n${n("Examples")}\n\n $ ${l} ${o("-w .")} ${s("bin/foo.js")}\n $ ${l} ${o("-w lib -w config")} ${s("bin/foo.js")}\n $ ${l} ${o("-w lib -w config")} ${s("bin/foo.js")} ${i("bar baz")}\n $ ${l} ${o("-w lib -w config -v")} ${s("bin/foo.js")} ${i("bar baz")}\n $ ${l} ${o("-w lib -w config -v -r")} ${s("bin/foo.js")} ${i("bar baz")}\n`},Te={dir:"config"},Ae={dir:"lib library src components"},Fe={};class Oe{constructor(r,o={}){const n=t.dir(r),s=e.splitList(o.config?.dir||Te.dir).map((e=>n.dir(e))),i={...Te,...o.config||{}},a=new K(s,i),l=e.splitList(o.library?.dir||Ae.dir).map((e=>n.dir(e))),c={...Ae,...o.library||{}},p=new Z(l,c);this.case={...Fe,...o.case||{}},this.state={rootDir:n,config:a,library:p},Y(this,o.debug,o.debugPrefix,o.debugColor),this.debug("root dir: ",n.path()),this.debug("config dir: ",s.map((e=>e.path()))),this.debug("libDirs: ",l),this.debug("libOpts: ",c)}dir(t,r){return this.debug("dir(%s, %o)",t,r),e.hasValue(t)?this.state.rootDir.dir(t,r):this.state.rootDir}file(e,t){return this.debug("file(%s, %o)",e,t),this.state.rootDir.file(e,t)}read(e,t){return this.debug("read(%s, %o)",e,t),this.file(e,t).read()}write(e,t,r){return this.debug("write(%s, %o, %o)",e,t,r),this.file(e,r).write(t)}configDir(t,r){return this.debug("configDir(%s, %o)",t,r),e.hasValue(t)?this.state.configDir.dir(t,r):this.state.configDir}async config(t,r){return this.debug("config(%s, %o)",t,r),e.hasValue(t)?this.state.config.config(t,r):this.state.config}async library(t){return this.debug("library(%s, %o)",t),e.hasValue(t)?this.state.library.library(t):this.state.library}async component(t,r){const[o,n]=t.split("#",2),s=this.convertCase("config",o),i=await this.config(s,{}),a=i.component?.library||this.convertCase("library",o),l=await this.library(a),c=n||i.component?.export||"default";return new(l[c]||e.fail("No '",c,"' export from component library: ",t))(this,{...i,...r})}convertCase(e,t){const r=this.case[e];return r?r(t):t}}exports.ANSIescape=g,exports.ANSIescapeCode=u,exports.ANSIreset=f,exports.ANSIresetCode=d,exports.Component=class{constructor(e,t={}){this.workspace=e,this.props=t,Y(this,t.debug,t.debugPrefix,t.debugColor),this.initComponent(t)}initComponent(){}},exports.Config=K,exports.Debugger=U,exports.Library=Z,exports.Progress=Ce,exports.Workspace=Oe,exports.abort=ae,exports.addDebug=Y,exports.allParams=de,exports.answer=he,exports.anyParams=function(t,r){let o=!1;const n=e.splitList(r),s=n.map((r=>{const n=t[r];return e.hasValue(n)&&(o=!0),n}));return o?s:e.fail("Missing value for one of: ",e.joinListOr(n))},exports.appStatus=ee,exports.args=function(e=r.argv){return e.slice(2)},exports.black=w,exports.blue=v,exports.brightBlack=V,exports.brightBlue=D,exports.brightCyan=L,exports.brightGreen=q,exports.brightGrey=T,exports.brightMagenta=S,exports.brightRed=E,exports.brightWhite=A,exports.brightYellow=N,exports.charTypes=fe,exports.cmdLine=re,exports.cmdLineArg=ne,exports.cmdLineArgs=async function(e,t=re()){let r=[];if(e){for(let o of e){const e=await ne(o,t);if(!e?.length)return;r.push(e)}return r}return t},exports.cmdLineFlags=oe,exports.color=b,exports.colours=be,exports.config=(e,t)=>new K(e,t),exports.confirm=async(t,r={})=>(r=e.isBoolean(r)?{default:r}:r,te(t,{...r,type:"confirm"})),exports.cyan=k,exports.darkBlack=F,exports.darkBlue=W,exports.darkCyan=G,exports.darkGreen=I,exports.darkGrey=M,exports.darkMagenta=B,exports.darkRed=O,exports.darkWhite=R,exports.darkYellow=z,exports.dataPath=_,exports.defaults=le,exports.exit=se,exports.findFirstFile=Ve,exports.findProgram=Ee,exports.formatElapsed=ve,exports.formatPicture=$e,exports.green=y,exports.grey=j,exports.library=(e,t)=>new Z(e,t),exports.magenta=C,exports.matchDoubleQuotedString=e=>{const t=e.match(/^"((?:\\[\\"bfnrtv]|.)*?)"$/);return t?t[1].replace(/\\([\\"bfnrtv])/g,((e,t)=>H[t]||`\\${t}`)):null},exports.matchSingleQuotedString=e=>{const t=e.match(/^'((?:\\[\\'bfnrtv]|.)*?)'$/);return t?t[1].replace(/\\([\\'bfnrtv])/g,((e,t)=>H[t]||`\\${t}`)):null},exports.options=ce,exports.palette=m,exports.picture=me,exports.prepareColours=ye,exports.preparePicture=xe,exports.progress=e=>new Ce(e),exports.prompt=te,exports.quit=ie,exports.red=x,exports.requiredParam=ue,exports.requiredParams=ge,exports.runSetup=Pe,exports.section=pe,exports.select=async(t,r,n)=>(await o([{type:"select",name:"selected",message:t,initial:n,choices:e.isArray(r)?r:Object.entries(r).map((([e,t])=>({value:e,title:t})))}])).selected,exports.setup=je,exports.splitDataPath=Q,exports.watch=async function(t){const{config:r,cmdLine:o}=De(t),n=await o(),{watch:s,restart:i,program:c,programArgs:p}=n,{scanning:h,watching:u,starting:g,stopping:d,failed:f,stopped:b,change:m}=Se({...r,...n});let w,x=!1;h(),u(s);const y=()=>{w||(g(),w=l.spawn(c,p,{stdio:"inherit"}),w.on("exit",((t,r)=>{r||(t?f(t):b(),i?(w=null,e.sleep(500).then(y)):se(t))})))},$=e.debounce((()=>{w&&(d(),w.kill(),w=null),y()}),300);a.watch(s).on("ready",(()=>{y(),x=!0})).on("all",((e,t)=>{x&&(m(e,t),$())}))},exports.watcherFunctions=De,exports.watcherHelpText=Le,exports.watcherStatus=Se,exports.white=P,exports.workspace=(e,t)=>new Oe(e,t),exports.yellow=$,Object.keys(t).forEach((function(e){"default"===e||Object.prototype.hasOwnProperty.call(exports,e)||Object.defineProperty(exports,e,{enumerable:!0,get:function(){return t[e]}})})),Object.keys(c).forEach((function(e){"default"===e||Object.prototype.hasOwnProperty.call(exports,e)||Object.defineProperty(exports,e,{enumerable:!0,get:function(){return c[e]}})}));
|
|
2
2
|
//# sourceMappingURL=badger.cjs.js.map
|