@clerc/core 0.32.0 → 0.32.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/index.d.ts +62 -9
- package/dist/index.js +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,65 @@ declare global {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
|
|
24
|
+
|
|
25
|
+
@example
|
|
26
|
+
```
|
|
27
|
+
import type {Simplify} from 'type-fest';
|
|
28
|
+
|
|
29
|
+
type PositionProps = {
|
|
30
|
+
top: number;
|
|
31
|
+
left: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
type SizeProps = {
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// In your editor, hovering over `Props` will show a flattened object with all the properties.
|
|
40
|
+
type Props = Simplify<PositionProps & SizeProps>;
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
|
|
44
|
+
|
|
45
|
+
If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
|
|
46
|
+
|
|
47
|
+
@example
|
|
48
|
+
```
|
|
49
|
+
import type {Simplify} from 'type-fest';
|
|
50
|
+
|
|
51
|
+
interface SomeInterface {
|
|
52
|
+
foo: number;
|
|
53
|
+
bar?: string;
|
|
54
|
+
baz: number | undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type SomeType = {
|
|
58
|
+
foo: number;
|
|
59
|
+
bar?: string;
|
|
60
|
+
baz: number | undefined;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const literal = {foo: 123, bar: 'hello', baz: 456};
|
|
64
|
+
const someType: SomeType = literal;
|
|
65
|
+
const someInterface: SomeInterface = literal;
|
|
66
|
+
|
|
67
|
+
function fn(object: Record<string, unknown>): void {}
|
|
68
|
+
|
|
69
|
+
fn(literal); // Good: literal object type is sealed
|
|
70
|
+
fn(someType); // Good: type is sealed
|
|
71
|
+
fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
|
|
72
|
+
fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
@link https://github.com/microsoft/TypeScript/issues/15300
|
|
76
|
+
|
|
77
|
+
@category Object
|
|
78
|
+
*/
|
|
79
|
+
type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
|
|
80
|
+
|
|
22
81
|
/**
|
|
23
82
|
Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
24
83
|
|
|
@@ -307,16 +366,10 @@ interface HandlerContext<C extends Commands = Commands, N extends keyof C = keyo
|
|
|
307
366
|
resolved: boolean;
|
|
308
367
|
hasRootOrAlias: boolean;
|
|
309
368
|
hasRoot: boolean;
|
|
310
|
-
raw:
|
|
311
|
-
|
|
312
|
-
};
|
|
313
|
-
parameters: {
|
|
314
|
-
[K in keyof ParseParameters<C, N>]: ParseParameters<C, N>[K];
|
|
315
|
-
};
|
|
369
|
+
raw: Simplify<ParseRaw<C[N]>>;
|
|
370
|
+
parameters: Simplify<ParseParameters<C, N>>;
|
|
316
371
|
unknownFlags: ParsedFlags["unknownFlags"];
|
|
317
|
-
flags:
|
|
318
|
-
[K in keyof ParseFlag<C, N>]: ParseFlag<C, N>[K];
|
|
319
|
-
};
|
|
372
|
+
flags: Simplify<ParseFlag<C, N>>;
|
|
320
373
|
cli: Clerc<C>;
|
|
321
374
|
}
|
|
322
375
|
type Handler<C extends Commands = Commands, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K>) => void;
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{format as ie}from"node:util";class Ie{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(t,r){return t==="*"?(this.wildcardListeners.add(r),this):(this.listenerMap[t]||(this.listenerMap[t]=new Set),this.listenerMap[t].add(r),this)}emit(t,...r){return this.listenerMap[t]&&(this.wildcardListeners.forEach(s=>s(t,...r)),this.listenerMap[t].forEach(s=>s(...r))),this}off(t,r){var s,o;return t==="**"?(this.listenerMap={},this.wildcardListeners.clear(),this):t==="*"?(r?this.wildcardListeners.delete(r):this.wildcardListeners.clear(),this):(r?(s=this.listenerMap[t])==null||s.delete(r):(o=this.listenerMap[t])==null||o.clear(),this)}}const xe="known-flag",Re="unknown-flag",$e="argument",{stringify:S}=JSON,je=/\B([A-Z])/g,qe=e=>e.replace(je,"-$1").toLowerCase(),{hasOwnProperty:Te}=Object.prototype,k=(e,t)=>Te.call(e,t),Ge=e=>Array.isArray(e),ce=e=>typeof e=="function"?[e,!1]:Ge(e)?[e[0],!0]:ce(e.type),He=(e,t)=>e===Boolean?t!=="false":t,Ue=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),Ve=/[\s.:=]/,Je=e=>{const t=`Flag name ${S(e)}`;if(e.length===0)throw new Error(`${t} cannot be empty`);if(e.length===1)throw new Error(`${t} must be longer than a character`);const r=e.match(Ve);if(r)throw new Error(`${t} cannot contain ${S(r==null?void 0:r[0])}`)},ze=e=>{const t={},r=(s,o)=>{if(k(t,s))throw new Error(`Duplicate flags named ${S(s)}`);t[s]=o};for(const s in e){if(!k(e,s))continue;Je(s);const o=e[s],n=[[],...ce(o),o];r(s,n);const a=qe(s);if(s!==a&&r(a,n),"alias"in o&&typeof o.alias=="string"){const{alias:c}=o,i=`Flag alias ${S(c)} for flag ${S(s)}`;if(c.length===0)throw new Error(`${i} cannot be empty`);if(c.length>1)throw new Error(`${i} must be a single character`);r(c,n)}}return t},Ke=(e,t)=>{const r={};for(const s in e){if(!k(e,s))continue;const[o,,n,a]=t[s];if(o.length===0&&"default"in a){let{default:c}=a;typeof c=="function"&&(c=c()),r[s]=c}else r[s]=n?o:o.pop()}return r},R="--",Ze=/[.:=]/,Qe=/^-{1,2}\w/,Xe=e=>{if(!Qe.test(e))return;const t=!e.startsWith(R);let r=e.slice(t?1:2),s;const o=r.match(Ze);if(o){const{index:n}=o;s=r.slice(n+1),r=r.slice(0,n)}return[r,s,t]},Ye=(e,{onFlag:t,onArgument:r})=>{let s;const o=(n,a)=>{if(typeof s!="function")return!0;s(n,a),s=void 0};for(let n=0;n<e.length;n+=1){const a=e[n];if(a===R){o();const i=e.slice(n+1);r==null||r(i,[n],!0);break}const c=Xe(a);if(c){if(o(),!t)continue;const[i,l,m]=c;if(m)for(let h=0;h<i.length;h+=1){o();const E=h===i.length-1;s=t(i[h],E?l:void 0,[n,h+1,E])}else s=t(i,l,[n])}else o(a,[n])&&(r==null||r([a],[n]))}o()},et=(e,t)=>{for(const[r,s,o]of t.reverse()){if(s){const n=e[r];let a=n.slice(0,s);if(o||(a+=n.slice(s+1)),a!=="-"){e[r]=a;continue}}e.splice(r,1)}},tt=(e,t=process.argv.slice(2),{ignore:r}={})=>{const s=[],o=ze(e),n={},a=[];return a[R]=[],Ye(t,{onFlag(c,i,l){const m=k(o,c);if(!(r!=null&&r(m?xe:Re,c,i))){if(m){const[h,E]=o[c],A=He(E,i),v=(T,G)=>{s.push(l),G&&s.push(G),h.push(Ue(E,T||""))};return A===void 0?v:v(A)}k(n,c)||(n[c]=[]),n[c].push(i===void 0?!0:i),s.push(l)}},onArgument(c,i,l){r!=null&&r($e,t[i[0]])||(a.push(...c),l?(a[R]=c,t.splice(i[0])):s.push(i))}}),et(t,s),{flags:Ke(e,o),unknownFlags:n,_:a}};function H(e){return e!==null&&typeof e=="object"}function U(e,t,r=".",s){if(!H(t))return U(e,{},r,s);const o=Object.assign({},t);for(const n in e){if(n==="__proto__"||n==="constructor")continue;const a=e[n];a!=null&&(s&&s(o,n,a,r)||(Array.isArray(a)&&Array.isArray(o[n])?o[n]=[...a,...o[n]]:H(a)&&H(o[n])?o[n]=U(a,o[n],(r?`${r}.`:"")+n.toString(),s):o[n]=a))}return o}function rt(e){return(...t)=>t.reduce((r,s)=>U(r,s,"",e),{})}const st=rt(),$=e=>Array.isArray(e)?e:[e],nt=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,r)=>r?r.toUpperCase():""),ue=(e,t)=>t.length!==e.length?!1:e.every((r,s)=>r===t[s]),le=(e,t)=>t.length>e.length?!1:ue(e.slice(0,t.length),t),L=JSON.stringify;class he extends Error{constructor(t,r){super(r("core.commandExists",L(t))),this.commandName=t}}class de extends Error{constructor(t,r){super(r("core.noSuchCommand",L(t))),this.commandName=t}}class fe extends Error{constructor(t){super(t("core.noCommandGiven"))}}class me extends Error{constructor(t,r,s){super(s("core.commandNameConflict",L(t),L(r))),this.n1=t,this.n2=r}}class pe extends Error{constructor(t){super(t("core.nameNotSet"))}}class Ce extends Error{constructor(t){super(t("core.descriptionNotSet"))}}class we extends Error{constructor(t){super(t("core.versionNotSet"))}}class Ee extends Error{constructor(t,r){super(r("core.badNameFormat",L(t))),this.commandName=t}}class V extends Error{constructor(t){super(t("core.localeMustBeCalledFirst"))}}const ge=typeof Deno!="undefined",ot=typeof process!="undefined"&&!ge,at=process.versions.electron&&!process.defaultApp;function _e(e,t,r,s){if(r.alias){const o=$(r.alias);for(const n of o){if(n in t)throw new me(t[n].name,r.name,s);e.set(typeof n=="symbol"?n:n.split(" "),{...r,__isAlias:!0})}}}function J(e,t){const r=new Map;e[f]&&(r.set(f,e[f]),_e(r,e,e[f],t));for(const s of Object.values(e))_e(r,e,s,t),r.set(s.name.split(" "),s);return r}function ve(e,t,r){if(t===f)return[e[f],f];const s=$(t),o=J(e,r);let n,a;return o.forEach((c,i)=>{if(i===f){n=e[f],a=f;return}le(s,i)&&(!a||a===f||i.length>a.length)&&(n=c,a=i)}),[n,a]}function it(e,t,r){if(t===f)return[e[f],f];const s=$(t),o=J(e,r);let n,a;return o.forEach((c,i)=>{i===f||a===f||ue(s,i)&&(n=c,a=i)}),[n,a]}function Me(e,t,r=1/0){const s=t===""?[]:Array.isArray(t)?t:t.split(" ");return Object.values(e).filter(o=>{const n=o.name.split(" ");return le(n,s)&&n.length-s.length<=r})}const ct=e=>Me(e,"",1);function Fe(e){const t=[];for(const r of e){if(r.startsWith("-"))break;t.push(r)}return t}const z=()=>ot?process.argv.slice(at?1:2):ge?Deno.args:[];function ye(e){const t={pre:[],normal:[],post:[]};for(const s of e){const o=typeof s=="object"?s:{fn:s},{enforce:n,fn:a}=o;n==="post"||n==="pre"?t[n].push(a):t.normal.push(a)}const r=[...t.pre,...t.normal,...t.post];return s=>{const o=[];n(0);for(let a=o.length-1;a>=0;a--)o[a]();function n(a){const c=r[a],i=c(s(),n.bind(null,a+1));i&&o.push(i)}}}const ut=/\s\s+/,Be=e=>e===f?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!ut.test(e),lt=(e,t)=>t?`[${e}]`:`<${e}>`,ht="<Root>",Ne=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:ht,Ae=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,{stringify:B}=JSON;function K(e,t){const r=[];let s,o;for(const n of e){if(o)throw new Error(t("core.spreadParameterMustBeLast",B(o)));const a=n[0],c=n[n.length-1];let i;if(a==="<"&&c===">"&&(i=!0,s))throw new Error(t("core.requiredParameterMustBeBeforeOptional",B(n),B(s)));if(a==="["&&c==="]"&&(i=!1,s=n),i===void 0)throw new Error(t("core.parameterMustBeWrappedInBrackets",B(n)));let l=n.slice(1,-1);const m=l.slice(-3)==="...";m&&(o=n,l=l.slice(0,-3)),r.push({name:l,required:i,spread:m})}return r}function Z(e,t,r,s){for(let o=0;o<t.length;o+=1){const{name:n,required:a,spread:c}=t[o],i=nt(n);if(i in e)throw new Error(s("core.parameterIsUsedMoreThanOnce",B(n)));const l=c?r.slice(o):r[o];if(c&&(o=t.length),a&&(!l||c&&l.length===0))throw new Error(s("core.missingRequiredParameter",B(n)));e[i]=l}}const dt={en:{"core.commandExists":'Command "%s" exists.',"core.noSuchCommand":"No such command: %s.","core.noCommandGiven":"No command given.","core.commandNameConflict":"Command name %s conflicts with %s. Maybe caused by alias.","core.nameNotSet":"Name not set.","core.descriptionNotSet":"Description not set.","core.versionNotSet":"Version not set.","core.badNameFormat":"Bad name format: %s.","core.localeMustBeCalledFirst":"locale() or fallbackLocale() must be called at first.","core.cliParseMustBeCalled":"cli.parse() must be called.","core.spreadParameterMustBeLast":"Invalid Parameter: Spread parameter %s must be last.","core.requiredParameterCannotComeAfterOptionalParameter":"Invalid Parameter: Required parameter %s cannot come after optional parameter %s.","core.parameterMustBeWrappedInBrackets":"Invalid Parameter: Parameter %s must be wrapped in <> (required parameter) or [] (optional parameter).","core.parameterIsUsedMoreThanOnce":"Invalid Parameter: Parameter %s is used more than once.","core.missingRequiredParameter":"Missing required parameter %s."},"zh-CN":{"core.commandExists":'\u547D\u4EE4 "%s" \u5DF2\u5B58\u5728\u3002',"core.noSuchCommand":"\u627E\u4E0D\u5230\u547D\u4EE4: %s\u3002","core.noCommandGiven":"\u6CA1\u6709\u8F93\u5165\u547D\u4EE4\u3002","core.commandNameConflict":"\u547D\u4EE4\u540D\u79F0 %s \u548C %s \u51B2\u7A81\u3002 \u53EF\u80FD\u662F\u7531\u4E8E\u522B\u540D\u5BFC\u81F4\u7684\u3002","core.nameNotSet":"\u672A\u8BBE\u7F6ECLI\u540D\u79F0\u3002","core.descriptionNotSet":"\u672A\u8BBE\u7F6ECLI\u63CF\u8FF0\u3002","core.versionNotSet":"\u672A\u8BBE\u7F6ECLI\u7248\u672C\u3002","core.badNameFormat":"\u9519\u8BEF\u7684\u547D\u4EE4\u540D\u5B57\u683C\u5F0F: %s\u3002","core.localeMustBeCalledFirst":"locale() \u6216 fallbackLocale() \u5FC5\u987B\u5728\u6700\u5F00\u59CB\u8C03\u7528\u3002","core.cliParseMustBeCalled":"cli.parse() \u5FC5\u987B\u88AB\u8C03\u7528\u3002","core.spreadParameterMustBeLast":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5C55\u5F00\u53C2\u6570 %s \u5FC5\u987B\u5728\u6700\u540E\u3002","core.requiredParameterCannotComeAfterOptionalParameter":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5FC5\u586B\u53C2\u6570 %s \u4E0D\u80FD\u5728\u53EF\u9009\u53C2\u6570 %s \u4E4B\u540E\u3002","core.parameterMustBeWrappedInBrackets":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u5FC5\u987B\u88AB <> (\u5FC5\u586B\u53C2\u6570) \u6216 [] (\u53EF\u9009\u53C2\u6570) \u5305\u88F9\u3002","core.parameterIsUsedMoreThanOnce":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u88AB\u4F7F\u7528\u4E86\u591A\u6B21\u3002","core.missingRequiredParameter":"\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570 %s\u3002"}};var Q=(e,t,r)=>{if(!t.has(e))throw TypeError("Cannot "+r)},u=(e,t,r)=>(Q(e,t,"read from private field"),r?r.call(e):t.get(e)),d=(e,t,r)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,r)},C=(e,t,r,s)=>(Q(e,t,"write to private field"),s?s.call(e,r):t.set(e,r),r),p=(e,t,r)=>(Q(e,t,"access private method"),r),_,M,F,D,W,j,N,b,P,O,I,x,y,X,Se,Y,ke,ee,Le,w,g,te,De,re,We,se,be,q,ne,oe,Pe;const f=Symbol.for("Clerc.Root"),Oe=class{constructor(e,t,r){d(this,X),d(this,Y),d(this,ee),d(this,w),d(this,te),d(this,re),d(this,se),d(this,q),d(this,oe),d(this,_,""),d(this,M,""),d(this,F,""),d(this,D,[]),d(this,W,{}),d(this,j,new Ie),d(this,N,new Set),d(this,b,void 0),d(this,P,[]),d(this,O,!1),d(this,I,"en"),d(this,x,"en"),d(this,y,{}),this.i18n={add:s=>{C(this,y,st(u(this,y),s))},t:(s,...o)=>{const n=u(this,y)[u(this,x)]||u(this,y)[u(this,I)],a=u(this,y)[u(this,I)];return n[s]?ie(n[s],...o):a[s]?ie(a[s],...o):void 0}},C(this,_,e||u(this,_)),C(this,M,t||u(this,M)),C(this,F,r||u(this,F)),C(this,x,Ae()),p(this,ee,Le).call(this)}get _name(){return u(this,_)}get _description(){return u(this,M)}get _version(){return u(this,F)}get _inspectors(){return u(this,D)}get _commands(){return u(this,W)}static create(e,t,r){return new Oe(e,t,r)}name(e){return p(this,w,g).call(this),C(this,_,e),this}description(e){return p(this,w,g).call(this),C(this,M,e),this}version(e){return p(this,w,g).call(this),C(this,F,e),this}locale(e){if(u(this,O))throw new V(this.i18n.t);return C(this,x,e),this}fallbackLocale(e){if(u(this,O))throw new V(this.i18n.t);return C(this,I,e),this}errorHandler(e){return u(this,P).push(e),this}command(e,t,r={}){return p(this,q,ne).call(this,()=>p(this,te,De).call(this,e,t,r)),this}on(e,t){return u(this,j).on(e,t),this}use(e){return p(this,w,g).call(this),e.setup(this)}inspector(e){return p(this,w,g).call(this),u(this,D).push(e),this}parse(e=z()){p(this,w,g).call(this);const{argv:t,run:r}=Array.isArray(e)?{argv:e,run:!0}:{argv:z(),...e};return C(this,b,[...t]),p(this,re,We).call(this),r&&this.runMatchedCommand(),this}runMatchedCommand(){return p(this,q,ne).call(this,()=>p(this,oe,Pe).call(this)),process.title=u(this,_),this}};let ft=Oe;_=new WeakMap,M=new WeakMap,F=new WeakMap,D=new WeakMap,W=new WeakMap,j=new WeakMap,N=new WeakMap,b=new WeakMap,P=new WeakMap,O=new WeakMap,I=new WeakMap,x=new WeakMap,y=new WeakMap,X=new WeakSet,Se=function(){return u(this,N).has(f)},Y=new WeakSet,ke=function(){return Object.prototype.hasOwnProperty.call(this._commands,f)},ee=new WeakSet,Le=function(){this.i18n.add(dt)},w=new WeakSet,g=function(){C(this,O,!0)},te=new WeakSet,De=function(e,t,r={}){p(this,w,g).call(this);const{t:s}=this.i18n,n=(h=>!(typeof h=="string"||h===f))(e),a=n?e.name:e;if(!Be(a))throw new Ee(a,s);const{handler:c=void 0,...i}=n?e:{name:a,description:t,...r},l=[i.name],m=i.alias?$(i.alias):[];i.alias&&l.push(...m);for(const h of l)if(u(this,N).has(h))throw new he(Ne(h),s);return u(this,W)[a]=i,u(this,N).add(i.name),m.forEach(h=>u(this,N).add(h)),n&&c&&this.on(e.name,c),this},re=new WeakSet,We=function(){const{t:e}=this.i18n;if(!u(this,_))throw new pe(e);if(!u(this,M))throw new Ce(e);if(!u(this,F))throw new we(e)},se=new WeakSet,be=function(e){const t=u(this,b),{t:r}=this.i18n,[s,o]=e(),n=!!s,a=tt((s==null?void 0:s.flags)||{},[...t]),{_:c,flags:i,unknownFlags:l}=a;let m=!n||s.name===f?c:c.slice(s.name.split(" ").length),h=(s==null?void 0:s.parameters)||[];const E=h.indexOf("--"),A=h.slice(E+1)||[],v=Object.create(null);if(E>-1&&A.length>0){h=h.slice(0,E);const ae=c["--"];m=m.slice(0,-ae.length||void 0),Z(v,K(h,r),m,r),Z(v,K(A,r),ae,r)}else Z(v,K(h,r),m,r);const T={...i,...l};return{name:s==null?void 0:s.name,called:Array.isArray(o)?o.join(" "):o,resolved:n,hasRootOrAlias:u(this,X,Se),hasRoot:u(this,Y,ke),raw:{...a,parameters:m,mergedFlags:T},parameters:v,flags:i,unknownFlags:l,cli:this}},q=new WeakSet,ne=function(e){try{e()}catch(t){if(u(this,P).length>0)u(this,P).forEach(r=>r(t));else throw t}},oe=new WeakSet,Pe=function(){p(this,w,g).call(this);const{t:e}=this.i18n,t=u(this,b);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const r=Fe(t),s=r.join(" "),o=()=>ve(u(this,W),r,e),n=()=>p(this,se,be).call(this,o),a={enforce:"post",fn:()=>{const[l]=o(),m=n();if(!l)throw s?new de(s,e):new fe(e);u(this,j).emit(l.name,m)}},c=[...u(this,D),a];ye(c)(n)};const mt=e=>e,pt=(e,t,r)=>r,Ct=(e,t)=>t,wt=(e,t)=>({...e,handler:t});export{ft as Clerc,he as CommandExistsError,me as CommandNameConflictError,Ce as DescriptionNotSetError,Ee as InvalidCommandNameError,V as LocaleNotCalledFirstError,pe as NameNotSetError,fe as NoCommandGivenError,de as NoSuchCommandError,f as Root,we as VersionNotSetError,ye as compose,wt as defineCommand,pt as defineHandler,Ct as defineInspector,mt as definePlugin,Ae as detectLocale,Ne as formatCommandName,Be as isValidName,z as resolveArgv,ve as resolveCommand,it as resolveCommandStrict,J as resolveFlattenCommands,Fe as resolveParametersBeforeFlag,ct as resolveRootCommands,Me as resolveSubcommandsByParent,lt as withBrackets};
|
|
1
|
+
import{format as ie}from"node:util";class Ie{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(t,r){return t==="*"?(this.wildcardListeners.add(r),this):(this.listenerMap[t]||(this.listenerMap[t]=new Set),this.listenerMap[t].add(r),this)}emit(t,...r){return this.listenerMap[t]&&(this.wildcardListeners.forEach(s=>s(t,...r)),this.listenerMap[t].forEach(s=>s(...r))),this}off(t,r){var s,n;return t==="**"?(this.listenerMap={},this.wildcardListeners.clear(),this):t==="*"?(r?this.wildcardListeners.delete(r):this.wildcardListeners.clear(),this):(r?(s=this.listenerMap[t])==null||s.delete(r):(n=this.listenerMap[t])==null||n.clear(),this)}}const xe="known-flag",Re="unknown-flag",$e="argument",{stringify:S}=JSON,je=/\B([A-Z])/g,qe=e=>e.replace(je,"-$1").toLowerCase(),{hasOwnProperty:Te}=Object.prototype,k=(e,t)=>Te.call(e,t),Ge=e=>Array.isArray(e),ce=e=>typeof e=="function"?[e,!1]:Ge(e)?[e[0],!0]:ce(e.type),He=(e,t)=>e===Boolean?t!=="false":t,Ue=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),Ve=/[\s.:=]/,Je=e=>{const t=`Flag name ${S(e)}`;if(e.length===0)throw new Error(`${t} cannot be empty`);if(e.length===1)throw new Error(`${t} must be longer than a character`);const r=e.match(Ve);if(r)throw new Error(`${t} cannot contain ${S(r==null?void 0:r[0])}`)},ze=e=>{const t={},r=(s,n)=>{if(k(t,s))throw new Error(`Duplicate flags named ${S(s)}`);t[s]=n};for(const s in e){if(!k(e,s))continue;Je(s);const n=e[s],o=[[],...ce(n),n];r(s,o);const a=qe(s);if(s!==a&&r(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:i}=n,c=`Flag alias ${S(i)} for flag ${S(s)}`;if(i.length===0)throw new Error(`${c} cannot be empty`);if(i.length>1)throw new Error(`${c} must be a single character`);r(i,o)}}return t},Ke=(e,t)=>{const r={};for(const s in e){if(!k(e,s))continue;const[n,,o,a]=t[s];if(n.length===0&&"default"in a){let{default:i}=a;typeof i=="function"&&(i=i()),r[s]=i}else r[s]=o?n:n.pop()}return r},R="--",Ze=/[.:=]/,Qe=/^-{1,2}\w/,Xe=e=>{if(!Qe.test(e))return;const t=!e.startsWith(R);let r=e.slice(t?1:2),s;const n=r.match(Ze);if(n){const{index:o}=n;s=r.slice(o+1),r=r.slice(0,o)}return[r,s,t]},Ye=(e,{onFlag:t,onArgument:r})=>{let s;const n=(o,a)=>{if(typeof s!="function")return!0;s(o,a),s=void 0};for(let o=0;o<e.length;o+=1){const a=e[o];if(a===R){n();const c=e.slice(o+1);r==null||r(c,[o],!0);break}const i=Xe(a);if(i){if(n(),!t)continue;const[c,l,m]=i;if(m)for(let h=0;h<c.length;h+=1){n();const E=h===c.length-1;s=t(c[h],E?l:void 0,[o,h+1,E])}else s=t(c,l,[o])}else n(a,[o])&&(r==null||r([a],[o]))}n()},et=(e,t)=>{for(const[r,s,n]of t.reverse()){if(s){const o=e[r];let a=o.slice(0,s);if(n||(a+=o.slice(s+1)),a!=="-"){e[r]=a;continue}}e.splice(r,1)}},tt=(e,t=process.argv.slice(2),{ignore:r}={})=>{const s=[],n=ze(e),o={},a=[];return a[R]=[],Ye(t,{onFlag(i,c,l){const m=k(n,i);if(!(r!=null&&r(m?xe:Re,i,c))){if(m){const[h,E]=n[i],A=He(E,c),v=(T,G)=>{s.push(l),G&&s.push(G),h.push(Ue(E,T||""))};return A===void 0?v:v(A)}k(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),s.push(l)}},onArgument(i,c,l){r!=null&&r($e,t[c[0]])||(a.push(...i),l?(a[R]=i,t.splice(c[0])):s.push(c))}}),et(t,s),{flags:Ke(e,n),unknownFlags:o,_:a}};function H(e){return e!==null&&typeof e=="object"}function U(e,t,r=".",s){if(!H(t))return U(e,{},r,s);const n=Object.assign({},t);for(const o in e){if(o==="__proto__"||o==="constructor")continue;const a=e[o];a!=null&&(s&&s(n,o,a,r)||(Array.isArray(a)&&Array.isArray(n[o])?n[o]=[...a,...n[o]]:H(a)&&H(n[o])?n[o]=U(a,n[o],(r?`${r}.`:"")+o.toString(),s):n[o]=a))}return n}function rt(e){return(...t)=>t.reduce((r,s)=>U(r,s,"",e),{})}const st=rt(),$=e=>Array.isArray(e)?e:[e],ot=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,r)=>r?r.toUpperCase():""),ue=(e,t)=>t.length!==e.length?!1:e.every((r,s)=>r===t[s]),le=(e,t)=>t.length>e.length?!1:ue(e.slice(0,t.length),t),L=JSON.stringify;class he extends Error{constructor(t,r){super(r("core.commandExists",L(t))),this.commandName=t}}class de extends Error{constructor(t,r){super(r("core.noSuchCommand",L(t))),this.commandName=t}}class fe extends Error{constructor(t){super(t("core.noCommandGiven"))}}class me extends Error{constructor(t,r,s){super(s("core.commandNameConflict",L(t),L(r))),this.n1=t,this.n2=r}}class pe extends Error{constructor(t){super(t("core.nameNotSet"))}}class Ce extends Error{constructor(t){super(t("core.descriptionNotSet"))}}class we extends Error{constructor(t){super(t("core.versionNotSet"))}}class Ee extends Error{constructor(t,r){super(r("core.badNameFormat",L(t))),this.commandName=t}}class V extends Error{constructor(t){super(t("core.localeMustBeCalledFirst"))}}const ge=typeof Deno!="undefined",nt=typeof process!="undefined"&&!ge,at=process.versions.electron&&!process.defaultApp;function _e(e,t,r,s){if(r.alias){const n=$(r.alias);for(const o of n){if(o in t)throw new me(t[o].name,r.name,s);e.set(typeof o=="symbol"?o:o.split(" "),{...r,__isAlias:!0})}}}function J(e,t){const r=new Map;e[f]&&(r.set(f,e[f]),_e(r,e,e[f],t));for(const s of Object.values(e))_e(r,e,s,t),r.set(s.name.split(" "),s);return r}function ve(e,t,r){if(t===f)return[e[f],f];const s=$(t),n=J(e,r);let o,a;return n.forEach((i,c)=>{if(c===f){o=e[f],a=f;return}le(s,c)&&(!a||a===f||c.length>a.length)&&(o=i,a=c)}),[o,a]}function it(e,t,r){if(t===f)return[e[f],f];const s=$(t),n=J(e,r);let o,a;return n.forEach((i,c)=>{c===f||a===f||ue(s,c)&&(o=i,a=c)}),[o,a]}function Me(e,t,r=1/0){const s=t===""?[]:Array.isArray(t)?t:t.split(" ");return Object.values(e).filter(n=>{const o=n.name.split(" ");return le(o,s)&&o.length-s.length<=r})}const ct=e=>Me(e,"",1);function Fe(e){const t=[];for(const r of e){if(r.startsWith("-"))break;t.push(r)}return t}const z=()=>nt?process.argv.slice(at?1:2):ge?Deno.args:[];function ye(e){const t={pre:[],normal:[],post:[]};for(const s of e){const n=typeof s=="object"?s:{fn:s},{enforce:o,fn:a}=n;o==="post"||o==="pre"?t[o].push(a):t.normal.push(a)}const r=[...t.pre,...t.normal,...t.post];return s=>{const n=[];let o=0;const a=i=>{o=i;const c=r[i],l=c(s(),a.bind(null,i+1));l&&n.push(l)};if(a(0),o+1===r.length)for(const i of n)i()}}const ut=/\s\s+/,Be=e=>e===f?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!ut.test(e),lt=(e,t)=>t?`[${e}]`:`<${e}>`,ht="<Root>",Ne=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:ht,Ae=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,{stringify:B}=JSON;function K(e,t){const r=[];let s,n;for(const o of e){if(n)throw new Error(t("core.spreadParameterMustBeLast",B(n)));const a=o[0],i=o[o.length-1];let c;if(a==="<"&&i===">"&&(c=!0,s))throw new Error(t("core.requiredParameterMustBeBeforeOptional",B(o),B(s)));if(a==="["&&i==="]"&&(c=!1,s=o),c===void 0)throw new Error(t("core.parameterMustBeWrappedInBrackets",B(o)));let l=o.slice(1,-1);const m=l.slice(-3)==="...";m&&(n=o,l=l.slice(0,-3)),r.push({name:l,required:c,spread:m})}return r}function Z(e,t,r,s){for(let n=0;n<t.length;n+=1){const{name:o,required:a,spread:i}=t[n],c=ot(o);if(c in e)throw new Error(s("core.parameterIsUsedMoreThanOnce",B(o)));const l=i?r.slice(n):r[n];if(i&&(n=t.length),a&&(!l||i&&l.length===0))throw new Error(s("core.missingRequiredParameter",B(o)));e[c]=l}}const dt={en:{"core.commandExists":'Command "%s" exists.',"core.noSuchCommand":"No such command: %s.","core.noCommandGiven":"No command given.","core.commandNameConflict":"Command name %s conflicts with %s. Maybe caused by alias.","core.nameNotSet":"Name not set.","core.descriptionNotSet":"Description not set.","core.versionNotSet":"Version not set.","core.badNameFormat":"Bad name format: %s.","core.localeMustBeCalledFirst":"locale() or fallbackLocale() must be called at first.","core.cliParseMustBeCalled":"cli.parse() must be called.","core.spreadParameterMustBeLast":"Invalid Parameter: Spread parameter %s must be last.","core.requiredParameterCannotComeAfterOptionalParameter":"Invalid Parameter: Required parameter %s cannot come after optional parameter %s.","core.parameterMustBeWrappedInBrackets":"Invalid Parameter: Parameter %s must be wrapped in <> (required parameter) or [] (optional parameter).","core.parameterIsUsedMoreThanOnce":"Invalid Parameter: Parameter %s is used more than once.","core.missingRequiredParameter":"Missing required parameter %s."},"zh-CN":{"core.commandExists":'\u547D\u4EE4 "%s" \u5DF2\u5B58\u5728\u3002',"core.noSuchCommand":"\u627E\u4E0D\u5230\u547D\u4EE4: %s\u3002","core.noCommandGiven":"\u6CA1\u6709\u8F93\u5165\u547D\u4EE4\u3002","core.commandNameConflict":"\u547D\u4EE4\u540D\u79F0 %s \u548C %s \u51B2\u7A81\u3002 \u53EF\u80FD\u662F\u7531\u4E8E\u522B\u540D\u5BFC\u81F4\u7684\u3002","core.nameNotSet":"\u672A\u8BBE\u7F6ECLI\u540D\u79F0\u3002","core.descriptionNotSet":"\u672A\u8BBE\u7F6ECLI\u63CF\u8FF0\u3002","core.versionNotSet":"\u672A\u8BBE\u7F6ECLI\u7248\u672C\u3002","core.badNameFormat":"\u9519\u8BEF\u7684\u547D\u4EE4\u540D\u5B57\u683C\u5F0F: %s\u3002","core.localeMustBeCalledFirst":"locale() \u6216 fallbackLocale() \u5FC5\u987B\u5728\u6700\u5F00\u59CB\u8C03\u7528\u3002","core.cliParseMustBeCalled":"cli.parse() \u5FC5\u987B\u88AB\u8C03\u7528\u3002","core.spreadParameterMustBeLast":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5C55\u5F00\u53C2\u6570 %s \u5FC5\u987B\u5728\u6700\u540E\u3002","core.requiredParameterCannotComeAfterOptionalParameter":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5FC5\u586B\u53C2\u6570 %s \u4E0D\u80FD\u5728\u53EF\u9009\u53C2\u6570 %s \u4E4B\u540E\u3002","core.parameterMustBeWrappedInBrackets":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u5FC5\u987B\u88AB <> (\u5FC5\u586B\u53C2\u6570) \u6216 [] (\u53EF\u9009\u53C2\u6570) \u5305\u88F9\u3002","core.parameterIsUsedMoreThanOnce":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u88AB\u4F7F\u7528\u4E86\u591A\u6B21\u3002","core.missingRequiredParameter":"\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570 %s\u3002"}};var Q=(e,t,r)=>{if(!t.has(e))throw TypeError("Cannot "+r)},u=(e,t,r)=>(Q(e,t,"read from private field"),r?r.call(e):t.get(e)),d=(e,t,r)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,r)},C=(e,t,r,s)=>(Q(e,t,"write to private field"),s?s.call(e,r):t.set(e,r),r),p=(e,t,r)=>(Q(e,t,"access private method"),r),_,M,F,D,W,j,N,b,P,O,I,x,y,X,Se,Y,ke,ee,Le,w,g,te,De,re,We,se,be,q,oe,ne,Pe;const f=Symbol.for("Clerc.Root"),Oe=class{constructor(e,t,r){d(this,X),d(this,Y),d(this,ee),d(this,w),d(this,te),d(this,re),d(this,se),d(this,q),d(this,ne),d(this,_,""),d(this,M,""),d(this,F,""),d(this,D,[]),d(this,W,{}),d(this,j,new Ie),d(this,N,new Set),d(this,b,void 0),d(this,P,[]),d(this,O,!1),d(this,I,"en"),d(this,x,"en"),d(this,y,{}),this.i18n={add:s=>{C(this,y,st(u(this,y),s))},t:(s,...n)=>{const o=u(this,y)[u(this,x)]||u(this,y)[u(this,I)],a=u(this,y)[u(this,I)];return o[s]?ie(o[s],...n):a[s]?ie(a[s],...n):void 0}},C(this,_,e||u(this,_)),C(this,M,t||u(this,M)),C(this,F,r||u(this,F)),C(this,x,Ae()),p(this,ee,Le).call(this)}get _name(){return u(this,_)}get _description(){return u(this,M)}get _version(){return u(this,F)}get _inspectors(){return u(this,D)}get _commands(){return u(this,W)}static create(e,t,r){return new Oe(e,t,r)}name(e){return p(this,w,g).call(this),C(this,_,e),this}description(e){return p(this,w,g).call(this),C(this,M,e),this}version(e){return p(this,w,g).call(this),C(this,F,e),this}locale(e){if(u(this,O))throw new V(this.i18n.t);return C(this,x,e),this}fallbackLocale(e){if(u(this,O))throw new V(this.i18n.t);return C(this,I,e),this}errorHandler(e){return u(this,P).push(e),this}command(e,t,r={}){return p(this,q,oe).call(this,()=>p(this,te,De).call(this,e,t,r)),this}on(e,t){return u(this,j).on(e,t),this}use(e){return p(this,w,g).call(this),e.setup(this)}inspector(e){return p(this,w,g).call(this),u(this,D).push(e),this}parse(e=z()){p(this,w,g).call(this);const{argv:t,run:r}=Array.isArray(e)?{argv:e,run:!0}:{argv:z(),...e};return C(this,b,[...t]),p(this,re,We).call(this),r&&this.runMatchedCommand(),this}runMatchedCommand(){return p(this,q,oe).call(this,()=>p(this,ne,Pe).call(this)),process.title=u(this,_),this}};let ft=Oe;_=new WeakMap,M=new WeakMap,F=new WeakMap,D=new WeakMap,W=new WeakMap,j=new WeakMap,N=new WeakMap,b=new WeakMap,P=new WeakMap,O=new WeakMap,I=new WeakMap,x=new WeakMap,y=new WeakMap,X=new WeakSet,Se=function(){return u(this,N).has(f)},Y=new WeakSet,ke=function(){return Object.prototype.hasOwnProperty.call(this._commands,f)},ee=new WeakSet,Le=function(){this.i18n.add(dt)},w=new WeakSet,g=function(){C(this,O,!0)},te=new WeakSet,De=function(e,t,r={}){p(this,w,g).call(this);const{t:s}=this.i18n,o=(h=>!(typeof h=="string"||h===f))(e),a=o?e.name:e;if(!Be(a))throw new Ee(a,s);const{handler:i=void 0,...c}=o?e:{name:a,description:t,...r},l=[c.name],m=c.alias?$(c.alias):[];c.alias&&l.push(...m);for(const h of l)if(u(this,N).has(h))throw new he(Ne(h),s);return u(this,W)[a]=c,u(this,N).add(c.name),m.forEach(h=>u(this,N).add(h)),o&&i&&this.on(e.name,i),this},re=new WeakSet,We=function(){const{t:e}=this.i18n;if(!u(this,_))throw new pe(e);if(!u(this,M))throw new Ce(e);if(!u(this,F))throw new we(e)},se=new WeakSet,be=function(e){const t=u(this,b),{t:r}=this.i18n,[s,n]=e(),o=!!s,a=tt((s==null?void 0:s.flags)||{},[...t]),{_:i,flags:c,unknownFlags:l}=a;let m=!o||s.name===f?i:i.slice(s.name.split(" ").length),h=(s==null?void 0:s.parameters)||[];const E=h.indexOf("--"),A=h.slice(E+1)||[],v=Object.create(null);if(E>-1&&A.length>0){h=h.slice(0,E);const ae=i["--"];m=m.slice(0,-ae.length||void 0),Z(v,K(h,r),m,r),Z(v,K(A,r),ae,r)}else Z(v,K(h,r),m,r);const T={...c,...l};return{name:s==null?void 0:s.name,called:Array.isArray(n)?n.join(" "):n,resolved:o,hasRootOrAlias:u(this,X,Se),hasRoot:u(this,Y,ke),raw:{...a,parameters:m,mergedFlags:T},parameters:v,flags:c,unknownFlags:l,cli:this}},q=new WeakSet,oe=function(e){try{e()}catch(t){if(u(this,P).length>0)u(this,P).forEach(r=>r(t));else throw t}},ne=new WeakSet,Pe=function(){p(this,w,g).call(this);const{t:e}=this.i18n,t=u(this,b);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const r=Fe(t),s=r.join(" "),n=()=>ve(u(this,W),r,e),o=()=>p(this,se,be).call(this,n),a={enforce:"post",fn:()=>{const[l]=n(),m=o();if(!l)throw s?new de(s,e):new fe(e);u(this,j).emit(l.name,m)}},i=[...u(this,D),a];ye(i)(o)};const mt=e=>e,pt=(e,t,r)=>r,Ct=(e,t)=>t,wt=(e,t)=>({...e,handler:t});export{ft as Clerc,he as CommandExistsError,me as CommandNameConflictError,Ce as DescriptionNotSetError,Ee as InvalidCommandNameError,V as LocaleNotCalledFirstError,pe as NameNotSetError,fe as NoCommandGivenError,de as NoSuchCommandError,f as Root,we as VersionNotSetError,ye as compose,wt as defineCommand,pt as defineHandler,Ct as defineInspector,mt as definePlugin,Ae as detectLocale,Ne as formatCommandName,Be as isValidName,z as resolveArgv,ve as resolveCommand,it as resolveCommandStrict,J as resolveFlattenCommands,Fe as resolveParametersBeforeFlag,ct as resolveRootCommands,Me as resolveSubcommandsByParent,lt as withBrackets};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "0.32.
|
|
3
|
+
"version": "0.32.3",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc core",
|
|
6
6
|
"keywords": [
|
|
@@ -50,9 +50,9 @@
|
|
|
50
50
|
"defu": "^6.1.2",
|
|
51
51
|
"is-platform": "^1.0.0",
|
|
52
52
|
"lite-emit": "^1.4.0",
|
|
53
|
-
"type-fest": "^3.
|
|
53
|
+
"type-fest": "^3.6.0",
|
|
54
54
|
"type-flag": "^3.0.0",
|
|
55
|
-
"@clerc/utils": "0.32.
|
|
55
|
+
"@clerc/utils": "0.32.3"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "puild --minify",
|