@clerc/core 0.35.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Ray <https://github.com/so1ve>
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Ray <https://github.com/so1ve>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{format as ae}from"node:util";import"@clerc/core";class Pe{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(t,...s)),this.listenerMap[t].forEach(r=>r(...s))),this}off(t,s){var r,n;return t==="**"?(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 Ie="known-flag",xe="unknown-flag",Re="argument",{stringify:A}=JSON,$e=/\B([A-Z])/g,qe=e=>e.replace($e,"-$1").toLowerCase(),{hasOwnProperty:Te}=Object.prototype,L=(e,t)=>Te.call(e,t),je=e=>Array.isArray(e),ie=e=>typeof e=="function"?[e,!1]:je(e)?[e[0],!0]:ie(e.type),Ge=(e,t)=>e===Boolean?t!=="false":t,He=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),Ue=/[\s.:=]/,Ve=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(Ue);if(s)throw new Error(`${t} cannot contain ${A(s==null?void 0:s[0])}`)},Je=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;Ve(r);const n=e[r],o=[[],...ie(n),n];s(r,o);const a=qe(r);if(r!==a&&s(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:i}=n,c=`Flag alias ${A(i)} for flag ${A(r)}`;if(i.length===0)throw new Error(`${c} cannot be empty`);if(i.length>1)throw new Error(`${c} must be a single character`);s(i,o)}}return t},ze=(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:i}=a;typeof i=="function"&&(i=i()),s[r]=i}else s[r]=o?n:n.pop()}return s},T="--",Ke=/[.:=]/,Ze=/^-{1,2}\w/,Qe=e=>{if(!Ze.test(e))return;const t=!e.startsWith(T);let s=e.slice(t?1:2),r;const n=s.match(Ke);if(n){const{index:o}=n;r=s.slice(o+1),s=s.slice(0,o)}return[s,r,t]},Xe=(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 c=e.slice(o+1);s==null||s(c,[o],!0);break}const i=Qe(a);if(i){if(n(),!t)continue;const[c,l,p]=i;if(p)for(let d=0;d<c.length;d+=1){n();const C=d===c.length-1;r=t(c[d],C?l:void 0,[o,d+1,C])}else r=t(c,l,[o])}else n(a,[o])&&(s==null||s([a],[o]))}n()},Ye=(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)}},ce=(e,t=process.argv.slice(2),{ignore:s}={})=>{const r=[],n=Je(e),o={},a=[];return a[T]=[],Xe(t,{onFlag(i,c,l){const p=L(n,i);if(!(s!=null&&s(p?Ie:xe,i,c))){if(p){const[d,C]=n[i],y=Ge(C,c),S=(B,q)=>{r.push(l),q&&r.push(q),d.push(He(C,B||""))};return y===void 0?S:S(y)}L(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),r.push(l)}},onArgument(i,c,l){s!=null&&s(Re,t[c[0]])||(a.push(...i),l?(a[T]=i,t.splice(c[0])):r.push(c))}}),Ye(t,r),{flags:ze(e,n),unknownFlags:o,_:a}};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 et(e){return(...t)=>t.reduce((s,r)=>U(s,r,"",e),{})}const tt=et(),ue=e=>Array.isArray(e)?e:[e],st=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,s)=>s?s.toUpperCase():""),rt=(e,t)=>t.length!==e.length?!1:e.every((s,r)=>s===t[r]),ot=(e,t)=>t.length>e.length?!1:rt(e.slice(0,t.length),t),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 me extends Error{constructor(t){super(t("core.nameNotSet"))}}class pe 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 ge=typeof Deno!="undefined",nt=typeof process!="undefined"&&!ge,at=process.versions.electron&&!process.defaultApp;function Ee(e,t,s,r){if(s.alias){const n=ue(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[m]&&(s.set(m,e[m]),Ee(s,e,e[m],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){const r=_e(e,s);for(const[n,o]of r.entries()){const a=ce((o==null?void 0:o.flags)||{},[...t]),{_:i}=a;if(n!==m&&ot(i,n))return[o,n]}return r.has(m)?[r.get(m),m]:[void 0,void 0]}const J=()=>nt?process.argv.slice(at?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=>{const n=[];let o=0;const a=i=>{o=i;const c=s[i],l=c(r,a.bind(null,i+1));l&&n.push(l)};if(a(0),o+1===s.length)for(const i of n)i()}}const it=/\s\s+/,Fe=e=>e===m?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!it.test(e),ct=(e,t)=>t?`[${e}]`:`<${e}>`,ut="<Root>",ye=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:ut,Be=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,Ne=e=>e.filter(t=>!t.startsWith("-")),{stringify:N}=JSON;function z(e,t){const s=[];let r,n;for(const o of e){if(n)throw new Error(t("core.spreadParameterMustBeLast",N(n)));const a=o[0],i=o[o.length-1];let c;if(a==="<"&&i===">"&&(c=!0,r))throw new Error(t("core.requiredParameterMustBeBeforeOptional",N(o),N(r)));if(a==="["&&i==="]"&&(c=!1,r=o),c===void 0)throw new Error(t("core.parameterMustBeWrappedInBrackets",N(o)));let l=o.slice(1,-1);const p=l.slice(-3)==="...";p&&(n=o,l=l.slice(0,-3)),s.push({name:l,required:c,spread:p})}return s}function K(e,t,s,r){for(let n=0;n<t.length;n+=1){const{name:o,required:a,spread:i}=t[n],c=st(o);if(c in e)throw new Error(r("core.parameterIsUsedMoreThanOnce",N(o)));const l=i?s.slice(n):s[n];if(i&&(n=t.length),a&&(!l||i&&l.length===0))throw new Error(r("core.missingRequiredParameter",N(o)));e[c]=l}}const lt={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 Z=(e,t,s)=>{if(!t.has(e))throw TypeError("Cannot "+s)},u=(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)},w=(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,M,D,b,j,O,k,P,I,x,R,$,F,Q,ke,X,Se,Y,Ae,g,E,ee,Le,te,We,se,De,G,re,oe,be;const m=Symbol.for("Clerc.Root"),Oe=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,_,""),h(this,v,""),h(this,M,""),h(this,D,[]),h(this,b,{}),h(this,j,new Pe),h(this,O,{}),h(this,k,new Set),h(this,P,void 0),h(this,I,[]),h(this,x,!1),h(this,R,"en"),h(this,$,"en"),h(this,F,{}),this.i18n={add:r=>{w(this,F,tt(u(this,F),r))},t:(r,...n)=>{const o=u(this,F)[u(this,$)]||u(this,F)[u(this,R)],a=u(this,F)[u(this,R)];return o[r]?ae(o[r],...n):a[r]?ae(a[r],...n):void 0}},w(this,_,e||u(this,_)),w(this,v,t||u(this,v)),w(this,M,s||u(this,M)),w(this,$,Be()),f(this,Y,Ae).call(this)}get _name(){return u(this,_)}get _description(){return u(this,v)}get _version(){return u(this,M)}get _inspectors(){return u(this,D)}get _commands(){return u(this,b)}get _flags(){return u(this,O)}static create(e,t,s){return new Oe(e,t,s)}name(e){return f(this,g,E).call(this),w(this,_,e),this}description(e){return f(this,g,E).call(this),w(this,v,e),this}version(e){return f(this,g,E).call(this),w(this,M,e),this}locale(e){if(u(this,x))throw new V(this.i18n.t);return w(this,$,e),this}fallbackLocale(e){if(u(this,x))throw new V(this.i18n.t);return w(this,R,e),this}errorHandler(e){return u(this,I).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 u(this,O)[e]={description:t,...s},this}on(e,t){return u(this,j).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),u(this,D).push(e),this}parse(e=J()){f(this,g,E).call(this);const{argv:t,run:s}=Array.isArray(e)?{argv:e,run:!0}:{argv:J(),...e};return w(this,P,[...t]),f(this,te,We).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){return f(this,G,re).call(this,()=>f(this,oe,be).call(this)),process.title=u(this,_),this}};let ht=Oe;_=new WeakMap,v=new WeakMap,M=new WeakMap,D=new WeakMap,b=new WeakMap,j=new WeakMap,O=new WeakMap,k=new WeakMap,P=new WeakMap,I=new WeakMap,x=new WeakMap,R=new WeakMap,$=new WeakMap,F=new WeakMap,Q=new WeakSet,ke=function(){return u(this,k).has(m)},X=new WeakSet,Se=function(){return Object.prototype.hasOwnProperty.call(this._commands,m)},Y=new WeakSet,Ae=function(){this.i18n.add(lt)},g=new WeakSet,E=function(){w(this,x,!0)},ee=new WeakSet,Le=function(e,t,s={}){f(this,g,E).call(this);const{t:r}=this.i18n,o=(d=>!(typeof d=="string"||d===m))(e),a=o?e.name:e;if(!Fe(a))throw new we(a,r);const{handler:i=void 0,...c}=o?e:{name:a,description:t,...s},l=[c.name],p=c.alias?ue(c.alias):[];c.alias&&l.push(...p);for(const d of l)if(u(this,k).has(d))throw new le(ye(d),r);return u(this,b)[a]=c,u(this,k).add(c.name),p.forEach(d=>u(this,k).add(d)),o&&i&&this.on(e.name,i),this},te=new WeakSet,We=function(){const{t:e}=this.i18n;if(!u(this,_))throw new me(e);if(!u(this,v))throw new pe(e);if(!u(this,M))throw new Ce(e)},se=new WeakSet,De=function(e){const t=u(this,P),{t:s}=this.i18n,[r,n]=e(),o=!!r,a={...u(this,O),...r==null?void 0:r.flags},i=ce(a,[...t]),{_:c,flags:l,unknownFlags:p}=i;let d=!o||r.name===m?c:c.slice(r.name.split(" ").length),C=(r==null?void 0:r.parameters)||[];const y=C.indexOf("--"),S=C.slice(y+1)||[],B=Object.create(null);if(y>-1&&S.length>0){C=C.slice(0,y);const ne=c["--"];d=d.slice(0,-ne.length||void 0),K(B,z(C,s),d,s),K(B,z(S,s),ne,s)}else K(B,z(C,s),d,s);const q={...l,...p};return{name:r==null?void 0:r.name,called:Array.isArray(n)?n.join(" "):n,resolved:o,hasRootOrAlias:u(this,Q,ke),hasRoot:u(this,X,Se),raw:{...i,parameters:d,mergedFlags:q},parameters:B,flags:l,unknownFlags:p,cli:this}},G=new WeakSet,re=function(e){try{e()}catch(t){if(u(this,I).length>0)u(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=u(this,P);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const s=()=>ve(u(this,b),t,e),r=()=>f(this,se,De).call(this,s),n={enforce:"post",fn:i=>{const[c]=s(),l=Ne(t).join(" ");if(!c)throw l?new he(l,e):new de(e);u(this,j).emit(c.name,i)}},o=[...u(this,D),n];Me(o)(r())};const dt=e=>e,ft=(e,t,s)=>s,mt=(e,t)=>t,pt=(e,t)=>({...e,handler:t});export{ht as Clerc,le as CommandExistsError,fe as CommandNameConflictError,pe as DescriptionNotSetError,we as InvalidCommandNameError,V as LocaleNotCalledFirstError,me as NameNotSetError,de as NoCommandGivenError,he as NoSuchCommandError,m as Root,Ce as VersionNotSetError,Me as compose,pt as defineCommand,ft as defineHandler,mt as defineInspector,dt as definePlugin,Be as detectLocale,ye as formatCommandName,Fe as isValidName,J as resolveArgv,ve as resolveCommand,_e as resolveFlattenCommands,Ne as stripFlags,ct as withBrackets};
|
|
1
|
+
import{format as ae}from"node:util";import"@clerc/core";class Pe{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 Ie="known-flag",xe="unknown-flag",Re="argument",{stringify:A}=JSON,$e=/\B([A-Z])/g,qe=e=>e.replace($e,"-$1").toLowerCase(),{hasOwnProperty:Te}=Object.prototype,L=(e,t)=>Te.call(e,t),je=e=>Array.isArray(e),ie=e=>typeof e=="function"?[e,!1]:je(e)?[e[0],!0]:ie(e.type),Ge=(e,t)=>e===Boolean?t!=="false":t,He=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),Ue=/[\s.:=]/,Ve=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(Ue);if(s)throw new Error(`${t} cannot contain ${A(s==null?void 0:s[0])}`)},Je=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;Ve(r);const n=e[r],o=[[],...ie(n),n];s(r,o);const a=qe(r);if(r!==a&&s(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:i}=n,c=`Flag alias ${A(i)} for flag ${A(r)}`;if(i.length===0)throw new Error(`${c} cannot be empty`);if(i.length>1)throw new Error(`${c} must be a single character`);s(i,o)}}return t},ze=(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:i}=a;typeof i=="function"&&(i=i()),s[r]=i}else s[r]=o?n:n.pop()}return s},T="--",Ke=/[.:=]/,Ze=/^-{1,2}\w/,Qe=e=>{if(!Ze.test(e))return;const t=!e.startsWith(T);let s=e.slice(t?1:2),r;const n=s.match(Ke);if(n){const{index:o}=n;r=s.slice(o+1),s=s.slice(0,o)}return[s,r,t]},Xe=(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 c=e.slice(o+1);s==null||s(c,[o],!0);break}const i=Qe(a);if(i){if(n(),!t)continue;const[c,l,m]=i;if(m)for(let d=0;d<c.length;d+=1){n();const C=d===c.length-1;r=t(c[d],C?l:void 0,[o,d+1,C])}else r=t(c,l,[o])}else n(a,[o])&&(s==null||s([a],[o]))}n()},Ye=(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)}},ce=(e,t=process.argv.slice(2),{ignore:s}={})=>{const r=[],n=Je(e),o={},a=[];return a[T]=[],Xe(t,{onFlag(i,c,l){const m=L(n,i);if(!(s!=null&&s(m?Ie:xe,i,c))){if(m){const[d,C]=n[i],y=Ge(C,c),S=(B,q)=>{r.push(l),q&&r.push(q),d.push(He(C,B||""))};return y===void 0?S:S(y)}L(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),r.push(l)}},onArgument(i,c,l){s!=null&&s(Re,t[c[0]])||(a.push(...i),l?(a[T]=i,t.splice(c[0])):r.push(c))}}),Ye(t,r),{flags:ze(e,n),unknownFlags:o,_:a}};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 et(e){return(...t)=>t.reduce((s,r)=>U(s,r,"",e),{})}const tt=et(),ue=e=>Array.isArray(e)?e:[e],st=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,s)=>s?s.toUpperCase():""),rt=(e,t)=>t.length!==e.length?!1:e.every((s,r)=>s===t[r]),ot=(e,t)=>t.length>e.length?!1:rt(e.slice(0,t.length),t),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 ge=typeof Deno!="undefined",nt=typeof process!="undefined"&&!ge,at=process.versions.electron&&!process.defaultApp;function Ee(e,t,s,r){if(s.alias){const n=ue(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){const r=_e(e,s);for(const[n,o]of r.entries()){const a=ce((o==null?void 0:o.flags)||{},[...t]),{_:i}=a;if(n!==p&&ot(i,n))return[o,n]}return r.has(p)?[r.get(p),p]:[void 0,void 0]}const J=()=>nt?process.argv.slice(at?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=>{const n=[];let o=0;const a=i=>{o=i;const c=s[i],l=c(r,a.bind(null,i+1));l&&n.push(l)};if(a(0),o+1===s.length)for(const i of n)i()}}const it=/\s\s+/,Fe=e=>e===p?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!it.test(e),ct=(e,t)=>t?`[${e}]`:`<${e}>`,ut="<Root>",ye=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:ut,Be=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,Ne=e=>e.filter(t=>!t.startsWith("-")),{stringify:N}=JSON;function z(e,t){const s=[];let r,n;for(const o of e){if(n)throw new Error(t("core.spreadParameterMustBeLast",N(n)));const a=o[0],i=o[o.length-1];let c;if(a==="<"&&i===">"&&(c=!0,r))throw new Error(t("core.requiredParameterMustBeBeforeOptional",N(o),N(r)));if(a==="["&&i==="]"&&(c=!1,r=o),c===void 0)throw new Error(t("core.parameterMustBeWrappedInBrackets",N(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:c,spread:m})}return s}function K(e,t,s,r){for(let n=0;n<t.length;n+=1){const{name:o,required:a,spread:i}=t[n],c=st(o);if(c in e)throw new Error(r("core.parameterIsUsedMoreThanOnce",N(o)));const l=i?s.slice(n):s[n];if(i&&(n=t.length),a&&(!l||i&&l.length===0))throw new Error(r("core.missingRequiredParameter",N(o)));e[c]=l}}const lt={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 Z=(e,t,s)=>{if(!t.has(e))throw TypeError("Cannot "+s)},u=(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)},w=(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,M,D,b,j,O,k,P,I,x,R,$,F,Q,ke,X,Se,Y,Ae,g,E,ee,Le,te,We,se,De,G,re,oe,be;const p=Symbol.for("Clerc.Root"),Oe=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,_,""),h(this,v,""),h(this,M,""),h(this,D,[]),h(this,b,{}),h(this,j,new Pe),h(this,O,{}),h(this,k,new Set),h(this,P,void 0),h(this,I,[]),h(this,x,!1),h(this,R,"en"),h(this,$,"en"),h(this,F,{}),this.i18n={add:r=>{w(this,F,tt(u(this,F),r))},t:(r,...n)=>{const o=u(this,F)[u(this,$)]||u(this,F)[u(this,R)],a=u(this,F)[u(this,R)];return o[r]?ae(o[r],...n):a[r]?ae(a[r],...n):void 0}},w(this,_,e||u(this,_)),w(this,v,t||u(this,v)),w(this,M,s||u(this,M)),w(this,$,Be()),f(this,Y,Ae).call(this)}get _name(){return u(this,_)}get _description(){return u(this,v)}get _version(){return u(this,M)}get _inspectors(){return u(this,D)}get _commands(){return u(this,b)}get _flags(){return u(this,O)}static create(e,t,s){return new Oe(e,t,s)}name(e){return f(this,g,E).call(this),w(this,_,e),this}description(e){return f(this,g,E).call(this),w(this,v,e),this}version(e){return f(this,g,E).call(this),w(this,M,e),this}locale(e){if(u(this,x))throw new V(this.i18n.t);return w(this,$,e),this}fallbackLocale(e){if(u(this,x))throw new V(this.i18n.t);return w(this,R,e),this}errorHandler(e){return u(this,I).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 u(this,O)[e]={description:t,...s},this}on(e,t){return u(this,j).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),u(this,D).push(e),this}parse(e=J()){f(this,g,E).call(this);const{argv:t,run:s}=Array.isArray(e)?{argv:e,run:!0}:{argv:J(),...e};return w(this,P,[...t]),f(this,te,We).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){return f(this,G,re).call(this,()=>f(this,oe,be).call(this)),process.title=u(this,_),this}};let ht=Oe;_=new WeakMap,v=new WeakMap,M=new WeakMap,D=new WeakMap,b=new WeakMap,j=new WeakMap,O=new WeakMap,k=new WeakMap,P=new WeakMap,I=new WeakMap,x=new WeakMap,R=new WeakMap,$=new WeakMap,F=new WeakMap,Q=new WeakSet,ke=function(){return u(this,k).has(p)},X=new WeakSet,Se=function(){return Object.prototype.hasOwnProperty.call(this._commands,p)},Y=new WeakSet,Ae=function(){this.i18n.add(lt)},g=new WeakSet,E=function(){w(this,x,!0)},ee=new WeakSet,Le=function(e,t,s={}){f(this,g,E).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:i=void 0,...c}=o?e:{name:a,description:t,...s},l=[c.name],m=c.alias?ue(c.alias):[];c.alias&&l.push(...m);for(const d of l)if(u(this,k).has(d))throw new le(ye(d),r);return u(this,b)[a]=c,u(this,k).add(c.name),m.forEach(d=>u(this,k).add(d)),o&&i&&this.on(e.name,i),this},te=new WeakSet,We=function(){const{t:e}=this.i18n;if(!u(this,_))throw new pe(e);if(!u(this,v))throw new me(e);if(!u(this,M))throw new Ce(e)},se=new WeakSet,De=function(e){const t=u(this,P),{t:s}=this.i18n,[r,n]=e(),o=!!r,a={...u(this,O),...r==null?void 0:r.flags},i=ce(a,[...t]),{_:c,flags:l,unknownFlags:m}=i;let d=!o||r.name===p?c:c.slice(r.name.split(" ").length),C=(r==null?void 0:r.parameters)||[];const y=C.indexOf("--"),S=C.slice(y+1)||[],B=Object.create(null);if(y>-1&&S.length>0){C=C.slice(0,y);const ne=c["--"];d=d.slice(0,-ne.length||void 0),K(B,z(C,s),d,s),K(B,z(S,s),ne,s)}else K(B,z(C,s),d,s);const q={...l,...m};return{name:r==null?void 0:r.name,called:Array.isArray(n)?n.join(" "):n,resolved:o,hasRootOrAlias:u(this,Q,ke),hasRoot:u(this,X,Se),raw:{...i,parameters:d,mergedFlags:q},parameters:B,flags:l,unknownFlags:m,cli:this}},G=new WeakSet,re=function(e){try{e()}catch(t){if(u(this,I).length>0)u(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=u(this,P);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const s=()=>ve(u(this,b),t,e),r=()=>f(this,se,De).call(this,s),n={enforce:"post",fn:i=>{const[c]=s(),l=Ne(t).join(" ");if(!c)throw l?new he(l,e):new de(e);u(this,j).emit(c.name,i)}},o=[...u(this,D),n];Me(o)(r())};const dt=e=>e,ft=(e,t,s)=>s,pt=(e,t)=>t,mt=(e,t)=>({...e,handler:t});export{ht 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,mt as defineCommand,ft as defineHandler,pt as defineInspector,dt as definePlugin,Be as detectLocale,ye as formatCommandName,Fe as isValidName,J as resolveArgv,ve as resolveCommand,_e as resolveFlattenCommands,Ne as stripFlags,ct as withBrackets};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc core",
|
|
6
6
|
"keywords": [
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"lite-emit": "^1.4.0",
|
|
53
53
|
"type-fest": "^3.6.0",
|
|
54
54
|
"type-flag": "^3.0.0",
|
|
55
|
-
"@clerc/utils": "0.
|
|
55
|
+
"@clerc/utils": "0.36.0"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "puild --minify",
|