bob-core 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/bob-core.cjs +8 -0
- package/dist/bob-core.js +515 -0
- package/dist/package-B_d-lCr-.js +29 -0
- package/dist/package-D1PCfkN9.cjs +1 -0
- package/dist/{cjs → types}/src/Cli.d.ts +4 -4
- package/dist/{esm → types}/src/Command.d.ts +2 -2
- package/dist/{cjs → types}/src/CommandParser.d.ts +1 -1
- package/dist/{cjs → types}/src/CommandRegistry.d.ts +1 -1
- package/dist/{cjs → types}/src/ExceptionHandler.d.ts +1 -1
- package/dist/{esm → types}/src/commands/HelpCommand.d.ts +2 -2
- package/dist/{esm → types}/src/errors/BadCommandOption.d.ts +1 -1
- package/dist/{cjs → types}/src/errors/BadCommandParameter.d.ts +1 -1
- package/dist/{cjs → types}/src/errors/CommandNotFoundError.d.ts +1 -1
- package/dist/{esm → types}/src/errors/InvalidOption.d.ts +2 -2
- package/dist/{cjs → types}/src/errors/MissingRequiredArgumentValue.d.ts +2 -2
- package/dist/{cjs → types}/src/errors/MissingSignatureArgument.d.ts +2 -2
- package/dist/{cjs → types}/src/errors/MissingSignatureOption.d.ts +2 -2
- package/dist/{cjs → types}/src/options/HelpOption.d.ts +2 -2
- package/package.json +13 -20
- package/dist/cjs/package.json +0 -1
- package/dist/cjs/src/Cli.js +0 -66
- package/dist/cjs/src/Command.d.ts +0 -34
- package/dist/cjs/src/Command.js +0 -97
- package/dist/cjs/src/Command.test.d.ts +0 -1
- package/dist/cjs/src/Command.test.js +0 -52
- package/dist/cjs/src/CommandParser.js +0 -239
- package/dist/cjs/src/CommandParser.test.d.ts +0 -1
- package/dist/cjs/src/CommandParser.test.js +0 -177
- package/dist/cjs/src/CommandRegistry.js +0 -149
- package/dist/cjs/src/ExceptionHandler.js +0 -14
- package/dist/cjs/src/commands/HelpCommand.d.ts +0 -14
- package/dist/cjs/src/commands/HelpCommand.js +0 -90
- package/dist/cjs/src/contracts/CommandOption.js +0 -2
- package/dist/cjs/src/contracts/index.js +0 -17
- package/dist/cjs/src/errors/BadCommandOption.d.ts +0 -11
- package/dist/cjs/src/errors/BadCommandOption.js +0 -36
- package/dist/cjs/src/errors/BadCommandParameter.js +0 -36
- package/dist/cjs/src/errors/BobError.js +0 -6
- package/dist/cjs/src/errors/CommandNotFoundError.js +0 -30
- package/dist/cjs/src/errors/InvalidOption.d.ts +0 -8
- package/dist/cjs/src/errors/InvalidOption.js +0 -32
- package/dist/cjs/src/errors/MissingRequiredArgumentValue.js +0 -23
- package/dist/cjs/src/errors/MissingSignatureArgument.js +0 -31
- package/dist/cjs/src/errors/MissingSignatureOption.js +0 -31
- package/dist/cjs/src/errors/index.js +0 -19
- package/dist/cjs/src/index.js +0 -21
- package/dist/cjs/src/lib/string.js +0 -6
- package/dist/cjs/src/options/HelpOption.js +0 -77
- package/dist/cjs/src/options/index.js +0 -17
- package/dist/esm/package.json +0 -50
- package/dist/esm/src/Cli.d.ts +0 -26
- package/dist/esm/src/Cli.js +0 -59
- package/dist/esm/src/Command.js +0 -93
- package/dist/esm/src/Command.test.d.ts +0 -1
- package/dist/esm/src/Command.test.js +0 -50
- package/dist/esm/src/CommandParser.d.ts +0 -46
- package/dist/esm/src/CommandParser.js +0 -232
- package/dist/esm/src/CommandParser.test.d.ts +0 -1
- package/dist/esm/src/CommandParser.test.js +0 -175
- package/dist/esm/src/CommandRegistry.d.ts +0 -17
- package/dist/esm/src/CommandRegistry.js +0 -109
- package/dist/esm/src/ExceptionHandler.d.ts +0 -4
- package/dist/esm/src/ExceptionHandler.js +0 -10
- package/dist/esm/src/commands/HelpCommand.js +0 -51
- package/dist/esm/src/contracts/CommandOption.d.ts +0 -7
- package/dist/esm/src/contracts/CommandOption.js +0 -1
- package/dist/esm/src/contracts/index.d.ts +0 -1
- package/dist/esm/src/contracts/index.js +0 -1
- package/dist/esm/src/errors/BadCommandOption.js +0 -29
- package/dist/esm/src/errors/BadCommandParameter.d.ts +0 -11
- package/dist/esm/src/errors/BadCommandParameter.js +0 -29
- package/dist/esm/src/errors/BobError.d.ts +0 -3
- package/dist/esm/src/errors/BobError.js +0 -2
- package/dist/esm/src/errors/CommandNotFoundError.d.ts +0 -7
- package/dist/esm/src/errors/CommandNotFoundError.js +0 -23
- package/dist/esm/src/errors/InvalidOption.js +0 -25
- package/dist/esm/src/errors/MissingRequiredArgumentValue.d.ts +0 -7
- package/dist/esm/src/errors/MissingRequiredArgumentValue.js +0 -16
- package/dist/esm/src/errors/MissingSignatureArgument.d.ts +0 -8
- package/dist/esm/src/errors/MissingSignatureArgument.js +0 -24
- package/dist/esm/src/errors/MissingSignatureOption.d.ts +0 -8
- package/dist/esm/src/errors/MissingSignatureOption.js +0 -24
- package/dist/esm/src/errors/index.d.ts +0 -3
- package/dist/esm/src/errors/index.js +0 -3
- package/dist/esm/src/index.d.ts +0 -5
- package/dist/esm/src/index.js +0 -5
- package/dist/esm/src/lib/string.d.ts +0 -1
- package/dist/esm/src/lib/string.js +0 -3
- package/dist/esm/src/options/HelpOption.d.ts +0 -9
- package/dist/esm/src/options/HelpOption.js +0 -70
- package/dist/esm/src/options/index.d.ts +0 -1
- package/dist/esm/src/options/index.js +0 -1
- /package/dist/{cjs → types}/src/contracts/CommandOption.d.ts +0 -0
- /package/dist/{cjs → types}/src/contracts/index.d.ts +0 -0
- /package/dist/{cjs → types}/src/errors/BobError.d.ts +0 -0
- /package/dist/{cjs → types}/src/errors/index.d.ts +0 -0
- /package/dist/{cjs → types}/src/index.d.ts +0 -0
- /package/dist/{cjs → types}/src/lib/string.d.ts +0 -0
- /package/dist/{cjs → types}/src/options/index.d.ts +0 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("minimist"),a=require("chalk"),R=require("node:fs"),C=require("path"),A=require("string-similarity");function S(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 V=S(R),x=S(A);class c extends Error{}class $ extends c{constructor(e){super(`Argument "${e.name}" is required.`),this.paramSignature=e}pretty(){const e=console.log;this.paramSignature.help&&e(a`{yellow Help}: ${this.paramSignature.help}\n`),e(a` {white.bgRed ERROR } Argument {bold.yellow ${this.paramSignature.name}} is required.`)}}class f extends c{constructor(e,t){super(`Missing ${e} in the command signature`),this.option=e,this.optionsSignature=t}pretty(){const e=console.log;if(this.optionsSignature.length){e(a`{yellow Available options}:`);for(const t of this.optionsSignature){const n=t.type?a`{white (${t.type})}`:"",i=" ".repeat(20-t.name.length);e(a` {green ${t.name}} ${i} ${t.help??"\b"} ${n}`)}e("")}e(a` {white.bgRed ERROR } Option {bold.yellow ${this.option}} is missing in the signature.`)}}class y extends c{constructor(e,t){super(`Missing ${e} in the command signature`),this.argument=e,this.argumentSignatures=t}pretty(){const e=console.log;if(this.argumentSignatures.length){e(a`\n{yellow Available arguments}:`);for(const t of this.argumentSignatures){const n=t.type?a`{white (${t.type})}`:"",i=" ".repeat(20-t.name.length);e(a` {green ${t.name}} ${i} ${t.help??"\b"} ${n}`)}e("")}e(a` {white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is missing in the signature.`)}}class E extends c{constructor(e,t){super(`Invalid option ${e} in not recognized`),this.option=e,this.optionsSignature=t}pretty(){const e=console.log;if(this.optionsSignature.length>0){e(a`\n{yellow Available options}:`);for(const t of this.optionsSignature){const n=t.type?a`{white (${t.type})}`:"",i=`--${t.name}${t.alias?.map(l=>`, -${l}`).join("")??""}`,o=" ".repeat(30-i.length);e(a` {green ${i}} ${o} ${t.help??"\b"} ${n}`)}e("")}e(a` {white.bgRed ERROR } Option {bold.yellow ${this.option}} is not recognized.`)}}class j{constructor(e,t,n,...i){this.signature=e,this.helperDefinitions=t,this.defaultCommandOptions=n;const[o,...l]=e.split(/\{(.*?)\}/g).map(m=>m.trim()).filter(Boolean),{_:p,...s}=O(i);this.command=o,this.parseSignature(l),this.parseDefaultOptions(),this.handleArguments(p),this.handleOptions(s)}command;arguments={};options={};argumentsSignature={};optionSignatures={};optionAliases={};option(e){if(!this.optionSignatures[e])throw new f(e,Object.values(this.optionSignatures));return this.options[e]}setOption(e,t){if(!this.optionSignatures[e])throw new f(e,Object.values(this.optionSignatures));this.options[e]=t}optionHelp(e){if(!this.optionSignatures[e])throw new f(e,Object.values(this.optionSignatures));return this.optionSignatures[e].help}argument(e){if(!this.argumentsSignature[e])throw new y(e,Object.values(this.argumentsSignature));return this.arguments[e]}setArgument(e,t){if(!this.argumentsSignature[e])throw new y(e,Object.values(this.argumentsSignature));this.arguments[e]=t}argumentHelp(e){if(!this.argumentsSignature[e])throw new y(e,Object.values(this.argumentsSignature));return this.argumentsSignature[e].help}getArgumentSignatures(){return this.argumentsSignature}getOptionSignatures(){return this.optionSignatures}getParamValue(e,t){return t.type==="boolean"?e==="true"||e==="1"?!0:e==="false"||e==="0"?!1:!!e:t.type==="array"?e?Array.isArray(e)?e:[e]:[]:e??t.defaultValue}handleArguments(e){for(const[t,n]of Object.entries(this.arguments)){const i=this.argumentsSignature[t];if(i.variadic)this.arguments[t]=e;else{const o=e.shift();this.arguments[t]=this.getParamValue(o,i)}}}handleOptions(e){for(const[t,n]of Object.entries(e)){const i=this.optionAliases[t],o=this.optionSignatures[t]??this.optionSignatures[i];if(!o)throw new E(t,Object.values(this.optionSignatures));this.options[t]=this.getParamValue(n,o);for(const l of o.alias??[])e[l]&&(this.options[o.name]=e[l])}}parseSignature(e){for(const t of e){const n=this.parseParamSignature(t);if(n.isOption){this.options[n.name]=n.defaultValue??null,this.optionSignatures[n.name]=n;for(const i of n.alias??[])this.optionAliases[i]=n.name}else this.arguments[n.name]=n.defaultValue??null,this.argumentsSignature[n.name]=n}}parseDefaultOptions(){if(this.defaultCommandOptions.length){for(const e of this.defaultCommandOptions)if(this.optionSignatures[e.option]={name:e.option,type:e.defaultValue==null?"string":typeof e.defaultValue,optional:!0,alias:e.alias,variadic:!1,help:e.description,defaultValue:e.defaultValue??null,isOption:!0},this.options[e.option]=e.defaultValue,e.alias)for(const t of e.alias)this.optionAliases[t]=e.option}}parseParamSignature(e){const t={name:e,optional:!1,type:"string",help:void 0,defaultValue:null,variadic:!1,isOption:!1};if(t.name.includes(":")){const[n,i]=t.name.split(":");t.name=n.trim(),t.help=i.trim()}if(t.name.includes("=")){const[n,i]=t.name.split("=");t.name=n.trim(),t.defaultValue=i.trim(),t.optional=!0,t.defaultValue.length?t.defaultValue==="true"?(t.defaultValue=!0,t.type="boolean"):t.defaultValue==="false"&&(t.defaultValue=!1,t.type="boolean"):t.defaultValue=null}else t.name.startsWith("--")&&(t.optional=!0,t.defaultValue=!1,t.type="boolean");if(t.name.includes("|")){const[n,...i]=t.name.split("|");t.name=n.trim(),t.alias=i.map(o=>o.trim())}return t.name.startsWith("--")&&(t.isOption=!0,t.name=t.name.slice(2)),t.defaultValue==="*"&&(t.defaultValue=[],t.type="array"),t.name.endsWith("?")&&(t.optional=!0,t.name=t.name.slice(0,-1)),t.name.endsWith("*")&&(t.type="array",t.variadic=!0,t.defaultValue=[],t.name=t.name.slice(0,-1)),t.help=t.help??this.helperDefinitions[t.name]??this.helperDefinitions[`--${t.name}`],t}validate(){for(const[e,t]of Object.entries(this.arguments)){const n=this.argumentsSignature[e];if(!t&&!n.optional)throw new $(n);if(!t?.length&&n.variadic&&!n.optional)throw new $(n)}}}function w(r){return new Array(r+5).join(" ")}class b{option="help";alias=["h"];defaultValue=!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=console.log,t=Object.values(this.parser.getArgumentSignatures()),n=Object.values(this.parser.getOptionSignatures()).map(s=>({...s,optionWithAlias:`--${s.name}${s.alias?.map(m=>`, -${m}`).join("")??""}`})),i=t.filter(s=>!s.optional);e(a`{yellow Description}:`),e(a` ${this.description}\n`),e(a`{yellow Usage}:`),e(a` ${this.command} ${i.length>0?i.map(s=>`<${s.name}>`).join(" "):"\b"} [options]`);const o=Math.max(...n.map(s=>s.optionWithAlias.length))??0,l=Math.max(...t.map(s=>s.name.length))??0,p=l>o?l:o;if(t.length>0){e(a`\n{yellow Arguments}:`);for(const s of t){const m=w(p-s.name.length);let u=a` {green ${s.name}} ${m} ${s.help??"\b"}`;if(s.defaultValue!==void 0&&s.optional){const h=s.type==="array"?JSON.stringify(s.defaultValue):s.defaultValue;u+=a` {yellow [default: ${h}]}`}s.variadic&&(u+=a` {white (variadic)}`),e(u)}}if(n.length>0){e(a`\n{yellow Options}:`);for(const s of n){const m=w(p-s.optionWithAlias.length);let u=a`{green ${s.optionWithAlias}} ${m} ${s.help??"\b"}`;if(s.type&&(u+=a` {white (${s.type})}`),s.defaultValue!==void 0&&s.optional){const h=s.type==="array"?JSON.stringify(s.defaultValue):s.defaultValue;u+=a` {yellow [default: ${h}]}`}e(u)}}if(this.commandsExamples.length>0){e(a`\n{yellow Examples}:`);let s=process.argv[0].split("/").pop();s==="node"&&(s+=" "+process.argv[1].split("/").pop());for(const[m,u]of this.commandsExamples.entries())m>0&&e(""),e(` ${u.description}
|
|
2
|
+
`),e(a` {green ${s} ${u.command}}`)}return-1}}class v{ctx;helperDefinitions={};commandsExamples=[];parser;get CommandParserClass(){return j}defaultOptions(){return[new b]}get command(){return this.parser?this.parser.command:this.signature.split(" ")[0]}async run(e,...t){this.ctx=e;const n=this.defaultOptions();this.parser=new this.CommandParserClass(this.signature,this.helperDefinitions,n,...t);for(const i of n)if(this.parser.option(i.option)){const o=await i.handler.call(this);if(o&&o!==0)return o}return this.parser.validate(),await this.handle()??0}setOption(e,t){this.parser.setOption(e,t)}setArgument(e,t){this.parser.setArgument(e,t)}option(e,t=null){return this.parser.option(e)??t}optionBoolean(e,t=!1){return this.parser.option(e)??t}optionArray(e,t=[]){const n=this.parser.option(e);if(!Array.isArray(n))throw new Error(`Option ${e} is not an array`);return n.length?n:t}optionNumber(e,t=null){const n=this.parser.option(e);return n?typeof n=="number"?n:parseInt(n):t}argument(e,t=null){return this.parser.argument(e)??t}argumentArray(e,t=[]){const n=this.parser.argument(e);if(!Array.isArray(n))throw new Error(`Argument ${e} is not an array`);return n?.length?n:t}argumentBoolean(e,t=!1){return this.parser.argument(e)??t}argumentNumber(e,t=null){const n=this.parser.argument(e);return n?typeof n=="number"?n:parseInt(n):t}}class P extends c{constructor(e,t){super(`Command "${e}" not found.`),this.command=e,this.similarCommands=t}pretty(){const e=console.log;if(this.similarCommands.length){e(a` {white.bgRed ERROR } Command "${this.command}" not found, Did you mean one of these?`);for(const t of this.similarCommands)e(a` {gray ⇂ ${t}}`)}else e(a` {white.bgRed ERROR } Command "${this.command}" not found.`)}}class H{commands={};get commandSuffix(){return"Command"}constructor(){}getAvailableCommands(){return Object.keys(this.commands)}getCommands(){return Object.values(this.commands)}commandResolver=async e=>{const t=(await import(e)).default;let n;return t?.default?n=new t.default:n=new t,n};setCommandResolver(e){this.commandResolver=e}registerCommand(e,t=!1){const n=e.signature.split(" ")[0];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}`)}}async runCommand(e,t,...n){const i=typeof t=="string"?this.commands[t]:t,o=typeof t=="string"?t:i.command;if(!i){const l=await this.suggestCommand(o);return l?await this.runCommand(e,l,...n):1}return await i.run(e,...n)}async suggestCommand(e){const t=this.getAvailableCommands(),{bestMatch:n,bestMatchIndex:i,ratings:o}=x.findBestMatch(e,t),l=o.filter(p=>p.rating>.3).map(p=>p.target);if(n.rating>0&&l.length<=1||n.rating>.7&&l.length>1){const p=t[i];return await this.askRunSimilarCommand(e,p)?p:null}throw new P(e,l)}async askRunSimilarCommand(e,t){const n=require("readline").createInterface({input:process.stdin,output:process.stdout});return console.log(a` {bgRed ERROR } Command {yellow ${e}} not found.\n`),new Promise(i=>{n.question(a`{green Do you want to run {yellow ${t}} instead?} {white (yes/no)} [{yellow no}]\n > `,o=>{i(o==="yes"||o==="y"),n.close()})})}async*listCommandsFiles(e){const t=V.readdirSync(e,{withFileTypes:!0});for(const n of t){const i=C.resolve(e,n.name);if(n.isDirectory())yield*this.listCommandsFiles(C.resolve(e,n.name));else{if(!i.endsWith(`${this.commandSuffix}.ts`)&&!i.endsWith(`${this.commandSuffix}.js`))continue;yield i}}}}class B extends v{constructor(e){super(),this.opts=e}signature="help";description="Show help";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-D1PCfkN9.cjs")))?.default?.version??"0.0.0";console.log(a`${t} {green ${n}} (core: {yellow ${i}})
|
|
3
|
+
|
|
4
|
+
{yellow Usage}:
|
|
5
|
+
command [options] [arguments]
|
|
6
|
+
|
|
7
|
+
{yellow Available commands}:
|
|
8
|
+
`);const o=Math.max(...e.map(s=>s.command.length))??0,l={};for(const s of e){const m=s.command.split(":")[0];l[m]||(l[m]=[]),l[m].push(s)}const p=Object.entries(l).sort(([s],[m])=>s.toLowerCase().localeCompare(m.toLowerCase())).sort(([,s],[,m])=>s.length-m.length);for(const[s,m]of p){const u=m.length>1;u&&console.log(a`{yellow ${s}}:`);const h=m.sort((d,g)=>d.command.toLowerCase().localeCompare(g.command.toLowerCase()));for(const d of h){let g=w(o-d.command.length);u&&(g=g.slice(2)),console.log(a`${u?" ":""}{green ${d.command}} ${g} ${d.description}`)}}}}class D extends c{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(){const e=console.log;e(a` {white.bgRed ERROR } Argument {bold.yellow ${this.param.param}} value is invalid. `),(this.param.value||this.param.reason)&&e(""),this.param.value&&e(a` {blue Value}: ${this.param.value}`),this.param.reason&&e(a` {yellow Reason}: ${this.param.reason}`)}}class M extends c{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(){const e=console.log;e(a` {white.bgRed ERROR } Option {bold.yellow ${this.param.option}} value is invalid. `),(this.param.value||this.param.reason)&&e(""),this.param.value&&e(a` {blue Value}: ${this.param.value}`),this.param.reason&&e(a` {yellow Reason}: ${this.param.reason}`)}}class q{handle(e){if(e instanceof c)return e.pretty(),-1;throw e}}class W{commandRegistry;exceptionHandler;ctx;helpCommand;get CommandRegistryClass(){return H}get HelpCommandClass(){return B}get ExceptionHandlerClass(){return q}constructor(e={}){this.ctx=e.ctx,this.commandRegistry=new this.CommandRegistryClass,this.exceptionHandler=new this.ExceptionHandlerClass,this.helpCommand=new this.HelpCommandClass({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):await this.runHelpCommand()}async runHelpCommand(){return await this.runCommand(this.helpCommand)}registerCommand(e){this.commandRegistry.registerCommand(e)}}exports.BadCommandOption=M;exports.BadCommandParameter=D;exports.BobError=c;exports.Cli=W;exports.Command=v;exports.HelpOption=b;
|
package/dist/bob-core.js
ADDED
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import S from "minimist";
|
|
2
|
+
import a from "chalk";
|
|
3
|
+
import * as v from "node:fs";
|
|
4
|
+
import C from "path";
|
|
5
|
+
import * as R from "string-similarity";
|
|
6
|
+
class h extends Error {
|
|
7
|
+
}
|
|
8
|
+
class $ extends h {
|
|
9
|
+
constructor(t) {
|
|
10
|
+
super(`Argument "${t.name}" is required.`), this.paramSignature = t;
|
|
11
|
+
}
|
|
12
|
+
pretty() {
|
|
13
|
+
const t = console.log;
|
|
14
|
+
this.paramSignature.help && t(a`{yellow Help}: ${this.paramSignature.help}\n`), t(a` {white.bgRed ERROR } Argument {bold.yellow ${this.paramSignature.name}} is required.`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
class f extends h {
|
|
18
|
+
constructor(t, e) {
|
|
19
|
+
super(`Missing ${t} in the command signature`), this.option = t, this.optionsSignature = e;
|
|
20
|
+
}
|
|
21
|
+
pretty() {
|
|
22
|
+
const t = console.log;
|
|
23
|
+
if (this.optionsSignature.length) {
|
|
24
|
+
t(a`{yellow Available options}:`);
|
|
25
|
+
for (const e of this.optionsSignature) {
|
|
26
|
+
const n = e.type ? a`{white (${e.type})}` : "", i = " ".repeat(20 - e.name.length);
|
|
27
|
+
t(a` {green ${e.name}} ${i} ${e.help ?? "\b"} ${n}`);
|
|
28
|
+
}
|
|
29
|
+
t("");
|
|
30
|
+
}
|
|
31
|
+
t(a` {white.bgRed ERROR } Option {bold.yellow ${this.option}} is missing in the signature.`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class y extends h {
|
|
35
|
+
constructor(t, e) {
|
|
36
|
+
super(`Missing ${t} in the command signature`), this.argument = t, this.argumentSignatures = e;
|
|
37
|
+
}
|
|
38
|
+
pretty() {
|
|
39
|
+
const t = console.log;
|
|
40
|
+
if (this.argumentSignatures.length) {
|
|
41
|
+
t(a`\n{yellow Available arguments}:`);
|
|
42
|
+
for (const e of this.argumentSignatures) {
|
|
43
|
+
const n = e.type ? a`{white (${e.type})}` : "", i = " ".repeat(20 - e.name.length);
|
|
44
|
+
t(a` {green ${e.name}} ${i} ${e.help ?? "\b"} ${n}`);
|
|
45
|
+
}
|
|
46
|
+
t("");
|
|
47
|
+
}
|
|
48
|
+
t(a` {white.bgRed ERROR } Argument {bold.yellow ${this.argument}} is missing in the signature.`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
class b extends h {
|
|
52
|
+
constructor(t, e) {
|
|
53
|
+
super(`Invalid option ${t} in not recognized`), this.option = t, this.optionsSignature = e;
|
|
54
|
+
}
|
|
55
|
+
pretty() {
|
|
56
|
+
const t = console.log;
|
|
57
|
+
if (this.optionsSignature.length > 0) {
|
|
58
|
+
t(a`\n{yellow Available options}:`);
|
|
59
|
+
for (const e of this.optionsSignature) {
|
|
60
|
+
const n = e.type ? a`{white (${e.type})}` : "", i = `--${e.name}${e.alias?.map((r) => `, -${r}`).join("") ?? ""}`, o = " ".repeat(30 - i.length);
|
|
61
|
+
t(a` {green ${i}} ${o} ${e.help ?? "\b"} ${n}`);
|
|
62
|
+
}
|
|
63
|
+
t("");
|
|
64
|
+
}
|
|
65
|
+
t(a` {white.bgRed ERROR } Option {bold.yellow ${this.option}} is not recognized.`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
class O {
|
|
69
|
+
constructor(t, e, n, ...i) {
|
|
70
|
+
this.signature = t, this.helperDefinitions = e, this.defaultCommandOptions = n;
|
|
71
|
+
const [o, ...r] = t.split(/\{(.*?)\}/g).map((l) => l.trim()).filter(Boolean), { _: p, ...s } = S(i);
|
|
72
|
+
this.command = o, this.parseSignature(r), this.parseDefaultOptions(), this.handleArguments(p), this.handleOptions(s);
|
|
73
|
+
}
|
|
74
|
+
command;
|
|
75
|
+
arguments = {};
|
|
76
|
+
options = {};
|
|
77
|
+
argumentsSignature = {};
|
|
78
|
+
optionSignatures = {};
|
|
79
|
+
optionAliases = {};
|
|
80
|
+
option(t) {
|
|
81
|
+
if (!this.optionSignatures[t])
|
|
82
|
+
throw new f(t, Object.values(this.optionSignatures));
|
|
83
|
+
return this.options[t];
|
|
84
|
+
}
|
|
85
|
+
setOption(t, e) {
|
|
86
|
+
if (!this.optionSignatures[t])
|
|
87
|
+
throw new f(t, Object.values(this.optionSignatures));
|
|
88
|
+
this.options[t] = e;
|
|
89
|
+
}
|
|
90
|
+
optionHelp(t) {
|
|
91
|
+
if (!this.optionSignatures[t])
|
|
92
|
+
throw new f(t, Object.values(this.optionSignatures));
|
|
93
|
+
return this.optionSignatures[t].help;
|
|
94
|
+
}
|
|
95
|
+
argument(t) {
|
|
96
|
+
if (!this.argumentsSignature[t])
|
|
97
|
+
throw new y(t, Object.values(this.argumentsSignature));
|
|
98
|
+
return this.arguments[t];
|
|
99
|
+
}
|
|
100
|
+
setArgument(t, e) {
|
|
101
|
+
if (!this.argumentsSignature[t])
|
|
102
|
+
throw new y(t, Object.values(this.argumentsSignature));
|
|
103
|
+
this.arguments[t] = e;
|
|
104
|
+
}
|
|
105
|
+
argumentHelp(t) {
|
|
106
|
+
if (!this.argumentsSignature[t])
|
|
107
|
+
throw new y(t, Object.values(this.argumentsSignature));
|
|
108
|
+
return this.argumentsSignature[t].help;
|
|
109
|
+
}
|
|
110
|
+
getArgumentSignatures() {
|
|
111
|
+
return this.argumentsSignature;
|
|
112
|
+
}
|
|
113
|
+
getOptionSignatures() {
|
|
114
|
+
return this.optionSignatures;
|
|
115
|
+
}
|
|
116
|
+
getParamValue(t, e) {
|
|
117
|
+
return e.type === "boolean" ? t === "true" || t === "1" ? !0 : t === "false" || t === "0" ? !1 : !!t : e.type === "array" ? t ? Array.isArray(t) ? t : [t] : [] : t ?? e.defaultValue;
|
|
118
|
+
}
|
|
119
|
+
handleArguments(t) {
|
|
120
|
+
for (const [e, n] of Object.entries(this.arguments)) {
|
|
121
|
+
const i = this.argumentsSignature[e];
|
|
122
|
+
if (i.variadic)
|
|
123
|
+
this.arguments[e] = t;
|
|
124
|
+
else {
|
|
125
|
+
const o = t.shift();
|
|
126
|
+
this.arguments[e] = this.getParamValue(o, i);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
handleOptions(t) {
|
|
131
|
+
for (const [e, n] of Object.entries(t)) {
|
|
132
|
+
const i = this.optionAliases[e], o = this.optionSignatures[e] ?? this.optionSignatures[i];
|
|
133
|
+
if (!o)
|
|
134
|
+
throw new b(e, Object.values(this.optionSignatures));
|
|
135
|
+
this.options[e] = this.getParamValue(n, o);
|
|
136
|
+
for (const r of o.alias ?? [])
|
|
137
|
+
t[r] && (this.options[o.name] = t[r]);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
parseSignature(t) {
|
|
141
|
+
for (const e of t) {
|
|
142
|
+
const n = this.parseParamSignature(e);
|
|
143
|
+
if (n.isOption) {
|
|
144
|
+
this.options[n.name] = n.defaultValue ?? null, this.optionSignatures[n.name] = n;
|
|
145
|
+
for (const i of n.alias ?? [])
|
|
146
|
+
this.optionAliases[i] = n.name;
|
|
147
|
+
} else
|
|
148
|
+
this.arguments[n.name] = n.defaultValue ?? null, this.argumentsSignature[n.name] = n;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
parseDefaultOptions() {
|
|
152
|
+
if (this.defaultCommandOptions.length) {
|
|
153
|
+
for (const t of this.defaultCommandOptions)
|
|
154
|
+
if (this.optionSignatures[t.option] = {
|
|
155
|
+
name: t.option,
|
|
156
|
+
type: t.defaultValue == null ? "string" : typeof t.defaultValue,
|
|
157
|
+
optional: !0,
|
|
158
|
+
alias: t.alias,
|
|
159
|
+
variadic: !1,
|
|
160
|
+
help: t.description,
|
|
161
|
+
defaultValue: t.defaultValue ?? null,
|
|
162
|
+
isOption: !0
|
|
163
|
+
}, this.options[t.option] = t.defaultValue, t.alias)
|
|
164
|
+
for (const e of t.alias)
|
|
165
|
+
this.optionAliases[e] = t.option;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
parseParamSignature(t) {
|
|
169
|
+
const e = {
|
|
170
|
+
name: t,
|
|
171
|
+
optional: !1,
|
|
172
|
+
type: "string",
|
|
173
|
+
help: void 0,
|
|
174
|
+
defaultValue: null,
|
|
175
|
+
variadic: !1,
|
|
176
|
+
isOption: !1
|
|
177
|
+
};
|
|
178
|
+
if (e.name.includes(":")) {
|
|
179
|
+
const [n, i] = e.name.split(":");
|
|
180
|
+
e.name = n.trim(), e.help = i.trim();
|
|
181
|
+
}
|
|
182
|
+
if (e.name.includes("=")) {
|
|
183
|
+
const [n, i] = e.name.split("=");
|
|
184
|
+
e.name = n.trim(), e.defaultValue = i.trim(), e.optional = !0, e.defaultValue.length ? e.defaultValue === "true" ? (e.defaultValue = !0, e.type = "boolean") : e.defaultValue === "false" && (e.defaultValue = !1, e.type = "boolean") : e.defaultValue = null;
|
|
185
|
+
} else
|
|
186
|
+
e.name.startsWith("--") && (e.optional = !0, e.defaultValue = !1, e.type = "boolean");
|
|
187
|
+
if (e.name.includes("|")) {
|
|
188
|
+
const [n, ...i] = e.name.split("|");
|
|
189
|
+
e.name = n.trim(), e.alias = i.map((o) => o.trim());
|
|
190
|
+
}
|
|
191
|
+
return e.name.startsWith("--") && (e.isOption = !0, e.name = e.name.slice(2)), e.defaultValue === "*" && (e.defaultValue = [], e.type = "array"), e.name.endsWith("?") && (e.optional = !0, e.name = e.name.slice(0, -1)), e.name.endsWith("*") && (e.type = "array", e.variadic = !0, e.defaultValue = [], e.name = e.name.slice(0, -1)), e.help = e.help ?? this.helperDefinitions[e.name] ?? this.helperDefinitions[`--${e.name}`], e;
|
|
192
|
+
}
|
|
193
|
+
validate() {
|
|
194
|
+
for (const [t, e] of Object.entries(this.arguments)) {
|
|
195
|
+
const n = this.argumentsSignature[t];
|
|
196
|
+
if (!e && !n.optional)
|
|
197
|
+
throw new $(n);
|
|
198
|
+
if (!e?.length && n.variadic && !n.optional)
|
|
199
|
+
throw new $(n);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function w(m) {
|
|
204
|
+
return new Array(m + 5).join(" ");
|
|
205
|
+
}
|
|
206
|
+
class A {
|
|
207
|
+
option = "help";
|
|
208
|
+
alias = ["h"];
|
|
209
|
+
defaultValue = !1;
|
|
210
|
+
description = a`Display help for the given command. When no command is given display help for the {green list} command`;
|
|
211
|
+
async handler() {
|
|
212
|
+
const t = console.log, e = Object.values(this.parser.getArgumentSignatures()), n = Object.values(this.parser.getOptionSignatures()).map((s) => ({
|
|
213
|
+
...s,
|
|
214
|
+
optionWithAlias: `--${s.name}${s.alias?.map((l) => `, -${l}`).join("") ?? ""}`
|
|
215
|
+
})), i = e.filter((s) => !s.optional);
|
|
216
|
+
t(a`{yellow Description}:`), t(a` ${this.description}\n`), t(a`{yellow Usage}:`), t(a` ${this.command} ${i.length > 0 ? i.map((s) => `<${s.name}>`).join(" ") : "\b"} [options]`);
|
|
217
|
+
const o = Math.max(...n.map((s) => s.optionWithAlias.length)) ?? 0, r = Math.max(...e.map((s) => s.name.length)) ?? 0, p = r > o ? r : o;
|
|
218
|
+
if (e.length > 0) {
|
|
219
|
+
t(a`\n{yellow Arguments}:`);
|
|
220
|
+
for (const s of e) {
|
|
221
|
+
const l = w(p - s.name.length);
|
|
222
|
+
let u = a` {green ${s.name}} ${l} ${s.help ?? "\b"}`;
|
|
223
|
+
if (s.defaultValue !== void 0 && s.optional) {
|
|
224
|
+
const c = s.type === "array" ? JSON.stringify(s.defaultValue) : s.defaultValue;
|
|
225
|
+
u += a` {yellow [default: ${c}]}`;
|
|
226
|
+
}
|
|
227
|
+
s.variadic && (u += a` {white (variadic)}`), t(u);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (n.length > 0) {
|
|
231
|
+
t(a`\n{yellow Options}:`);
|
|
232
|
+
for (const s of n) {
|
|
233
|
+
const l = w(p - s.optionWithAlias.length);
|
|
234
|
+
let u = a`{green ${s.optionWithAlias}} ${l} ${s.help ?? "\b"}`;
|
|
235
|
+
if (s.type && (u += a` {white (${s.type})}`), s.defaultValue !== void 0 && s.optional) {
|
|
236
|
+
const c = s.type === "array" ? JSON.stringify(s.defaultValue) : s.defaultValue;
|
|
237
|
+
u += a` {yellow [default: ${c}]}`;
|
|
238
|
+
}
|
|
239
|
+
t(u);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (this.commandsExamples.length > 0) {
|
|
243
|
+
t(a`\n{yellow Examples}:`);
|
|
244
|
+
let s = process.argv[0].split("/").pop();
|
|
245
|
+
s === "node" && (s += " " + process.argv[1].split("/").pop());
|
|
246
|
+
for (const [l, u] of this.commandsExamples.entries())
|
|
247
|
+
l > 0 && t(""), t(` ${u.description}
|
|
248
|
+
`), t(a` {green ${s} ${u.command}}`);
|
|
249
|
+
}
|
|
250
|
+
return -1;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
class V {
|
|
254
|
+
ctx;
|
|
255
|
+
helperDefinitions = {};
|
|
256
|
+
commandsExamples = [];
|
|
257
|
+
parser;
|
|
258
|
+
get CommandParserClass() {
|
|
259
|
+
return O;
|
|
260
|
+
}
|
|
261
|
+
defaultOptions() {
|
|
262
|
+
return [new A()];
|
|
263
|
+
}
|
|
264
|
+
get command() {
|
|
265
|
+
return this.parser ? this.parser.command : this.signature.split(" ")[0];
|
|
266
|
+
}
|
|
267
|
+
async run(t, ...e) {
|
|
268
|
+
this.ctx = t;
|
|
269
|
+
const n = this.defaultOptions();
|
|
270
|
+
this.parser = new this.CommandParserClass(this.signature, this.helperDefinitions, n, ...e);
|
|
271
|
+
for (const i of n)
|
|
272
|
+
if (this.parser.option(i.option)) {
|
|
273
|
+
const o = await i.handler.call(this);
|
|
274
|
+
if (o && o !== 0)
|
|
275
|
+
return o;
|
|
276
|
+
}
|
|
277
|
+
return this.parser.validate(), await this.handle() ?? 0;
|
|
278
|
+
}
|
|
279
|
+
setOption(t, e) {
|
|
280
|
+
this.parser.setOption(t, e);
|
|
281
|
+
}
|
|
282
|
+
setArgument(t, e) {
|
|
283
|
+
this.parser.setArgument(t, e);
|
|
284
|
+
}
|
|
285
|
+
option(t, e = null) {
|
|
286
|
+
return this.parser.option(t) ?? e;
|
|
287
|
+
}
|
|
288
|
+
optionBoolean(t, e = !1) {
|
|
289
|
+
return this.parser.option(t) ?? e;
|
|
290
|
+
}
|
|
291
|
+
optionArray(t, e = []) {
|
|
292
|
+
const n = this.parser.option(t);
|
|
293
|
+
if (!Array.isArray(n))
|
|
294
|
+
throw new Error(`Option ${t} is not an array`);
|
|
295
|
+
return n.length ? n : e;
|
|
296
|
+
}
|
|
297
|
+
optionNumber(t, e = null) {
|
|
298
|
+
const n = this.parser.option(t);
|
|
299
|
+
return n ? typeof n == "number" ? n : parseInt(n) : e;
|
|
300
|
+
}
|
|
301
|
+
argument(t, e = null) {
|
|
302
|
+
return this.parser.argument(t) ?? e;
|
|
303
|
+
}
|
|
304
|
+
argumentArray(t, e = []) {
|
|
305
|
+
const n = this.parser.argument(t);
|
|
306
|
+
if (!Array.isArray(n))
|
|
307
|
+
throw new Error(`Argument ${t} is not an array`);
|
|
308
|
+
return n?.length ? n : e;
|
|
309
|
+
}
|
|
310
|
+
argumentBoolean(t, e = !1) {
|
|
311
|
+
return this.parser.argument(t) ?? e;
|
|
312
|
+
}
|
|
313
|
+
argumentNumber(t, e = null) {
|
|
314
|
+
const n = this.parser.argument(t);
|
|
315
|
+
return n ? typeof n == "number" ? n : parseInt(n) : e;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
class x extends h {
|
|
319
|
+
constructor(t, e) {
|
|
320
|
+
super(`Command "${t}" not found.`), this.command = t, this.similarCommands = e;
|
|
321
|
+
}
|
|
322
|
+
pretty() {
|
|
323
|
+
const t = console.log;
|
|
324
|
+
if (this.similarCommands.length) {
|
|
325
|
+
t(a` {white.bgRed ERROR } Command "${this.command}" not found, Did you mean one of these?`);
|
|
326
|
+
for (const e of this.similarCommands)
|
|
327
|
+
t(a` {gray ⇂ ${e}}`);
|
|
328
|
+
} else
|
|
329
|
+
t(a` {white.bgRed ERROR } Command "${this.command}" not found.`);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
class E {
|
|
333
|
+
commands = {};
|
|
334
|
+
get commandSuffix() {
|
|
335
|
+
return "Command";
|
|
336
|
+
}
|
|
337
|
+
constructor() {
|
|
338
|
+
}
|
|
339
|
+
getAvailableCommands() {
|
|
340
|
+
return Object.keys(this.commands);
|
|
341
|
+
}
|
|
342
|
+
getCommands() {
|
|
343
|
+
return Object.values(this.commands);
|
|
344
|
+
}
|
|
345
|
+
commandResolver = async (t) => {
|
|
346
|
+
const e = (await import(t)).default;
|
|
347
|
+
let n;
|
|
348
|
+
return e?.default ? n = new e.default() : n = new e(), n;
|
|
349
|
+
};
|
|
350
|
+
setCommandResolver(t) {
|
|
351
|
+
this.commandResolver = t;
|
|
352
|
+
}
|
|
353
|
+
registerCommand(t, e = !1) {
|
|
354
|
+
const n = t.signature.split(" ")[0];
|
|
355
|
+
if (!n)
|
|
356
|
+
throw new Error("Command signature is invalid, it must have a command name.");
|
|
357
|
+
if (!e && this.commands[n])
|
|
358
|
+
throw new Error(`Command ${n} already registered.`);
|
|
359
|
+
this.commands[n] = t;
|
|
360
|
+
}
|
|
361
|
+
async loadCommandsPath(t) {
|
|
362
|
+
for await (const e of this.listCommandsFiles(t))
|
|
363
|
+
try {
|
|
364
|
+
const n = await this.commandResolver(e);
|
|
365
|
+
this.registerCommand(n);
|
|
366
|
+
} catch (n) {
|
|
367
|
+
throw new Error(`Command ${e} failed to load. ${n}`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
async runCommand(t, e, ...n) {
|
|
371
|
+
const i = typeof e == "string" ? this.commands[e] : e, o = typeof e == "string" ? e : i.command;
|
|
372
|
+
if (!i) {
|
|
373
|
+
const r = await this.suggestCommand(o);
|
|
374
|
+
return r ? await this.runCommand(t, r, ...n) : 1;
|
|
375
|
+
}
|
|
376
|
+
return await i.run(t, ...n);
|
|
377
|
+
}
|
|
378
|
+
async suggestCommand(t) {
|
|
379
|
+
const e = this.getAvailableCommands(), { bestMatch: n, bestMatchIndex: i, ratings: o } = R.findBestMatch(t, e), r = o.filter((p) => p.rating > 0.3).map((p) => p.target);
|
|
380
|
+
if (n.rating > 0 && r.length <= 1 || n.rating > 0.7 && r.length > 1) {
|
|
381
|
+
const p = e[i];
|
|
382
|
+
return await this.askRunSimilarCommand(t, p) ? p : null;
|
|
383
|
+
}
|
|
384
|
+
throw new x(t, r);
|
|
385
|
+
}
|
|
386
|
+
async askRunSimilarCommand(t, e) {
|
|
387
|
+
const n = require("readline").createInterface({
|
|
388
|
+
input: process.stdin,
|
|
389
|
+
output: process.stdout
|
|
390
|
+
});
|
|
391
|
+
return console.log(a` {bgRed ERROR } Command {yellow ${t}} not found.\n`), new Promise((i) => {
|
|
392
|
+
n.question(a`{green Do you want to run {yellow ${e}} instead?} {white (yes/no)} [{yellow no}]\n > `, (o) => {
|
|
393
|
+
i(o === "yes" || o === "y"), n.close();
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
async *listCommandsFiles(t) {
|
|
398
|
+
const e = v.readdirSync(t, { withFileTypes: !0 });
|
|
399
|
+
for (const n of e) {
|
|
400
|
+
const i = C.resolve(t, n.name);
|
|
401
|
+
if (n.isDirectory())
|
|
402
|
+
yield* this.listCommandsFiles(C.resolve(t, n.name));
|
|
403
|
+
else {
|
|
404
|
+
if (!i.endsWith(`${this.commandSuffix}.ts`) && !i.endsWith(`${this.commandSuffix}.js`))
|
|
405
|
+
continue;
|
|
406
|
+
yield i;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
class j extends V {
|
|
412
|
+
constructor(t) {
|
|
413
|
+
super(), this.opts = t;
|
|
414
|
+
}
|
|
415
|
+
signature = "help";
|
|
416
|
+
description = "Show help";
|
|
417
|
+
async handle() {
|
|
418
|
+
const t = this.opts.commandRegistry.getCommands(), e = this.opts.cliName ?? "Bob CLI", n = this.opts.cliVersion ?? "0.0.0", i = (await import("./package-B_d-lCr-.js"))?.default?.version ?? "0.0.0";
|
|
419
|
+
console.log(a`${e} {green ${n}} (core: {yellow ${i}})
|
|
420
|
+
|
|
421
|
+
{yellow Usage}:
|
|
422
|
+
command [options] [arguments]
|
|
423
|
+
|
|
424
|
+
{yellow Available commands}:
|
|
425
|
+
`);
|
|
426
|
+
const o = Math.max(...t.map((s) => s.command.length)) ?? 0, r = {};
|
|
427
|
+
for (const s of t) {
|
|
428
|
+
const l = s.command.split(":")[0];
|
|
429
|
+
r[l] || (r[l] = []), r[l].push(s);
|
|
430
|
+
}
|
|
431
|
+
const p = Object.entries(r).sort(([s], [l]) => s.toLowerCase().localeCompare(l.toLowerCase())).sort(([, s], [, l]) => s.length - l.length);
|
|
432
|
+
for (const [s, l] of p) {
|
|
433
|
+
const u = l.length > 1;
|
|
434
|
+
u && console.log(a`{yellow ${s}}:`);
|
|
435
|
+
const c = l.sort((d, g) => d.command.toLowerCase().localeCompare(g.command.toLowerCase()));
|
|
436
|
+
for (const d of c) {
|
|
437
|
+
let g = w(o - d.command.length);
|
|
438
|
+
u && (g = g.slice(2)), console.log(a`${u ? " " : ""}{green ${d.command}} ${g} ${d.description}`);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
class B extends h {
|
|
444
|
+
constructor(t) {
|
|
445
|
+
let e = `Argument "${t.param}" value is invalid.`;
|
|
446
|
+
t.reason ? e += ` Reason: ${t.reason}` : e += ` Value: "${t.value}"`, super(e), this.param = t;
|
|
447
|
+
}
|
|
448
|
+
pretty() {
|
|
449
|
+
const t = console.log;
|
|
450
|
+
t(a` {white.bgRed ERROR } Argument {bold.yellow ${this.param.param}} value is invalid. `), (this.param.value || this.param.reason) && t(""), this.param.value && t(a` {blue Value}: ${this.param.value}`), this.param.reason && t(a` {yellow Reason}: ${this.param.reason}`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
class M extends h {
|
|
454
|
+
constructor(t) {
|
|
455
|
+
let e = `Option "${t.option}" value is invalid.`;
|
|
456
|
+
t.reason ? e += ` Reason: ${t.reason}` : e += ` Value: "${t.value}"`, super(e), this.param = t;
|
|
457
|
+
}
|
|
458
|
+
pretty() {
|
|
459
|
+
const t = console.log;
|
|
460
|
+
t(a` {white.bgRed ERROR } Option {bold.yellow ${this.param.option}} value is invalid. `), (this.param.value || this.param.reason) && t(""), this.param.value && t(a` {blue Value}: ${this.param.value}`), this.param.reason && t(a` {yellow Reason}: ${this.param.reason}`);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
class H {
|
|
464
|
+
handle(t) {
|
|
465
|
+
if (t instanceof h)
|
|
466
|
+
return t.pretty(), -1;
|
|
467
|
+
throw t;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
class N {
|
|
471
|
+
commandRegistry;
|
|
472
|
+
exceptionHandler;
|
|
473
|
+
ctx;
|
|
474
|
+
helpCommand;
|
|
475
|
+
get CommandRegistryClass() {
|
|
476
|
+
return E;
|
|
477
|
+
}
|
|
478
|
+
get HelpCommandClass() {
|
|
479
|
+
return j;
|
|
480
|
+
}
|
|
481
|
+
get ExceptionHandlerClass() {
|
|
482
|
+
return H;
|
|
483
|
+
}
|
|
484
|
+
constructor(t = {}) {
|
|
485
|
+
this.ctx = t.ctx, this.commandRegistry = new this.CommandRegistryClass(), this.exceptionHandler = new this.ExceptionHandlerClass(), this.helpCommand = new this.HelpCommandClass({
|
|
486
|
+
cliName: t.name,
|
|
487
|
+
cliVersion: t.version,
|
|
488
|
+
commandRegistry: this.commandRegistry
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
setCommandResolver(t) {
|
|
492
|
+
this.commandRegistry.setCommandResolver(t);
|
|
493
|
+
}
|
|
494
|
+
async withCommands(...t) {
|
|
495
|
+
for (const e of t)
|
|
496
|
+
typeof e == "string" ? await this.commandRegistry.loadCommandsPath(e) : typeof e == "function" ? this.registerCommand(new e()) : this.registerCommand(e);
|
|
497
|
+
}
|
|
498
|
+
async runCommand(t, ...e) {
|
|
499
|
+
return t ? await this.commandRegistry.runCommand(this.ctx, t, ...e).catch(this.exceptionHandler.handle) : await this.runHelpCommand();
|
|
500
|
+
}
|
|
501
|
+
async runHelpCommand() {
|
|
502
|
+
return await this.runCommand(this.helpCommand);
|
|
503
|
+
}
|
|
504
|
+
registerCommand(t) {
|
|
505
|
+
this.commandRegistry.registerCommand(t);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
export {
|
|
509
|
+
M as BadCommandOption,
|
|
510
|
+
B as BadCommandParameter,
|
|
511
|
+
h as BobError,
|
|
512
|
+
N as Cli,
|
|
513
|
+
V as Command,
|
|
514
|
+
A as HelpOption
|
|
515
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const t = "bob-core", s = "1.2.3", e = "BOB Core", i = "module", n = ["/dist"], o = { ".": { types: "./dist/types/index.d.ts", import: "./dist/bob-core.js", require: "./dist/bob-core.cjs" } }, r = "./dist/types/index.d.ts", c = { start: "node -r @swc-node/register debug/main.ts", build: "rimraf ./dist && vite build --config vite.config.ts", prepare: "npm run build", test: "vitest run" }, p = "Léo Hubert", d = "ISC", m = { "@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", rimraf: "^6.0.1", typescript: "^5.7.3", vite: "^7.1.6", "vite-plugin-dts": "^4.5.4", vitest: "^3.2.4" }, a = { chalk: "^4.1.2", minimist: "^1.2.8", prompts: "^2.4.2", "string-similarity": "^4.0.4" }, u = {
|
|
2
|
+
name: t,
|
|
3
|
+
version: s,
|
|
4
|
+
description: e,
|
|
5
|
+
type: i,
|
|
6
|
+
files: n,
|
|
7
|
+
exports: o,
|
|
8
|
+
types: r,
|
|
9
|
+
scripts: c,
|
|
10
|
+
author: p,
|
|
11
|
+
license: d,
|
|
12
|
+
devDependencies: m,
|
|
13
|
+
dependencies: a
|
|
14
|
+
};
|
|
15
|
+
export {
|
|
16
|
+
p as author,
|
|
17
|
+
u as default,
|
|
18
|
+
a as dependencies,
|
|
19
|
+
e as description,
|
|
20
|
+
m as devDependencies,
|
|
21
|
+
o as exports,
|
|
22
|
+
n as files,
|
|
23
|
+
d as license,
|
|
24
|
+
t as name,
|
|
25
|
+
c as scripts,
|
|
26
|
+
i as type,
|
|
27
|
+
r as types,
|
|
28
|
+
s as version
|
|
29
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e="bob-core",t="1.2.3",s="BOB Core",i="module",n=["/dist"],o={".":{types:"./dist/types/index.d.ts",import:"./dist/bob-core.js",require:"./dist/bob-core.cjs"}},r="./dist/types/index.d.ts",c={start:"node -r @swc-node/register debug/main.ts",build:"rimraf ./dist && vite build --config vite.config.ts",prepare:"npm run build",test:"vitest run"},p="Léo Hubert",d="ISC",a={"@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",rimraf:"^6.0.1",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"},l={name:e,version:t,description:s,type:i,files:n,exports:o,types:r,scripts:c,author:p,license:d,devDependencies:a,dependencies:m};exports.author=p;exports.default=l;exports.dependencies=m;exports.description=s;exports.devDependencies=a;exports.exports=o;exports.files=n;exports.license=d;exports.name=e;exports.scripts=c;exports.type=i;exports.types=r;exports.version=t;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { CommandRegistry } from
|
|
2
|
-
import { Command } from
|
|
3
|
-
import HelpCommand from
|
|
4
|
-
import { ExceptionHandler } from
|
|
1
|
+
import { CommandRegistry } from './CommandRegistry.js';
|
|
2
|
+
import { Command } from './Command.js';
|
|
3
|
+
import { default as HelpCommand } from './commands/HelpCommand.js';
|
|
4
|
+
import { ExceptionHandler } from './ExceptionHandler.js';
|
|
5
5
|
export type CliOptions<C> = {
|
|
6
6
|
ctx?: C;
|
|
7
7
|
name?: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CommandParser } from
|
|
2
|
-
import { CommandOption } from
|
|
1
|
+
import { CommandParser } from './CommandParser.js';
|
|
2
|
+
import { CommandOption } from './contracts/index.js';
|
|
3
3
|
export type CommandExample = {
|
|
4
4
|
description: string;
|
|
5
5
|
command: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Command } from
|
|
2
|
-
import { CommandRegistry } from
|
|
1
|
+
import { Command } from '../Command.js';
|
|
2
|
+
import { CommandRegistry } from '../CommandRegistry.js';
|
|
3
3
|
export type HelpCommandOptions = {
|
|
4
4
|
commandRegistry: CommandRegistry;
|
|
5
5
|
cliName?: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BobError } from
|
|
2
|
-
import { ArgSignature } from
|
|
1
|
+
import { BobError } from './BobError.js';
|
|
2
|
+
import { ArgSignature } from '../CommandParser.js';
|
|
3
3
|
export declare class InvalidOption extends BobError {
|
|
4
4
|
private option;
|
|
5
5
|
private optionsSignature;
|