cleye 1.3.1 → 1.3.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/README.md +20 -3
- package/dist/index.d.ts +23 -20
- package/dist/index.js +7 -7
- package/dist/index.mjs +6 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
<p align="center">
|
|
3
|
-
<img width="110" src=".github/logo.
|
|
3
|
+
<img width="110" src=".github/logo.webp">
|
|
4
4
|
</p>
|
|
5
|
-
<h1 align="center">
|
|
5
|
+
<h1 align="center">
|
|
6
|
+
cleye
|
|
7
|
+
<br>
|
|
8
|
+
<a href="https://npm.im/cleye"><img src="https://badgen.net/npm/v/cleye"></a> <a href="https://npm.im/cleye"><img src="https://badgen.net/npm/dm/cleye"></a>
|
|
9
|
+
</h1>
|
|
6
10
|
|
|
7
11
|
The intuitive command-line interface (CLI) development tool.
|
|
8
12
|
|
|
@@ -15,7 +19,13 @@ The intuitive command-line interface (CLI) development tool.
|
|
|
15
19
|
|
|
16
20
|
→ [Try it out online](https://stackblitz.com/edit/cleye-demo?devtoolsheight=50&file=examples/greet.ts&view=editor)
|
|
17
21
|
|
|
18
|
-
<
|
|
22
|
+
<br>
|
|
23
|
+
|
|
24
|
+
<p align="center">
|
|
25
|
+
<a href="https://github.com/sponsors/privatenumber/sponsorships?tier_id=398771"><img width="412" src="https://raw.githubusercontent.com/privatenumber/sponsors/master/banners/assets/donate.webp"></a>
|
|
26
|
+
<a href="https://github.com/sponsors/privatenumber/sponsorships?tier_id=397608"><img width="412" src="https://raw.githubusercontent.com/privatenumber/sponsors/master/banners/assets/sponsor.webp"></a>
|
|
27
|
+
</p>
|
|
28
|
+
<p align="center"><sup><i>Already a sponsor?</i> Join the discussion in the <a href="https://github.com/pvtnbr/cleye">Development repo</a>!</sup></p>
|
|
19
29
|
|
|
20
30
|
## Install
|
|
21
31
|
|
|
@@ -583,3 +593,10 @@ The raw parameters array to parse.
|
|
|
583
593
|
Type:
|
|
584
594
|
|
|
585
595
|
Optional callback function that is called when the command is invoked.
|
|
596
|
+
|
|
597
|
+
## Sponsors
|
|
598
|
+
<p align="center">
|
|
599
|
+
<a href="https://github.com/sponsors/privatenumber">
|
|
600
|
+
<img src="https://cdn.jsdelivr.net/gh/privatenumber/sponsors/sponsorkit/sponsors.svg">
|
|
601
|
+
</a>
|
|
602
|
+
</p>
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TypeFlagOptions, Flags as Flags$1, TypeFlag } from 'type-flag';
|
|
2
2
|
import { Options } from 'terminal-columns';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
type CommandOptions<Parameters = string[]> = {
|
|
5
5
|
/**
|
|
6
6
|
Name of the command used to invoke it. Also displayed in `--help` output.
|
|
7
7
|
*/
|
|
@@ -33,13 +33,13 @@ declare type CommandOptions<Parameters = string[]> = {
|
|
|
33
33
|
ignoreArgv?: TypeFlagOptions['ignore'];
|
|
34
34
|
};
|
|
35
35
|
declare function command<Options extends CommandOptions<[...Parameters]>, Parameters extends string[]>(options: Readonly<Options> & CommandOptions<[...Parameters]>, callback?: CallbackFunction<ParseArgv<Options, Parameters>>): Command<Options, ParseArgv<Options, Parameters, Options['name']>>;
|
|
36
|
-
|
|
36
|
+
type Command<Options extends CommandOptions = CommandOptions, ParsedType = any> = {
|
|
37
37
|
readonly options: Options;
|
|
38
38
|
readonly callback?: CallbackFunction<any>;
|
|
39
39
|
[parsedType]: ParsedType;
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
type FlagData = {
|
|
43
43
|
name: string;
|
|
44
44
|
flag: Flags[string];
|
|
45
45
|
flagFormatted: string;
|
|
@@ -47,8 +47,8 @@ declare type FlagData = {
|
|
|
47
47
|
aliasFormatted: string | undefined;
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
type TypeFunction = (value: any) => any;
|
|
51
|
+
type HelpDocumentNodeOrString<Type extends PropertyKey> = string | HelpDocumentNode<Type>;
|
|
52
52
|
declare class Renderers {
|
|
53
53
|
text(text: string): string;
|
|
54
54
|
bold(text: string): string;
|
|
@@ -76,7 +76,7 @@ declare class Renderers {
|
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
declare const parsedType: unique symbol;
|
|
79
|
-
|
|
79
|
+
type Flags = Flags$1<{
|
|
80
80
|
/**
|
|
81
81
|
Description to be used in help output
|
|
82
82
|
|
|
@@ -96,32 +96,32 @@ declare type Flags = Flags$1<{
|
|
|
96
96
|
*/
|
|
97
97
|
placeholder?: string;
|
|
98
98
|
}>;
|
|
99
|
-
|
|
99
|
+
type CallbackFunction<Parsed> = (parsed: {
|
|
100
100
|
[Key in keyof Parsed]: Parsed[Key];
|
|
101
101
|
}) => void;
|
|
102
|
-
|
|
102
|
+
type HasVersion<Options extends {
|
|
103
103
|
flags?: Flags;
|
|
104
104
|
}> = (Options extends {
|
|
105
105
|
version: string;
|
|
106
106
|
} ? Options['flags'] & {
|
|
107
107
|
version: BooleanConstructor;
|
|
108
108
|
} : Options['flags']);
|
|
109
|
-
|
|
109
|
+
type HasHelp<Options extends {
|
|
110
110
|
flags?: Flags;
|
|
111
111
|
}> = (Options extends {
|
|
112
112
|
help: false;
|
|
113
113
|
} ? Options['flags'] : Options['flags'] & {
|
|
114
114
|
help: BooleanConstructor;
|
|
115
115
|
});
|
|
116
|
-
|
|
116
|
+
type HasHelpOrVersion<Options extends {
|
|
117
117
|
flags?: Flags;
|
|
118
118
|
}> = (HasVersion<Options> & HasHelp<Options>);
|
|
119
|
-
|
|
119
|
+
type HelpDocumentNode<Types extends PropertyKey = keyof Renderers> = {
|
|
120
120
|
id?: string;
|
|
121
121
|
type: Types;
|
|
122
122
|
data: any;
|
|
123
123
|
};
|
|
124
|
-
|
|
124
|
+
type HelpOptions = {
|
|
125
125
|
/**
|
|
126
126
|
Version of the script displayed in `--help` output. Use to avoid enabling `--version` flag.
|
|
127
127
|
*/
|
|
@@ -143,7 +143,7 @@ declare type HelpOptions = {
|
|
|
143
143
|
*/
|
|
144
144
|
render?: (nodes: HelpDocumentNode<keyof Renderers>[], renderers: Renderers) => string;
|
|
145
145
|
};
|
|
146
|
-
|
|
146
|
+
type CliOptions<Commands = Command[], Parameters extends string[] = string[]> = {
|
|
147
147
|
/**
|
|
148
148
|
Name of the script displayed in `--help` output.
|
|
149
149
|
*/
|
|
@@ -178,24 +178,27 @@ declare type CliOptions<Commands = Command[], Parameters extends string[] = stri
|
|
|
178
178
|
*/
|
|
179
179
|
ignoreArgv?: TypeFlagOptions['ignore'];
|
|
180
180
|
};
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
181
|
+
type AlphabetLowercase = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' | 'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
|
|
182
|
+
type Numeric = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
183
|
+
type AlphaNumeric = AlphabetLowercase | Uppercase<AlphabetLowercase> | Numeric;
|
|
184
|
+
type CamelCase<Word extends string> = (Word extends `${infer FirstCharacter}${infer Rest}` ? (FirstCharacter extends AlphaNumeric ? `${FirstCharacter}${CamelCase<Rest>}` : Capitalize<CamelCase<Rest>>) : Word);
|
|
185
|
+
type StripBrackets<Parameter extends string> = (Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? (ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName) : never);
|
|
186
|
+
type ParameterType<Parameter extends string> = (Parameter extends `<${infer _ParameterName}...>` | `[${infer _ParameterName}...]` ? string[] : Parameter extends `<${infer _ParameterName}>` ? string : Parameter extends `[${infer _ParameterName}]` ? string | undefined : never);
|
|
187
|
+
type WithCommand<Options extends {
|
|
185
188
|
flags?: Flags;
|
|
186
189
|
}, Command extends string | undefined = undefined> = {
|
|
187
190
|
command: Command;
|
|
188
191
|
} & Options;
|
|
189
|
-
|
|
192
|
+
type TypeFlagWrapper<Options extends {
|
|
190
193
|
flags?: Flags;
|
|
191
194
|
}, Parameters extends string[]> = TypeFlag<HasHelpOrVersion<Options>> & {
|
|
192
195
|
_: {
|
|
193
|
-
[Parameter in Parameters[number] as
|
|
196
|
+
[Parameter in Parameters[number] as CamelCase<StripBrackets<Parameter>>]: ParameterType<Parameter>;
|
|
194
197
|
};
|
|
195
198
|
showHelp: (options?: HelpOptions) => void;
|
|
196
199
|
showVersion: () => void;
|
|
197
200
|
};
|
|
198
|
-
|
|
201
|
+
type ParseArgv<Options extends {
|
|
199
202
|
flags?: Flags;
|
|
200
203
|
}, Parameters extends string[], Command extends string | undefined = ''> = (Command extends '' ? TypeFlagWrapper<Options, Parameters> : WithCommand<TypeFlagWrapper<Options, Parameters>, Command>);
|
|
201
204
|
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var q=require("type-flag"),N=require("tty"),C=require("terminal-columns");function A(t){return t&&typeof t=="object"&&"default"in t?t:{default:t}}var
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var q=require("type-flag"),N=require("tty"),C=require("terminal-columns");function A(t){return t&&typeof t=="object"&&"default"in t?t:{default:t}}var B=A(N),P=A(C);const S=t=>t.replace(/[\W_]([a-z\d])?/gi,(e,r)=>r?r.toUpperCase():""),D=t=>t.replace(/\B([A-Z])/g,"-$1").toLowerCase(),I={"> 80":[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"auto"}],"> 40":[{width:"auto",paddingLeft:2,paddingRight:8,preprocess:t=>t.trim()},{width:"100%",paddingLeft:2,paddingBottom:1}],"> 0":{stdoutColumns:1e3,columns:[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"content-width"}]}};function L(t){let e=!1;return{type:"table",data:{tableData:Object.keys(t).sort((a,i)=>a.localeCompare(i)).map(a=>{const i=t[a],s="alias"in i;return s&&(e=!0),{name:a,flag:i,flagFormatted:`--${D(a)}`,aliasesEnabled:e,aliasFormatted:s?`-${i.alias}`:void 0}}).map(a=>(a.aliasesEnabled=e,[{type:"flagName",data:a},{type:"flagDescription",data:a}])),tableBreakpoints:I}}}const x=t=>!t||(t.version??(t.help?t.help.version:void 0)),O=t=>{const e="parent"in t&&t.parent?.name;return(e?`${e} `:"")+t.name};function R(t){const e=[];t.name&&e.push(O(t));const r=x(t)??("parent"in t&&x(t.parent));if(r&&e.push(`v${r}`),e.length!==0)return{id:"name",type:"text",data:`${e.join(" ")}
|
|
2
2
|
`}}function T(t){const{help:e}=t;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
|
|
3
3
|
`}}function F(t){const e=t.help||{};if("usage"in e)return e.usage?{id:"usage",type:"section",data:{title:"Usage:",body:Array.isArray(e.usage)?e.usage.join(`
|
|
4
|
-
`):e.usage}}:void 0;if(t.name){const r=[],n=[O(t)];if(t.flags&&Object.keys(t.flags).length>0&&n.push("[flags...]"),t.parameters&&t.parameters.length>0){const{parameters:a}=t,i=a.indexOf("--"),s=i>-1&&a.slice(i+1).some(
|
|
5
|
-
`)}}}}function k(t){
|
|
6
|
-
`)),r)return{id:"examples",type:"section",data:{title:"Examples:",body:r}}}function U(t){if(!("alias"in t)||!t.alias)return;const{alias:e}=t
|
|
4
|
+
`):e.usage}}:void 0;if(t.name){const r=[],n=[O(t)];if(t.flags&&Object.keys(t.flags).length>0&&n.push("[flags...]"),t.parameters&&t.parameters.length>0){const{parameters:a}=t,i=a.indexOf("--"),s=i>-1&&a.slice(i+1).some(l=>l.startsWith("<"));n.push(a.map(l=>l!=="--"?l:s?"--":"[--]").join(" "))}if(n.length>1&&r.push(n.join(" ")),"commands"in t&&t.commands?.length&&r.push(`${t.name} <command>`),r.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:r.join(`
|
|
5
|
+
`)}}}}function k(t){return!("commands"in t)||!t.commands?.length?void 0:{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:t.commands.map(n=>[n.options.name,n.options.help?n.options.help.description:""]),tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function H(t){if(!(!t.flags||Object.keys(t.flags).length===0))return{id:"flags",type:"section",data:{title:"Flags:",body:L(t.flags),indentBody:0}}}function M(t){const{help:e}=t;if(!e||!e.examples||e.examples.length===0)return;let{examples:r}=e;if(Array.isArray(r)&&(r=r.join(`
|
|
6
|
+
`)),r)return{id:"examples",type:"section",data:{title:"Examples:",body:r}}}function U(t){if(!("alias"in t)||!t.alias)return;const{alias:e}=t;return{id:"aliases",type:"section",data:{title:"Aliases:",body:Array.isArray(e)?e.join(", "):e}}}const V=t=>[R,T,F,k,H,M,U].map(e=>e(t)).filter(Boolean),J=B.default.WriteStream.prototype.hasColors();class W{text(e){return e}bold(e){return J?`\x1B[1m${e}\x1B[22m`:e.toLocaleUpperCase()}indentText({text:e,spaces:r}){return e.replace(/^/gm," ".repeat(r))}heading(e){return this.bold(e)}section({title:e,body:r,indentBody:n=2}){return`${(e?`${this.heading(e)}
|
|
7
7
|
`:"")+(r?this.indentText({text:this.render(r),spaces:n}):"")}
|
|
8
|
-
`}table({tableData:e,tableOptions:r,tableBreakpoints:n}){return
|
|
9
|
-
`);if("type"in e&&this[e.type]){const r=this[e.type];if(typeof r=="function")return r.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const y
|
|
10
|
-
`),n(),process.exit(1);t[
|
|
8
|
+
`}table({tableData:e,tableOptions:r,tableBreakpoints:n}){return P.default(e.map(a=>a.map(i=>this.render(i))),n?C.breakpoints(n):r)}flagParameter(e){return e===Boolean?"":e===String?"<string>":e===Number?"<number>":Array.isArray(e)?this.flagParameter(e[0]):"<value>"}flagOperator(e){return" "}flagName(e){const{flag:r,flagFormatted:n,aliasesEnabled:a,aliasFormatted:i}=e;let s="";if(i?s+=`${i}, `:a&&(s+=" "),s+=n,"placeholder"in r&&typeof r.placeholder=="string")s+=`${this.flagOperator(e)}${r.placeholder}`;else{const l=this.flagParameter("type"in r?r.type:r);l&&(s+=`${this.flagOperator(e)}${l}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){let r="description"in e?e.description??"":"";if("default"in e){let{default:n}=e;typeof n=="function"&&(n=n()),n&&(r+=` (default: ${this.flagDefault(n)})`)}return r}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(r=>this.render(r)).join(`
|
|
9
|
+
`);if("type"in e&&this[e.type]){const r=this[e.type];if(typeof r=="function")return r.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const y=t=>t.length>0&&!t.includes(" "),{stringify:d}=JSON,z=/[|\\{}()[\]^$+*?.]/;function w(t){const e=[];let r,n;for(const a of t){if(n)throw new Error(`Invalid parameter: Spread parameter ${d(n)} must be last`);const i=a[0],s=a[a.length-1];let l;if(i==="<"&&s===">"&&(l=!0,r))throw new Error(`Invalid parameter: Required parameter ${d(a)} cannot come after optional parameter ${d(r)}`);if(i==="["&&s==="]"&&(l=!1,r=a),l===void 0)throw new Error(`Invalid parameter: ${d(a)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let o=a.slice(1,-1);const f=o.slice(-3)==="...";f&&(n=a,o=o.slice(0,-3));const u=o.match(z);if(u)throw new Error(`Invalid parameter: ${d(a)}. Invalid character found ${d(u[0])}`);e.push({name:o,required:l,spread:f})}return e}function b(t,e,r,n){for(let a=0;a<e.length;a+=1){const{name:i,required:s,spread:l}=e[a],o=S(i);if(o in t)throw new Error(`Invalid parameter: ${d(i)} is used more than once.`);const f=l?r.slice(a):r[a];if(l&&(a=e.length),s&&(!f||l&&f.length===0))return console.error(`Error: Missing required parameter ${d(i)}
|
|
10
|
+
`),n(),process.exit(1);t[o]=f}}function Z(t){return t===void 0||t!==!1}function j(t,e,r,n){const a={...e.flags},i=e.version;i&&(a.version={type:Boolean,description:"Show version"});const{help:s}=e,l=Z(s);l&&!("help"in a)&&(a.help={type:Boolean,alias:"h",description:"Show help"});const o=q.typeFlag(a,n,{ignore:e.ignoreArgv}),f=()=>{console.log(e.version)};if(i&&o.flags.version===!0)return f(),process.exit(0);const u=new W,_=l&&s?.render?s.render:c=>u.render(c),p=c=>{const m=V({...e,...c?{help:c}:{},flags:a});console.log(_(m,u))};if(l&&o.flags.help===!0)return p(),process.exit(0);if(e.parameters){let{parameters:c}=e,m=o._;const g=c.indexOf("--"),$=c.slice(g+1),h=Object.create(null);if(g>-1&&$.length>0){c=c.slice(0,g);const E=o._["--"];m=m.slice(0,-E.length||void 0),b(h,w(c),m,p),b(h,w($),E,p)}else b(h,w(c),m,p);Object.assign(o._,h)}const v={...o,showVersion:f,showHelp:p};return typeof r=="function"&&r(v),{command:t,...v}}function G(t,e){const r=new Map;for(const n of e){const a=[n.options.name],{alias:i}=n.options;i&&(Array.isArray(i)?a.push(...i):a.push(i));for(const s of a){if(r.has(s))throw new Error(`Duplicate command name found: ${d(s)}`);r.set(s,n)}}return r.get(t)}function K(t,e,r=process.argv.slice(2)){if(!t)throw new Error("Options is required");if("name"in t&&(!t.name||!y(t.name)))throw new Error(`Invalid script name: ${d(t.name)}`);const n=r[0];if(t.commands&&y(n)){const a=G(n,t.commands);if(a)return j(a.options.name,{...a.options,parent:t},a.callback,r.slice(1))}return j(void 0,t,e,r)}function Q(t,e){if(!t)throw new Error("Command options are required");const{name:r}=t;if(t.name===void 0)throw new Error("Command name is required");if(!y(r))throw new Error(`Invalid command name ${JSON.stringify(r)}. Command names must be one word.`);return{options:t,callback:e}}exports.cli=K,exports.command=Q;
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import{typeFlag as j}from"type-flag";import N from"tty";import B,{breakpoints as
|
|
1
|
+
import{typeFlag as j}from"type-flag";import N from"tty";import B,{breakpoints as S}from"terminal-columns";const P=t=>t.replace(/[\W_]([a-z\d])?/gi,(e,r)=>r?r.toUpperCase():""),q=t=>t.replace(/\B([A-Z])/g,"-$1").toLowerCase(),I={"> 80":[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"auto"}],"> 40":[{width:"auto",paddingLeft:2,paddingRight:8,preprocess:t=>t.trim()},{width:"100%",paddingLeft:2,paddingBottom:1}],"> 0":{stdoutColumns:1e3,columns:[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"content-width"}]}};function D(t){let e=!1;return{type:"table",data:{tableData:Object.keys(t).sort((a,i)=>a.localeCompare(i)).map(a=>{const i=t[a],s="alias"in i;return s&&(e=!0),{name:a,flag:i,flagFormatted:`--${q(a)}`,aliasesEnabled:e,aliasFormatted:s?`-${i.alias}`:void 0}}).map(a=>(a.aliasesEnabled=e,[{type:"flagName",data:a},{type:"flagDescription",data:a}])),tableBreakpoints:I}}}const A=t=>!t||(t.version??(t.help?t.help.version:void 0)),C=t=>{const e="parent"in t&&t.parent?.name;return(e?`${e} `:"")+t.name};function R(t){const e=[];t.name&&e.push(C(t));const r=A(t)??("parent"in t&&A(t.parent));if(r&&e.push(`v${r}`),e.length!==0)return{id:"name",type:"text",data:`${e.join(" ")}
|
|
2
2
|
`}}function L(t){const{help:e}=t;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
|
|
3
3
|
`}}function T(t){const e=t.help||{};if("usage"in e)return e.usage?{id:"usage",type:"section",data:{title:"Usage:",body:Array.isArray(e.usage)?e.usage.join(`
|
|
4
4
|
`):e.usage}}:void 0;if(t.name){const r=[],n=[C(t)];if(t.flags&&Object.keys(t.flags).length>0&&n.push("[flags...]"),t.parameters&&t.parameters.length>0){const{parameters:a}=t,i=a.indexOf("--"),s=i>-1&&a.slice(i+1).some(o=>o.startsWith("<"));n.push(a.map(o=>o!=="--"?o:s?"--":"[--]").join(" "))}if(n.length>1&&r.push(n.join(" ")),"commands"in t&&t.commands?.length&&r.push(`${t.name} <command>`),r.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:r.join(`
|
|
5
|
-
`)}}}}function _(t){
|
|
6
|
-
`)),r)return{id:"examples",type:"section",data:{title:"Examples:",body:r}}}function H(t){if(!("alias"in t)||!t.alias)return;const{alias:e}=t
|
|
5
|
+
`)}}}}function _(t){return!("commands"in t)||!t.commands?.length?void 0:{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:t.commands.map(n=>[n.options.name,n.options.help?n.options.help.description:""]),tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function k(t){if(!(!t.flags||Object.keys(t.flags).length===0))return{id:"flags",type:"section",data:{title:"Flags:",body:D(t.flags),indentBody:0}}}function F(t){const{help:e}=t;if(!e||!e.examples||e.examples.length===0)return;let{examples:r}=e;if(Array.isArray(r)&&(r=r.join(`
|
|
6
|
+
`)),r)return{id:"examples",type:"section",data:{title:"Examples:",body:r}}}function H(t){if(!("alias"in t)||!t.alias)return;const{alias:e}=t;return{id:"aliases",type:"section",data:{title:"Aliases:",body:Array.isArray(e)?e.join(", "):e}}}const U=t=>[R,L,T,_,k,F,H].map(e=>e(t)).filter(Boolean),V=N.WriteStream.prototype.hasColors();class J{text(e){return e}bold(e){return V?`\x1B[1m${e}\x1B[22m`:e.toLocaleUpperCase()}indentText({text:e,spaces:r}){return e.replace(/^/gm," ".repeat(r))}heading(e){return this.bold(e)}section({title:e,body:r,indentBody:n=2}){return`${(e?`${this.heading(e)}
|
|
7
7
|
`:"")+(r?this.indentText({text:this.render(r),spaces:n}):"")}
|
|
8
|
-
`}table({tableData:e,tableOptions:r,tableBreakpoints:n}){return B(e.map(a=>a.map(i=>this.render(i))),n?
|
|
9
|
-
`);if("type"in e&&this[e.type]){const r=this[e.type];if(typeof r=="function")return r.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const y
|
|
10
|
-
`),n(),process.exit(1);t[l]=f}}function W(t){return t===void 0||t!==!1}function x(t,e,r,n){const a={...e.flags},i=e.version;i&&(a.version={type:Boolean,description:"Show version"});const{help:s}=e,o=W(s);o&&!("help"in a)&&(a.help={type:Boolean,alias:"h",description:"Show help"});const l=j(a,n,{ignore:e.ignoreArgv}),f=()=>{console.log(e.version)};if(i&&l.flags.version===!0)return f(),process.exit(0);const
|
|
8
|
+
`}table({tableData:e,tableOptions:r,tableBreakpoints:n}){return B(e.map(a=>a.map(i=>this.render(i))),n?S(n):r)}flagParameter(e){return e===Boolean?"":e===String?"<string>":e===Number?"<number>":Array.isArray(e)?this.flagParameter(e[0]):"<value>"}flagOperator(e){return" "}flagName(e){const{flag:r,flagFormatted:n,aliasesEnabled:a,aliasFormatted:i}=e;let s="";if(i?s+=`${i}, `:a&&(s+=" "),s+=n,"placeholder"in r&&typeof r.placeholder=="string")s+=`${this.flagOperator(e)}${r.placeholder}`;else{const o=this.flagParameter("type"in r?r.type:r);o&&(s+=`${this.flagOperator(e)}${o}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){let r="description"in e?e.description??"":"";if("default"in e){let{default:n}=e;typeof n=="function"&&(n=n()),n&&(r+=` (default: ${this.flagDefault(n)})`)}return r}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(r=>this.render(r)).join(`
|
|
9
|
+
`);if("type"in e&&this[e.type]){const r=this[e.type];if(typeof r=="function")return r.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const y=t=>t.length>0&&!t.includes(" "),{stringify:d}=JSON,M=/[|\\{}()[\]^$+*?.]/;function w(t){const e=[];let r,n;for(const a of t){if(n)throw new Error(`Invalid parameter: Spread parameter ${d(n)} must be last`);const i=a[0],s=a[a.length-1];let o;if(i==="<"&&s===">"&&(o=!0,r))throw new Error(`Invalid parameter: Required parameter ${d(a)} cannot come after optional parameter ${d(r)}`);if(i==="["&&s==="]"&&(o=!1,r=a),o===void 0)throw new Error(`Invalid parameter: ${d(a)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let l=a.slice(1,-1);const f=l.slice(-3)==="...";f&&(n=a,l=l.slice(0,-3));const u=l.match(M);if(u)throw new Error(`Invalid parameter: ${d(a)}. Invalid character found ${d(u[0])}`);e.push({name:l,required:o,spread:f})}return e}function b(t,e,r,n){for(let a=0;a<e.length;a+=1){const{name:i,required:s,spread:o}=e[a],l=P(i);if(l in t)throw new Error(`Invalid parameter: ${d(i)} is used more than once.`);const f=o?r.slice(a):r[a];if(o&&(a=e.length),s&&(!f||o&&f.length===0))return console.error(`Error: Missing required parameter ${d(i)}
|
|
10
|
+
`),n(),process.exit(1);t[l]=f}}function W(t){return t===void 0||t!==!1}function x(t,e,r,n){const a={...e.flags},i=e.version;i&&(a.version={type:Boolean,description:"Show version"});const{help:s}=e,o=W(s);o&&!("help"in a)&&(a.help={type:Boolean,alias:"h",description:"Show help"});const l=j(a,n,{ignore:e.ignoreArgv}),f=()=>{console.log(e.version)};if(i&&l.flags.version===!0)return f(),process.exit(0);const u=new J,O=o&&s?.render?s.render:c=>u.render(c),p=c=>{const m=U({...e,...c?{help:c}:{},flags:a});console.log(O(m,u))};if(o&&l.flags.help===!0)return p(),process.exit(0);if(e.parameters){let{parameters:c}=e,m=l._;const g=c.indexOf("--"),v=c.slice(g+1),h=Object.create(null);if(g>-1&&v.length>0){c=c.slice(0,g);const E=l._["--"];m=m.slice(0,-E.length||void 0),b(h,w(c),m,p),b(h,w(v),E,p)}else b(h,w(c),m,p);Object.assign(l._,h)}const $={...l,showVersion:f,showHelp:p};return typeof r=="function"&&r($),{command:t,...$}}function z(t,e){const r=new Map;for(const n of e){const a=[n.options.name],{alias:i}=n.options;i&&(Array.isArray(i)?a.push(...i):a.push(i));for(const s of a){if(r.has(s))throw new Error(`Duplicate command name found: ${d(s)}`);r.set(s,n)}}return r.get(t)}function Z(t,e,r=process.argv.slice(2)){if(!t)throw new Error("Options is required");if("name"in t&&(!t.name||!y(t.name)))throw new Error(`Invalid script name: ${d(t.name)}`);const n=r[0];if(t.commands&&y(n)){const a=z(n,t.commands);if(a)return x(a.options.name,{...a.options,parent:t},a.callback,r.slice(1))}return x(void 0,t,e,r)}function G(t,e){if(!t)throw new Error("Command options are required");const{name:r}=t;if(t.name===void 0)throw new Error("Command name is required");if(!y(r))throw new Error(`Invalid command name ${JSON.stringify(r)}. Command names must be one word.`);return{options:t,callback:e}}export{Z as cli,G as command};
|