bob-core 2.0.0-beta.2 → 2.0.0-beta.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/cjs/index.js +3 -3
- package/dist/cjs/{package-88kG_8yl.cjs → package-DmD-4Dwv.cjs} +1 -1
- package/dist/cjs/src/Logger.d.ts +2 -3
- package/dist/cjs/src/contracts/LoggerContract.d.ts +1 -2
- package/dist/cjs/src/index.d.ts +1 -0
- package/dist/esm/index.js +22 -25
- package/dist/esm/{package-COKp1myj.js → package-BiJvpS-3.js} +1 -1
- package/dist/esm/src/Logger.d.ts +2 -3
- package/dist/esm/src/contracts/LoggerContract.d.ts +1 -2
- package/dist/esm/src/index.d.ts +1 -0
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("minimist"),a=require("chalk"),w=require("prompts"),W=require("node:fs"),D=require("path"),B=require("string-similarity");function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const P=require("minimist"),a=require("chalk"),w=require("prompts"),W=require("node:fs"),D=require("path"),B=require("string-similarity");function I(r){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const t in r)if(t!=="default"){const n=Object.getOwnPropertyDescriptor(r,t);Object.defineProperty(e,t,n.get?n:{enumerable:!0,get:()=>r[t]})}}return e.default=r,Object.freeze(e)}const _=I(W),G=I(B);class y extends Error{}class U extends y{constructor(e){let t=`Argument "${e.param}" value is invalid.`;e.reason?t+=` Reason: ${e.reason}`:t+=` Value: "${e.value}"`,super(t),this.param=e}pretty(e){e.log(a` {white.bgRed ERROR } Argument {bold.yellow ${this.param.param}} value is invalid. `),(this.param.value||this.param.reason)&&e.log(""),this.param.value&&e.log(a` {blue Value}: ${this.param.value}`),this.param.reason&&e.log(a` {yellow Reason}: ${this.param.reason}`)}}class C extends y{constructor(e){let t=`Option "${e.option}" value is invalid.`;e.reason?t+=` Reason: ${e.reason}`:t+=` Value: "${e.value}"`,super(t),this.param=e}pretty(e){e.log(a` {white.bgRed ERROR } Option {bold.yellow ${this.param.option}} value is invalid. `),(this.param.value||this.param.reason)&&e.log(""),this.param.value&&e.log(a` {blue Value}: ${this.param.value}`),this.param.reason&&e.log(a` {yellow Reason}: ${this.param.reason}`)}}function k(r){if(r==="string"||r==="number")return null;if(r==="boolean")return!1;if(Array.isArray(r)&&r.length===1){if(r[0]==="string")return[];if(r[0]==="number")return[]}throw new Error("Invalid option type: "+r)}function N(r){return typeof r=="string"?k(r):Array.isArray(r)?[]:typeof r=="object"&&r.type?r.default!==void 0?r.default:k(r.type):null}function f(r){return typeof r=="string"||Array.isArray(r)?{type:r,default:N(r),description:"",alias:[],required:!1,variadic:!1}:{type:r.type,default:r.default??N(r.type),description:r.description??"",alias:r.alias?Array.isArray(r.alias)?r.alias:[r.alias]:[],required:r.required??!1,variadic:r.variadic??!1}}class L extends y{constructor(e,t={}){super(`Invalid option ${e} in not recognized`),this.option=e,this.optionsSchema=t}pretty(e){const t=Object.entries(this.optionsSchema);if(t.length>0){e.log(a`\n{yellow Available options}:`);for(const[n,i]of t){const s=f(i),o=typeof s.alias=="string"?[s.alias]:s.alias,l=Array.isArray(s.type)?`[${s.type[0]}]`:s.type,u=`--${n}${o.length>0?o.map(m=>`, -${m}`).join(""):""}`,d=" ".repeat(30-u.length);e.log(a` {green ${u}} ${d} ${s.description||"\b"} {white (${l})}`)}e.log("")}e.log(a`{white.bgRed ERROR } Option {bold.yellow ${this.option}} is not recognized.`)}}class j extends y{constructor(e){super(`Command "${e}" not found.`),this.command=e}pretty(e){e.log(a`{bgRed ERROR } Command {yellow ${this.command}} not found.`)}}class v extends y{constructor(e,t){super(`Missing ${e} in the command signature`),this.argument=e,this.argumentsSchema=t}pretty(e){const t=Object.entries(this.argumentsSchema);if(t.length){e.log(a`\n{yellow Available arguments}:`);for(const[n,i]of t){const s=f(i),o=Array.isArray(s.type)?`[${s.type[0]}]`:s.type,l=o?a`{white (${o})}`:"",u=" ".repeat(20-n.length);e.log(a` {green ${n}} ${u} ${s.description??"\b"} ${l}`)}e.log("")}e.log(a`{white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is missing in the signature.`)}}class b extends y{constructor(e,t){super(`Missing ${e} in the command signature`),this.option=e,this.optionsSchema=t}pretty(e){const t=Object.entries(this.optionsSchema);if(t.length){e.log(a`{yellow Available options}:`);for(const[n,i]of t){const s=f(i),o=Array.isArray(s.type)?`[${s.type[0]}]`:s.type,l=o?a`{white (${o})}`:"",u=" ".repeat(20-n.length);e.log(a` {green ${n}} ${u} ${s.description??"\b"} ${l}`)}e.log("")}e.log(a`{white.bgRed ERROR } Option {bold.yellow ${this.option}} is missing in the signature.`)}}class O extends y{constructor(e){super(`Argument "${e}" is required.`),this.argument=e}pretty(e){e.log(a`{white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is required.`)}}class F extends y{constructor(e){super(`Argument "${e}" is required.`),this.option=e}pretty(e){e.log(a`{white.bgRed ERROR } Option {bold.yellow ${this.option}} is required.`)}}function $(r,e,t,n){if(r==null)return n??null;if(e==="string")return String(r);if(e==="number"){const i=Number(r);if(isNaN(i))throw new C({option:t,reason:`Expected a number, got "${r}"`});return i}if(e==="boolean")return typeof r=="boolean"?r:r==="true"||r==="1"?!0:r==="false"||r==="0"?!1:!!r;if(Array.isArray(e)){const i=e[0],s=Array.isArray(r)?r:[r];if(i==="string")return s.map(o=>String(o));if(i==="number")return s.map(o=>{const l=Number(o);if(isNaN(l))throw new C({option:t,reason:`Expected array of numbers, got "${o}" in array`});return l})}return r}class S{options;parsedOptions=null;arguments;parsedArguments=null;io;constructor(e){this.options=e.options,this.arguments=e.arguments,this.io=e.io}init(e){const{_:t,...n}=P(e);return this.validateUnknownOptions(n),this.parsedOptions=this.handleOptions(n),this.parsedArguments=this.handleArguments(t),{options:this.parsedOptions,arguments:this.parsedArguments}}async validate(){for(const e in this.options)if(f(this.options[e]).required&&(this.parsedOptions?.[e]===void 0||this.parsedOptions?.[e]===null))throw new F(e);for(const e in this.arguments)if(f(this.arguments[e]).required&&(this.parsedArguments?.[e]===void 0||this.parsedArguments?.[e]===null))throw new O(e)}option(e){if(!this.parsedOptions)throw new Error("Options have not been parsed yet. Call init() first.");return this.parsedOptions[e]}argument(e){if(!this.parsedArguments)throw new Error("Arguments have not been parsed yet. Call init() first.");return this.parsedArguments[e]}validateUnknownOptions(e){const t=new Set;for(const n in this.options){t.add(n);const i=f(this.options[n]);for(const s of i.alias)t.add(s)}for(const n in e)if(!t.has(n))throw new L(n,this.options)}handleOptions(e){const t={};for(const n in this.options){const i=f(this.options[n]);t[n]=this.resolveOptionValue(n,i,e,"option")}return t}handleArguments(e){const t={},n=[...e];for(const i in this.arguments){const s=f(this.arguments[i]);if(s.variadic){t[i]=this.handleVariadicArgument(i,s,n);continue}t[i]=this.resolveArgumentValue(i,s,n.shift())}return t}handleVariadicArgument(e,t,n){return n.length?$(n,t.type,e,t.default):t.default}resolveArgumentValue(e,t,n){return n===void 0?t.default:$(n,t.type,e,t.default)}resolveOptionValue(e,t,n,i){let s;const o=[e,...t.alias];for(const l of o)if(l in n){s=n[l];break}if(s===void 0){if(t.required)throw new C({option:e,reason:`${i==="option"?"Option":"Argument"} is required but not provided`});return t.default}return $(s,t.type,e,t.default)}optionDefinitions(){const e={};for(const t in this.options)e[t]=f(this.options[t]);return e}argumentDefinitions(){const e={};for(const t in this.arguments)e[t]=f(this.arguments[t]);return e}availableOptions(){return Object.keys(this.options)}availableArguments(){return Object.keys(this.arguments)}}function R(r){return new Array(r+5).join(" ")}class V{type="boolean";option="help";alias=["h"];default=!1;description=a`Display help for the given command. When no command is given display help for the {green list} command`;async handler(){const e=this.parser.argumentDefinitions(),t=this.parser.optionDefinitions(),n=Object.entries(e),i=Object.entries(t),s=i.map(([m,c])=>{const h=Array.isArray(c.alias)?c.alias:c.alias?[c.alias]:[];return{name:m,...c,optionWithAlias:`--${m}${h.map(p=>`, -${p}`).join("")}`}}),o=n.filter(([,m])=>m.required);this.io.log(a`{yellow Description}:`),this.io.log(a` ${this.description}\n`),this.io.log(a`{yellow Usage}:`),this.io.log(a` ${this.command} ${o.length>0?o.map(([m])=>`<${m}>`).join(" "):"\b"} [options]`);const l=Math.max(...s.map(m=>m.optionWithAlias.length),0),u=Math.max(...n.map(([m])=>m.length),0),d=u>l?u:l;if(n.length>0){this.io.log(a`\n{yellow Arguments}:`);for(const[m,c]of n){const h=R(d-m.length);let p=a` {green ${m}} ${h} ${c.description??"\b"}`;if(c.default!==void 0&&!c.required){const T=(Array.isArray(c.type)?`[${c.type[0]}]`:c.type)==="array"||Array.isArray(c.type)?JSON.stringify(c.default):c.default;p+=a` {yellow [default: ${T}]}`}c.variadic&&(p+=a` {white (variadic)}`),this.io.log(p)}}if(i.length>0){this.io.log(a`\n{yellow Options}:`);for(const m of s){const c=R(d-m.optionWithAlias.length);let h=a`{green ${m.optionWithAlias}} ${c} ${m.description??"\b"}`;if(m.type){const p=Array.isArray(m.type)?`[${m.type[0]}]`:m.type;h+=a` {white (${p})}`}if(m.default!==void 0&&!m.required){const q=(Array.isArray(m.type)?`[${m.type[0]}]`:m.type)==="array"||Array.isArray(m.type)?JSON.stringify(m.default):m.default;h+=a` {yellow [default: ${q}]}`}this.io.log(h)}}if(this.commandsExamples.length>0){this.io.log(a`\n{yellow Examples}:`);let m=process.argv[0].split("/").pop();m==="node"&&(m+=" "+process.argv[1].split("/").pop());for(const[c,h]of this.commandsExamples.entries())c>0&&this.io.log(""),this.io.log(` ${h.description}
|
|
2
2
|
`),this.io.log(a` {green ${m} ${h.command}}`)}return-1}}class x{logger;constructor(e){this.logger=e}log(...e){this.logger.log(...e)}info(...e){this.logger.info(...e)}warn(...e){this.logger.warn(...e)}error(...e){this.logger.error(...e)}debug(...e){this.logger.debug(...e)}verbose(...e){this.logger.verbose(...e)}async askForConfirmation(e="Do you want to continue?",t){return(await w({type:"confirm",name:"value",message:e,initial:t??!1})).value}async askForInput(e,t,n){return(await w({type:"text",name:"value",message:e,initial:t,...n}))?.value??null}async askForToggle(e,t,n){return(await w({type:"toggle",name:"value",message:e,initial:t,...n}))?.value??null}async askForSelect(e,t,n){if(t.length===0)throw new Error("No options provided");const i=[];for(const o of t)typeof o=="string"?i.push({title:o,value:o}):i.push(o);return(await w({type:"select",name:"value",message:e,choices:i,...n}))?.value??null}newLoader(e="",t=["⠙","⠘","⠰","⠴","⠤","⠦","⠆","⠃","⠋","⠉"],n=100){let i=e,s=null,o=0;const l=setInterval(function(){s&&(process.stdout.write(new TextEncoder().encode("\r"+" ".repeat(s.length+5)+"\r")),s=null),process.stdout.write(new TextEncoder().encode("\r"+t[o++]+" "+i)),o=o%t.length},n),u=()=>{clearInterval(l),process.stdout.write(new TextEncoder().encode("\r"+" ".repeat(i.length+5)+"\r"))};return{[Symbol.dispose]:u,[Symbol.asyncDispose]:u,updateText:d=>{s=i,i=d},stop:u}}}class A{_command;description="";commandsExamples=[];get command(){return this._command}ctx;io;logger;_handler;parser;tmp;defaultOptions(){return[new V]}newCommandParser(e){return new S({io:e.io,options:e.options,arguments:e.arguments})}newCommandIO(e){return new x(e.logger)}constructor(e,t){this._command=e,this.description=t?.description??"";const n=this.defaultOptions();if(n.length>0){this.tmp={options:{},arguments:{}};for(const i of n)this.tmp.options[i.option]=i}}handler(e){return this._handler=e,this}options(e){return this.tmp={options:{...this.tmp?.options??{},...e},arguments:this.tmp?.arguments??{}},this}arguments(e){return this.tmp={options:this.tmp?.options??{},arguments:{...this.tmp?.arguments??{},...e}},this}async run(e){if(!this._handler&&!this.handle)throw new Error(`No handler defined for command ${this.command||"(unknown)"}`);let t;if(this.ctx=e.ctx,this.logger=e.logger,this.io=this.newCommandIO({logger:e.logger}),e&&"args"in e){const s=this.tmp?.options??{};for(const o of this.defaultOptions())o.option in s||(s[o.option]=o);this.parser=this.newCommandParser({io:this.io,options:s,arguments:this.tmp?.arguments??{}}),t=this.parser.init(e.args);for(const o of this.defaultOptions())if(t.options[o.option]===!0){const l=await o.handler.call(this);if(l&&l!==0)return l}await this.parser.validate()}else t={options:e.options,arguments:e.arguments};const n=this.preHandle?await this.preHandle():null;if(n&&n!==0)return n;if(!this._handler&&this.handle)this._handler=this.handle.bind(this);else if(!this._handler)throw new Error(`No handler defined for command ${this.command||"(unknown)"}`);return await this._handler(e.ctx,t)??0}}class g extends S{command;argumentsSchema;optionsSchema;constructor(e){const t=g.parseSignature(e.signature,e.helperDefinitions,e.defaultOptions);super({io:e.io,options:t.options,arguments:t.arguments}),this.command=t.command,this.optionsSchema=t.options,this.argumentsSchema=t.arguments}static parseSignature(e,t,n){const[i,...s]=e.split(/\{(.*?)\}/g).map(u=>u.trim()).filter(Boolean),o={},l={};for(const u of s){const{name:d,isOption:m,definition:c}=g.parseParamSignature(u,t);m?o[d]=c:l[d]=c}for(const u of n)o[u.option]={type:u.type,required:u.required,alias:u.alias,variadic:u.variadic??!1,description:u.description,default:u.default??null};return{command:i,options:o,arguments:l}}option(e){if(!this.optionsSchema[e])throw new b(e,this.optionsSchema);return super.option(e)}setOption(e,t){if(!this.optionsSchema[e])throw new b(e,this.optionsSchema);this.parsedOptions&&(this.parsedOptions[e]=t)}optionHelp(e){if(!this.optionsSchema[e])throw new b(e,this.optionsSchema);return f(this.optionsSchema[e]).description}argumentHelp(e){if(!this.argumentsSchema[e])throw new v(e,this.argumentsSchema);return f(this.argumentsSchema[e]).description}argument(e){if(!this.argumentsSchema[e])throw new v(e,this.argumentsSchema);return super.argument(e)}setArgument(e,t){if(!this.argumentsSchema[e])throw new v(e,this.argumentsSchema);this.parsedArguments&&(this.parsedArguments[e]=t)}getArgumentSignatures(){return this.argumentsSchema}getOptionSignatures(){return this.optionsSchema}static parseParamSignature(e,t){let n=e,i=!1;const s={required:!0,type:"string",description:void 0,default:null,variadic:!1};if(n.includes(":")){const[o,l]=n.split(":");n=o.trim(),s.description=l.trim()}if(n.includes("=")){const[o,l]=n.split("=");n=o.trim(),s.default=l.trim(),s.required=!1,s.default.length?s.default==="true"?(s.default=!0,s.type="boolean"):s.default==="false"&&(s.default=!1,s.type="boolean"):s.default=null}else n.startsWith("--")&&(s.required=!1,s.default=!1,s.type="boolean");if(n.includes("|")){const[o,...l]=n.split("|");n=o.trim(),s.alias=l.map(u=>u.trim())}return n.startsWith("--")&&(i=!0,n=n.slice(2)),s.default==="*"&&(s.default=[],s.type=["string"]),n.endsWith("?")&&(s.required=!1,n=n.slice(0,-1)),n.endsWith("*")&&(s.type=["string"],s.variadic=!0,s.default=[],n=n.slice(0,-1)),s.description=s.description??t[n]??t[`--${n}`],{name:n,isOption:i,definition:s}}async validate(){for(const e in this.argumentsSchema){const t=f(this.argumentsSchema[e]),n=this.argument(e);if(!n&&t.required){const i=await this.promptForArgument(e,t);if(i)this.setArgument(e,i);else throw new O(e)}if(t.variadic&&t.required&&typeof n=="object"&&!n?.length)throw new O(e)}}async promptForArgument(e,t){if(t.type!=="string")return null;let n=a`{yellow.bold ${e}} is required`;return t.description&&(n+=a`: {gray (${t.description})}`),n+=`
|
|
3
|
-
`,await this.io.askForInput(n,t.default,{validate:i=>i?.trim()?.length?!0:`${e} cannot be empty`})}optionValues(){if(!this.parsedOptions)throw new Error("Options have not been parsed yet. Call init() first.");return this.parsedOptions}argumentValues(){if(!this.parsedArguments)throw new Error("Arguments have not been parsed yet. Call init() first.");return this.parsedArguments}}class z extends A{helperDefinitions={};get command(){return this.parser?this.parser.command:this.signature.split(" ")[0]}newCommandParser(e){return new g({io:e.io,signature:this.signature,helperDefinitions:this.helperDefinitions,defaultOptions:this.defaultOptions()})}constructor(){super("")}setOption(e,t){this.parser.setOption(e,t)}setArgument(e,t){this.parser instanceof g&&this.parser.setArgument(e,t)}option(e,t=null){return this.parser instanceof g?this.parser.option(e)??t:t}optionBoolean(e,t=!1){return this.parser instanceof g?this.parser.option(e)??t:t}optionArray(e,t=[]){if(this.parser instanceof g){const n=this.parser.option(e);if(!Array.isArray(n))throw new Error(`Option ${e} is not an array`);if(n.length)return n}return t}optionNumber(e,t=null){if(this.parser instanceof g){const n=this.parser.option(e);return n?typeof n=="number"?n:parseInt(n):t}return t}argument(e,t=null){return this.parser instanceof g?this.parser.argument(e)??t:t}argumentArray(e,t=[]){if(this.parser instanceof g){const n=this.parser.argument(e);if(!Array.isArray(n))throw new Error(`Argument ${e} is not an array`);if(n?.length)return n}return t}argumentBoolean(e,t=!1){return this.parser instanceof g?this.parser.argument(e)??t:t}argumentNumber(e,t=null){if(this.parser instanceof g){const n=this.parser.argument(e);return n?typeof n=="number"?n:parseInt(n):t}return t}async askForConfirmation(...e){return this.io.askForConfirmation(...e)}async askForInput(...e){return this.io.askForInput(...e)}async askForSelect(...e){return this.io.askForSelect(...e)}newLoader(...e){return this.io.newLoader(...e)}}class E{level;constructor(e={}){this.level=e.level??"info"}shouldLog(e){const t=["debug","
|
|
3
|
+
`,await this.io.askForInput(n,t.default,{validate:i=>i?.trim()?.length?!0:`${e} cannot be empty`})}optionValues(){if(!this.parsedOptions)throw new Error("Options have not been parsed yet. Call init() first.");return this.parsedOptions}argumentValues(){if(!this.parsedArguments)throw new Error("Arguments have not been parsed yet. Call init() first.");return this.parsedArguments}}class z extends A{helperDefinitions={};get command(){return this.parser?this.parser.command:this.signature.split(" ")[0]}newCommandParser(e){return new g({io:e.io,signature:this.signature,helperDefinitions:this.helperDefinitions,defaultOptions:this.defaultOptions()})}constructor(){super("")}setOption(e,t){this.parser.setOption(e,t)}setArgument(e,t){this.parser instanceof g&&this.parser.setArgument(e,t)}option(e,t=null){return this.parser instanceof g?this.parser.option(e)??t:t}optionBoolean(e,t=!1){return this.parser instanceof g?this.parser.option(e)??t:t}optionArray(e,t=[]){if(this.parser instanceof g){const n=this.parser.option(e);if(!Array.isArray(n))throw new Error(`Option ${e} is not an array`);if(n.length)return n}return t}optionNumber(e,t=null){if(this.parser instanceof g){const n=this.parser.option(e);return n?typeof n=="number"?n:parseInt(n):t}return t}argument(e,t=null){return this.parser instanceof g?this.parser.argument(e)??t:t}argumentArray(e,t=[]){if(this.parser instanceof g){const n=this.parser.argument(e);if(!Array.isArray(n))throw new Error(`Argument ${e} is not an array`);if(n?.length)return n}return t}argumentBoolean(e,t=!1){return this.parser instanceof g?this.parser.argument(e)??t:t}argumentNumber(e,t=null){if(this.parser instanceof g){const n=this.parser.argument(e);return n?typeof n=="number"?n:parseInt(n):t}return t}async askForConfirmation(...e){return this.io.askForConfirmation(...e)}async askForInput(...e){return this.io.askForInput(...e)}async askForSelect(...e){return this.io.askForSelect(...e)}newLoader(...e){return this.io.newLoader(...e)}}class E{level;constructor(e={}){this.level=e.level??"info"}shouldLog(e){const t=["debug","info","warn","error"],n=t.indexOf(this.level);return t.indexOf(e)>=n}setLevel(e){this.level=e}getLevel(){return this.level}log(...e){console.log(...e)}info(...e){this.shouldLog("info")&&console.log(...e)}warn(...e){this.shouldLog("warn")&&console.warn(...e)}error(...e){this.shouldLog("error")&&console.error(...e)}debug(...e){this.shouldLog("debug")&&console.log(...e)}}class H{commands={};io;logger;get CommandIOClass(){return x}constructor(e){this.logger=e??new E,this.io=new this.CommandIOClass(this.logger)}getAvailableCommands(){return Object.keys(this.commands)}getCommands(){return Object.values(this.commands)}commandResolver=async e=>{let t=(await import(e)).default;if(!t)throw new Error(`The command at path ${e} does not have a default export.`);t?.default&&(t=t.default);let n;if(typeof t=="function")n=new t;else if(t instanceof A)n=t;else throw new Error(`The command at path ${e} is not a valid command class.`);return n};setCommandResolver(e){this.commandResolver=e}registerCommand(e,t=!1){const n=e.command;if(!n)throw new Error("Command signature is invalid, it must have a command name.");if(!t&&this.commands[n])throw new Error(`Command ${n} already registered.`);this.commands[n]=e}async loadCommandsPath(e){for await(const t of this.listCommandsFiles(e))try{const n=await this.commandResolver(t);this.registerCommand(n)}catch(n){throw new Error(`Command ${t} failed to load. ${n}`,{cause:n})}}async runCommand(e,t,...n){const i=typeof t=="string"?this.commands[t]:t,s=typeof t=="string"?t:i.command;if(!i){const o=await this.suggestCommand(s);return o?await this.runCommand(e,o,...n):1}return await i.run({ctx:e,logger:this.logger,args:n})??0}async suggestCommand(e){const t=this.getAvailableCommands(),{bestMatch:n,bestMatchIndex:i,ratings:s}=G.findBestMatch(e,t),o=s.filter(l=>l.rating>.3).map(l=>l.target);if(n.rating>0&&o.length<=1||n.rating>.7&&o.length>1){const l=t[i];return await this.askRunSimilarCommand(e,l)?l:null}if(o.length){this.io.error(a`{bgRed ERROR } Command {yellow ${e}} not found.\n`);const l=await this.io.askForSelect(a`{green Did you mean to run one of these commands instead?}`,o);if(l)return l}throw new j(e)}async askRunSimilarCommand(e,t){return this.io.error(a`{bgRed ERROR } Command {yellow ${e}} not found.\n`),this.io.askForConfirmation(a`{green Do you want to run {yellow ${t}} instead?} `)}async*listCommandsFiles(e){const t=_.readdirSync(e,{withFileTypes:!0});for(const n of t){const i=D.resolve(e,n.name);if(n.isDirectory())yield*this.listCommandsFiles(D.resolve(e,n.name));else{if(!i.endsWith(".ts")&&!i.endsWith(".js"))continue;yield i}}}}class J extends A{constructor(e){super("help",{description:a.bold("Show help information about the CLI and its commands")}),this.opts=e}async handle(){const e=this.opts.commandRegistry.getCommands(),t=this.opts.cliName??"Bob CLI",n=this.opts.cliVersion??"0.0.0",i=(await Promise.resolve().then(()=>require("./package-DmD-4Dwv.cjs")))?.default?.version??"0.0.0";this.io.log(a`${t} {green ${n}} (core: {yellow ${i}})
|
|
4
4
|
|
|
5
5
|
{yellow Usage}:
|
|
6
6
|
command [options] [arguments]
|
|
7
7
|
|
|
8
8
|
{yellow Available commands}:
|
|
9
|
-
`);const s=Math.max(...e.map(u=>u.command.length))??0,o={};for(const u of e){const d=u.command.split(":")[0];o[d]||(o[d]=[]),o[d].push(u)}const l=Object.entries(o).sort(([u],[d])=>u.toLowerCase().localeCompare(d.toLowerCase())).sort(([,u],[,d])=>u.length-d.length);for(const[u,d]of l){const m=d.length>1;m&&this.io.log(a`{yellow ${u}}:`);const c=d.sort((h,p)=>h.command.toLowerCase().localeCompare(p.command.toLowerCase()));for(const h of c){let p=R(s-h.command.length);m&&(p=p.slice(2)),this.io.log(a`${m?" ":""}{green ${h.command}} ${p} ${h.description}`)}}}}class M{logger;constructor(e){this.logger=e}handle(e){if(e instanceof y)return e.pretty(this.logger),-1;throw e}}class K{ctx;logger;commandRegistry;exceptionHandler;helpCommand;newCommandRegistry(e){return new H(e.logger)}newHelpCommand(e){return new J(e)}newExceptionHandler(e){return new M(e.logger)}constructor(e={}){this.ctx=e.ctx,this.logger=e.logger??new E,this.commandRegistry=this.newCommandRegistry({logger:this.logger}),this.exceptionHandler=this.newExceptionHandler({logger:this.logger}),this.helpCommand=this.newHelpCommand({cliName:e.name,cliVersion:e.version,commandRegistry:this.commandRegistry})}setCommandResolver(e){this.commandRegistry.setCommandResolver(e)}async withCommands(...e){for(const t of e)typeof t=="string"?await this.commandRegistry.loadCommandsPath(t):typeof t=="function"?this.registerCommand(new t):this.registerCommand(t)}async runCommand(e,...t){return e?await this.commandRegistry.runCommand(this.ctx,e,...t).catch(this.exceptionHandler.handle.bind(this.exceptionHandler)):await this.runHelpCommand()}async runHelpCommand(){return await this.runCommand(this.helpCommand)}registerCommand(e){this.commandRegistry.registerCommand(e)}}exports.BadCommandOption=C;exports.BadCommandParameter=U;exports.BobError=y;exports.Cli=K;exports.Command=A;exports.CommandIO=x;exports.CommandNotFoundError=j;exports.CommandParser=S;exports.CommandRegistry=H;exports.CommandSignatureParser=g;exports.CommandWithSignature=z;exports.ExceptionHandler=M;exports.HelpOption=V;exports.InvalidOption=
|
|
9
|
+
`);const s=Math.max(...e.map(u=>u.command.length))??0,o={};for(const u of e){const d=u.command.split(":")[0];o[d]||(o[d]=[]),o[d].push(u)}const l=Object.entries(o).sort(([u],[d])=>u.toLowerCase().localeCompare(d.toLowerCase())).sort(([,u],[,d])=>u.length-d.length);for(const[u,d]of l){const m=d.length>1;m&&this.io.log(a`{yellow ${u}}:`);const c=d.sort((h,p)=>h.command.toLowerCase().localeCompare(p.command.toLowerCase()));for(const h of c){let p=R(s-h.command.length);m&&(p=p.slice(2)),this.io.log(a`${m?" ":""}{green ${h.command}} ${p} ${h.description}`)}}}}class M{logger;constructor(e){this.logger=e}handle(e){if(e instanceof y)return e.pretty(this.logger),-1;throw e}}class K{ctx;logger;commandRegistry;exceptionHandler;helpCommand;newCommandRegistry(e){return new H(e.logger)}newHelpCommand(e){return new J(e)}newExceptionHandler(e){return new M(e.logger)}constructor(e={}){this.ctx=e.ctx,this.logger=e.logger??new E,this.commandRegistry=this.newCommandRegistry({logger:this.logger}),this.exceptionHandler=this.newExceptionHandler({logger:this.logger}),this.helpCommand=this.newHelpCommand({cliName:e.name,cliVersion:e.version,commandRegistry:this.commandRegistry})}setCommandResolver(e){this.commandRegistry.setCommandResolver(e)}async withCommands(...e){for(const t of e)typeof t=="string"?await this.commandRegistry.loadCommandsPath(t):typeof t=="function"?this.registerCommand(new t):this.registerCommand(t)}async runCommand(e,...t){return e?await this.commandRegistry.runCommand(this.ctx,e,...t).catch(this.exceptionHandler.handle.bind(this.exceptionHandler)):await this.runHelpCommand()}async runHelpCommand(){return await this.runCommand(this.helpCommand)}registerCommand(e){this.commandRegistry.registerCommand(e)}}exports.BadCommandOption=C;exports.BadCommandParameter=U;exports.BobError=y;exports.Cli=K;exports.Command=A;exports.CommandIO=x;exports.CommandNotFoundError=j;exports.CommandParser=S;exports.CommandRegistry=H;exports.CommandSignatureParser=g;exports.CommandWithSignature=z;exports.ExceptionHandler=M;exports.HelpOption=V;exports.InvalidOption=L;exports.Logger=E;exports.MissingRequiredArgumentValue=O;exports.MissingRequiredOptionValue=F;exports.MissingSignatureArgument=v;exports.MissingSignatureOption=b;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e="bob-core",s="2.0.0-beta.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e="bob-core",s="2.0.0-beta.3",t="BOB Core",i="module",n=!1,r=["dist/**"],o={".":{import:"./dist/esm/index.js",require:"./dist/cjs/index.js"}},c={"*":{"*":["./dist/cjs/*.d.ts"]}},d={start:"node -r @swc-node/register debug/main.ts",build:"rimraf ./dist && vite build",typecheck:"tsc --noEmit",prepare:"npm run build",test:"vitest run"},p="Léo Hubert",a="ISC",m={"@faker-js/faker":"^10.0.0","@swc-node/register":"^1.11.1","@types/minimist":"^1.2.5","@types/node":"^20.14.5","@types/prompts":"^2.4.9","@types/string-similarity":"^4.0.2","@vitest/coverage-v8":"^3.2.4",rimraf:"^6.0.1",tsx:"^4.20.6",typescript:"^5.7.3",vite:"^7.1.6","vite-plugin-dts":"^4.5.4",vitest:"^3.2.4"},l={chalk:"^4.1.2",minimist:"^1.2.8",prompts:"^2.4.2","string-similarity":"^4.0.4"},u={name:e,version:s,description:t,type:i,sideEffects:n,files:r,exports:o,typesVersions:c,scripts:d,author:p,license:a,devDependencies:m,dependencies:l};exports.author=p;exports.default=u;exports.dependencies=l;exports.description=t;exports.devDependencies=m;exports.exports=o;exports.files=r;exports.license=a;exports.name=e;exports.scripts=d;exports.sideEffects=n;exports.type=i;exports.typesVersions=c;exports.version=s;
|
package/dist/cjs/src/Logger.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { LoggerContract, LogLevel } from './contracts/index.js';
|
|
2
2
|
export type LoggerOptions = {
|
|
3
3
|
level?: LogLevel;
|
|
4
4
|
};
|
|
5
|
-
export declare class Logger {
|
|
5
|
+
export declare class Logger implements LoggerContract {
|
|
6
6
|
private level;
|
|
7
7
|
constructor(opts?: LoggerOptions);
|
|
8
8
|
private shouldLog;
|
|
@@ -13,5 +13,4 @@ export declare class Logger {
|
|
|
13
13
|
warn(...args: any[]): void;
|
|
14
14
|
error(...args: any[]): void;
|
|
15
15
|
debug(...args: any[]): void;
|
|
16
|
-
verbose(...args: any[]): void;
|
|
17
16
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type LogLevel = 'debug' | 'info' | 'warn' | 'error'
|
|
1
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
2
2
|
/**
|
|
3
3
|
* Logger interface defining standard logging methods and level management
|
|
4
4
|
*/
|
|
@@ -8,7 +8,6 @@ export interface LoggerContract {
|
|
|
8
8
|
warn(message: string, ...args: any[]): void;
|
|
9
9
|
error(message: string, ...args: any[]): void;
|
|
10
10
|
debug(message: string, ...args: any[]): void;
|
|
11
|
-
verbose(message: string, ...args: any[]): void;
|
|
12
11
|
setLevel(level: LogLevel): void;
|
|
13
12
|
getLevel(): LogLevel;
|
|
14
13
|
}
|
package/dist/cjs/src/index.d.ts
CHANGED
package/dist/esm/index.js
CHANGED
|
@@ -15,7 +15,7 @@ class J extends y {
|
|
|
15
15
|
t.log(a` {white.bgRed ERROR } Argument {bold.yellow ${this.param.param}} value is invalid. `), (this.param.value || this.param.reason) && t.log(""), this.param.value && t.log(a` {blue Value}: ${this.param.value}`), this.param.reason && t.log(a` {yellow Reason}: ${this.param.reason}`);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
class
|
|
18
|
+
class b extends y {
|
|
19
19
|
constructor(t) {
|
|
20
20
|
let e = `Option "${t.option}" value is invalid.`;
|
|
21
21
|
t.reason ? e += ` Reason: ${t.reason}` : e += ` Value: "${t.value}"`, super(e), this.param = t;
|
|
@@ -95,7 +95,7 @@ class v extends y {
|
|
|
95
95
|
t.log(a`{white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is missing in the signature.`);
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
class
|
|
98
|
+
class A extends y {
|
|
99
99
|
constructor(t, e) {
|
|
100
100
|
super(`Missing ${t} in the command signature`), this.option = t, this.optionsSchema = e;
|
|
101
101
|
}
|
|
@@ -128,7 +128,7 @@ class T extends y {
|
|
|
128
128
|
t.log(a`{white.bgRed ERROR } Option {bold.yellow ${this.option}} is required.`);
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
|
-
function
|
|
131
|
+
function $(r, t, e, n) {
|
|
132
132
|
if (r == null)
|
|
133
133
|
return n ?? null;
|
|
134
134
|
if (t === "string")
|
|
@@ -136,7 +136,7 @@ function A(r, t, e, n) {
|
|
|
136
136
|
if (t === "number") {
|
|
137
137
|
const i = Number(r);
|
|
138
138
|
if (isNaN(i))
|
|
139
|
-
throw new
|
|
139
|
+
throw new b({
|
|
140
140
|
option: e,
|
|
141
141
|
reason: `Expected a number, got "${r}"`
|
|
142
142
|
});
|
|
@@ -152,7 +152,7 @@ function A(r, t, e, n) {
|
|
|
152
152
|
return s.map((o) => {
|
|
153
153
|
const l = Number(o);
|
|
154
154
|
if (isNaN(l))
|
|
155
|
-
throw new
|
|
155
|
+
throw new b({
|
|
156
156
|
option: e,
|
|
157
157
|
reason: `Expected array of numbers, got "${o}" in array`
|
|
158
158
|
});
|
|
@@ -271,14 +271,14 @@ class k {
|
|
|
271
271
|
* Handles variadic arguments that consume all remaining positional values
|
|
272
272
|
*/
|
|
273
273
|
handleVariadicArgument(t, e, n) {
|
|
274
|
-
return n.length ?
|
|
274
|
+
return n.length ? $(n, e.type, t, e.default) : e.default;
|
|
275
275
|
}
|
|
276
276
|
/**
|
|
277
277
|
* Resolves a single positional argument value with defaults and type conversion
|
|
278
278
|
* Note: Does not validate required arguments - validation happens in subclass validate() methods
|
|
279
279
|
*/
|
|
280
280
|
resolveArgumentValue(t, e, n) {
|
|
281
|
-
return n === void 0 ? e.default :
|
|
281
|
+
return n === void 0 ? e.default : $(n, e.type, t, e.default);
|
|
282
282
|
}
|
|
283
283
|
/**
|
|
284
284
|
* Resolves an option value from the parsed option values object
|
|
@@ -294,13 +294,13 @@ class k {
|
|
|
294
294
|
}
|
|
295
295
|
if (s === void 0) {
|
|
296
296
|
if (e.required)
|
|
297
|
-
throw new
|
|
297
|
+
throw new b({
|
|
298
298
|
option: t,
|
|
299
299
|
reason: `${i === "option" ? "Option" : "Argument"} is required but not provided`
|
|
300
300
|
});
|
|
301
301
|
return e.default;
|
|
302
302
|
}
|
|
303
|
-
return
|
|
303
|
+
return $(s, e.type, t, e.default);
|
|
304
304
|
}
|
|
305
305
|
optionDefinitions() {
|
|
306
306
|
const t = {};
|
|
@@ -347,8 +347,8 @@ class W {
|
|
|
347
347
|
const d = O(c - m.length);
|
|
348
348
|
let p = a` {green ${m}} ${d} ${h.description ?? "\b"}`;
|
|
349
349
|
if (h.default !== void 0 && !h.required) {
|
|
350
|
-
const
|
|
351
|
-
p += a` {yellow [default: ${
|
|
350
|
+
const L = (Array.isArray(h.type) ? `[${h.type[0]}]` : h.type) === "array" || Array.isArray(h.type) ? JSON.stringify(h.default) : h.default;
|
|
351
|
+
p += a` {yellow [default: ${L}]}`;
|
|
352
352
|
}
|
|
353
353
|
h.variadic && (p += a` {white (variadic)}`), this.io.log(p);
|
|
354
354
|
}
|
|
@@ -604,7 +604,7 @@ class g extends k {
|
|
|
604
604
|
*/
|
|
605
605
|
option(t) {
|
|
606
606
|
if (!this.optionsSchema[t])
|
|
607
|
-
throw new
|
|
607
|
+
throw new A(t, this.optionsSchema);
|
|
608
608
|
return super.option(t);
|
|
609
609
|
}
|
|
610
610
|
/**
|
|
@@ -612,7 +612,7 @@ class g extends k {
|
|
|
612
612
|
*/
|
|
613
613
|
setOption(t, e) {
|
|
614
614
|
if (!this.optionsSchema[t])
|
|
615
|
-
throw new
|
|
615
|
+
throw new A(t, this.optionsSchema);
|
|
616
616
|
this.parsedOptions && (this.parsedOptions[t] = e);
|
|
617
617
|
}
|
|
618
618
|
/**
|
|
@@ -620,7 +620,7 @@ class g extends k {
|
|
|
620
620
|
*/
|
|
621
621
|
optionHelp(t) {
|
|
622
622
|
if (!this.optionsSchema[t])
|
|
623
|
-
throw new
|
|
623
|
+
throw new A(t, this.optionsSchema);
|
|
624
624
|
return f(this.optionsSchema[t]).description;
|
|
625
625
|
}
|
|
626
626
|
/**
|
|
@@ -825,13 +825,13 @@ class K extends R {
|
|
|
825
825
|
return this.io.newLoader(...t);
|
|
826
826
|
}
|
|
827
827
|
}
|
|
828
|
-
class
|
|
828
|
+
class N {
|
|
829
829
|
level;
|
|
830
830
|
constructor(t = {}) {
|
|
831
831
|
this.level = t.level ?? "info";
|
|
832
832
|
}
|
|
833
833
|
shouldLog(t) {
|
|
834
|
-
const e = ["debug", "
|
|
834
|
+
const e = ["debug", "info", "warn", "error"], n = e.indexOf(this.level);
|
|
835
835
|
return e.indexOf(t) >= n;
|
|
836
836
|
}
|
|
837
837
|
setLevel(t) {
|
|
@@ -855,9 +855,6 @@ class L {
|
|
|
855
855
|
debug(...t) {
|
|
856
856
|
this.shouldLog("debug") && console.log(...t);
|
|
857
857
|
}
|
|
858
|
-
verbose(...t) {
|
|
859
|
-
this.shouldLog("verbose") && console.log(...t);
|
|
860
|
-
}
|
|
861
858
|
}
|
|
862
859
|
class M {
|
|
863
860
|
commands = {};
|
|
@@ -867,7 +864,7 @@ class M {
|
|
|
867
864
|
return q;
|
|
868
865
|
}
|
|
869
866
|
constructor(t) {
|
|
870
|
-
this.logger = t ?? new
|
|
867
|
+
this.logger = t ?? new N(), this.io = new this.CommandIOClass(this.logger);
|
|
871
868
|
}
|
|
872
869
|
getAvailableCommands() {
|
|
873
870
|
return Object.keys(this.commands);
|
|
@@ -964,7 +961,7 @@ class B extends R {
|
|
|
964
961
|
}), this.opts = t;
|
|
965
962
|
}
|
|
966
963
|
async handle() {
|
|
967
|
-
const t = this.opts.commandRegistry.getCommands(), e = this.opts.cliName ?? "Bob CLI", n = this.opts.cliVersion ?? "0.0.0", i = (await import("./package-
|
|
964
|
+
const t = this.opts.commandRegistry.getCommands(), e = this.opts.cliName ?? "Bob CLI", n = this.opts.cliVersion ?? "0.0.0", i = (await import("./package-BiJvpS-3.js"))?.default?.version ?? "0.0.0";
|
|
968
965
|
this.io.log(a`${e} {green ${n}} (core: {yellow ${i}})
|
|
969
966
|
|
|
970
967
|
{yellow Usage}:
|
|
@@ -1016,7 +1013,7 @@ class Q {
|
|
|
1016
1013
|
return new P(t.logger);
|
|
1017
1014
|
}
|
|
1018
1015
|
constructor(t = {}) {
|
|
1019
|
-
this.ctx = t.ctx, this.logger = t.logger ?? new
|
|
1016
|
+
this.ctx = t.ctx, this.logger = t.logger ?? new N(), this.commandRegistry = this.newCommandRegistry({
|
|
1020
1017
|
logger: this.logger
|
|
1021
1018
|
}), this.exceptionHandler = this.newExceptionHandler({
|
|
1022
1019
|
logger: this.logger
|
|
@@ -1044,7 +1041,7 @@ class Q {
|
|
|
1044
1041
|
}
|
|
1045
1042
|
}
|
|
1046
1043
|
export {
|
|
1047
|
-
|
|
1044
|
+
b as BadCommandOption,
|
|
1048
1045
|
J as BadCommandParameter,
|
|
1049
1046
|
y as BobError,
|
|
1050
1047
|
Q as Cli,
|
|
@@ -1058,9 +1055,9 @@ export {
|
|
|
1058
1055
|
P as ExceptionHandler,
|
|
1059
1056
|
W as HelpOption,
|
|
1060
1057
|
H as InvalidOption,
|
|
1061
|
-
|
|
1058
|
+
N as Logger,
|
|
1062
1059
|
C as MissingRequiredArgumentValue,
|
|
1063
1060
|
T as MissingRequiredOptionValue,
|
|
1064
1061
|
v as MissingSignatureArgument,
|
|
1065
|
-
|
|
1062
|
+
A as MissingSignatureOption
|
|
1066
1063
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const s = "bob-core", t = "2.0.0-beta.
|
|
1
|
+
const s = "bob-core", t = "2.0.0-beta.3", e = "BOB Core", i = "module", l = !1, n = ["dist/**"], r = { ".": { import: "./dist/esm/index.js", require: "./dist/cjs/index.js" } }, o = { "*": { "*": ["./dist/cjs/*.d.ts"] } }, c = { start: "node -r @swc-node/register debug/main.ts", build: "rimraf ./dist && vite build", typecheck: "tsc --noEmit", prepare: "npm run build", test: "vitest run" }, d = "Léo Hubert", p = "ISC", a = { "@faker-js/faker": "^10.0.0", "@swc-node/register": "^1.11.1", "@types/minimist": "^1.2.5", "@types/node": "^20.14.5", "@types/prompts": "^2.4.9", "@types/string-similarity": "^4.0.2", "@vitest/coverage-v8": "^3.2.4", rimraf: "^6.0.1", tsx: "^4.20.6", typescript: "^5.7.3", vite: "^7.1.6", "vite-plugin-dts": "^4.5.4", vitest: "^3.2.4" }, m = { chalk: "^4.1.2", minimist: "^1.2.8", prompts: "^2.4.2", "string-similarity": "^4.0.4" }, u = {
|
|
2
2
|
name: s,
|
|
3
3
|
version: t,
|
|
4
4
|
description: e,
|
package/dist/esm/src/Logger.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { LoggerContract, LogLevel } from './contracts/index.js';
|
|
2
2
|
export type LoggerOptions = {
|
|
3
3
|
level?: LogLevel;
|
|
4
4
|
};
|
|
5
|
-
export declare class Logger {
|
|
5
|
+
export declare class Logger implements LoggerContract {
|
|
6
6
|
private level;
|
|
7
7
|
constructor(opts?: LoggerOptions);
|
|
8
8
|
private shouldLog;
|
|
@@ -13,5 +13,4 @@ export declare class Logger {
|
|
|
13
13
|
warn(...args: any[]): void;
|
|
14
14
|
error(...args: any[]): void;
|
|
15
15
|
debug(...args: any[]): void;
|
|
16
|
-
verbose(...args: any[]): void;
|
|
17
16
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type LogLevel = 'debug' | 'info' | 'warn' | 'error'
|
|
1
|
+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
2
2
|
/**
|
|
3
3
|
* Logger interface defining standard logging methods and level management
|
|
4
4
|
*/
|
|
@@ -8,7 +8,6 @@ export interface LoggerContract {
|
|
|
8
8
|
warn(message: string, ...args: any[]): void;
|
|
9
9
|
error(message: string, ...args: any[]): void;
|
|
10
10
|
debug(message: string, ...args: any[]): void;
|
|
11
|
-
verbose(message: string, ...args: any[]): void;
|
|
12
11
|
setLevel(level: LogLevel): void;
|
|
13
12
|
getLevel(): LogLevel;
|
|
14
13
|
}
|
package/dist/esm/src/index.d.ts
CHANGED