cleye 1.0.1 → 1.2.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -65,7 +65,12 @@ if (argv.flags.time === 'morning') {
65
65
 
66
66
  🛠 In development, type hints are provided on parsed flags and parameters:
67
67
  <p align="center">
68
+ <br>
68
69
  <img src=".github/typed-flags.png" width="600">
70
+ <br>
71
+ <i>Type hints for Cleye's output are very verbose and readable</i>
72
+ <br>
73
+ <br>
69
74
  </p>
70
75
 
71
76
  📖 Generated help documentation can be viewed with the `--help` flag:
@@ -155,6 +160,38 @@ argv._.optionalParameter // => "b" (string | undefined)
155
160
  argv._.optionalSpread // => ["c", "d"] (string[])
156
161
  ```
157
162
 
163
+ #### End-of-flags
164
+ End-of-flags (`--`) (aka _end-of-options_) allows users to pass in a subset of arguments. This is useful for passing in arguments that should be parsed separately from the rest of the arguments or passing in arguments that look like flags.
165
+
166
+ An example of this is [`npm run`](https://docs.npmjs.com/cli/v8/commands/npm-run-script):
167
+ ```sh
168
+ $ npm run <script> -- <script arguments>
169
+ ```
170
+ The `--` indicates that all arguments afterwards should be passed into the _script_ rather than _npm_.
171
+
172
+ All end-of-flag arguments will be accessible from `argv._['--']`.
173
+
174
+ Additionally, you can specify `--` in the `parameters` array to parse end-of-flags arguments.
175
+
176
+ Example:
177
+
178
+ ```ts
179
+ const argv = cli({
180
+ name: 'npm-run',
181
+ parameters: [
182
+ '<script>',
183
+ '--',
184
+ '[arguments...]'
185
+ ]
186
+ })
187
+
188
+ // $ npm-run echo -- hello world
189
+
190
+ argv._.script // => "echo" (string)
191
+ argv._.arguments // => ["hello", "world] (string[])
192
+ ```
193
+
194
+
158
195
  ### Flags
159
196
  Flags (aka Options) are key-value pairs passed into the script in the format `--flag-name <value>`.
160
197
 
@@ -170,7 +207,7 @@ _Cleye_'s flag parsing is powered by [`type-flag`](https://github.com/privatenum
170
207
  - Array & Custom types
171
208
  - Flag delimiters: `--flag value`, `--flag=value`, `--flag:value`, and `--flag.value`
172
209
  - Combined aliases: `-abcd 2` → `-a -b -c -d 2`
173
- - Early termination: Pass in `--` to end flag parsing
210
+ - [End of flags](https://unix.stackexchange.com/a/11382): Pass in `--` to end flag parsing
174
211
  - Unknown flags: Unexpected flags stored in `unknownFlags`
175
212
 
176
213
 
package/dist/index.d.ts CHANGED
@@ -64,8 +64,8 @@ declare class Renderers {
64
64
  tableBreakpoints?: Record<string, Options>;
65
65
  }): string;
66
66
  flagParameter(typeFunction: TypeFunction | readonly [TypeFunction]): string;
67
- flagOperator(): string;
68
- flagName({ flag, flagFormatted, aliasesEnabled, aliasFormatted, }: FlagData): string;
67
+ flagOperator(_: FlagData): string;
68
+ flagName(flagData: FlagData): string;
69
69
  flagDefault(value: any): string;
70
70
  flagDescription({ flag }: FlagData): string;
71
71
  render(nodes: (HelpDocumentNodeOrString<keyof this> | HelpDocumentNodeOrString<keyof this>[])): string;
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- var M=Object.create;var y=Object.defineProperty,W=Object.defineProperties,Z=Object.getOwnPropertyDescriptor,z=Object.getOwnPropertyDescriptors,G=Object.getOwnPropertyNames,F=Object.getOwnPropertySymbols,Q=Object.getPrototypeOf,k=Object.prototype.hasOwnProperty,X=Object.prototype.propertyIsEnumerable;var D=(t,e,n)=>e in t?y(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,c=(t,e)=>{for(var n in e||(e={}))k.call(e,n)&&D(t,n,e[n]);if(F)for(var n of F(e))X.call(e,n)&&D(t,n,e[n]);return t},O=(t,e)=>W(t,z(e)),T=t=>y(t,"__esModule",{value:!0});var Y=(t,e)=>{for(var n in e)y(t,n,{get:e[n],enumerable:!0})},E=(t,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of G(e))!k.call(t,r)&&(n||r!=="default")&&y(t,r,{get:()=>e[r],enumerable:!(a=Z(e,r))||a.enumerable});return t},A=(t,e)=>E(T(y(t!=null?M(Q(t)):{},"default",!e&&t&&t.__esModule?{get:()=>t.default,enumerable:!0}:{value:t,enumerable:!0})),t),ee=(t=>(e,n)=>t&&t.get(e)||(n=E(T({}),e,1),t&&t.set(e,n),n))(typeof WeakMap!="undefined"?new WeakMap:0);var ue={};Y(ue,{cli:()=>V,command:()=>_});var q=A(require("type-flag"));var S=t=>t.replace(/[-_ ](\w)/g,(e,n)=>n.toUpperCase()),B=t=>t.replace(/\B([A-Z])/g,"-$1").toLowerCase();var te={"> 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 I(t){let e=!1,a=Object.keys(t).sort((r,s)=>r.localeCompare(s)).map(r=>{let s=t[r],i="alias"in s;return i&&(e=!0),{name:r,flag:s,flagFormatted:`--${B(r)}`,aliasesEnabled:e,aliasFormatted:i?`-${s.alias}`:void 0}}).map(r=>(r.aliasesEnabled=e,[{type:"flagName",data:r},{type:"flagDescription",data:r}]));return{type:"table",data:{tableData:a,tableBreakpoints:te}}}var R=t=>{var e;return!t||((e=t.version)!=null?e:t.help?t.help.version:void 0)},j=t=>{var n;let e="parent"in t&&((n=t.parent)==null?void 0:n.name);return(e?`${e} `:"")+t.name};function ne(t){var a;let e=[];t.name&&e.push(j(t));let n=(a=R(t))!=null?a:"parent"in t&&R(t.parent);if(n&&e.push(`v${n}`),e.length!==0)return{id:"name",type:"text",data:`${e.join(" ")}
2
- `}}function re(t){let{help:e}=t;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
3
- `}}function ae(t){var n;let 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){let a=[],r=[j(t)];if(t.flags&&Object.keys(t.flags).length>0&&r.push("[flags...]"),t.parameters&&t.parameters.length>0&&r.push(t.parameters.join(" ")),r.length>1&&a.push(r.join(" ")),"commands"in t&&((n=t.commands)==null?void 0:n.length)&&a.push(`${t.name} <command>`),a.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:a.join(`
5
- `)}}}}function se(t){var a;if(!("commands"in t)||!((a=t.commands)==null?void 0:a.length))return;let e=t.commands.map(r=>[r.options.name,r.options.help?r.options.help.description:""]);return{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:e,tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function ie(t){if(!(!t.flags||Object.keys(t.flags).length===0))return{id:"flags",type:"section",data:{title:"Flags:",body:I(t.flags),indentBody:0}}}function oe(t){let{help:e}=t;if(!e||!e.examples||e.examples.length===0)return;let{examples:n}=e;if(Array.isArray(n)&&(n=n.join(`
6
- `)),n)return{id:"examples",type:"section",data:{title:"Examples:",body:n}}}function me(t){if(!("alias"in t)||!t.alias)return;let{alias:e}=t,n=Array.isArray(e)?e.join(", "):e;return{id:"aliases",type:"section",data:{title:"Aliases:",body:n}}}var $=t=>[ne,re,ae,se,ie,oe,me].map(e=>e(t)).filter(e=>Boolean(e));var K=A(require("tty")),C=A(require("terminal-columns")),pe=K.default.WriteStream.prototype.hasColors(),b=class{text(e){return e}bold(e){return`${e}`}indentText({text:e,spaces:n}){return e.replace(/^/gm," ".repeat(n))}heading(e){return pe?this.bold(e):e.toLocaleUpperCase()}section({title:e,body:n,indentBody:a=2}){return`${(e?`${this.heading(e)}
7
- `:"")+(n?this.indentText({text:this.render(n),spaces:a}):"")}
8
- `}table({tableData:e,tableOptions:n,tableBreakpoints:a}){return(0,C.default)(e.map(r=>r.map(s=>this.render(s))),a?(0,C.breakpoints)(a):n)}flagParameter(e){return e===Boolean?"":e===String?"<string>":e===Number?"<number>":Array.isArray(e)?this.flagParameter(e[0]):"<value>"}flagOperator(){return" "}flagName({flag:e,flagFormatted:n,aliasesEnabled:a,aliasFormatted:r}){let s="";if(r?s+=`${r}, `:a&&(s+=" "),s+=n,"placeholder"in e&&typeof e.placeholder=="string")s+=`${this.flagOperator()}${e.placeholder}`;else{let i=this.flagParameter("type"in e?e.type:e);i&&(s+=`${this.flagOperator()}${i}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){var a;let n="description"in e&&(a=e.description)!=null?a:"";if("default"in e){let{default:r}=e;typeof r=="function"&&(r=r()),r&&(n+=` (default: ${this.flagDefault(r)})`)}return n}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(n=>this.render(n)).join(`
9
- `);if("type"in e&&this[e.type]){let n=this[e.type];if(typeof n=="function")return n.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}};var h=/^[\w.-]+$/;var{stringify:m}=JSON,le=/[|\\{}()[\]^$+*?.]/;function de(t){let e=[],n,a;for(let r of t){if(a)throw new Error(`Invalid parameter: Spread parameter ${m(a)} must be last`);let s=r[0],i=r[r.length-1],p;if(s==="<"&&i===">"&&(p=!0,n))throw new Error(`Invalid parameter: Required parameter ${m(r)} cannot come after optional parameter ${m(n)}`);if(s==="["&&i==="]"&&(p=!1,n=r),p===void 0)throw new Error(`Invalid parameter: ${m(r)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let o=r.slice(1,-1),u=o.slice(-3)==="...";u&&(a=r,o=o.slice(0,-3));let g=o.match(le);if(g)throw new Error(`Invalid parameter: ${m(r)}. Invalid character found ${m(g[0])}`);e.push({name:o,required:p,spread:u})}return e}function ce(t){return t===void 0||t!==!1}function L(t,e,n,a){let r=c({},e.flags),s=e.version;s&&(r.version={type:Boolean,description:"Show version"});let{help:i}=e,p=ce(i);p&&!("help"in r)&&(r.help={type:Boolean,alias:"h",description:"Show help"});let o=(0,q.default)(r,a),u=()=>{console.log(e.version)};if(s&&o.flags.version===!0)return u(),process.exit(0);let g=new b,U=p&&(i==null?void 0:i.render)?i.render:l=>g.render(l),P=l=>{let x=$(O(c(c({},e),l?{help:l}:{}),{flags:r}));console.log(U(x,g))};if(p&&o.flags.help===!0)return P(),process.exit(0);if(e.parameters){let l=de(e.parameters),x=o._;for(let f=0;f<l.length;f+=1){let{name:N,required:J,spread:w}=l[f],v=w?o._.slice(f):o._[f];if(w&&(f=l.length),J&&(!v||w&&v.length===0))return console.error(`Error: Missing required parameter ${m(N)}
10
- `),P(),process.exit(1);x[S(N)]=v}}let H=O(c({},o),{showVersion:u,showHelp:P});return typeof n=="function"&&n(H),c({command:t},H)}function fe(t,e){let n=new Map;for(let a of e){let r=[a.options.name],{alias:s}=a.options;s&&(Array.isArray(s)?r.push(...s):r.push(s));for(let i of r){if(n.has(i))throw new Error(`Duplicate command name found: ${m(i)}`);n.set(i,a)}}return n.get(t)}function V(t,e,n=process.argv.slice(2)){if(!t)throw new Error("Options is required");if("name"in t&&(!t.name||!h.test(t.name)))throw new Error(`Invalid script name: ${m(t.name)}`);let a=n[0];if(t.commands&&h.test(a)){let r=fe(a,t.commands);if(r)return L(r.options.name,O(c({},r.options),{parent:t}),r.callback,n.slice(1))}return L(void 0,t,e,n)}function _(t,e){if(!t)throw new Error("Command options are required");let{name:n}=t;if(t.name===void 0)throw new Error("Command name is required");if(!h.test(n))throw new Error(`Invalid command name ${JSON.stringify(n)}. Command names must be one word.`);return{options:t,callback:e}}module.exports=ee(ue);0&&(module.exports={cli,command});
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var B=require("type-flag"),I=require("tty"),C=require("terminal-columns");function v(r){return r&&typeof r=="object"&&"default"in r?r:{default:r}}var D=v(B),L=v(I),R=v(C);const T=r=>r.replace(/[-_ ](\w)/g,(e,t)=>t.toUpperCase()),F=r=>r.replace(/\B([A-Z])/g,"-$1").toLowerCase(),H={"> 80":[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"auto"}],"> 40":[{width:"auto",paddingLeft:2,paddingRight:8,preprocess:r=>r.trim()},{width:"100%",paddingLeft:2,paddingBottom:1}],"> 0":{stdoutColumns:1e3,columns:[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"content-width"}]}};function M(r){let e=!1;const n=Object.keys(r).sort((a,i)=>a.localeCompare(i)).map(a=>{const i=r[a],s="alias"in i;return s&&(e=!0),{name:a,flag:i,flagFormatted:`--${F(a)}`,aliasesEnabled:e,aliasFormatted:s?`-${i.alias}`:void 0}}).map(a=>(a.aliasesEnabled=e,[{type:"flagName",data:a},{type:"flagDescription",data:a}]));return{type:"table",data:{tableData:n,tableBreakpoints:H}}}const A=r=>{var e;return!r||((e=r.version)!=null?e:r.help?r.help.version:void 0)},x=r=>{var e;const t="parent"in r&&((e=r.parent)==null?void 0:e.name);return(t?`${t} `:"")+r.name};function U(r){var e;const t=[];r.name&&t.push(x(r));const n=(e=A(r))!=null?e:"parent"in r&&A(r.parent);if(n&&t.push(`v${n}`),t.length!==0)return{id:"name",type:"text",data:`${t.join(" ")}
2
+ `}}function V(r){const{help:e}=r;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
3
+ `}}function J(r){var e;const t=r.help||{};if("usage"in t)return t.usage?{id:"usage",type:"section",data:{title:"Usage:",body:Array.isArray(t.usage)?t.usage.join(`
4
+ `):t.usage}}:void 0;if(r.name){const n=[],a=[x(r)];if(r.flags&&Object.keys(r.flags).length>0&&a.push("[flags...]"),r.parameters&&r.parameters.length>0){const{parameters:i}=r,s=i.indexOf("--"),l=s>-1&&i.slice(s+1).some(o=>o.startsWith("<"));a.push(i.map(o=>o!=="--"?o:l?"--":"[--]").join(" "))}if(a.length>1&&n.push(a.join(" ")),"commands"in r&&((e=r.commands)==null?void 0:e.length)&&n.push(`${r.name} <command>`),n.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:n.join(`
5
+ `)}}}}function k(r){var e;if(!("commands"in r)||!((e=r.commands)!=null&&e.length))return;const t=r.commands.map(a=>[a.options.name,a.options.help?a.options.help.description:""]);return{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:t,tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function W(r){if(!(!r.flags||Object.keys(r.flags).length===0))return{id:"flags",type:"section",data:{title:"Flags:",body:M(r.flags),indentBody:0}}}function Z(r){const{help:e}=r;if(!e||!e.examples||e.examples.length===0)return;let{examples:t}=e;if(Array.isArray(t)&&(t=t.join(`
6
+ `)),t)return{id:"examples",type:"section",data:{title:"Examples:",body:t}}}function z(r){if(!("alias"in r)||!r.alias)return;const{alias:e}=r,t=Array.isArray(e)?e.join(", "):e;return{id:"aliases",type:"section",data:{title:"Aliases:",body:t}}}const G=r=>[U,V,J,k,W,Z,z].map(e=>e(r)).filter(e=>Boolean(e)),K=L.default.WriteStream.prototype.hasColors();class Q{text(e){return e}bold(e){return K?`\x1B[1m${e}\x1B[22m`:e.toLocaleUpperCase()}indentText({text:e,spaces:t}){return e.replace(/^/gm," ".repeat(t))}heading(e){return this.bold(e)}section({title:e,body:t,indentBody:n=2}){return`${(e?`${this.heading(e)}
7
+ `:"")+(t?this.indentText({text:this.render(t),spaces:n}):"")}
8
+ `}table({tableData:e,tableOptions:t,tableBreakpoints:n}){return R.default(e.map(a=>a.map(i=>this.render(i))),n?C.breakpoints(n):t)}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:t,flagFormatted:n,aliasesEnabled:a,aliasFormatted:i}=e;let s="";if(i?s+=`${i}, `:a&&(s+=" "),s+=n,"placeholder"in t&&typeof t.placeholder=="string")s+=`${this.flagOperator(e)}${t.placeholder}`;else{const l=this.flagParameter("type"in t?t.type:t);l&&(s+=`${this.flagOperator(e)}${l}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){var t;let n="description"in e&&(t=e.description)!=null?t:"";if("default"in e){let{default:a}=e;typeof a=="function"&&(a=a()),a&&(n+=` (default: ${this.flagDefault(a)})`)}return n}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(t=>this.render(t)).join(`
9
+ `);if("type"in e&&this[e.type]){const t=this[e.type];if(typeof t=="function")return t.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const w=/^[\w.-]+$/;var X=Object.defineProperty,Y=Object.defineProperties,ee=Object.getOwnPropertyDescriptors,j=Object.getOwnPropertySymbols,re=Object.prototype.hasOwnProperty,te=Object.prototype.propertyIsEnumerable,N=(r,e,t)=>e in r?X(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,m=(r,e)=>{for(var t in e||(e={}))re.call(e,t)&&N(r,t,e[t]);if(j)for(var t of j(e))te.call(e,t)&&N(r,t,e[t]);return r},b=(r,e)=>Y(r,ee(e));const{stringify:d}=JSON,ae=/[|\\{}()[\]^$+*?.]/;function _(r){const e=[];let t,n;for(const a of r){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,t))throw new Error(`Invalid parameter: Required parameter ${d(a)} cannot come after optional parameter ${d(t)}`);if(i==="["&&s==="]"&&(l=!1,t=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 p=o.match(ae);if(p)throw new Error(`Invalid parameter: ${d(a)}. Invalid character found ${d(p[0])}`);e.push({name:o,required:l,spread:f})}return e}function $(r,e,t,n){for(let a=0;a<e.length;a+=1){const{name:i,required:s,spread:l}=e[a],o=T(i);if(o in r)throw new Error(`Invalid parameter: ${d(i)} is used more than once.`);const f=l?t.slice(a):t[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);r[o]=f}}function ne(r){return r===void 0||r!==!1}function S(r,e,t,n){const a=m({},e.flags),i=e.version;i&&(a.version={type:Boolean,description:"Show version"});const{help:s}=e,l=ne(s);l&&!("help"in a)&&(a.help={type:Boolean,alias:"h",description:"Show help"});const o=D.default(a,n),f=()=>{console.log(e.version)};if(i&&o.flags.version===!0)return f(),process.exit(0);const p=new Q,q=l&&(s==null?void 0:s.render)?s.render:c=>p.render(c),h=c=>{const u=G(b(m(m({},e),c?{help:c}:{}),{flags:a}));console.log(q(u,p))};if(l&&o.flags.help===!0)return h(),process.exit(0);if(e.parameters){let{parameters:c}=e,u=o._;const y=c.indexOf("--"),E=c.slice(y+1),g=Object.create(null);if(y>-1&&E.length>0){c=c.slice(0,y);const P=o._["--"];u=u.slice(0,-P.length||void 0),$(g,_(c),u,h),$(g,_(E),P,h)}else $(g,_(c),u,h);Object.assign(o._,g)}const O=b(m({},o),{showVersion:f,showHelp:h});return typeof t=="function"&&t(O),m({command:r},O)}function ie(r,e){const t=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(t.has(s))throw new Error(`Duplicate command name found: ${d(s)}`);t.set(s,n)}}return t.get(r)}function se(r,e,t=process.argv.slice(2)){if(!r)throw new Error("Options is required");if("name"in r&&(!r.name||!w.test(r.name)))throw new Error(`Invalid script name: ${d(r.name)}`);const n=t[0];if(r.commands&&w.test(n)){const a=ie(n,r.commands);if(a)return S(a.options.name,b(m({},a.options),{parent:r}),a.callback,t.slice(1))}return S(void 0,r,e,t)}function oe(r,e){if(!r)throw new Error("Command options are required");const{name:t}=r;if(r.name===void 0)throw new Error("Command name is required");if(!w.test(t))throw new Error(`Invalid command name ${JSON.stringify(t)}. Command names must be one word.`);return{options:r,callback:e}}exports.cli=se,exports.command=oe;
package/dist/index.mjs CHANGED
@@ -1,10 +1,10 @@
1
- var R=Object.defineProperty,j=Object.defineProperties;var K=Object.getOwnPropertyDescriptors;var H=Object.getOwnPropertySymbols;var q=Object.prototype.hasOwnProperty,L=Object.prototype.propertyIsEnumerable;var N=(t,e,n)=>e in t?R(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,f=(t,e)=>{for(var n in e||(e={}))q.call(e,n)&&N(t,n,e[n]);if(H)for(var n of H(e))L.call(e,n)&&N(t,n,e[n]);return t},O=(t,e)=>j(t,K(e));import ee from"type-flag";var F=t=>t.replace(/[-_ ](\w)/g,(e,n)=>n.toUpperCase()),k=t=>t.replace(/\B([A-Z])/g,"-$1").toLowerCase();var V={"> 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,a=Object.keys(t).sort((r,s)=>r.localeCompare(s)).map(r=>{let s=t[r],i="alias"in s;return i&&(e=!0),{name:r,flag:s,flagFormatted:`--${k(r)}`,aliasesEnabled:e,aliasFormatted:i?`-${s.alias}`:void 0}}).map(r=>(r.aliasesEnabled=e,[{type:"flagName",data:r},{type:"flagDescription",data:r}]));return{type:"table",data:{tableData:a,tableBreakpoints:V}}}var T=t=>{var e;return!t||((e=t.version)!=null?e:t.help?t.help.version:void 0)},E=t=>{var n;let e="parent"in t&&((n=t.parent)==null?void 0:n.name);return(e?`${e} `:"")+t.name};function _(t){var a;let e=[];t.name&&e.push(E(t));let n=(a=T(t))!=null?a:"parent"in t&&T(t.parent);if(n&&e.push(`v${n}`),e.length!==0)return{id:"name",type:"text",data:`${e.join(" ")}
2
- `}}function U(t){let{help:e}=t;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
3
- `}}function J(t){var n;let 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){let a=[],r=[E(t)];if(t.flags&&Object.keys(t.flags).length>0&&r.push("[flags...]"),t.parameters&&t.parameters.length>0&&r.push(t.parameters.join(" ")),r.length>1&&a.push(r.join(" ")),"commands"in t&&((n=t.commands)==null?void 0:n.length)&&a.push(`${t.name} <command>`),a.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:a.join(`
5
- `)}}}}function M(t){var a;if(!("commands"in t)||!((a=t.commands)==null?void 0:a.length))return;let e=t.commands.map(r=>[r.options.name,r.options.help?r.options.help.description:""]);return{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:e,tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function W(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 Z(t){let{help:e}=t;if(!e||!e.examples||e.examples.length===0)return;let{examples:n}=e;if(Array.isArray(n)&&(n=n.join(`
6
- `)),n)return{id:"examples",type:"section",data:{title:"Examples:",body:n}}}function z(t){if(!("alias"in t)||!t.alias)return;let{alias:e}=t,n=Array.isArray(e)?e.join(", "):e;return{id:"aliases",type:"section",data:{title:"Aliases:",body:n}}}var v=t=>[_,U,J,M,W,Z,z].map(e=>e(t)).filter(e=>Boolean(e));import G from"tty";import Q,{breakpoints as X}from"terminal-columns";var Y=G.WriteStream.prototype.hasColors(),C=class{text(e){return e}bold(e){return`${e}`}indentText({text:e,spaces:n}){return e.replace(/^/gm," ".repeat(n))}heading(e){return Y?this.bold(e):e.toLocaleUpperCase()}section({title:e,body:n,indentBody:a=2}){return`${(e?`${this.heading(e)}
7
- `:"")+(n?this.indentText({text:this.render(n),spaces:a}):"")}
8
- `}table({tableData:e,tableOptions:n,tableBreakpoints:a}){return Q(e.map(r=>r.map(s=>this.render(s))),a?X(a):n)}flagParameter(e){return e===Boolean?"":e===String?"<string>":e===Number?"<number>":Array.isArray(e)?this.flagParameter(e[0]):"<value>"}flagOperator(){return" "}flagName({flag:e,flagFormatted:n,aliasesEnabled:a,aliasFormatted:r}){let s="";if(r?s+=`${r}, `:a&&(s+=" "),s+=n,"placeholder"in e&&typeof e.placeholder=="string")s+=`${this.flagOperator()}${e.placeholder}`;else{let i=this.flagParameter("type"in e?e.type:e);i&&(s+=`${this.flagOperator()}${i}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){var a;let n="description"in e&&(a=e.description)!=null?a:"";if("default"in e){let{default:r}=e;typeof r=="function"&&(r=r()),r&&(n+=` (default: ${this.flagDefault(r)})`)}return n}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(n=>this.render(n)).join(`
9
- `);if("type"in e&&this[e.type]){let n=this[e.type];if(typeof n=="function")return n.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}};var h=/^[\w.-]+$/;var{stringify:m}=JSON,te=/[|\\{}()[\]^$+*?.]/;function ne(t){let e=[],n,a;for(let r of t){if(a)throw new Error(`Invalid parameter: Spread parameter ${m(a)} must be last`);let s=r[0],i=r[r.length-1],p;if(s==="<"&&i===">"&&(p=!0,n))throw new Error(`Invalid parameter: Required parameter ${m(r)} cannot come after optional parameter ${m(n)}`);if(s==="["&&i==="]"&&(p=!1,n=r),p===void 0)throw new Error(`Invalid parameter: ${m(r)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let o=r.slice(1,-1),g=o.slice(-3)==="...";g&&(a=r,o=o.slice(0,-3));let y=o.match(te);if(y)throw new Error(`Invalid parameter: ${m(r)}. Invalid character found ${m(y[0])}`);e.push({name:o,required:p,spread:g})}return e}function re(t){return t===void 0||t!==!1}function S(t,e,n,a){let r=f({},e.flags),s=e.version;s&&(r.version={type:Boolean,description:"Show version"});let{help:i}=e,p=re(i);p&&!("help"in r)&&(r.help={type:Boolean,alias:"h",description:"Show help"});let o=ee(r,a),g=()=>{console.log(e.version)};if(s&&o.flags.version===!0)return g(),process.exit(0);let y=new C,B=p&&(i==null?void 0:i.render)?i.render:l=>y.render(l),b=l=>{let P=v(O(f(f({},e),l?{help:l}:{}),{flags:r}));console.log(B(P,y))};if(p&&o.flags.help===!0)return b(),process.exit(0);if(e.parameters){let l=ne(e.parameters),P=o._;for(let u=0;u<l.length;u+=1){let{name:$,required:I,spread:x}=l[u],w=x?o._.slice(u):o._[u];if(x&&(u=l.length),I&&(!w||x&&w.length===0))return console.error(`Error: Missing required parameter ${m($)}
10
- `),b(),process.exit(1);P[F($)]=w}}let A=O(f({},o),{showVersion:g,showHelp:b});return typeof n=="function"&&n(A),f({command:t},A)}function ae(t,e){let n=new Map;for(let a of e){let r=[a.options.name],{alias:s}=a.options;s&&(Array.isArray(s)?r.push(...s):r.push(s));for(let i of r){if(n.has(i))throw new Error(`Duplicate command name found: ${m(i)}`);n.set(i,a)}}return n.get(t)}function se(t,e,n=process.argv.slice(2)){if(!t)throw new Error("Options is required");if("name"in t&&(!t.name||!h.test(t.name)))throw new Error(`Invalid script name: ${m(t.name)}`);let a=n[0];if(t.commands&&h.test(a)){let r=ae(a,t.commands);if(r)return S(r.options.name,O(f({},r.options),{parent:t}),r.callback,n.slice(1))}return S(void 0,t,e,n)}function ie(t,e){if(!t)throw new Error("Command options are required");let{name:n}=t;if(t.name===void 0)throw new Error("Command name is required");if(!h.test(n))throw new Error(`Invalid command name ${JSON.stringify(n)}. Command names must be one word.`);return{options:t,callback:e}}export{se as cli,ie as command};
1
+ import S from"type-flag";import B from"tty";import I,{breakpoints as q}from"terminal-columns";const D=r=>r.replace(/[-_ ](\w)/g,(e,t)=>t.toUpperCase()),R=r=>r.replace(/\B([A-Z])/g,"-$1").toLowerCase(),L={"> 80":[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"auto"}],"> 40":[{width:"auto",paddingLeft:2,paddingRight:8,preprocess:r=>r.trim()},{width:"100%",paddingLeft:2,paddingBottom:1}],"> 0":{stdoutColumns:1e3,columns:[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"content-width"}]}};function T(r){let e=!1;const n=Object.keys(r).sort((a,i)=>a.localeCompare(i)).map(a=>{const i=r[a],s="alias"in i;return s&&(e=!0),{name:a,flag:i,flagFormatted:`--${R(a)}`,aliasesEnabled:e,aliasFormatted:s?`-${i.alias}`:void 0}}).map(a=>(a.aliasesEnabled=e,[{type:"flagName",data:a},{type:"flagDescription",data:a}]));return{type:"table",data:{tableData:n,tableBreakpoints:L}}}const P=r=>{var e;return!r||((e=r.version)!=null?e:r.help?r.help.version:void 0)},C=r=>{var e;const t="parent"in r&&((e=r.parent)==null?void 0:e.name);return(t?`${t} `:"")+r.name};function F(r){var e;const t=[];r.name&&t.push(C(r));const n=(e=P(r))!=null?e:"parent"in r&&P(r.parent);if(n&&t.push(`v${n}`),t.length!==0)return{id:"name",type:"text",data:`${t.join(" ")}
2
+ `}}function H(r){const{help:e}=r;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
3
+ `}}function U(r){var e;const t=r.help||{};if("usage"in t)return t.usage?{id:"usage",type:"section",data:{title:"Usage:",body:Array.isArray(t.usage)?t.usage.join(`
4
+ `):t.usage}}:void 0;if(r.name){const n=[],a=[C(r)];if(r.flags&&Object.keys(r.flags).length>0&&a.push("[flags...]"),r.parameters&&r.parameters.length>0){const{parameters:i}=r,s=i.indexOf("--"),l=s>-1&&i.slice(s+1).some(o=>o.startsWith("<"));a.push(i.map(o=>o!=="--"?o:l?"--":"[--]").join(" "))}if(a.length>1&&n.push(a.join(" ")),"commands"in r&&((e=r.commands)==null?void 0:e.length)&&n.push(`${r.name} <command>`),n.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:n.join(`
5
+ `)}}}}function V(r){var e;if(!("commands"in r)||!((e=r.commands)!=null&&e.length))return;const t=r.commands.map(a=>[a.options.name,a.options.help?a.options.help.description:""]);return{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:t,tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function J(r){if(!(!r.flags||Object.keys(r.flags).length===0))return{id:"flags",type:"section",data:{title:"Flags:",body:T(r.flags),indentBody:0}}}function M(r){const{help:e}=r;if(!e||!e.examples||e.examples.length===0)return;let{examples:t}=e;if(Array.isArray(t)&&(t=t.join(`
6
+ `)),t)return{id:"examples",type:"section",data:{title:"Examples:",body:t}}}function k(r){if(!("alias"in r)||!r.alias)return;const{alias:e}=r,t=Array.isArray(e)?e.join(", "):e;return{id:"aliases",type:"section",data:{title:"Aliases:",body:t}}}const W=r=>[F,H,U,V,J,M,k].map(e=>e(r)).filter(e=>Boolean(e)),Z=B.WriteStream.prototype.hasColors();class z{text(e){return e}bold(e){return Z?`\x1B[1m${e}\x1B[22m`:e.toLocaleUpperCase()}indentText({text:e,spaces:t}){return e.replace(/^/gm," ".repeat(t))}heading(e){return this.bold(e)}section({title:e,body:t,indentBody:n=2}){return`${(e?`${this.heading(e)}
7
+ `:"")+(t?this.indentText({text:this.render(t),spaces:n}):"")}
8
+ `}table({tableData:e,tableOptions:t,tableBreakpoints:n}){return I(e.map(a=>a.map(i=>this.render(i))),n?q(n):t)}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:t,flagFormatted:n,aliasesEnabled:a,aliasFormatted:i}=e;let s="";if(i?s+=`${i}, `:a&&(s+=" "),s+=n,"placeholder"in t&&typeof t.placeholder=="string")s+=`${this.flagOperator(e)}${t.placeholder}`;else{const l=this.flagParameter("type"in t?t.type:t);l&&(s+=`${this.flagOperator(e)}${l}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){var t;let n="description"in e&&(t=e.description)!=null?t:"";if("default"in e){let{default:a}=e;typeof a=="function"&&(a=a()),a&&(n+=` (default: ${this.flagDefault(a)})`)}return n}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(t=>this.render(t)).join(`
9
+ `);if("type"in e&&this[e.type]){const t=this[e.type];if(typeof t=="function")return t.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const w=/^[\w.-]+$/;var G=Object.defineProperty,K=Object.defineProperties,Q=Object.getOwnPropertyDescriptors,x=Object.getOwnPropertySymbols,X=Object.prototype.hasOwnProperty,Y=Object.prototype.propertyIsEnumerable,A=(r,e,t)=>e in r?G(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,p=(r,e)=>{for(var t in e||(e={}))X.call(e,t)&&A(r,t,e[t]);if(x)for(var t of x(e))Y.call(e,t)&&A(r,t,e[t]);return r},v=(r,e)=>K(r,Q(e));const{stringify:d}=JSON,ee=/[|\\{}()[\]^$+*?.]/;function b(r){const e=[];let t,n;for(const a of r){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,t))throw new Error(`Invalid parameter: Required parameter ${d(a)} cannot come after optional parameter ${d(t)}`);if(i==="["&&s==="]"&&(l=!1,t=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(ee);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 $(r,e,t,n){for(let a=0;a<e.length;a+=1){const{name:i,required:s,spread:l}=e[a],o=D(i);if(o in r)throw new Error(`Invalid parameter: ${d(i)} is used more than once.`);const f=l?t.slice(a):t[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);r[o]=f}}function re(r){return r===void 0||r!==!1}function j(r,e,t,n){const a=p({},e.flags),i=e.version;i&&(a.version={type:Boolean,description:"Show version"});const{help:s}=e,l=re(s);l&&!("help"in a)&&(a.help={type:Boolean,alias:"h",description:"Show help"});const o=S(a,n),f=()=>{console.log(e.version)};if(i&&o.flags.version===!0)return f(),process.exit(0);const u=new z,N=l&&(s==null?void 0:s.render)?s.render:c=>u.render(c),h=c=>{const m=W(v(p(p({},e),c?{help:c}:{}),{flags:a}));console.log(N(m,u))};if(l&&o.flags.help===!0)return h(),process.exit(0);if(e.parameters){let{parameters:c}=e,m=o._;const y=c.indexOf("--"),O=c.slice(y+1),g=Object.create(null);if(y>-1&&O.length>0){c=c.slice(0,y);const E=o._["--"];m=m.slice(0,-E.length||void 0),$(g,b(c),m,h),$(g,b(O),E,h)}else $(g,b(c),m,h);Object.assign(o._,g)}const _=v(p({},o),{showVersion:f,showHelp:h});return typeof t=="function"&&t(_),p({command:r},_)}function te(r,e){const t=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(t.has(s))throw new Error(`Duplicate command name found: ${d(s)}`);t.set(s,n)}}return t.get(r)}function ae(r,e,t=process.argv.slice(2)){if(!r)throw new Error("Options is required");if("name"in r&&(!r.name||!w.test(r.name)))throw new Error(`Invalid script name: ${d(r.name)}`);const n=t[0];if(r.commands&&w.test(n)){const a=te(n,r.commands);if(a)return j(a.options.name,v(p({},a.options),{parent:r}),a.callback,t.slice(1))}return j(void 0,r,e,t)}function ne(r,e){if(!r)throw new Error("Command options are required");const{name:t}=r;if(r.name===void 0)throw new Error("Command name is required");if(!w.test(t))throw new Error(`Invalid command name ${JSON.stringify(t)}. Command names must be one word.`);return{options:r,callback:e}}export{ae as cli,ne as command};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cleye",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "description": "The intuitive CLI development tool",
5
5
  "keywords": [
6
6
  "cli",
@@ -25,16 +25,16 @@
25
25
  "module": "./dist/index.mjs",
26
26
  "types": "./dist/index.d.ts",
27
27
  "exports": {
28
- ".": {
29
- "require": "./dist/index.js",
30
- "import": "./dist/index.mjs",
31
- "types": "./dist/index.d.ts"
32
- }
28
+ "require": "./dist/index.js",
29
+ "import": "./dist/index.mjs",
30
+ "types": "./dist/index.d.ts"
33
31
  },
34
32
  "scripts": {
35
- "build": "tsup src/index.ts --dts --format esm,cjs --minify",
33
+ "build": "pkgroll --minify",
36
34
  "lint": "eslint .",
37
- "test": "CI=true jest"
35
+ "pretest": "npm run build",
36
+ "test": "CI=true jest",
37
+ "test:dts": "tsd"
38
38
  },
39
39
  "husky": {
40
40
  "hooks": {
@@ -47,27 +47,31 @@
47
47
  ]
48
48
  },
49
49
  "dependencies": {
50
- "terminal-columns": "^1.4.0",
51
- "type-flag": "^2.0.0"
50
+ "terminal-columns": "^1.4.1",
51
+ "type-flag": "^2.1.0"
52
52
  },
53
53
  "devDependencies": {
54
- "@pvtnbr/eslint-config": "^0.11.0",
55
- "@types/jest": "^27.4.0",
56
- "@types/node": "^17.0.8",
54
+ "@pvtnbr/eslint-config": "^0.19.1",
55
+ "@types/jest": "^27.4.1",
56
+ "@types/node": "^17.0.29",
57
57
  "colorette": "^2.0.16",
58
58
  "es-jest": "^1.5.0",
59
- "eslint": "^8.6.0",
59
+ "eslint": "^8.14.0",
60
60
  "esno": "^0.13.0",
61
61
  "husky": "^4.3.8",
62
62
  "jest": "^27.4.7",
63
63
  "lint-staged": "^12.1.2",
64
- "tsup": "^5.11.10",
65
- "typescript": "^4.5.4"
64
+ "pkgroll": "^1.0.5",
65
+ "tsd": "^0.20.0",
66
+ "typescript": "^4.6.3"
66
67
  },
67
68
  "eslintConfig": {
68
69
  "extends": "@pvtnbr",
69
70
  "rules": {
70
71
  "unicorn/no-process-exit": "off"
71
72
  }
73
+ },
74
+ "tsd": {
75
+ "directory": "tests"
72
76
  }
73
77
  }