@clerc/core 0.28.0 → 0.29.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 +67 -19
- package/dist/index.js +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -164,7 +164,7 @@ type LiteralUnion<
|
|
|
164
164
|
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
|
|
165
165
|
type Dict<T> = Record<string, T>;
|
|
166
166
|
type MaybeArray$1<T> = T | T[];
|
|
167
|
-
type CamelCase<T extends string> = T extends `${infer
|
|
167
|
+
type CamelCase<T extends string> = (T extends `${infer Prefix}-${infer Suffix}` | `${infer Prefix} ${infer Suffix}` ? `${Prefix}${Capitalize<CamelCase<Suffix>>}` : T);
|
|
168
168
|
|
|
169
169
|
declare const DOUBLE_DASH = "--";
|
|
170
170
|
type TypeFunction<ReturnType = any> = (value: any) => ReturnType;
|
|
@@ -257,6 +257,13 @@ interface Plugin<T extends Clerc = Clerc, U extends Clerc = Clerc> {
|
|
|
257
257
|
setup: (cli: T) => U;
|
|
258
258
|
}
|
|
259
259
|
|
|
260
|
+
type Locales = Dict<Dict<string>>;
|
|
261
|
+
type TranslateFn = (name: string, ...args: string[]) => string | undefined;
|
|
262
|
+
interface I18N {
|
|
263
|
+
add: (locales: Locales) => void;
|
|
264
|
+
t: TranslateFn;
|
|
265
|
+
}
|
|
266
|
+
|
|
260
267
|
type CommandType = RootType | string;
|
|
261
268
|
type FlagOptions = FlagSchema & {
|
|
262
269
|
description?: string;
|
|
@@ -323,15 +330,13 @@ type InspectorFn<C extends CommandRecord = CommandRecord> = (ctx: InspectorConte
|
|
|
323
330
|
interface InspectorObject<C extends CommandRecord = CommandRecord> {
|
|
324
331
|
enforce?: "pre" | "post";
|
|
325
332
|
fn: InspectorFn<C>;
|
|
326
|
-
}
|
|
327
|
-
interface I18N {
|
|
328
|
-
add: () => void;
|
|
329
333
|
}
|
|
330
334
|
|
|
331
335
|
declare const Root: unique symbol;
|
|
332
336
|
type RootType = typeof Root;
|
|
333
337
|
declare class Clerc<C extends CommandRecord = {}> {
|
|
334
338
|
#private;
|
|
339
|
+
i18n: I18N;
|
|
335
340
|
private constructor();
|
|
336
341
|
get _name(): string;
|
|
337
342
|
get _description(): string;
|
|
@@ -365,7 +370,8 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
365
370
|
* @example
|
|
366
371
|
* ```ts
|
|
367
372
|
* Clerc.create()
|
|
368
|
-
*
|
|
373
|
+
* .description("test cli")
|
|
374
|
+
* ```
|
|
369
375
|
*/
|
|
370
376
|
description(description: string): this;
|
|
371
377
|
/**
|
|
@@ -375,9 +381,36 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
375
381
|
* @example
|
|
376
382
|
* ```ts
|
|
377
383
|
* Clerc.create()
|
|
378
|
-
*
|
|
384
|
+
* .version("1.0.0")
|
|
385
|
+
* ```
|
|
379
386
|
*/
|
|
380
387
|
version(version: string): this;
|
|
388
|
+
/**
|
|
389
|
+
* Set the Locale
|
|
390
|
+
* It's recommended to call this method once after you created the Clerc instance.
|
|
391
|
+
* @param locale
|
|
392
|
+
* @returns
|
|
393
|
+
* @example
|
|
394
|
+
* ```ts
|
|
395
|
+
* Clerc.create()
|
|
396
|
+
* .locale("en")
|
|
397
|
+
* .command(...)
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
locale(locale: string): this;
|
|
401
|
+
/**
|
|
402
|
+
* Set the fallback Locale
|
|
403
|
+
* It's recommended to call this method once after you created the Clerc instance.
|
|
404
|
+
* @param fallbackLocale
|
|
405
|
+
* @returns
|
|
406
|
+
* @example
|
|
407
|
+
* ```ts
|
|
408
|
+
* Clerc.create()
|
|
409
|
+
* .fallbackLocale("en")
|
|
410
|
+
* .command(...)
|
|
411
|
+
* ```
|
|
412
|
+
*/
|
|
413
|
+
fallbackLocale(fallbackLocale: string): this;
|
|
381
414
|
/**
|
|
382
415
|
* Register a command
|
|
383
416
|
* @param name
|
|
@@ -426,7 +459,7 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
426
459
|
* })
|
|
427
460
|
* ```
|
|
428
461
|
*/
|
|
429
|
-
on<K extends LiteralUnion<keyof CM, string>, CM extends this["_commands"] = this["_commands"]>(name: K, handler: Handler<CM, K>): this;
|
|
462
|
+
on<K extends LiteralUnion<keyof CM, string | RootType>, CM extends this["_commands"] = this["_commands"]>(name: K, handler: Handler<CM, K>): this;
|
|
430
463
|
/**
|
|
431
464
|
* Use a plugin
|
|
432
465
|
* @param plugin
|
|
@@ -463,6 +496,16 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
463
496
|
* ```
|
|
464
497
|
*/
|
|
465
498
|
parse(optionsOrArgv?: string[] | ParseOptions): this;
|
|
499
|
+
/**
|
|
500
|
+
* Run matched command
|
|
501
|
+
* @returns
|
|
502
|
+
* @example
|
|
503
|
+
* ```ts
|
|
504
|
+
* Clerc.create()
|
|
505
|
+
* .parse({ run: false })
|
|
506
|
+
* .runMatchedCommand()
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
466
509
|
runMatchedCommand(): this;
|
|
467
510
|
}
|
|
468
511
|
|
|
@@ -475,36 +518,40 @@ declare const defineCommand: <N extends string | typeof Root, O extends CommandO
|
|
|
475
518
|
|
|
476
519
|
declare class CommandExistsError extends Error {
|
|
477
520
|
commandName: string;
|
|
478
|
-
constructor(commandName: string);
|
|
521
|
+
constructor(commandName: string, t: TranslateFn);
|
|
479
522
|
}
|
|
480
523
|
declare class NoSuchCommandError extends Error {
|
|
481
524
|
commandName: string;
|
|
482
|
-
constructor(commandName: string);
|
|
525
|
+
constructor(commandName: string, t: TranslateFn);
|
|
483
526
|
}
|
|
484
527
|
declare class NoCommandGivenError extends Error {
|
|
485
|
-
constructor();
|
|
528
|
+
constructor(t: TranslateFn);
|
|
486
529
|
}
|
|
487
530
|
declare class CommandNameConflictError extends Error {
|
|
488
531
|
n1: string;
|
|
489
532
|
n2: string;
|
|
490
|
-
constructor(n1: string, n2: string);
|
|
533
|
+
constructor(n1: string, n2: string, t: TranslateFn);
|
|
491
534
|
}
|
|
492
535
|
declare class NameNotSetError extends Error {
|
|
493
|
-
constructor();
|
|
536
|
+
constructor(t: TranslateFn);
|
|
494
537
|
}
|
|
495
538
|
declare class DescriptionNotSetError extends Error {
|
|
496
|
-
constructor();
|
|
539
|
+
constructor(t: TranslateFn);
|
|
497
540
|
}
|
|
498
541
|
declare class VersionNotSetError extends Error {
|
|
499
|
-
constructor();
|
|
542
|
+
constructor(t: TranslateFn);
|
|
500
543
|
}
|
|
501
544
|
declare class InvalidCommandNameError extends Error {
|
|
502
545
|
commandName: string;
|
|
503
|
-
constructor(commandName: string);
|
|
546
|
+
constructor(commandName: string, t: TranslateFn);
|
|
547
|
+
}
|
|
548
|
+
declare class LocaleNotCalledFirstError extends Error {
|
|
549
|
+
constructor(t: TranslateFn);
|
|
504
550
|
}
|
|
505
551
|
|
|
506
|
-
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof Root, CommandAlias<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>>;
|
|
507
|
-
declare function resolveCommand(commands: CommandRecord, name:
|
|
552
|
+
declare function resolveFlattenCommands(commands: CommandRecord, t: TranslateFn): Map<string[] | typeof Root, CommandAlias<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>>;
|
|
553
|
+
declare function resolveCommand(commands: CommandRecord, name: CommandType | string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
|
|
554
|
+
declare function resolveCommandStrict(commands: CommandRecord, name: CommandType | string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
|
|
508
555
|
declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>[];
|
|
509
556
|
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>[];
|
|
510
557
|
declare function resolveParametersBeforeFlag(argv: string[]): string[];
|
|
@@ -512,6 +559,7 @@ declare const resolveArgv: () => string[];
|
|
|
512
559
|
declare function compose(inspectors: Inspector[]): (getCtx: () => InspectorContext) => void;
|
|
513
560
|
declare const isInvalidName: (name: CommandType) => boolean;
|
|
514
561
|
declare const withBrackets: (s: string, isOptional?: boolean) => string;
|
|
515
|
-
declare const formatCommandName: (name: string | string[] | RootType) => string;
|
|
562
|
+
declare const formatCommandName: (name: string | string[] | RootType) => string;
|
|
563
|
+
declare const detectLocale: () => string;
|
|
516
564
|
|
|
517
|
-
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, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, PossibleInputKind, Root, RootType, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, formatCommandName, isInvalidName, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class me{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 ge="known-flag",we="unknown-flag",ve="argument",{stringify:S}=JSON,Ee=/\B([A-Z])/g,ye=e=>e.replace(Ee,"-$1").toLowerCase(),{hasOwnProperty:_e}=Object.prototype,k=(e,t)=>_e.call(e,t),Ce=e=>Array.isArray(e),H=e=>typeof e=="function"?[e,!1]:Ce(e)?[e[0],!0]:H(e.type),Me=(e,t)=>e===Boolean?t!=="false":t,Ne=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),$e=/[\s.:=]/,Se=e=>{const t=`Flag name ${S(e)}`;if(e.length===0)throw new Error(`${t} cannot be empty`);if(e.length===1)throw new Error(`${t} must be longer than a character`);const s=e.match($e);if(s)throw new Error(`${t} cannot contain ${S(s==null?void 0:s[0])}`)},ke=e=>{const t={},s=(r,n)=>{if(k(t,r))throw new Error(`Duplicate flags named ${S(r)}`);t[r]=n};for(const r in e){if(!k(e,r))continue;Se(r);const n=e[r],o=[[],...H(n),n];s(r,o);const a=ye(r);if(r!==a&&s(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:i}=n,c=`Flag alias ${S(i)} for flag ${S(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},Ae=(e,t)=>{const s={};for(const r in e){if(!k(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},I="--",be=/[.:=]/,xe=/^-{1,2}\w/,Ie=e=>{if(!xe.test(e))return;const t=!e.startsWith(I);let s=e.slice(t?1:2),r;const n=s.match(be);if(n){const{index:o}=n;r=s.slice(o+1),s=s.slice(0,o)}return[s,r,t]},Oe=(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===I){n();const c=e.slice(o+1);s==null||s(c,[o],!0);break}const i=Ie(a);if(i){if(n(),!t)continue;const[c,l,f]=i;if(f)for(let p=0;p<c.length;p+=1){n();const m=p===c.length-1;r=t(c[p],m?l:void 0,[o,p+1,m])}else r=t(c,l,[o])}else n(a,[o])&&(s==null||s([a],[o]))}n()},We=(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)}},Re=(e,t=process.argv.slice(2),{ignore:s}={})=>{const r=[],n=ke(e),o={},a=[];return a[I]=[],Oe(t,{onFlag(i,c,l){const f=k(n,i);if(!(s!=null&&s(f?ge:we,i,c))){if(f){const[p,m]=n[i],N=Me(m,c),$=(w,g)=>{r.push(l),g&&r.push(g),p.push(Ne(m,w||""))};return N===void 0?$:$(N)}k(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),r.push(l)}},onArgument(i,c,l){s!=null&&s(ve,t[c[0]])||(a.push(...i),l?(a[I]=i,t.splice(c[0])):r.push(c))}}),We(t,r),{flags:Ae(e,n),unknownFlags:o,_:a}},L=e=>Array.isArray(e)?e:[e],Le=e=>e.replace(/-([a-z])/g,(t,s)=>s.toUpperCase()),Fe=(e,t)=>t.length!==e.length?!1:e.every((s,r)=>s===t[r]),J=(e,t)=>t.length>e.length?!1:Fe(e.slice(0,t.length),t);class U extends Error{constructor(t){super(`Command "${t}" exists.`),this.commandName=t}}class z extends Error{constructor(t){super(`No such command: ${t}`),this.commandName=t}}class K extends Error{constructor(){super("No command given.")}}class Z extends Error{constructor(t,s){super(`Command name ${t} conflicts with ${s}. Maybe caused by alias.`),this.n1=t,this.n2=s}}class Q extends Error{constructor(){super("Name not set.")}}class X extends Error{constructor(){super("Description not set.")}}class Y extends Error{constructor(){super("Version not set.")}}class ee extends Error{constructor(t){super(`Bad name format: ${t}`),this.commandName=t}}const te=typeof Deno!="undefined",Pe=typeof process!="undefined"&&!te,Be=process.versions.electron&&!process.defaultApp;function se(e,t,s){if(s.alias){const r=L(s.alias);for(const n of r){if(n in t)throw new Z(t[n].name,s.name);e.set(typeof n=="symbol"?n:n.split(" "),{...s,__isAlias:!0})}}}function re(e){const t=new Map;e[u]&&(t.set(u,e[u]),se(t,e,e[u]));for(const s of Object.values(e))se(t,e,s),t.set(s.name.split(" "),s);return t}function ne(e,t){if(t===u)return e[u];const s=L(t),r=re(e);let n,o;return r.forEach((a,i)=>{if(i===u){n=r.get(u),o=u;return}J(s,i)&&(!o||o===u||i.length>o.length)&&(n=a,o=i)}),n}function oe(e,t,s=1/0){const r=t===""?[]:Array.isArray(t)?t:t.split(" ");return Object.values(e).filter(n=>{const o=n.name.split(" ");return J(o,r)&&o.length-r.length<=s})}const De=e=>oe(e,"",1);function ie(e){const t=[];for(const s of e){if(s.startsWith("-"))break;t.push(s)}return t}const F=()=>Pe?process.argv.slice(Be?1:2):te?Deno.args:[];function ae(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 ce=e=>typeof e=="string"&&(e.startsWith(" ")||e.endsWith(" ")),Te=(e,t)=>t?`[${e}]`:`<${e}>`,je="<Root>",le=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:je,{stringify:C}=JSON;function P(e){const t=[];let s,r;for(const n of e){if(r)throw new Error(`Invalid parameter: Spread parameter ${C(r)} must be last`);const o=n[0],a=n[n.length-1];let i;if(o==="<"&&a===">"&&(i=!0,s))throw new Error(`Invalid parameter: Required parameter ${C(n)} cannot come after optional parameter ${C(s)}`);if(o==="["&&a==="]"&&(i=!1,s=n),i===void 0)throw new Error(`Invalid parameter: ${C(n)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let c=n.slice(1,-1);const l=c.slice(-3)==="...";l&&(r=n,c=c.slice(0,-3)),t.push({name:c,required:i,spread:l})}return t}function B(e,t,s){for(let r=0;r<t.length;r+=1){const{name:n,required:o,spread:a}=t[r],i=Le(n);if(i in e)return new Error(`Invalid parameter: ${C(n)} is used more than once.`);const c=a?s.slice(r):s[r];if(a&&(r=t.length),o&&(!c||a&&c.length===0))return new Error(`Error: Missing required parameter ${C(n)}`);e[i]=c}}var D=(e,t,s)=>{if(!t.has(e))throw TypeError("Cannot "+s)},h=(e,t,s)=>(D(e,t,"read from private field"),s?s.call(e):t.get(e)),d=(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)},v=(e,t,s,r)=>(D(e,t,"write to private field"),r?r.call(e,s):t.set(e,s),s),qe=(e,t,s)=>(D(e,t,"access private method"),s),E,y,_,A,b,O,M,W,T,he,j,fe,q,ue;const u=Symbol("Root"),pe=class{constructor(e,t,s){d(this,T),d(this,j),d(this,q),d(this,E,""),d(this,y,""),d(this,_,""),d(this,A,[]),d(this,b,{}),d(this,O,new me),d(this,M,new Set),d(this,W,void 0),v(this,E,e||h(this,E)),v(this,y,t||h(this,y)),v(this,_,s||h(this,_))}get _name(){return h(this,E)}get _description(){return h(this,y)}get _version(){return h(this,_)}get _inspectors(){return h(this,A)}get _commands(){return h(this,b)}static create(e,t,s){return new pe(e,t,s)}name(e){return v(this,E,e),this}description(e){return v(this,y,e),this}version(e){return v(this,_,e),this}command(e,t,s={}){const n=(f=>!(typeof f=="string"||f===u))(e),o=n?e.name:e;if(ce(o))throw new ee(o);const{handler:a=void 0,...i}=n?e:{name:o,description:t,...s},c=[i.name],l=i.alias?L(i.alias):[];i.alias&&c.push(...l);for(const f of c)if(h(this,M).has(f))throw new U(le(f));return h(this,b)[o]=i,h(this,M).add(i.name),l.forEach(f=>h(this,M).add(f)),n&&a&&this.on(e.name,a),this}on(e,t){return h(this,O).on(e,t),this}use(e){return e.setup(this)}inspector(e){return h(this,A).push(e),this}parse(e=F()){const{argv:t,run:s}=Array.isArray(e)?{argv:e,run:!0}:{argv:F(),...e};return v(this,W,t),qe(this,q,ue).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){const e=h(this,W);if(!e)throw new Error("cli.parse() must be called.");const t=ie(e),s=t.join(" "),r=()=>ne(h(this,b),t),n=[],o=()=>{n.length=0;const l=r(),f=!!l,p=Re((l==null?void 0:l.flags)||{},[...e]),{_:m,flags:N,unknownFlags:$}=p;let w=!f||l.name===u?m:m.slice(l.name.split(" ").length),g=(l==null?void 0:l.parameters)||[];const R=g.indexOf("--"),G=g.slice(R+1)||[],x=Object.create(null);if(R>-1&&G.length>0){g=g.slice(0,R);const V=m["--"];w=w.slice(0,-V.length||void 0),n.push(B(x,P(g),w)),n.push(B(x,P(G),V))}else n.push(B(x,P(g),w));const de={...N,...$};return{name:l==null?void 0:l.name,called:t.length===0&&(l==null?void 0:l.name)?u:s,resolved:f,hasRootOrAlias:h(this,T,he),hasRoot:h(this,j,fe),raw:{...p,parameters:w,mergedFlags:de},parameters:x,flags:N,unknownFlags:$,cli:this}},a={enforce:"post",fn:()=>{const l=r(),f=o(),p=n.filter(Boolean);if(p.length>0)throw p[0];if(!l)throw s?new z(s):new K;h(this,O).emit(l.name,f)}},i=[...h(this,A),a];return ae(i)(o),this}};let Ge=pe;E=new WeakMap,y=new WeakMap,_=new WeakMap,A=new WeakMap,b=new WeakMap,O=new WeakMap,M=new WeakMap,W=new WeakMap,T=new WeakSet,he=function(){return h(this,M).has(u)},j=new WeakSet,fe=function(){return Object.prototype.hasOwnProperty.call(this._commands,u)},q=new WeakSet,ue=function(){if(!h(this,E))throw new Q;if(!h(this,y))throw new X;if(!h(this,_))throw new Y};const Ve=e=>e,He=(e,t,s)=>s,Je=(e,t)=>t,Ue=(e,t)=>({...e,handler:t});export{Ge as Clerc,U as CommandExistsError,Z as CommandNameConflictError,X as DescriptionNotSetError,ee as InvalidCommandNameError,Q as NameNotSetError,K as NoCommandGivenError,z as NoSuchCommandError,u as Root,Y as VersionNotSetError,ae as compose,Ue as defineCommand,He as defineHandler,Je as defineInspector,Ve as definePlugin,le as formatCommandName,ce as isInvalidName,F as resolveArgv,ne as resolveCommand,re as resolveFlattenCommands,ie as resolveParametersBeforeFlag,De as resolveRootCommands,oe as resolveSubcommandsByParent,Te as withBrackets};
|
|
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};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.29.0",
|
|
4
4
|
"author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc core",
|
|
6
6
|
"keywords": [
|
|
@@ -47,11 +47,12 @@
|
|
|
47
47
|
"access": "public"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
+
"defu": "^6.1.2",
|
|
50
51
|
"is-platform": "^1.0.0",
|
|
51
52
|
"lite-emit": "^1.4.0",
|
|
52
53
|
"type-fest": "^3.5.3",
|
|
53
54
|
"type-flag": "^3.0.0",
|
|
54
|
-
"@clerc/utils": "0.
|
|
55
|
+
"@clerc/utils": "0.29.0"
|
|
55
56
|
},
|
|
56
57
|
"scripts": {
|
|
57
58
|
"build": "puild --minify",
|