@clerc/core 0.29.0 → 0.30.1

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 CHANGED
@@ -298,7 +298,6 @@ interface ParseOptions {
298
298
  argv?: string[];
299
299
  run?: boolean;
300
300
  }
301
- type PossibleInputKind = string | number | boolean | Dict<any>;
302
301
  interface HandlerContext<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> {
303
302
  name?: LiteralUnion<N, string>;
304
303
  called?: string | RootType;
@@ -387,7 +386,7 @@ declare class Clerc<C extends CommandRecord = {}> {
387
386
  version(version: string): this;
388
387
  /**
389
388
  * Set the Locale
390
- * It's recommended to call this method once after you created the Clerc instance.
389
+ * You must call this method once after you created the Clerc instance.
391
390
  * @param locale
392
391
  * @returns
393
392
  * @example
@@ -400,7 +399,7 @@ declare class Clerc<C extends CommandRecord = {}> {
400
399
  locale(locale: string): this;
401
400
  /**
402
401
  * Set the fallback Locale
403
- * It's recommended to call this method once after you created the Clerc instance.
402
+ * You must call this method once after you created the Clerc instance.
404
403
  * @param fallbackLocale
405
404
  * @returns
406
405
  * @example
@@ -411,6 +410,17 @@ declare class Clerc<C extends CommandRecord = {}> {
411
410
  * ```
412
411
  */
413
412
  fallbackLocale(fallbackLocale: string): this;
413
+ /**
414
+ * Register a error handler
415
+ * @param handler
416
+ * @returns
417
+ * @example
418
+ * ```ts
419
+ * Clerc.create()
420
+ * .errorHandler((err) => { console.log(err); })
421
+ * ```
422
+ */
423
+ errorHandler(handler: (err: any) => void): this;
414
424
  /**
415
425
  * Register a command
416
426
  * @param name
@@ -557,9 +567,9 @@ declare const resolveRootCommands: (commands: CommandRecord) => Command<string,
557
567
  declare function resolveParametersBeforeFlag(argv: string[]): string[];
558
568
  declare const resolveArgv: () => string[];
559
569
  declare function compose(inspectors: Inspector[]): (getCtx: () => InspectorContext) => void;
560
- declare const isInvalidName: (name: CommandType) => boolean;
570
+ declare const isValidName: (name: CommandType) => boolean;
561
571
  declare const withBrackets: (s: string, isOptional?: boolean) => string;
562
572
  declare const formatCommandName: (name: string | string[] | RootType) => string;
563
573
  declare const detectLocale: () => string;
564
574
 
565
- export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandRecord, CommandType, CommandWithHandler, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, PossibleInputKind, Root, RootType, TranslateFn, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, detectLocale, formatCommandName, isInvalidName, resolveArgv, resolveCommand, resolveCommandStrict, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
575
+ export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandRecord, CommandType, CommandWithHandler, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, Root, RootType, TranslateFn, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveCommandStrict, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{format as nt}from"node:util";class Dt{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(e,s){return e==="*"?(this.wildcardListeners.add(s),this):(this.listenerMap[e]||(this.listenerMap[e]=new Set),this.listenerMap[e].add(s),this)}emit(e,...s){return this.listenerMap[e]&&(this.wildcardListeners.forEach(r=>r(e,...s)),this.listenerMap[e].forEach(r=>r(...s))),this}off(e,s){var r,n;return e==="**"?(this.listenerMap={},this.wildcardListeners.clear(),this):e==="*"?(s?this.wildcardListeners.delete(s):this.wildcardListeners.clear(),this):(s?(r=this.listenerMap[e])==null||r.delete(s):(n=this.listenerMap[e])==null||n.clear(),this)}}const Ot="known-flag",Wt="unknown-flag",xt="argument",{stringify:B}=JSON,It=/\B([A-Z])/g,$t=t=>t.replace(It,"-$1").toLowerCase(),{hasOwnProperty:Rt}=Object.prototype,D=(t,e)=>Rt.call(t,e),jt=t=>Array.isArray(t),it=t=>typeof t=="function"?[t,!1]:jt(t)?[t[0],!0]:it(t.type),Pt=(t,e)=>t===Boolean?e!=="false":e,Tt=(t,e)=>typeof e=="boolean"?e:t===Number&&e===""?Number.NaN:t(e),Gt=/[\s.:=]/,qt=t=>{const e=`Flag name ${B(t)}`;if(t.length===0)throw new Error(`${e} cannot be empty`);if(t.length===1)throw new Error(`${e} must be longer than a character`);const s=t.match(Gt);if(s)throw new Error(`${e} cannot contain ${B(s==null?void 0:s[0])}`)},Vt=t=>{const e={},s=(r,n)=>{if(D(e,r))throw new Error(`Duplicate flags named ${B(r)}`);e[r]=n};for(const r in t){if(!D(t,r))continue;qt(r);const n=t[r],o=[[],...it(n),n];s(r,o);const i=$t(r);if(r!==i&&s(i,o),"alias"in n&&typeof n.alias=="string"){const{alias:c}=n,a=`Flag alias ${B(c)} for flag ${B(r)}`;if(c.length===0)throw new Error(`${a} cannot be empty`);if(c.length>1)throw new Error(`${a} must be a single character`);s(c,o)}}return e},Ht=(t,e)=>{const s={};for(const r in t){if(!D(t,r))continue;const[n,,o,i]=e[r];if(n.length===0&&"default"in i){let{default:c}=i;typeof c=="function"&&(c=c()),s[r]=c}else s[r]=o?n:n.pop()}return s},P="--",Jt=/[.:=]/,Ut=/^-{1,2}\w/,zt=t=>{if(!Ut.test(t))return;const e=!t.startsWith(P);let s=t.slice(e?1:2),r;const n=s.match(Jt);if(n){const{index:o}=n;r=s.slice(o+1),s=s.slice(0,o)}return[s,r,e]},Kt=(t,{onFlag:e,onArgument:s})=>{let r;const n=(o,i)=>{if(typeof r!="function")return!0;r(o,i),r=void 0};for(let o=0;o<t.length;o+=1){const i=t[o];if(i===P){n();const a=t.slice(o+1);s==null||s(a,[o],!0);break}const c=zt(i);if(c){if(n(),!e)continue;const[a,p,f]=c;if(f)for(let u=0;u<a.length;u+=1){n();const w=u===a.length-1;r=e(a[u],w?p:void 0,[o,u+1,w])}else r=e(a,p,[o])}else n(i,[o])&&(s==null||s([i],[o]))}n()},Zt=(t,e)=>{for(const[s,r,n]of e.reverse()){if(r){const o=t[s];let i=o.slice(0,r);if(n||(i+=o.slice(r+1)),i!=="-"){t[s]=i;continue}}t.splice(s,1)}},Qt=(t,e=process.argv.slice(2),{ignore:s}={})=>{const r=[],n=Vt(t),o={},i=[];return i[P]=[],Kt(e,{onFlag(c,a,p){const f=D(n,c);if(!(s!=null&&s(f?Ot:Wt,c,a))){if(f){const[u,w]=n[c],L=Pt(w,a),M=(R,k)=>{r.push(p),k&&r.push(k),u.push(Tt(w,R||""))};return L===void 0?M:M(L)}D(o,c)||(o[c]=[]),o[c].push(a===void 0?!0:a),r.push(p)}},onArgument(c,a,p){s!=null&&s(xt,e[a[0]])||(i.push(...c),p?(i[P]=c,e.splice(a[0])):r.push(a))}}),Zt(e,r),{flags:Ht(t,n),unknownFlags:o,_:i}};function H(t){return t!==null&&typeof t=="object"}function J(t,e,s=".",r){if(!H(e))return J(t,{},s,r);const n=Object.assign({},e);for(const o in t){if(o==="__proto__"||o==="constructor")continue;const i=t[o];i!=null&&(r&&r(n,o,i,s)||(Array.isArray(i)&&Array.isArray(n[o])?n[o]=[...i,...n[o]]:H(i)&&H(n[o])?n[o]=J(i,n[o],(s?`${s}.`:"")+o.toString(),r):n[o]=i))}return n}function Xt(t){return(...e)=>e.reduce((s,r)=>J(s,r,"",t),{})}const Yt=Xt(),T=t=>Array.isArray(t)?t:[t],te=t=>t.replace(/[-_ ](\w)/g,(e,s)=>s.toUpperCase()),at=(t,e)=>e.length!==t.length?!1:t.every((s,r)=>s===e[r]),ct=(t,e)=>e.length>t.length?!1:at(t.slice(0,e.length),e);class lt extends Error{constructor(e,s){super(s("core.commandExists",e)),this.commandName=e}}class ut extends Error{constructor(e,s){super(s("core.noSuchCommand",e)),this.commandName=e}}class ht extends Error{constructor(e){super(e("core.noCommandGiven"))}}class ft extends Error{constructor(e,s,r){super(r("core.commandNameConflict",e,s)),this.n1=e,this.n2=s}}class dt extends Error{constructor(e){super(e("core.nameNotSet"))}}class pt extends Error{constructor(e){super(e("core.descriptionNotSet"))}}class mt extends Error{constructor(e){super(e("core.versionNotSet"))}}class wt extends Error{constructor(e,s){super(s("core.badNameFormat",e)),this.commandName=e}}class U extends Error{constructor(e){super(e("core.localeMustBeCalledFirst"))}}const gt=typeof Deno!="undefined",ee=typeof process!="undefined"&&!gt,se=process.versions.electron&&!process.defaultApp;function Et(t,e,s,r){if(s.alias){const n=T(s.alias);for(const o of n){if(o in e)throw new ft(e[o].name,s.name,r);t.set(typeof o=="symbol"?o:o.split(" "),{...s,__isAlias:!0})}}}function z(t,e){const s=new Map;t[h]&&(s.set(h,t[h]),Et(s,t,t[h],e));for(const r of Object.values(t))Et(s,t,r,e),s.set(r.name.split(" "),r);return s}function Ct(t,e,s){if(e===h)return[t[h],h];const r=T(e),n=z(t,s);let o,i;return n.forEach((c,a)=>{if(a===h){o=t[h],i=h;return}ct(r,a)&&(!i||i===h||a.length>i.length)&&(o=c,i=a)}),[o,i]}function re(t,e,s){if(e===h)return[t[h],h];const r=T(e),n=z(t,s);let o,i;return n.forEach((c,a)=>{a===h||i===h||at(r,a)&&(o=c,i=a)}),[o,i]}function vt(t,e,s=1/0){const r=e===""?[]:Array.isArray(e)?e:e.split(" ");return Object.values(t).filter(n=>{const o=n.name.split(" ");return ct(o,r)&&o.length-r.length<=s})}const oe=t=>vt(t,"",1);function _t(t){const e=[];for(const s of t){if(s.startsWith("-"))break;e.push(s)}return e}const K=()=>ee?process.argv.slice(se?1:2):gt?Deno.args:[];function yt(t){const e={pre:[],normal:[],post:[]};for(const r of t){const n=typeof r=="object"?r:{fn:r},{enforce:o,fn:i}=n;o==="post"||o==="pre"?e[o].push(i):e.normal.push(i)}const s=[...e.pre,...e.normal,...e.post];return r=>{return n(0);function n(o){const i=s[o];return i(r(),n.bind(null,o+1))}}}const Nt=t=>typeof t=="string"&&(t.startsWith(" ")||t.endsWith(" ")),ne=(t,e)=>e?`[${t}]`:`<${t}>`,ie="<Root>",Mt=t=>Array.isArray(t)?t.join(" "):typeof t=="string"?t:ie,Ft=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,{stringify:S}=JSON;function Z(t){const e=[];let s,r;for(const n of t){if(r)throw new Error(`Invalid parameter: Spread parameter ${S(r)} must be last`);const o=n[0],i=n[n.length-1];let c;if(o==="<"&&i===">"&&(c=!0,s))throw new Error(`Invalid parameter: Required parameter ${S(n)} cannot come after optional parameter ${S(s)}`);if(o==="["&&i==="]"&&(c=!1,s=n),c===void 0)throw new Error(`Invalid parameter: ${S(n)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let a=n.slice(1,-1);const p=a.slice(-3)==="...";p&&(r=n,a=a.slice(0,-3)),e.push({name:a,required:c,spread:p})}return e}function Q(t,e,s){for(let r=0;r<e.length;r+=1){const{name:n,required:o,spread:i}=e[r],c=te(n);if(c in t)return new Error(`Invalid parameter: ${S(n)} is used more than once.`);const a=i?s.slice(r):s[r];if(i&&(r=e.length),o&&(!a||i&&a.length===0))return new Error(`Error: Missing required parameter ${S(n)}`);t[c]=a}}const ae={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."},"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"}};var X=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},l=(t,e,s)=>(X(t,e,"read from private field"),s?s.call(t):e.get(t)),d=(t,e,s)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,s)},m=(t,e,s,r)=>(X(t,e,"write to private field"),r?r.call(t,s):e.set(t,s),s),g=(t,e,s)=>(X(t,e,"access private method"),s),v,_,y,O,W,G,A,q,x,I,$,N,Y,St,tt,At,et,Lt,E,C,st,kt;const h=Symbol("Root"),bt=class{constructor(t,e,s){d(this,Y),d(this,tt),d(this,et),d(this,E),d(this,st),d(this,v,""),d(this,_,""),d(this,y,""),d(this,O,[]),d(this,W,{}),d(this,G,new Dt),d(this,A,new Set),d(this,q,void 0),d(this,x,!1),d(this,I,"en"),d(this,$,"en"),d(this,N,{}),this.i18n={add:r=>{m(this,N,Yt(l(this,N),r))},t:(r,...n)=>{const o=l(this,N)[l(this,$)]||l(this,N)[l(this,I)],i=l(this,N)[l(this,I)];return o[r]?nt(o[r],...n):i[r]?nt(i[r],...n):void 0}},m(this,v,t||l(this,v)),m(this,_,e||l(this,_)),m(this,y,s||l(this,y)),m(this,$,Ft()),g(this,et,Lt).call(this)}get _name(){return l(this,v)}get _description(){return l(this,_)}get _version(){return l(this,y)}get _inspectors(){return l(this,O)}get _commands(){return l(this,W)}static create(t,e,s){return new bt(t,e,s)}name(t){return g(this,E,C).call(this),m(this,v,t),this}description(t){return g(this,E,C).call(this),m(this,_,t),this}version(t){return g(this,E,C).call(this),m(this,y,t),this}locale(t){if(l(this,x))throw new U(this.i18n.t);return m(this,$,t),this}fallbackLocale(t){if(l(this,x))throw new U(this.i18n.t);return m(this,I,t),this}command(t,e,s={}){g(this,E,C).call(this);const{t:r}=this.i18n,o=(u=>!(typeof u=="string"||u===h))(t),i=o?t.name:t;if(Nt(i))throw new wt(i,r);const{handler:c=void 0,...a}=o?t:{name:i,description:e,...s},p=[a.name],f=a.alias?T(a.alias):[];a.alias&&p.push(...f);for(const u of p)if(l(this,A).has(u))throw new lt(Mt(u),r);return l(this,W)[i]=a,l(this,A).add(a.name),f.forEach(u=>l(this,A).add(u)),o&&c&&this.on(t.name,c),this}on(t,e){return l(this,G).on(t,e),this}use(t){return g(this,E,C).call(this),t.setup(this)}inspector(t){return g(this,E,C).call(this),l(this,O).push(t),this}parse(t=K()){g(this,E,C).call(this);const{argv:e,run:s}=Array.isArray(t)?{argv:t,run:!0}:{argv:K(),...t};return m(this,q,e),g(this,st,kt).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){g(this,E,C).call(this);const{t}=this.i18n,e=l(this,q);if(!e)throw new Error("cli.parse() must be called.");const s=_t(e),r=s.join(" "),n=()=>Ct(l(this,W),s,t),o=[],i=()=>{o.length=0;const[f,u]=n(),w=!!f,L=Qt((f==null?void 0:f.flags)||{},[...e]),{_:M,flags:R,unknownFlags:k}=L;let b=!w||f.name===h?M:M.slice(f.name.split(" ").length),F=(f==null?void 0:f.parameters)||[];const V=F.indexOf("--"),rt=F.slice(V+1)||[],j=Object.create(null);if(V>-1&&rt.length>0){F=F.slice(0,V);const ot=M["--"];b=b.slice(0,-ot.length||void 0),o.push(Q(j,Z(F),b)),o.push(Q(j,Z(rt),ot))}else o.push(Q(j,Z(F),b));const Bt={...R,...k};return{name:f==null?void 0:f.name,called:Array.isArray(u)?u.join(" "):u,resolved:w,hasRootOrAlias:l(this,Y,St),hasRoot:l(this,tt,At),raw:{...L,parameters:b,mergedFlags:Bt},parameters:j,flags:R,unknownFlags:k,cli:this}},c={enforce:"post",fn:()=>{const[f]=n(),u=i(),w=o.filter(Boolean);if(w.length>0)throw w[0];if(!f)throw r?new ut(r,t):new ht(t);l(this,G).emit(f.name,u)}},a=[...l(this,O),c];return yt(a)(i),this}};let ce=bt;v=new WeakMap,_=new WeakMap,y=new WeakMap,O=new WeakMap,W=new WeakMap,G=new WeakMap,A=new WeakMap,q=new WeakMap,x=new WeakMap,I=new WeakMap,$=new WeakMap,N=new WeakMap,Y=new WeakSet,St=function(){return l(this,A).has(h)},tt=new WeakSet,At=function(){return Object.prototype.hasOwnProperty.call(this._commands,h)},et=new WeakSet,Lt=function(){this.i18n.add(ae)},E=new WeakSet,C=function(){m(this,x,!0)},st=new WeakSet,kt=function(){const{t}=this.i18n;if(!l(this,v))throw new dt(t);if(!l(this,_))throw new pt(t);if(!l(this,y))throw new mt(t)};const le=t=>t,ue=(t,e,s)=>s,he=(t,e)=>e,fe=(t,e)=>({...t,handler:e});export{ce as Clerc,lt as CommandExistsError,ft as CommandNameConflictError,pt as DescriptionNotSetError,wt as InvalidCommandNameError,U as LocaleNotCalledFirstError,dt as NameNotSetError,ht as NoCommandGivenError,ut as NoSuchCommandError,h as Root,mt as VersionNotSetError,yt as compose,fe as defineCommand,ue as defineHandler,he as defineInspector,le as definePlugin,Ft as detectLocale,Mt as formatCommandName,Nt as isInvalidName,K as resolveArgv,Ct as resolveCommand,re as resolveCommandStrict,z as resolveFlattenCommands,_t as resolveParametersBeforeFlag,oe as resolveRootCommands,vt as resolveSubcommandsByParent,ne as withBrackets};
1
+ import{format as it}from"node:util";class Ot{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(e,s){return e==="*"?(this.wildcardListeners.add(s),this):(this.listenerMap[e]||(this.listenerMap[e]=new Set),this.listenerMap[e].add(s),this)}emit(e,...s){return this.listenerMap[e]&&(this.wildcardListeners.forEach(r=>r(e,...s)),this.listenerMap[e].forEach(r=>r(...s))),this}off(e,s){var r,o;return e==="**"?(this.listenerMap={},this.wildcardListeners.clear(),this):e==="*"?(s?this.wildcardListeners.delete(s):this.wildcardListeners.clear(),this):(s?(r=this.listenerMap[e])==null||r.delete(s):(o=this.listenerMap[e])==null||o.clear(),this)}}const It="known-flag",$t="unknown-flag",Rt="argument",{stringify:k}=JSON,Pt=/\B([A-Z])/g,jt=t=>t.replace(Pt,"-$1").toLowerCase(),{hasOwnProperty:Tt}=Object.prototype,L=(t,e)=>Tt.call(t,e),Gt=t=>Array.isArray(t),at=t=>typeof t=="function"?[t,!1]:Gt(t)?[t[0],!0]:at(t.type),Ht=(t,e)=>t===Boolean?e!=="false":e,qt=(t,e)=>typeof e=="boolean"?e:t===Number&&e===""?Number.NaN:t(e),Vt=/[\s.:=]/,Jt=t=>{const e=`Flag name ${k(t)}`;if(t.length===0)throw new Error(`${e} cannot be empty`);if(t.length===1)throw new Error(`${e} must be longer than a character`);const s=t.match(Vt);if(s)throw new Error(`${e} cannot contain ${k(s==null?void 0:s[0])}`)},Ut=t=>{const e={},s=(r,o)=>{if(L(e,r))throw new Error(`Duplicate flags named ${k(r)}`);e[r]=o};for(const r in t){if(!L(t,r))continue;Jt(r);const o=t[r],n=[[],...at(o),o];s(r,n);const i=jt(r);if(r!==i&&s(i,n),"alias"in o&&typeof o.alias=="string"){const{alias:c}=o,a=`Flag alias ${k(c)} for flag ${k(r)}`;if(c.length===0)throw new Error(`${a} cannot be empty`);if(c.length>1)throw new Error(`${a} must be a single character`);s(c,n)}}return e},zt=(t,e)=>{const s={};for(const r in t){if(!L(t,r))continue;const[o,,n,i]=e[r];if(o.length===0&&"default"in i){let{default:c}=i;typeof c=="function"&&(c=c()),s[r]=c}else s[r]=n?o:o.pop()}return s},P="--",Kt=/[.:=]/,Zt=/^-{1,2}\w/,Qt=t=>{if(!Zt.test(t))return;const e=!t.startsWith(P);let s=t.slice(e?1:2),r;const o=s.match(Kt);if(o){const{index:n}=o;r=s.slice(n+1),s=s.slice(0,n)}return[s,r,e]},Xt=(t,{onFlag:e,onArgument:s})=>{let r;const o=(n,i)=>{if(typeof r!="function")return!0;r(n,i),r=void 0};for(let n=0;n<t.length;n+=1){const i=t[n];if(i===P){o();const a=t.slice(n+1);s==null||s(a,[n],!0);break}const c=Qt(i);if(c){if(o(),!e)continue;const[a,u,p]=c;if(p)for(let d=0;d<a.length;d+=1){o();const C=d===a.length-1;r=e(a[d],C?u:void 0,[n,d+1,C])}else r=e(a,u,[n])}else o(i,[n])&&(s==null||s([i],[n]))}o()},Yt=(t,e)=>{for(const[s,r,o]of e.reverse()){if(r){const n=t[s];let i=n.slice(0,r);if(o||(i+=n.slice(r+1)),i!=="-"){t[s]=i;continue}}t.splice(s,1)}},te=(t,e=process.argv.slice(2),{ignore:s}={})=>{const r=[],o=Ut(t),n={},i=[];return i[P]=[],Xt(e,{onFlag(c,a,u){const p=L(o,c);if(!(s!=null&&s(p?It:$t,c,a))){if(p){const[d,C]=o[c],_=Ht(C,a),R=(ot,A)=>{r.push(u),A&&r.push(A),d.push(qt(C,ot||""))};return _===void 0?R:R(_)}L(n,c)||(n[c]=[]),n[c].push(a===void 0?!0:a),r.push(u)}},onArgument(c,a,u){s!=null&&s(Rt,e[a[0]])||(i.push(...c),u?(i[P]=c,e.splice(a[0])):r.push(a))}}),Yt(e,r),{flags:zt(t,o),unknownFlags:n,_:i}};function H(t){return t!==null&&typeof t=="object"}function q(t,e,s=".",r){if(!H(e))return q(t,{},s,r);const o=Object.assign({},e);for(const n in t){if(n==="__proto__"||n==="constructor")continue;const i=t[n];i!=null&&(r&&r(o,n,i,s)||(Array.isArray(i)&&Array.isArray(o[n])?o[n]=[...i,...o[n]]:H(i)&&H(o[n])?o[n]=q(i,o[n],(s?`${s}.`:"")+n.toString(),r):o[n]=i))}return o}function ee(t){return(...e)=>e.reduce((s,r)=>q(s,r,"",t),{})}const se=ee(),j=t=>Array.isArray(t)?t:[t],re=t=>t.replace(/[-_ ](\w)/g,(e,s)=>s.toUpperCase()),ct=(t,e)=>e.length!==t.length?!1:t.every((s,r)=>s===e[r]),lt=(t,e)=>e.length>t.length?!1:ct(t.slice(0,e.length),e),B=JSON.stringify;class ut extends Error{constructor(e,s){super(s("core.commandExists",B(e))),this.commandName=e}}class ht extends Error{constructor(e,s){super(s("core.noSuchCommand",B(e))),this.commandName=e}}class ft extends Error{constructor(e){super(e("core.noCommandGiven"))}}class dt extends Error{constructor(e,s,r){super(r("core.commandNameConflict",B(e),B(s))),this.n1=e,this.n2=s}}class pt extends Error{constructor(e){super(e("core.nameNotSet"))}}class mt extends Error{constructor(e){super(e("core.descriptionNotSet"))}}class wt extends Error{constructor(e){super(e("core.versionNotSet"))}}class gt extends Error{constructor(e,s){super(s("core.badNameFormat",B(e))),this.commandName=e}}class V extends Error{constructor(e){super(e("core.localeMustBeCalledFirst"))}}const Et=typeof Deno!="undefined",ne=typeof process!="undefined"&&!Et,oe=process.versions.electron&&!process.defaultApp;function Ct(t,e,s,r){if(s.alias){const o=j(s.alias);for(const n of o){if(n in e)throw new dt(e[n].name,s.name,r);t.set(typeof n=="symbol"?n:n.split(" "),{...s,__isAlias:!0})}}}function J(t,e){const s=new Map;t[f]&&(s.set(f,t[f]),Ct(s,t,t[f],e));for(const r of Object.values(t))Ct(s,t,r,e),s.set(r.name.split(" "),r);return s}function _t(t,e,s){if(e===f)return[t[f],f];const r=j(e),o=J(t,s);let n,i;return o.forEach((c,a)=>{if(a===f){n=t[f],i=f;return}lt(r,a)&&(!i||i===f||a.length>i.length)&&(n=c,i=a)}),[n,i]}function ie(t,e,s){if(e===f)return[t[f],f];const r=j(e),o=J(t,s);let n,i;return o.forEach((c,a)=>{a===f||i===f||ct(r,a)&&(n=c,i=a)}),[n,i]}function vt(t,e,s=1/0){const r=e===""?[]:Array.isArray(e)?e:e.split(" ");return Object.values(t).filter(o=>{const n=o.name.split(" ");return lt(n,r)&&n.length-r.length<=s})}const ae=t=>vt(t,"",1);function yt(t){const e=[];for(const s of t){if(s.startsWith("-"))break;e.push(s)}return e}const U=()=>ne?process.argv.slice(oe?1:2):Et?Deno.args:[];function Nt(t){const e={pre:[],normal:[],post:[]};for(const r of t){const o=typeof r=="object"?r:{fn:r},{enforce:n,fn:i}=o;n==="post"||n==="pre"?e[n].push(i):e.normal.push(i)}const s=[...e.pre,...e.normal,...e.post];return r=>{return o(0);function o(n){const i=s[n];return i(r(),o.bind(null,n+1))}}}const ce=/\s\s+/,Mt=t=>t===f?!0:!(t.startsWith(" ")||t.endsWith(" "))&&!ce.test(t),le=(t,e)=>e?`[${t}]`:`<${t}>`,ue="<Root>",St=t=>Array.isArray(t)?t.join(" "):typeof t=="string"?t:ue,Ft=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,{stringify:S}=JSON;function z(t){const e=[];let s,r;for(const o of t){if(r)throw new Error(`Invalid parameter: Spread parameter ${S(r)} must be last`);const n=o[0],i=o[o.length-1];let c;if(n==="<"&&i===">"&&(c=!0,s))throw new Error(`Invalid parameter: Required parameter ${S(o)} cannot come after optional parameter ${S(s)}`);if(n==="["&&i==="]"&&(c=!1,s=o),c===void 0)throw new Error(`Invalid parameter: ${S(o)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let a=o.slice(1,-1);const u=a.slice(-3)==="...";u&&(r=o,a=a.slice(0,-3)),e.push({name:a,required:c,spread:u})}return e}function K(t,e,s){for(let r=0;r<e.length;r+=1){const{name:o,required:n,spread:i}=e[r],c=re(o);if(c in t)throw new Error(`Invalid parameter: ${S(o)} is used more than once.`);const a=i?s.slice(r):s[r];if(i&&(r=e.length),n&&(!a||i&&a.length===0))throw new Error(`Missing required parameter ${S(o)}`);t[c]=a}}const he={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."},"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"}};var Z=(t,e,s)=>{if(!e.has(t))throw TypeError("Cannot "+s)},l=(t,e,s)=>(Z(t,e,"read from private field"),s?s.call(t):e.get(t)),h=(t,e,s)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,s)},w=(t,e,s,r)=>(Z(t,e,"write to private field"),r?r.call(t,s):e.set(t,s),s),m=(t,e,s)=>(Z(t,e,"access private method"),s),v,y,N,W,b,T,F,x,D,O,I,$,M,Q,At,X,kt,Y,Lt,g,E,tt,Bt,et,Wt,st,bt,G,rt,nt,xt;const f=Symbol.for("Clerc.Root"),Dt=class{constructor(t,e,s){h(this,Q),h(this,X),h(this,Y),h(this,g),h(this,tt),h(this,et),h(this,st),h(this,G),h(this,nt),h(this,v,""),h(this,y,""),h(this,N,""),h(this,W,[]),h(this,b,{}),h(this,T,new Ot),h(this,F,new Set),h(this,x,void 0),h(this,D,[]),h(this,O,!1),h(this,I,"en"),h(this,$,"en"),h(this,M,{}),this.i18n={add:r=>{w(this,M,se(l(this,M),r))},t:(r,...o)=>{const n=l(this,M)[l(this,$)]||l(this,M)[l(this,I)],i=l(this,M)[l(this,I)];return n[r]?it(n[r],...o):i[r]?it(i[r],...o):void 0}},w(this,v,t||l(this,v)),w(this,y,e||l(this,y)),w(this,N,s||l(this,N)),w(this,$,Ft()),m(this,Y,Lt).call(this)}get _name(){return l(this,v)}get _description(){return l(this,y)}get _version(){return l(this,N)}get _inspectors(){return l(this,W)}get _commands(){return l(this,b)}static create(t,e,s){return new Dt(t,e,s)}name(t){return m(this,g,E).call(this),w(this,v,t),this}description(t){return m(this,g,E).call(this),w(this,y,t),this}version(t){return m(this,g,E).call(this),w(this,N,t),this}locale(t){if(l(this,O))throw new V(this.i18n.t);return w(this,$,t),this}fallbackLocale(t){if(l(this,O))throw new V(this.i18n.t);return w(this,I,t),this}errorHandler(t){return l(this,D).push(t),this}command(t,e,s={}){return m(this,G,rt).call(this,()=>m(this,tt,Bt).call(this,t,e,s)),this}on(t,e){return l(this,T).on(t,e),this}use(t){return m(this,g,E).call(this),t.setup(this)}inspector(t){return m(this,g,E).call(this),l(this,W).push(t),this}parse(t=U()){m(this,g,E).call(this);const{argv:e,run:s}=Array.isArray(t)?{argv:t,run:!0}:{argv:U(),...t};return w(this,x,e),m(this,et,Wt).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){return m(this,G,rt).call(this,()=>m(this,nt,xt).call(this)),this}};let fe=Dt;v=new WeakMap,y=new WeakMap,N=new WeakMap,W=new WeakMap,b=new WeakMap,T=new WeakMap,F=new WeakMap,x=new WeakMap,D=new WeakMap,O=new WeakMap,I=new WeakMap,$=new WeakMap,M=new WeakMap,Q=new WeakSet,At=function(){return l(this,F).has(f)},X=new WeakSet,kt=function(){return Object.prototype.hasOwnProperty.call(this._commands,f)},Y=new WeakSet,Lt=function(){this.i18n.add(he)},g=new WeakSet,E=function(){w(this,O,!0)},tt=new WeakSet,Bt=function(t,e,s={}){m(this,g,E).call(this);const{t:r}=this.i18n,n=(d=>!(typeof d=="string"||d===f))(t),i=n?t.name:t;if(!Mt(i))throw new gt(i,r);const{handler:c=void 0,...a}=n?t:{name:i,description:e,...s},u=[a.name],p=a.alias?j(a.alias):[];a.alias&&u.push(...p);for(const d of u)if(l(this,F).has(d))throw new ut(St(d),r);return l(this,b)[i]=a,l(this,F).add(a.name),p.forEach(d=>l(this,F).add(d)),n&&c&&this.on(t.name,c),this},et=new WeakSet,Wt=function(){const{t}=this.i18n;if(!l(this,v))throw new pt(t);if(!l(this,y))throw new mt(t);if(!l(this,N))throw new wt(t)},st=new WeakSet,bt=function(t){const e=l(this,x),[s,r]=t(),o=!!s,n=te((s==null?void 0:s.flags)||{},[...e]),{_:i,flags:c,unknownFlags:a}=n;let u=!o||s.name===f?i:i.slice(s.name.split(" ").length),p=(s==null?void 0:s.parameters)||[];const d=p.indexOf("--"),C=p.slice(d+1)||[],_=Object.create(null);if(d>-1&&C.length>0){p=p.slice(0,d);const A=i["--"];u=u.slice(0,-A.length||void 0),K(_,z(p),u),K(_,z(C),A)}else K(_,z(p),u);const R={...c,...a};return{name:s==null?void 0:s.name,called:Array.isArray(r)?r.join(" "):r,resolved:o,hasRootOrAlias:l(this,Q,At),hasRoot:l(this,X,kt),raw:{...n,parameters:u,mergedFlags:R},parameters:_,flags:c,unknownFlags:a,cli:this}},G=new WeakSet,rt=function(t){try{t()}catch(e){if(l(this,D).length>0)l(this,D).forEach(s=>s(e));else throw e}},nt=new WeakSet,xt=function(){m(this,g,E).call(this);const{t}=this.i18n,e=l(this,x);if(!e)throw new Error(t("core.cliParseMustBeCalled"));const s=yt(e),r=s.join(" "),o=()=>_t(l(this,b),s,t),n=()=>m(this,st,bt).call(this,o),i={enforce:"post",fn:()=>{const[u]=o(),p=n();if(!u)throw r?new ht(r,t):new ft(t);l(this,T).emit(u.name,p)}},c=[...l(this,W),i];Nt(c)(n)};const de=t=>t,pe=(t,e,s)=>s,me=(t,e)=>e,we=(t,e)=>({...t,handler:e});export{fe as Clerc,ut as CommandExistsError,dt as CommandNameConflictError,mt as DescriptionNotSetError,gt as InvalidCommandNameError,V as LocaleNotCalledFirstError,pt as NameNotSetError,ft as NoCommandGivenError,ht as NoSuchCommandError,f as Root,wt as VersionNotSetError,Nt as compose,we as defineCommand,pe as defineHandler,me as defineInspector,de as definePlugin,Ft as detectLocale,St as formatCommandName,Mt as isValidName,U as resolveArgv,_t as resolveCommand,ie as resolveCommandStrict,J as resolveFlattenCommands,yt as resolveParametersBeforeFlag,ae as resolveRootCommands,vt as resolveSubcommandsByParent,le as withBrackets};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@clerc/core",
3
- "version": "0.29.0",
4
- "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
3
+ "version": "0.30.1",
4
+ "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "description": "Clerc core",
6
6
  "keywords": [
7
7
  "cli",
@@ -52,7 +52,7 @@
52
52
  "lite-emit": "^1.4.0",
53
53
  "type-fest": "^3.5.3",
54
54
  "type-flag": "^3.0.0",
55
- "@clerc/utils": "0.29.0"
55
+ "@clerc/utils": "0.30.1"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "puild --minify",