@clerc/core 0.38.0 → 0.40.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/dist/index.d.ts CHANGED
@@ -391,6 +391,7 @@ declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}>
391
391
  i18n: I18N;
392
392
  private constructor();
393
393
  get _name(): string;
394
+ get _scriptName(): string;
394
395
  get _description(): string;
395
396
  get _version(): string;
396
397
  get _inspectors(): Inspector[];
@@ -421,6 +422,18 @@ declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}>
421
422
  * ```
422
423
  */
423
424
  name(name: string): this;
425
+ /**
426
+ * Set the script name of the cli
427
+ *
428
+ * @param scriptName
429
+ * @returns
430
+ * @example
431
+ * ```ts
432
+ * Clerc.create()
433
+ * .scriptName("test")
434
+ * ```
435
+ */
436
+ scriptName(scriptName: string): this;
424
437
  /**
425
438
  * Set the description of the cli
426
439
  *
@@ -623,7 +636,7 @@ declare class CommandNameConflictError extends Error {
623
636
  n2: string;
624
637
  constructor(n1: string, n2: string, t: TranslateFn);
625
638
  }
626
- declare class NameNotSetError extends Error {
639
+ declare class ScriptNameNotSetError extends Error {
627
640
  constructor(t: TranslateFn);
628
641
  }
629
642
  declare class DescriptionNotSetError extends Error {
@@ -657,4 +670,4 @@ declare const formatCommandName: (name: string | string[] | RootType) => string;
657
670
  declare const detectLocale: () => string;
658
671
  declare const stripFlags: (argv: string[]) => string[];
659
672
 
660
- export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, GlobalFlagOption, GlobalFlagOptions, 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, resolveFlattenCommands, stripFlags, withBrackets };
673
+ export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, GlobalFlagOption, GlobalFlagOptions, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, Root, RootType, ScriptNameNotSetError, TranslateFn, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveFlattenCommands, stripFlags, withBrackets };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{format as ae}from"node:util";import"@clerc/core";const ie=e=>Array.isArray(e)?e:[e],be=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,s)=>s?s.toUpperCase():""),xe=(e,t)=>t.length!==e.length?!1:e.every((s,r)=>s===t[r]),Re=(e,t)=>t.length>e.length?!1:xe(e.slice(0,t.length),t);function H(e){return e!==null&&typeof e=="object"}function U(e,t,s=".",r){if(!H(t))return U(e,{},s,r);const n=Object.assign({},t);for(const o in e){if(o==="__proto__"||o==="constructor")continue;const a=e[o];a!=null&&(r&&r(n,o,a,s)||(Array.isArray(a)&&Array.isArray(n[o])?n[o]=[...a,...n[o]]:H(a)&&H(n[o])?n[o]=U(a,n[o],(s?`${s}.`:"")+o.toString(),r):n[o]=a))}return n}function $e(e){return(...t)=>t.reduce((s,r)=>U(s,r,"",e),{})}const qe=$e();class Te{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(t,s){return t==="*"?(this.wildcardListeners.add(s),this):(this.listenerMap[t]||(this.listenerMap[t]=new Set),this.listenerMap[t].add(s),this)}emit(t,...s){return this.listenerMap[t]&&(this.wildcardListeners.forEach(r=>r.apply(null,[t,...s])),this.listenerMap[t].forEach(r=>r(...s))),this}off(t,s){var r,n;return t===void 0?(this.listenerMap={},this.wildcardListeners.clear(),this):t==="*"?(s?this.wildcardListeners.delete(s):this.wildcardListeners.clear(),this):(s?(r=this.listenerMap[t])==null||r.delete(s):(n=this.listenerMap[t])==null||n.clear(),this)}}const je="known-flag",Ge="unknown-flag",He="argument",{stringify:A}=JSON,Ue=/\B([A-Z])/g,Ve=e=>e.replace(Ue,"-$1").toLowerCase(),{hasOwnProperty:Je}=Object.prototype,L=(e,t)=>Je.call(e,t),ze=e=>Array.isArray(e),ce=e=>typeof e=="function"?[e,!1]:ze(e)?[e[0],!0]:ce(e.type),Ke=(e,t)=>e===Boolean?t!=="false":t,Ze=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),Qe=/[\s.:=]/,Xe=e=>{const t=`Flag name ${A(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 s=e.match(Qe);if(s)throw new Error(`${t} cannot contain ${A(s==null?void 0:s[0])}`)},Ye=e=>{const t={},s=(r,n)=>{if(L(t,r))throw new Error(`Duplicate flags named ${A(r)}`);t[r]=n};for(const r in e){if(!L(e,r))continue;Xe(r);const n=e[r],o=[[],...ce(n),n];s(r,o);const a=Ve(r);if(r!==a&&s(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:u}=n,i=`Flag alias ${A(u)} for flag ${A(r)}`;if(u.length===0)throw new Error(`${i} cannot be empty`);if(u.length>1)throw new Error(`${i} must be a single character`);s(u,o)}}return t},et=(e,t)=>{const s={};for(const r in e){if(!L(e,r))continue;const[n,,o,a]=t[r];if(n.length===0&&"default"in a){let{default:u}=a;typeof u=="function"&&(u=u()),s[r]=u}else s[r]=o?n:n.pop()}return s},T="--",tt=/[.:=]/,st=/^-{1,2}\w/,rt=e=>{if(!st.test(e))return;const t=!e.startsWith(T);let s=e.slice(t?1:2),r;const n=s.match(tt);if(n){const{index:o}=n;r=s.slice(o+1),s=s.slice(0,o)}return[s,r,t]},ot=(e,{onFlag:t,onArgument:s})=>{let r;const n=(o,a)=>{if(typeof r!="function")return!0;r(o,a),r=void 0};for(let o=0;o<e.length;o+=1){const a=e[o];if(a===T){n();const i=e.slice(o+1);s==null||s(i,[o],!0);break}const u=rt(a);if(u){if(n(),!t)continue;const[i,l,m]=u;if(m)for(let d=0;d<i.length;d+=1){n();const w=d===i.length-1;r=t(i[d],w?l:void 0,[o,d+1,w])}else r=t(i,l,[o])}else n(a,[o])&&(s==null||s([a],[o]))}n()},nt=(e,t)=>{for(const[s,r,n]of t.reverse()){if(r){const o=e[s];let a=o.slice(0,r);if(n||(a+=o.slice(r+1)),a!=="-"){e[s]=a;continue}}e.splice(s,1)}},ue=(e,t=process.argv.slice(2),{ignore:s}={})=>{const r=[],n=Ye(e),o={},a=[];return a[T]=[],ot(t,{onFlag(u,i,l){const m=L(n,u);if(!(s!=null&&s(m?je:Ge,u,i))){if(m){const[d,w]=n[u],E=Ke(w,i),N=(q,M)=>{r.push(l),M&&r.push(M),d.push(Ze(w,q||""))};return E===void 0?N:N(E)}L(o,u)||(o[u]=[]),o[u].push(i===void 0?!0:i),r.push(l)}},onArgument(u,i,l){s!=null&&s(He,t[i[0]])||(a.push(...u),l?(a[T]=u,t.splice(i[0])):r.push(i))}}),nt(t,r),{flags:et(e,n),unknownFlags:o,_:a}},W=JSON.stringify;class le extends Error{constructor(t,s){super(s("core.commandExists",W(t))),this.commandName=t}}class he extends Error{constructor(t,s){super(s("core.noSuchCommand",W(t))),this.commandName=t}}class de extends Error{constructor(t){super(t("core.noCommandGiven"))}}class fe extends Error{constructor(t,s,r){super(r("core.commandNameConflict",W(t),W(s))),this.n1=t,this.n2=s}}class pe extends Error{constructor(t){super(t("core.nameNotSet"))}}class me extends Error{constructor(t){super(t("core.descriptionNotSet"))}}class Ce extends Error{constructor(t){super(t("core.versionNotSet"))}}class we extends Error{constructor(t,s){super(s("core.badNameFormat",W(t))),this.commandName=t}}class V extends Error{constructor(t){super(t("core.localeMustBeCalledFirst"))}}const at={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"}},{stringify:k}=JSON;function J(e,t){const s=[];let r,n;for(const o of e){if(n)throw new Error(t("core.spreadParameterMustBeLast",k(n)));const a=o[0],u=o[o.length-1];let i;if(a==="<"&&u===">"&&(i=!0,r))throw new Error(t("core.requiredParameterMustBeBeforeOptional",k(o),k(r)));if(a==="["&&u==="]"&&(i=!1,r=o),i===void 0)throw new Error(t("core.parameterMustBeWrappedInBrackets",k(o)));let l=o.slice(1,-1);const m=l.slice(-3)==="...";m&&(n=o,l=l.slice(0,-3)),s.push({name:l,required:i,spread:m})}return s}function z(e,t,s,r){for(let n=0;n<t.length;n+=1){const{name:o,required:a,spread:u}=t[n],i=be(o);if(i in e)throw new Error(r("core.parameterIsUsedMoreThanOnce",k(o)));const l=u?s.slice(n):s[n];if(u&&(n=t.length),a&&(!l||u&&l.length===0))throw new Error(r("core.missingRequiredParameter",k(o)));e[i]=l}}const ge=typeof Deno!="undefined",it=typeof process!="undefined"&&!ge,ct=process.versions.electron&&!process.defaultApp;function Ee(e,t,s,r){if(s.alias){const n=ie(s.alias);for(const o of n){if(o in t)throw new fe(t[o].name,s.name,r);e.set(typeof o=="symbol"?o:o.split(" "),{...s,__isAlias:!0})}}}function _e(e,t){const s=new Map;e[p]&&(s.set(p,e[p]),Ee(s,e,e[p],t));for(const r of Object.values(e))Ee(s,e,r,t),s.set(r.name.split(" "),r);return s}function ve(e,t,s){var r;const n=_e(e,s);for(const[o,a]of n.entries()){const u=ue((r=a==null?void 0:a.flags)!=null?r:{},[...t]),{_:i}=u;if(o!==p&&Re(i,o))return[a,o]}return n.has(p)?[n.get(p),p]:[void 0,void 0]}const K=()=>it?process.argv.slice(ct?1:2):ge?Deno.args:[];function Me(e){const t={pre:[],normal:[],post:[]};for(const r of e){const n=typeof r=="object"?r:{fn:r},{enforce:o,fn:a}=n;o==="post"||o==="pre"?t[o].push(a):t.normal.push(a)}const s=[...t.pre,...t.normal,...t.post];return r=>{return n(0);function n(o){const a=s[o];return a(r,n.bind(null,o+1))}}}const ut=/\s\s+/,Fe=e=>e===p?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!ut.test(e),lt=(e,t)=>t?`[${e}]`:`<${e}>`,ht="<Root>",ye=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:ht,Be=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,Ne=e=>e.filter(t=>!t.startsWith("-"));var Z=(e,t,s)=>{if(!t.has(e))throw TypeError("Cannot "+s)},c=(e,t,s)=>(Z(e,t,"read from private field"),s?s.call(e):t.get(e)),h=(e,t,s)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,s)},C=(e,t,s,r)=>(Z(e,t,"write to private field"),r?r.call(e,s):t.set(e,s),s),f=(e,t,s)=>(Z(e,t,"access private method"),s),v,F,y,D,O,j,P,S,I,b,x,R,$,B,Q,ke,X,Se,Y,Ae,g,_,ee,Le,te,We,se,De,G,re,oe,Oe;const p=Symbol.for("Clerc.Root"),Pe=class{constructor(e,t,s){h(this,Q),h(this,X),h(this,Y),h(this,g),h(this,ee),h(this,te),h(this,se),h(this,G),h(this,oe),h(this,v,""),h(this,F,""),h(this,y,""),h(this,D,[]),h(this,O,{}),h(this,j,new Te),h(this,P,{}),h(this,S,new Set),h(this,I,void 0),h(this,b,[]),h(this,x,!1),h(this,R,"en"),h(this,$,"en"),h(this,B,{}),this.i18n={add:r=>{C(this,B,qe(c(this,B),r))},t:(r,...n)=>{const o=c(this,B)[c(this,$)]||c(this,B)[c(this,R)],a=c(this,B)[c(this,R)];return o[r]?ae(o[r],...n):a[r]?ae(a[r],...n):void 0}},C(this,v,e!=null?e:c(this,v)),C(this,F,t!=null?t:c(this,F)),C(this,y,s!=null?s:c(this,y)),C(this,$,Be()),f(this,Y,Ae).call(this)}get _name(){return c(this,v)}get _description(){return c(this,F)}get _version(){return c(this,y)}get _inspectors(){return c(this,D)}get _commands(){return c(this,O)}get _flags(){return c(this,P)}static create(e,t,s){return new Pe(e,t,s)}name(e){return f(this,g,_).call(this),C(this,v,e),this}description(e){return f(this,g,_).call(this),C(this,F,e),this}version(e){return f(this,g,_).call(this),C(this,y,e),this}locale(e){if(c(this,x))throw new V(this.i18n.t);return C(this,$,e),this}fallbackLocale(e){if(c(this,x))throw new V(this.i18n.t);return C(this,R,e),this}errorHandler(e){return c(this,b).push(e),this}command(e,t,s={}){return f(this,G,re).call(this,()=>f(this,ee,Le).call(this,e,t,s)),this}flag(e,t,s){return c(this,P)[e]={description:t,...s},this}on(e,t){return c(this,j).on(e,t),this}use(e){return f(this,g,_).call(this),e.setup(this)}inspector(e){return f(this,g,_).call(this),c(this,D).push(e),this}parse(e=K()){f(this,g,_).call(this);const{argv:t,run:s}=Array.isArray(e)?{argv:e,run:!0}:{argv:K(),...e};return C(this,I,[...t]),f(this,te,We).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){return f(this,G,re).call(this,()=>f(this,oe,Oe).call(this)),process.title=c(this,v),this}};let dt=Pe;v=new WeakMap,F=new WeakMap,y=new WeakMap,D=new WeakMap,O=new WeakMap,j=new WeakMap,P=new WeakMap,S=new WeakMap,I=new WeakMap,b=new WeakMap,x=new WeakMap,R=new WeakMap,$=new WeakMap,B=new WeakMap,Q=new WeakSet,ke=function(){return c(this,S).has(p)},X=new WeakSet,Se=function(){return Object.prototype.hasOwnProperty.call(this._commands,p)},Y=new WeakSet,Ae=function(){this.i18n.add(at)},g=new WeakSet,_=function(){C(this,x,!0)},ee=new WeakSet,Le=function(e,t,s={}){f(this,g,_).call(this);const{t:r}=this.i18n,o=(d=>!(typeof d=="string"||d===p))(e),a=o?e.name:e;if(!Fe(a))throw new we(a,r);const{handler:u=void 0,...i}=o?e:{name:a,description:t,...s},l=[i.name],m=i.alias?ie(i.alias):[];i.alias&&l.push(...m);for(const d of l)if(c(this,S).has(d))throw new le(ye(d),r);return c(this,O)[a]=i,c(this,S).add(i.name),m.forEach(d=>c(this,S).add(d)),o&&u&&this.on(e.name,u),this},te=new WeakSet,We=function(){const{t:e}=this.i18n;if(!c(this,v))throw new pe(e);if(!c(this,F))throw new me(e);if(!c(this,y))throw new Ce(e)},se=new WeakSet,De=function(e){var t;const s=c(this,I),{t:r}=this.i18n,[n,o]=e(),a=!!n,u={...c(this,P),...n==null?void 0:n.flags},i=ue(u,[...s]),{_:l,flags:m,unknownFlags:d}=i;let w=!a||n.name===p?l:l.slice(n.name.split(" ").length),E=(t=n==null?void 0:n.parameters)!=null?t:[];const N=E.indexOf("--"),q=E.slice(N+1)||[],M=Object.create(null);if(N>-1&&q.length>0){E=E.slice(0,N);const ne=l["--"];w=w.slice(0,-ne.length||void 0),z(M,J(E,r),w,r),z(M,J(q,r),ne,r)}else z(M,J(E,r),w,r);const Ie={...m,...d};return{name:n==null?void 0:n.name,called:Array.isArray(o)?o.join(" "):o,resolved:a,hasRootOrAlias:c(this,Q,ke),hasRoot:c(this,X,Se),raw:{...i,parameters:w,mergedFlags:Ie},parameters:M,flags:m,unknownFlags:d,cli:this}},G=new WeakSet,re=function(e){try{e()}catch(t){if(c(this,b).length>0)c(this,b).forEach(s=>s(t));else throw t}},oe=new WeakSet,Oe=function(){f(this,g,_).call(this);const{t:e}=this.i18n,t=c(this,I);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const s=()=>ve(c(this,O),t,e),r=()=>f(this,se,De).call(this,s),n={enforce:"post",fn:u=>{const[i]=s(),l=Ne(t).join(" ");if(!i)throw l?new he(l,e):new de(e);c(this,j).emit(i.name,u)}},o=[...c(this,D),n];Me(o)(r())};const ft=e=>e,pt=(e,t,s)=>s,mt=(e,t)=>t,Ct=(e,t)=>({...e,handler:t});export{dt as Clerc,le as CommandExistsError,fe as CommandNameConflictError,me as DescriptionNotSetError,we as InvalidCommandNameError,V as LocaleNotCalledFirstError,pe as NameNotSetError,de as NoCommandGivenError,he as NoSuchCommandError,p as Root,Ce as VersionNotSetError,Me as compose,Ct as defineCommand,pt as defineHandler,mt as defineInspector,ft as definePlugin,Be as detectLocale,ye as formatCommandName,Fe as isValidName,K as resolveArgv,ve as resolveCommand,_e as resolveFlattenCommands,Ne as stripFlags,lt as withBrackets};
1
+ import{format as ie}from"node:util";import"@clerc/core";const ce=e=>Array.isArray(e)?e:[e],xe=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,s)=>s?s.toUpperCase():""),Re=(e,t)=>t.length!==e.length?!1:e.every((s,r)=>s===t[r]),$e=(e,t)=>t.length>e.length?!1:Re(e.slice(0,t.length),t);function U(e){return e!==null&&typeof e=="object"}function V(e,t,s=".",r){if(!U(t))return V(e,{},s,r);const o=Object.assign({},t);for(const n in e){if(n==="__proto__"||n==="constructor")continue;const a=e[n];a!=null&&(r&&r(o,n,a,s)||(Array.isArray(a)&&Array.isArray(o[n])?o[n]=[...a,...o[n]]:U(a)&&U(o[n])?o[n]=V(a,o[n],(s?`${s}.`:"")+n.toString(),r):o[n]=a))}return o}function je(e){return(...t)=>t.reduce((s,r)=>V(s,r,"",e),{})}const qe=je();class Te{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(t,s){return t==="*"?(this.wildcardListeners.add(s),this):(this.listenerMap[t]||(this.listenerMap[t]=new Set),this.listenerMap[t].add(s),this)}emit(t,...s){return this.listenerMap[t]&&(this.wildcardListeners.forEach(r=>r.apply(null,[t,...s])),this.listenerMap[t].forEach(r=>r(...s))),this}off(t,s){var r,o;return t===void 0?(this.listenerMap={},this.wildcardListeners.clear(),this):t==="*"?(s?this.wildcardListeners.delete(s):this.wildcardListeners.clear(),this):(s?(r=this.listenerMap[t])==null||r.delete(s):(o=this.listenerMap[t])==null||o.clear(),this)}}const Ge="known-flag",He="unknown-flag",Ue="argument",{stringify:A}=JSON,Ve=/\B([A-Z])/g,Je=e=>e.replace(Ve,"-$1").toLowerCase(),{hasOwnProperty:ze}=Object.prototype,W=(e,t)=>ze.call(e,t),Ke=e=>Array.isArray(e),ue=e=>typeof e=="function"?[e,!1]:Ke(e)?[e[0],!0]:ue(e.type),Ze=(e,t)=>e===Boolean?t!=="false":t,Qe=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),Xe=/[\s.:=]/,Ye=e=>{const t=`Flag name ${A(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 s=e.match(Xe);if(s)throw new Error(`${t} cannot contain ${A(s==null?void 0:s[0])}`)},et=e=>{const t={},s=(r,o)=>{if(W(t,r))throw new Error(`Duplicate flags named ${A(r)}`);t[r]=o};for(const r in e){if(!W(e,r))continue;Ye(r);const o=e[r],n=[[],...ue(o),o];s(r,n);const a=Je(r);if(r!==a&&s(a,n),"alias"in o&&typeof o.alias=="string"){const{alias:u}=o,c=`Flag alias ${A(u)} for flag ${A(r)}`;if(u.length===0)throw new Error(`${c} cannot be empty`);if(u.length>1)throw new Error(`${c} must be a single character`);s(u,n)}}return t},tt=(e,t)=>{const s={};for(const r in e){if(!W(e,r))continue;const[o,,n,a]=t[r];if(o.length===0&&"default"in a){let{default:u}=a;typeof u=="function"&&(u=u()),s[r]=u}else s[r]=n?o:o.pop()}return s},q="--",st=/[.:=]/,rt=/^-{1,2}\w/,nt=e=>{if(!rt.test(e))return;const t=!e.startsWith(q);let s=e.slice(t?1:2),r;const o=s.match(st);if(o){const{index:n}=o;r=s.slice(n+1),s=s.slice(0,n)}return[s,r,t]},ot=(e,{onFlag:t,onArgument:s})=>{let r;const o=(n,a)=>{if(typeof r!="function")return!0;r(n,a),r=void 0};for(let n=0;n<e.length;n+=1){const a=e[n];if(a===q){o();const c=e.slice(n+1);s==null||s(c,[n],!0);break}const u=nt(a);if(u){if(o(),!t)continue;const[c,l,m]=u;if(m)for(let d=0;d<c.length;d+=1){o();const w=d===c.length-1;r=t(c[d],w?l:void 0,[n,d+1,w])}else r=t(c,l,[n])}else o(a,[n])&&(s==null||s([a],[n]))}o()},at=(e,t)=>{for(const[s,r,o]of t.reverse()){if(r){const n=e[s];let a=n.slice(0,r);if(o||(a+=n.slice(r+1)),a!=="-"){e[s]=a;continue}}e.splice(s,1)}},le=(e,t=process.argv.slice(2),{ignore:s}={})=>{const r=[],o=et(e),n={},a=[];return a[q]=[],ot(t,{onFlag(u,c,l){const m=W(o,u);if(!(s!=null&&s(m?Ge:He,u,c))){if(m){const[d,w]=o[u],_=Ze(w,c),B=(j,M)=>{r.push(l),M&&r.push(M),d.push(Qe(w,j||""))};return _===void 0?B:B(_)}W(n,u)||(n[u]=[]),n[u].push(c===void 0?!0:c),r.push(l)}},onArgument(u,c,l){s!=null&&s(Ue,t[c[0]])||(a.push(...u),l?(a[q]=u,t.splice(c[0])):r.push(c))}}),at(t,r),{flags:tt(e,o),unknownFlags:n,_:a}},L=JSON.stringify;class he extends Error{constructor(t,s){super(s("core.commandExists",L(t))),this.commandName=t}}class de extends Error{constructor(t,s){super(s("core.noSuchCommand",L(t))),this.commandName=t}}class fe extends Error{constructor(t){super(t("core.noCommandGiven"))}}class pe extends Error{constructor(t,s,r){super(r("core.commandNameConflict",L(t),L(s))),this.n1=t,this.n2=s}}class me extends Error{constructor(t){super(t("core.scriptNameNotSet"))}}class Ce extends Error{constructor(t){super(t("core.descriptionNotSet"))}}class we extends Error{constructor(t){super(t("core.versionNotSet"))}}class ge extends Error{constructor(t,s){super(s("core.badNameFormat",L(t))),this.commandName=t}}class J extends Error{constructor(t){super(t("core.localeMustBeCalledFirst"))}}const it={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.scriptNameNotSet":"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.scriptNameNotSet":"\u672A\u8BBE\u7F6E CLI Script \u540D\u79F0\u3002","core.descriptionNotSet":"\u672A\u8BBE\u7F6E CLI \u63CF\u8FF0\u3002","core.versionNotSet":"\u672A\u8BBE\u7F6E CLI \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"}},{stringify:k}=JSON;function z(e,t){const s=[];let r,o;for(const n of e){if(o)throw new Error(t("core.spreadParameterMustBeLast",k(o)));const a=n[0],u=n[n.length-1];let c;if(a==="<"&&u===">"&&(c=!0,r))throw new Error(t("core.requiredParameterMustBeBeforeOptional",k(n),k(r)));if(a==="["&&u==="]"&&(c=!1,r=n),c===void 0)throw new Error(t("core.parameterMustBeWrappedInBrackets",k(n)));let l=n.slice(1,-1);const m=l.slice(-3)==="...";m&&(o=n,l=l.slice(0,-3)),s.push({name:l,required:c,spread:m})}return s}function K(e,t,s,r){for(let o=0;o<t.length;o+=1){const{name:n,required:a,spread:u}=t[o],c=xe(n);if(c in e)throw new Error(r("core.parameterIsUsedMoreThanOnce",k(n)));const l=u?s.slice(o):s[o];if(u&&(o=t.length),a&&(!l||u&&l.length===0))throw new Error(r("core.missingRequiredParameter",k(n)));e[c]=l}}const Ee=typeof Deno!="undefined",ct=typeof process!="undefined"&&!Ee,ut=process.versions.electron&&!process.defaultApp;function _e(e,t,s,r){if(s.alias){const o=ce(s.alias);for(const n of o){if(n in t)throw new pe(t[n].name,s.name,r);e.set(typeof n=="symbol"?n:n.split(" "),{...s,__isAlias:!0})}}}function ve(e,t){const s=new Map;e[p]&&(s.set(p,e[p]),_e(s,e,e[p],t));for(const r of Object.values(e))_e(s,e,r,t),s.set(r.name.split(" "),r);return s}function Me(e,t,s){var r;const o=ve(e,s);for(const[n,a]of o.entries()){const u=le((r=a==null?void 0:a.flags)!=null?r:Object.create(null),[...t]),{_:c}=u;if(n!==p&&$e(c,n))return[a,n]}return o.has(p)?[o.get(p),p]:[void 0,void 0]}const Z=()=>ct?process.argv.slice(ut?1:2):Ee?Deno.args:[];function Fe(e){const t={pre:[],normal:[],post:[]};for(const r of e){const o=typeof r=="object"?r:{fn:r},{enforce:n,fn:a}=o;n==="post"||n==="pre"?t[n].push(a):t.normal.push(a)}const s=[...t.pre,...t.normal,...t.post];return r=>{return o(0);function o(n){const a=s[n];return a(r,o.bind(null,n+1))}}}const lt=/\s\s+/,Ne=e=>e===p?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!lt.test(e),ht=(e,t)=>t?`[${e}]`:`<${e}>`,dt="<Root>",ye=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:dt,Be=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,ke=e=>e.filter(t=>!t.startsWith("-"));var Q=(e,t,s)=>{if(!t.has(e))throw TypeError("Cannot "+s)},i=(e,t,s)=>(Q(e,t,"read from private field"),s?s.call(e):t.get(e)),h=(e,t,s)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,s)},C=(e,t,s,r)=>(Q(e,t,"write to private field"),r?r.call(e,s):t.set(e,s),s),f=(e,t,s)=>(Q(e,t,"access private method"),s),T,v,F,N,D,O,G,b,S,P,I,x,R,$,y,X,Se,Y,Ae,ee,We,g,E,te,Le,se,De,re,Oe,H,ne,oe,be;const p=Symbol.for("Clerc.Root"),Pe=class{constructor(e,t,s){h(this,X),h(this,Y),h(this,ee),h(this,g),h(this,te),h(this,se),h(this,re),h(this,H),h(this,oe),h(this,T,""),h(this,v,""),h(this,F,""),h(this,N,""),h(this,D,[]),h(this,O,Object.create(null)),h(this,G,new Te),h(this,b,Object.create(null)),h(this,S,new Set),h(this,P,void 0),h(this,I,[]),h(this,x,!1),h(this,R,"en"),h(this,$,"en"),h(this,y,Object.create(null)),this.i18n={add:r=>{C(this,y,qe(i(this,y),r))},t:(r,...o)=>{const n=i(this,y)[i(this,$)]||i(this,y)[i(this,R)],a=i(this,y)[i(this,R)];return n[r]?ie(n[r],...o):a[r]?ie(a[r],...o):void 0}},C(this,v,e!=null?e:i(this,v)),C(this,F,t!=null?t:i(this,F)),C(this,N,s!=null?s:i(this,N)),C(this,$,Be()),f(this,ee,We).call(this)}get _name(){return i(this,T)||i(this,v)}get _scriptName(){return i(this,v)}get _description(){return i(this,F)}get _version(){return i(this,N)}get _inspectors(){return i(this,D)}get _commands(){return i(this,O)}get _flags(){return i(this,b)}static create(e,t,s){return new Pe(e,t,s)}name(e){return f(this,g,E).call(this),C(this,T,e),this}scriptName(e){return f(this,g,E).call(this),C(this,v,e),this}description(e){return f(this,g,E).call(this),C(this,F,e),this}version(e){return f(this,g,E).call(this),C(this,N,e),this}locale(e){if(i(this,x))throw new J(this.i18n.t);return C(this,$,e),this}fallbackLocale(e){if(i(this,x))throw new J(this.i18n.t);return C(this,R,e),this}errorHandler(e){return i(this,I).push(e),this}command(e,t,s={}){return f(this,H,ne).call(this,()=>f(this,te,Le).call(this,e,t,s)),this}flag(e,t,s){return i(this,b)[e]={description:t,...s},this}on(e,t){return i(this,G).on(e,t),this}use(e){return f(this,g,E).call(this),e.setup(this)}inspector(e){return f(this,g,E).call(this),i(this,D).push(e),this}parse(e=Z()){f(this,g,E).call(this);const{argv:t,run:s}=Array.isArray(e)?{argv:e,run:!0}:{argv:Z(),...e};return C(this,P,[...t]),f(this,se,De).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){return f(this,H,ne).call(this,()=>f(this,oe,be).call(this)),process.title=i(this,v),this}};let ft=Pe;T=new WeakMap,v=new WeakMap,F=new WeakMap,N=new WeakMap,D=new WeakMap,O=new WeakMap,G=new WeakMap,b=new WeakMap,S=new WeakMap,P=new WeakMap,I=new WeakMap,x=new WeakMap,R=new WeakMap,$=new WeakMap,y=new WeakMap,X=new WeakSet,Se=function(){return i(this,S).has(p)},Y=new WeakSet,Ae=function(){return Object.prototype.hasOwnProperty.call(this._commands,p)},ee=new WeakSet,We=function(){this.i18n.add(it)},g=new WeakSet,E=function(){C(this,x,!0)},te=new WeakSet,Le=function(e,t,s={}){f(this,g,E).call(this);const{t:r}=this.i18n,n=(d=>!(typeof d=="string"||d===p))(e),a=n?e.name:e;if(!Ne(a))throw new ge(a,r);const{handler:u=void 0,...c}=n?e:{name:a,description:t,...s},l=[c.name],m=c.alias?ce(c.alias):[];c.alias&&l.push(...m);for(const d of l)if(i(this,S).has(d))throw new he(ye(d),r);return i(this,O)[a]=c,i(this,S).add(c.name),m.forEach(d=>i(this,S).add(d)),n&&u&&this.on(e.name,u),this},se=new WeakSet,De=function(){const{t:e}=this.i18n;if(!i(this,v))throw new me(e);if(!i(this,F))throw new Ce(e);if(!i(this,N))throw new we(e)},re=new WeakSet,Oe=function(e){var t;const s=i(this,P),{t:r}=this.i18n,[o,n]=e(),a=!!o,u={...i(this,b),...o==null?void 0:o.flags},c=le(u,[...s]),{_:l,flags:m,unknownFlags:d}=c;let w=!a||o.name===p?l:l.slice(o.name.split(" ").length),_=(t=o==null?void 0:o.parameters)!=null?t:[];const B=_.indexOf("--"),j=_.slice(B+1)||[],M=Object.create(null);if(B>-1&&j.length>0){_=_.slice(0,B);const ae=l["--"];w=w.slice(0,-ae.length||void 0),K(M,z(_,r),w,r),K(M,z(j,r),ae,r)}else K(M,z(_,r),w,r);const Ie={...m,...d};return{name:o==null?void 0:o.name,called:Array.isArray(n)?n.join(" "):n,resolved:a,hasRootOrAlias:i(this,X,Se),hasRoot:i(this,Y,Ae),raw:{...c,parameters:w,mergedFlags:Ie},parameters:M,flags:m,unknownFlags:d,cli:this}},H=new WeakSet,ne=function(e){try{e()}catch(t){if(i(this,I).length>0)i(this,I).forEach(s=>s(t));else throw t}},oe=new WeakSet,be=function(){f(this,g,E).call(this);const{t:e}=this.i18n,t=i(this,P);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const s=()=>Me(i(this,O),t,e),r=()=>f(this,re,Oe).call(this,s),o={enforce:"post",fn:u=>{const[c]=s(),l=ke(t).join(" ");if(!c)throw l?new de(l,e):new fe(e);i(this,G).emit(c.name,u)}},n=[...i(this,D),o];Fe(n)(r())};const pt=e=>e,mt=(e,t,s)=>s,Ct=(e,t)=>t,wt=(e,t)=>({...e,handler:t});export{ft as Clerc,he as CommandExistsError,pe as CommandNameConflictError,Ce as DescriptionNotSetError,ge as InvalidCommandNameError,J as LocaleNotCalledFirstError,fe as NoCommandGivenError,de as NoSuchCommandError,p as Root,me as ScriptNameNotSetError,we as VersionNotSetError,Fe as compose,wt as defineCommand,mt as defineHandler,Ct as defineInspector,pt as definePlugin,Be as detectLocale,ye as formatCommandName,Ne as isValidName,Z as resolveArgv,Me as resolveCommand,ve as resolveFlattenCommands,ke as stripFlags,ht as withBrackets};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/core",
3
- "version": "0.38.0",
3
+ "version": "0.40.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "description": "Clerc core",
6
6
  "keywords": [
@@ -52,10 +52,10 @@
52
52
  "lite-emit": "^2.0.0",
53
53
  "type-fest": "^3.8.0",
54
54
  "type-flag": "^3.0.0",
55
- "@clerc/utils": "0.38.0"
55
+ "@clerc/utils": "0.40.0"
56
56
  },
57
57
  "scripts": {
58
- "build": "puild --minify",
59
- "watch": "puild --watch"
58
+ "build": "pkgroll --minify",
59
+ "watch": "pkgroll --watch"
60
60
  }
61
61
  }