@clerc/core 0.32.3 → 0.34.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +45 -26
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -305,13 +305,13 @@ type TransformParameters<C extends Command> = {
|
|
|
305
305
|
type MakeEventMap<T extends Commands> = {
|
|
306
306
|
[K in keyof T]: [InspectorContext];
|
|
307
307
|
};
|
|
308
|
-
type FallbackFlags<
|
|
309
|
-
type NonNullableFlag<
|
|
310
|
-
type ParseFlag<C extends Commands, N extends keyof C> = N extends keyof C ? OmitIndexSignature<NonNullableFlag<C[N]>["flags"]> : FallbackFlags<C[N]>["flags"];
|
|
311
|
-
type ParseRaw<C extends Command> = NonNullableFlag<C> & {
|
|
312
|
-
flags: FallbackFlags<C>;
|
|
308
|
+
type FallbackFlags<F extends Flags | undefined> = Equals<NonNullableFlag<F>["flags"], {}> extends true ? Dict<any> : NonNullableFlag<F>["flags"];
|
|
309
|
+
type NonNullableFlag<F extends Flags | undefined> = TypeFlag<NonNullable<F>>;
|
|
310
|
+
type ParseFlag<C extends Commands, N extends keyof C, GF extends GlobalFlagOptions = {}> = N extends keyof C ? OmitIndexSignature<NonNullableFlag<C[N]["flags"] & GF>["flags"]> : FallbackFlags<C[N]["flags"] & GF>["flags"];
|
|
311
|
+
type ParseRaw<C extends Command, GF extends GlobalFlagOptions = {}> = NonNullableFlag<C["flags"] & GF> & {
|
|
312
|
+
flags: FallbackFlags<C["flags"] & GF>;
|
|
313
313
|
parameters: string[];
|
|
314
|
-
mergedFlags: FallbackFlags<C> & NonNullableFlag<C>["unknownFlags"];
|
|
314
|
+
mergedFlags: FallbackFlags<C["flags"] & GF> & NonNullableFlag<C["flags"] & GF>["unknownFlags"];
|
|
315
315
|
};
|
|
316
316
|
type ParseParameters<C extends Commands = Commands, N extends keyof C = keyof C> = Equals<TransformParameters<C[N]>, {}> extends true ? N extends keyof C ? TransformParameters<C[N]> : Dict<string | string[] | undefined> : TransformParameters<C[N]>;
|
|
317
317
|
|
|
@@ -328,12 +328,14 @@ interface I18N {
|
|
|
328
328
|
|
|
329
329
|
type CommandType = RootType | string;
|
|
330
330
|
type FlagOptions = FlagSchema & {
|
|
331
|
-
description
|
|
331
|
+
description: string;
|
|
332
332
|
};
|
|
333
333
|
type Flag = FlagOptions & {
|
|
334
334
|
name: string;
|
|
335
335
|
};
|
|
336
|
+
type GlobalFlagOption = FlagSchema;
|
|
336
337
|
type Flags = Dict<FlagOptions>;
|
|
338
|
+
type GlobalFlagOptions = Dict<GlobalFlagOption>;
|
|
337
339
|
declare interface CommandCustomProperties {
|
|
338
340
|
}
|
|
339
341
|
interface CommandOptions<P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags> extends CommandCustomProperties {
|
|
@@ -360,19 +362,19 @@ interface ParseOptions {
|
|
|
360
362
|
argv?: string[];
|
|
361
363
|
run?: boolean;
|
|
362
364
|
}
|
|
363
|
-
interface HandlerContext<C extends Commands = Commands, N extends keyof C = keyof C> {
|
|
365
|
+
interface HandlerContext<C extends Commands = Commands, N extends keyof C = keyof C, GF extends GlobalFlagOptions = {}> {
|
|
364
366
|
name?: LiteralUnion<N, string>;
|
|
365
367
|
called?: string | RootType;
|
|
366
368
|
resolved: boolean;
|
|
367
369
|
hasRootOrAlias: boolean;
|
|
368
370
|
hasRoot: boolean;
|
|
369
|
-
raw: Simplify<ParseRaw<C[N]>>;
|
|
371
|
+
raw: Simplify<ParseRaw<C[N], GF>>;
|
|
370
372
|
parameters: Simplify<ParseParameters<C, N>>;
|
|
371
373
|
unknownFlags: ParsedFlags["unknownFlags"];
|
|
372
|
-
flags: Simplify<ParseFlag<C, N>>;
|
|
373
|
-
cli: Clerc<C>;
|
|
374
|
+
flags: Simplify<ParseFlag<C, N, GF> & Record<string, any>>;
|
|
375
|
+
cli: Clerc<C, GF>;
|
|
374
376
|
}
|
|
375
|
-
type Handler<C extends Commands = Commands, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K>) => void;
|
|
377
|
+
type Handler<C extends Commands = Commands, K extends keyof C = keyof C, GF extends GlobalFlagOptions = {}> = (ctx: HandlerContext<C, K, GF>) => void;
|
|
376
378
|
type HandlerInCommand<C extends HandlerContext> = (ctx: {
|
|
377
379
|
[K in keyof C]: C[K];
|
|
378
380
|
}) => void;
|
|
@@ -389,7 +391,7 @@ interface InspectorObject<C extends Commands = Commands> {
|
|
|
389
391
|
|
|
390
392
|
declare const Root: unique symbol;
|
|
391
393
|
type RootType = typeof Root;
|
|
392
|
-
declare class Clerc<C extends Commands = {}> {
|
|
394
|
+
declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}> {
|
|
393
395
|
#private;
|
|
394
396
|
i18n: I18N;
|
|
395
397
|
private constructor();
|
|
@@ -398,6 +400,7 @@ declare class Clerc<C extends Commands = {}> {
|
|
|
398
400
|
get _version(): string;
|
|
399
401
|
get _inspectors(): Inspector<Commands>[];
|
|
400
402
|
get _commands(): C;
|
|
403
|
+
get _flags(): GF;
|
|
401
404
|
/**
|
|
402
405
|
* Create a new cli
|
|
403
406
|
* @returns
|
|
@@ -406,7 +409,7 @@ declare class Clerc<C extends Commands = {}> {
|
|
|
406
409
|
* const cli = Clerc.create()
|
|
407
410
|
* ```
|
|
408
411
|
*/
|
|
409
|
-
static create(name?: string, description?: string, version?: string): Clerc<{}>;
|
|
412
|
+
static create(name?: string, description?: string, version?: string): Clerc<{}, {}>;
|
|
410
413
|
/**
|
|
411
414
|
* Set the name of the cli
|
|
412
415
|
* @param name
|
|
@@ -509,8 +512,24 @@ declare class Clerc<C extends Commands = {}> {
|
|
|
509
512
|
* })
|
|
510
513
|
* ```
|
|
511
514
|
*/
|
|
512
|
-
command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O
|
|
513
|
-
command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(name: N, description: string, options?: O & CommandOptions<[...P], A, F>): this & Clerc<C & Record<N, Command<N, O
|
|
515
|
+
command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O>>, GF>;
|
|
516
|
+
command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(name: N, description: string, options?: O & CommandOptions<[...P], A, F>): this & Clerc<C & Record<N, Command<N, O>>, GF>;
|
|
517
|
+
/**
|
|
518
|
+
* Register a global flag
|
|
519
|
+
* @param name
|
|
520
|
+
* @param description
|
|
521
|
+
* @param options
|
|
522
|
+
* @returns
|
|
523
|
+
* @example
|
|
524
|
+
* ```ts
|
|
525
|
+
* Clerc.create()
|
|
526
|
+
* .flag("help", "help", {
|
|
527
|
+
* alias: "h",
|
|
528
|
+
* type: Boolean,
|
|
529
|
+
* })
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
flag<N extends string, O extends GlobalFlagOption>(name: N, description: string, options: O): this & Clerc<C, GF & Record<N, O>>;
|
|
514
533
|
/**
|
|
515
534
|
* Register a handler
|
|
516
535
|
* @param name
|
|
@@ -525,7 +544,7 @@ declare class Clerc<C extends Commands = {}> {
|
|
|
525
544
|
* })
|
|
526
545
|
* ```
|
|
527
546
|
*/
|
|
528
|
-
on<K extends LiteralUnion<keyof CM, string | RootType>, CM extends this["_commands"] = this["_commands"]>(name: K, handler: Handler<CM, K>): this;
|
|
547
|
+
on<K extends LiteralUnion<keyof CM, string | RootType>, CM extends this["_commands"] = this["_commands"]>(name: K, handler: Handler<CM, K, this["_flags"]>): this;
|
|
529
548
|
/**
|
|
530
549
|
* Use a plugin
|
|
531
550
|
* @param plugin
|
|
@@ -577,10 +596,10 @@ declare class Clerc<C extends Commands = {}> {
|
|
|
577
596
|
|
|
578
597
|
type MaybeArray<T> = T | T[];
|
|
579
598
|
|
|
580
|
-
declare const definePlugin: <T extends Clerc<{}>, U extends Clerc<{}>>(p: Plugin<T, U>) => Plugin<T, U>;
|
|
581
|
-
declare const defineHandler: <C extends Clerc<{}>, K extends keyof C["_commands"]>(_cli: C, _key: K, handler: Handler<C["_commands"], K>) => Handler<C["_commands"], K>;
|
|
582
|
-
declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
583
|
-
declare const defineCommand: <N extends string | typeof Root, O extends CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>, P extends string[]>(command: Command<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>, handler?: HandlerInCommand<HandlerContext<Record<N, Command<N, O>> & Record<never, never>, N>> | undefined) => CommandWithHandler<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>;
|
|
599
|
+
declare const definePlugin: <T extends Clerc<{}, {}>, U extends Clerc<{}, {}>>(p: Plugin<T, U>) => Plugin<T, U>;
|
|
600
|
+
declare const defineHandler: <C extends Clerc<{}, {}>, K extends keyof C["_commands"]>(_cli: C, _key: K, handler: Handler<C["_commands"], K, {}>) => Handler<C["_commands"], K, {}>;
|
|
601
|
+
declare const defineInspector: <C extends Clerc<{}, {}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
602
|
+
declare const defineCommand: <N extends string | typeof Root, O extends CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>, P extends string[]>(command: Command<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>, handler?: HandlerInCommand<HandlerContext<Record<N, Command<N, O>> & Record<never, never>, N, {}>> | undefined) => CommandWithHandler<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>;
|
|
584
603
|
|
|
585
604
|
declare class CommandExistsError extends Error {
|
|
586
605
|
commandName: string;
|
|
@@ -616,16 +635,16 @@ declare class LocaleNotCalledFirstError extends Error {
|
|
|
616
635
|
}
|
|
617
636
|
|
|
618
637
|
declare function resolveFlattenCommands(commands: Commands, t: TranslateFn): Map<string[] | typeof Root, CommandAlias<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>>;
|
|
619
|
-
declare function resolveCommand(commands: Commands,
|
|
638
|
+
declare function resolveCommand(commands: Commands, argv: string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
|
|
620
639
|
declare function resolveCommandStrict(commands: Commands, name: CommandType | string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
|
|
621
640
|
declare function resolveSubcommandsByParent(commands: Commands, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>[];
|
|
622
641
|
declare const resolveRootCommands: (commands: Commands) => Command<string, CommandOptions<string[], MaybeArray$1<string | typeof Root>, Flags>>[];
|
|
623
|
-
declare function resolveParametersBeforeFlag(argv: string[]): string[];
|
|
624
642
|
declare const resolveArgv: () => string[];
|
|
625
|
-
declare function compose(inspectors: Inspector[]): (
|
|
643
|
+
declare function compose(inspectors: Inspector[]): (ctx: InspectorContext) => void;
|
|
626
644
|
declare const isValidName: (name: CommandType) => boolean;
|
|
627
645
|
declare const withBrackets: (s: string, isOptional?: boolean) => string;
|
|
628
646
|
declare const formatCommandName: (name: string | string[] | RootType) => string;
|
|
629
|
-
declare const detectLocale: () => string;
|
|
647
|
+
declare const detectLocale: () => string;
|
|
648
|
+
declare const stripFlags: (argv: string[]) => string[];
|
|
630
649
|
|
|
631
|
-
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, Root, RootType, TranslateFn, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveCommandStrict, resolveFlattenCommands,
|
|
650
|
+
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, resolveCommandStrict, resolveFlattenCommands, resolveRootCommands, resolveSubcommandsByParent, stripFlags, withBrackets };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{format as ie}from"node:util";class Ie{constructor(){this.listenerMap={},this.wildcardListeners=new Set}on(t,r){return t==="*"?(this.wildcardListeners.add(r),this):(this.listenerMap[t]||(this.listenerMap[t]=new Set),this.listenerMap[t].add(r),this)}emit(t,...r){return this.listenerMap[t]&&(this.wildcardListeners.forEach(s=>s(t,...r)),this.listenerMap[t].forEach(s=>s(...r))),this}off(t,r){var s,n;return t==="**"?(this.listenerMap={},this.wildcardListeners.clear(),this):t==="*"?(r?this.wildcardListeners.delete(r):this.wildcardListeners.clear(),this):(r?(s=this.listenerMap[t])==null||s.delete(r):(n=this.listenerMap[t])==null||n.clear(),this)}}const xe="known-flag",Re="unknown-flag",$e="argument",{stringify:S}=JSON,je=/\B([A-Z])/g,qe=e=>e.replace(je,"-$1").toLowerCase(),{hasOwnProperty:Te}=Object.prototype,k=(e,t)=>Te.call(e,t),Ge=e=>Array.isArray(e),ce=e=>typeof e=="function"?[e,!1]:Ge(e)?[e[0],!0]:ce(e.type),He=(e,t)=>e===Boolean?t!=="false":t,Ue=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),Ve=/[\s.:=]/,Je=e=>{const t=`Flag name ${S(e)}`;if(e.length===0)throw new Error(`${t} cannot be empty`);if(e.length===1)throw new Error(`${t} must be longer than a character`);const r=e.match(Ve);if(r)throw new Error(`${t} cannot contain ${S(r==null?void 0:r[0])}`)},ze=e=>{const t={},r=(s,n)=>{if(k(t,s))throw new Error(`Duplicate flags named ${S(s)}`);t[s]=n};for(const s in e){if(!k(e,s))continue;Je(s);const n=e[s],o=[[],...ce(n),n];r(s,o);const a=qe(s);if(s!==a&&r(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:i}=n,c=`Flag alias ${S(i)} for flag ${S(s)}`;if(i.length===0)throw new Error(`${c} cannot be empty`);if(i.length>1)throw new Error(`${c} must be a single character`);r(i,o)}}return t},Ke=(e,t)=>{const r={};for(const s in e){if(!k(e,s))continue;const[n,,o,a]=t[s];if(n.length===0&&"default"in a){let{default:i}=a;typeof i=="function"&&(i=i()),r[s]=i}else r[s]=o?n:n.pop()}return r},R="--",Ze=/[.:=]/,Qe=/^-{1,2}\w/,Xe=e=>{if(!Qe.test(e))return;const t=!e.startsWith(R);let r=e.slice(t?1:2),s;const n=r.match(Ze);if(n){const{index:o}=n;s=r.slice(o+1),r=r.slice(0,o)}return[r,s,t]},Ye=(e,{onFlag:t,onArgument:r})=>{let s;const n=(o,a)=>{if(typeof s!="function")return!0;s(o,a),s=void 0};for(let o=0;o<e.length;o+=1){const a=e[o];if(a===R){n();const c=e.slice(o+1);r==null||r(c,[o],!0);break}const i=Xe(a);if(i){if(n(),!t)continue;const[c,l,m]=i;if(m)for(let h=0;h<c.length;h+=1){n();const E=h===c.length-1;s=t(c[h],E?l:void 0,[o,h+1,E])}else s=t(c,l,[o])}else n(a,[o])&&(r==null||r([a],[o]))}n()},et=(e,t)=>{for(const[r,s,n]of t.reverse()){if(s){const o=e[r];let a=o.slice(0,s);if(n||(a+=o.slice(s+1)),a!=="-"){e[r]=a;continue}}e.splice(r,1)}},tt=(e,t=process.argv.slice(2),{ignore:r}={})=>{const s=[],n=ze(e),o={},a=[];return a[R]=[],Ye(t,{onFlag(i,c,l){const m=k(n,i);if(!(r!=null&&r(m?xe:Re,i,c))){if(m){const[h,E]=n[i],A=He(E,c),v=(T,G)=>{s.push(l),G&&s.push(G),h.push(Ue(E,T||""))};return A===void 0?v:v(A)}k(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),s.push(l)}},onArgument(i,c,l){r!=null&&r($e,t[c[0]])||(a.push(...i),l?(a[R]=i,t.splice(c[0])):s.push(c))}}),et(t,s),{flags:Ke(e,n),unknownFlags:o,_:a}};function H(e){return e!==null&&typeof e=="object"}function U(e,t,r=".",s){if(!H(t))return U(e,{},r,s);const n=Object.assign({},t);for(const o in e){if(o==="__proto__"||o==="constructor")continue;const a=e[o];a!=null&&(s&&s(n,o,a,r)||(Array.isArray(a)&&Array.isArray(n[o])?n[o]=[...a,...n[o]]:H(a)&&H(n[o])?n[o]=U(a,n[o],(r?`${r}.`:"")+o.toString(),s):n[o]=a))}return n}function rt(e){return(...t)=>t.reduce((r,s)=>U(r,s,"",e),{})}const st=rt(),$=e=>Array.isArray(e)?e:[e],ot=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,r)=>r?r.toUpperCase():""),ue=(e,t)=>t.length!==e.length?!1:e.every((r,s)=>r===t[s]),le=(e,t)=>t.length>e.length?!1:ue(e.slice(0,t.length),t),L=JSON.stringify;class he extends Error{constructor(t,r){super(r("core.commandExists",L(t))),this.commandName=t}}class de extends Error{constructor(t,r){super(r("core.noSuchCommand",L(t))),this.commandName=t}}class fe extends Error{constructor(t){super(t("core.noCommandGiven"))}}class me extends Error{constructor(t,r,s){super(s("core.commandNameConflict",L(t),L(r))),this.n1=t,this.n2=r}}class pe extends Error{constructor(t){super(t("core.nameNotSet"))}}class Ce extends Error{constructor(t){super(t("core.descriptionNotSet"))}}class we extends Error{constructor(t){super(t("core.versionNotSet"))}}class Ee extends Error{constructor(t,r){super(r("core.badNameFormat",L(t))),this.commandName=t}}class V extends Error{constructor(t){super(t("core.localeMustBeCalledFirst"))}}const ge=typeof Deno!="undefined",nt=typeof process!="undefined"&&!ge,at=process.versions.electron&&!process.defaultApp;function _e(e,t,r,s){if(r.alias){const n=$(r.alias);for(const o of n){if(o in t)throw new me(t[o].name,r.name,s);e.set(typeof o=="symbol"?o:o.split(" "),{...r,__isAlias:!0})}}}function J(e,t){const r=new Map;e[f]&&(r.set(f,e[f]),_e(r,e,e[f],t));for(const s of Object.values(e))_e(r,e,s,t),r.set(s.name.split(" "),s);return r}function ve(e,t,r){if(t===f)return[e[f],f];const s=$(t),n=J(e,r);let o,a;return n.forEach((i,c)=>{if(c===f){o=e[f],a=f;return}le(s,c)&&(!a||a===f||c.length>a.length)&&(o=i,a=c)}),[o,a]}function it(e,t,r){if(t===f)return[e[f],f];const s=$(t),n=J(e,r);let o,a;return n.forEach((i,c)=>{c===f||a===f||ue(s,c)&&(o=i,a=c)}),[o,a]}function Me(e,t,r=1/0){const s=t===""?[]:Array.isArray(t)?t:t.split(" ");return Object.values(e).filter(n=>{const o=n.name.split(" ");return le(o,s)&&o.length-s.length<=r})}const ct=e=>Me(e,"",1);function Fe(e){const t=[];for(const r of e){if(r.startsWith("-"))break;t.push(r)}return t}const z=()=>nt?process.argv.slice(at?1:2):ge?Deno.args:[];function ye(e){const t={pre:[],normal:[],post:[]};for(const s of e){const n=typeof s=="object"?s:{fn:s},{enforce:o,fn:a}=n;o==="post"||o==="pre"?t[o].push(a):t.normal.push(a)}const r=[...t.pre,...t.normal,...t.post];return s=>{const n=[];let o=0;const a=i=>{o=i;const c=r[i],l=c(s(),a.bind(null,i+1));l&&n.push(l)};if(a(0),o+1===r.length)for(const i of n)i()}}const ut=/\s\s+/,Be=e=>e===f?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!ut.test(e),lt=(e,t)=>t?`[${e}]`:`<${e}>`,ht="<Root>",Ne=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:ht,Ae=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,{stringify:B}=JSON;function K(e,t){const r=[];let s,n;for(const o of e){if(n)throw new Error(t("core.spreadParameterMustBeLast",B(n)));const a=o[0],i=o[o.length-1];let c;if(a==="<"&&i===">"&&(c=!0,s))throw new Error(t("core.requiredParameterMustBeBeforeOptional",B(o),B(s)));if(a==="["&&i==="]"&&(c=!1,s=o),c===void 0)throw new Error(t("core.parameterMustBeWrappedInBrackets",B(o)));let l=o.slice(1,-1);const m=l.slice(-3)==="...";m&&(n=o,l=l.slice(0,-3)),r.push({name:l,required:c,spread:m})}return r}function Z(e,t,r,s){for(let n=0;n<t.length;n+=1){const{name:o,required:a,spread:i}=t[n],c=ot(o);if(c in e)throw new Error(s("core.parameterIsUsedMoreThanOnce",B(o)));const l=i?r.slice(n):r[n];if(i&&(n=t.length),a&&(!l||i&&l.length===0))throw new Error(s("core.missingRequiredParameter",B(o)));e[c]=l}}const dt={en:{"core.commandExists":'Command "%s" exists.',"core.noSuchCommand":"No such command: %s.","core.noCommandGiven":"No command given.","core.commandNameConflict":"Command name %s conflicts with %s. Maybe caused by alias.","core.nameNotSet":"Name not set.","core.descriptionNotSet":"Description not set.","core.versionNotSet":"Version not set.","core.badNameFormat":"Bad name format: %s.","core.localeMustBeCalledFirst":"locale() or fallbackLocale() must be called at first.","core.cliParseMustBeCalled":"cli.parse() must be called.","core.spreadParameterMustBeLast":"Invalid Parameter: Spread parameter %s must be last.","core.requiredParameterCannotComeAfterOptionalParameter":"Invalid Parameter: Required parameter %s cannot come after optional parameter %s.","core.parameterMustBeWrappedInBrackets":"Invalid Parameter: Parameter %s must be wrapped in <> (required parameter) or [] (optional parameter).","core.parameterIsUsedMoreThanOnce":"Invalid Parameter: Parameter %s is used more than once.","core.missingRequiredParameter":"Missing required parameter %s."},"zh-CN":{"core.commandExists":'\u547D\u4EE4 "%s" \u5DF2\u5B58\u5728\u3002',"core.noSuchCommand":"\u627E\u4E0D\u5230\u547D\u4EE4: %s\u3002","core.noCommandGiven":"\u6CA1\u6709\u8F93\u5165\u547D\u4EE4\u3002","core.commandNameConflict":"\u547D\u4EE4\u540D\u79F0 %s \u548C %s \u51B2\u7A81\u3002 \u53EF\u80FD\u662F\u7531\u4E8E\u522B\u540D\u5BFC\u81F4\u7684\u3002","core.nameNotSet":"\u672A\u8BBE\u7F6ECLI\u540D\u79F0\u3002","core.descriptionNotSet":"\u672A\u8BBE\u7F6ECLI\u63CF\u8FF0\u3002","core.versionNotSet":"\u672A\u8BBE\u7F6ECLI\u7248\u672C\u3002","core.badNameFormat":"\u9519\u8BEF\u7684\u547D\u4EE4\u540D\u5B57\u683C\u5F0F: %s\u3002","core.localeMustBeCalledFirst":"locale() \u6216 fallbackLocale() \u5FC5\u987B\u5728\u6700\u5F00\u59CB\u8C03\u7528\u3002","core.cliParseMustBeCalled":"cli.parse() \u5FC5\u987B\u88AB\u8C03\u7528\u3002","core.spreadParameterMustBeLast":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5C55\u5F00\u53C2\u6570 %s \u5FC5\u987B\u5728\u6700\u540E\u3002","core.requiredParameterCannotComeAfterOptionalParameter":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u5FC5\u586B\u53C2\u6570 %s \u4E0D\u80FD\u5728\u53EF\u9009\u53C2\u6570 %s \u4E4B\u540E\u3002","core.parameterMustBeWrappedInBrackets":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u5FC5\u987B\u88AB <> (\u5FC5\u586B\u53C2\u6570) \u6216 [] (\u53EF\u9009\u53C2\u6570) \u5305\u88F9\u3002","core.parameterIsUsedMoreThanOnce":"\u4E0D\u5408\u6CD5\u7684\u53C2\u6570: \u53C2\u6570 %s \u88AB\u4F7F\u7528\u4E86\u591A\u6B21\u3002","core.missingRequiredParameter":"\u7F3A\u5C11\u5FC5\u586B\u53C2\u6570 %s\u3002"}};var Q=(e,t,r)=>{if(!t.has(e))throw TypeError("Cannot "+r)},u=(e,t,r)=>(Q(e,t,"read from private field"),r?r.call(e):t.get(e)),d=(e,t,r)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,r)},C=(e,t,r,s)=>(Q(e,t,"write to private field"),s?s.call(e,r):t.set(e,r),r),p=(e,t,r)=>(Q(e,t,"access private method"),r),_,M,F,D,W,j,N,b,P,O,I,x,y,X,Se,Y,ke,ee,Le,w,g,te,De,re,We,se,be,q,oe,ne,Pe;const f=Symbol.for("Clerc.Root"),Oe=class{constructor(e,t,r){d(this,X),d(this,Y),d(this,ee),d(this,w),d(this,te),d(this,re),d(this,se),d(this,q),d(this,ne),d(this,_,""),d(this,M,""),d(this,F,""),d(this,D,[]),d(this,W,{}),d(this,j,new Ie),d(this,N,new Set),d(this,b,void 0),d(this,P,[]),d(this,O,!1),d(this,I,"en"),d(this,x,"en"),d(this,y,{}),this.i18n={add:s=>{C(this,y,st(u(this,y),s))},t:(s,...n)=>{const o=u(this,y)[u(this,x)]||u(this,y)[u(this,I)],a=u(this,y)[u(this,I)];return o[s]?ie(o[s],...n):a[s]?ie(a[s],...n):void 0}},C(this,_,e||u(this,_)),C(this,M,t||u(this,M)),C(this,F,r||u(this,F)),C(this,x,Ae()),p(this,ee,Le).call(this)}get _name(){return u(this,_)}get _description(){return u(this,M)}get _version(){return u(this,F)}get _inspectors(){return u(this,D)}get _commands(){return u(this,W)}static create(e,t,r){return new Oe(e,t,r)}name(e){return p(this,w,g).call(this),C(this,_,e),this}description(e){return p(this,w,g).call(this),C(this,M,e),this}version(e){return p(this,w,g).call(this),C(this,F,e),this}locale(e){if(u(this,O))throw new V(this.i18n.t);return C(this,x,e),this}fallbackLocale(e){if(u(this,O))throw new V(this.i18n.t);return C(this,I,e),this}errorHandler(e){return u(this,P).push(e),this}command(e,t,r={}){return p(this,q,oe).call(this,()=>p(this,te,De).call(this,e,t,r)),this}on(e,t){return u(this,j).on(e,t),this}use(e){return p(this,w,g).call(this),e.setup(this)}inspector(e){return p(this,w,g).call(this),u(this,D).push(e),this}parse(e=z()){p(this,w,g).call(this);const{argv:t,run:r}=Array.isArray(e)?{argv:e,run:!0}:{argv:z(),...e};return C(this,b,[...t]),p(this,re,We).call(this),r&&this.runMatchedCommand(),this}runMatchedCommand(){return p(this,q,oe).call(this,()=>p(this,ne,Pe).call(this)),process.title=u(this,_),this}};let ft=Oe;_=new WeakMap,M=new WeakMap,F=new WeakMap,D=new WeakMap,W=new WeakMap,j=new WeakMap,N=new WeakMap,b=new WeakMap,P=new WeakMap,O=new WeakMap,I=new WeakMap,x=new WeakMap,y=new WeakMap,X=new WeakSet,Se=function(){return u(this,N).has(f)},Y=new WeakSet,ke=function(){return Object.prototype.hasOwnProperty.call(this._commands,f)},ee=new WeakSet,Le=function(){this.i18n.add(dt)},w=new WeakSet,g=function(){C(this,O,!0)},te=new WeakSet,De=function(e,t,r={}){p(this,w,g).call(this);const{t:s}=this.i18n,o=(h=>!(typeof h=="string"||h===f))(e),a=o?e.name:e;if(!Be(a))throw new Ee(a,s);const{handler:i=void 0,...c}=o?e:{name:a,description:t,...r},l=[c.name],m=c.alias?$(c.alias):[];c.alias&&l.push(...m);for(const h of l)if(u(this,N).has(h))throw new he(Ne(h),s);return u(this,W)[a]=c,u(this,N).add(c.name),m.forEach(h=>u(this,N).add(h)),o&&i&&this.on(e.name,i),this},re=new WeakSet,We=function(){const{t:e}=this.i18n;if(!u(this,_))throw new pe(e);if(!u(this,M))throw new Ce(e);if(!u(this,F))throw new we(e)},se=new WeakSet,be=function(e){const t=u(this,b),{t:r}=this.i18n,[s,n]=e(),o=!!s,a=tt((s==null?void 0:s.flags)||{},[...t]),{_:i,flags:c,unknownFlags:l}=a;let m=!o||s.name===f?i:i.slice(s.name.split(" ").length),h=(s==null?void 0:s.parameters)||[];const E=h.indexOf("--"),A=h.slice(E+1)||[],v=Object.create(null);if(E>-1&&A.length>0){h=h.slice(0,E);const ae=i["--"];m=m.slice(0,-ae.length||void 0),Z(v,K(h,r),m,r),Z(v,K(A,r),ae,r)}else Z(v,K(h,r),m,r);const T={...c,...l};return{name:s==null?void 0:s.name,called:Array.isArray(n)?n.join(" "):n,resolved:o,hasRootOrAlias:u(this,X,Se),hasRoot:u(this,Y,ke),raw:{...a,parameters:m,mergedFlags:T},parameters:v,flags:c,unknownFlags:l,cli:this}},q=new WeakSet,oe=function(e){try{e()}catch(t){if(u(this,P).length>0)u(this,P).forEach(r=>r(t));else throw t}},ne=new WeakSet,Pe=function(){p(this,w,g).call(this);const{t:e}=this.i18n,t=u(this,b);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const r=Fe(t),s=r.join(" "),n=()=>ve(u(this,W),r,e),o=()=>p(this,se,be).call(this,n),a={enforce:"post",fn:()=>{const[l]=n(),m=o();if(!l)throw s?new de(s,e):new fe(e);u(this,j).emit(l.name,m)}},i=[...u(this,D),a];ye(i)(o)};const mt=e=>e,pt=(e,t,r)=>r,Ct=(e,t)=>t,wt=(e,t)=>({...e,handler:t});export{ft as Clerc,he as CommandExistsError,me as CommandNameConflictError,Ce as DescriptionNotSetError,Ee as InvalidCommandNameError,V as LocaleNotCalledFirstError,pe as NameNotSetError,fe as NoCommandGivenError,de as NoSuchCommandError,f as Root,we as VersionNotSetError,ye as compose,wt as defineCommand,pt as defineHandler,Ct as defineInspector,mt as definePlugin,Ae as detectLocale,Ne as formatCommandName,Be as isValidName,z as resolveArgv,ve as resolveCommand,it as resolveCommandStrict,J as resolveFlattenCommands,Fe as resolveParametersBeforeFlag,ct as resolveRootCommands,Me as resolveSubcommandsByParent,lt as withBrackets};
|
|
1
|
+
import{format as ce}from"node:util";class Re{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 $e="known-flag",je="unknown-flag",qe="argument",{stringify:k}=JSON,Te=/\B([A-Z])/g,Ge=e=>e.replace(Te,"-$1").toLowerCase(),{hasOwnProperty:He}=Object.prototype,L=(e,t)=>He.call(e,t),Ue=e=>Array.isArray(e),ue=e=>typeof e=="function"?[e,!1]:Ue(e)?[e[0],!0]:ue(e.type),Ve=(e,t)=>e===Boolean?t!=="false":t,Je=(e,t)=>typeof t=="boolean"?t:e===Number&&t===""?Number.NaN:e(t),ze=/[\s.:=]/,Ke=e=>{const t=`Flag name ${k(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(ze);if(s)throw new Error(`${t} cannot contain ${k(s==null?void 0:s[0])}`)},Ze=e=>{const t={},s=(r,n)=>{if(L(t,r))throw new Error(`Duplicate flags named ${k(r)}`);t[r]=n};for(const r in e){if(!L(e,r))continue;Ke(r);const n=e[r],o=[[],...ue(n),n];s(r,o);const a=Ge(r);if(r!==a&&s(a,o),"alias"in n&&typeof n.alias=="string"){const{alias:i}=n,c=`Flag alias ${k(i)} for flag ${k(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},Qe=(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},q="--",Xe=/[.:=]/,Ye=/^-{1,2}\w/,et=e=>{if(!Ye.test(e))return;const t=!e.startsWith(q);let s=e.slice(t?1:2),r;const n=s.match(Xe);if(n){const{index:o}=n;r=s.slice(o+1),s=s.slice(0,o)}return[s,r,t]},tt=(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===q){n();const c=e.slice(o+1);s==null||s(c,[o],!0);break}const i=et(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()},st=(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)}},le=(e,t=process.argv.slice(2),{ignore:s}={})=>{const r=[],n=Ze(e),o={},a=[];return a[q]=[],tt(t,{onFlag(i,c,l){const p=L(n,i);if(!(s!=null&&s(p?$e:je,i,c))){if(p){const[d,C]=n[i],y=Ve(C,c),A=(B,j)=>{r.push(l),j&&r.push(j),d.push(Je(C,B||""))};return y===void 0?A:A(y)}L(o,i)||(o[i]=[]),o[i].push(c===void 0?!0:c),r.push(l)}},onArgument(i,c,l){s!=null&&s(qe,t[c[0]])||(a.push(...i),l?(a[q]=i,t.splice(c[0])):r.push(c))}}),st(t,r),{flags:Qe(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 rt(e){return(...t)=>t.reduce((s,r)=>U(s,r,"",e),{})}const ot=rt(),V=e=>Array.isArray(e)?e:[e],nt=e=>e.replace(/[\W_]([a-z\d])?/gi,(t,s)=>s?s.toUpperCase():""),he=(e,t)=>t.length!==e.length?!1:e.every((s,r)=>s===t[r]),de=(e,t)=>t.length>e.length?!1:he(e.slice(0,t.length),t),W=JSON.stringify;class fe extends Error{constructor(t,s){super(s("core.commandExists",W(t))),this.commandName=t}}class me extends Error{constructor(t,s){super(s("core.noSuchCommand",W(t))),this.commandName=t}}class pe extends Error{constructor(t){super(t("core.noCommandGiven"))}}class Ce extends Error{constructor(t,s,r){super(r("core.commandNameConflict",W(t),W(s))),this.n1=t,this.n2=s}}class we extends Error{constructor(t){super(t("core.nameNotSet"))}}class ge extends Error{constructor(t){super(t("core.descriptionNotSet"))}}class Ee extends Error{constructor(t){super(t("core.versionNotSet"))}}class _e extends Error{constructor(t,s){super(s("core.badNameFormat",W(t))),this.commandName=t}}class J extends Error{constructor(t){super(t("core.localeMustBeCalledFirst"))}}const ve=typeof Deno!="undefined",at=typeof process!="undefined"&&!ve,it=process.versions.electron&&!process.defaultApp;function Me(e,t,s,r){if(s.alias){const n=V(s.alias);for(const o of n){if(o in t)throw new Ce(t[o].name,s.name,r);e.set(typeof o=="symbol"?o:o.split(" "),{...s,__isAlias:!0})}}}function z(e,t){const s=new Map;e[f]&&(s.set(f,e[f]),Me(s,e,e[f],t));for(const r of Object.values(e))Me(s,e,r,t),s.set(r.name.split(" "),r);return s}function Fe(e,t,s){const r=z(e,s);for(const[n,o]of r.entries()){const a=le((o==null?void 0:o.flags)||{},[...t]),{_:i}=a;if(n!==f&&de(i,n))return[o,n]}return r.has(f)?[r.get(f),f]:[void 0,void 0]}function ct(e,t,s){if(t===f)return[e[f],f];const r=V(t),n=z(e,s);let o,a;return n.forEach((i,c)=>{c===f||a===f||he(r,c)&&(o=i,a=c)}),[o,a]}function ye(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 de(o,r)&&o.length-r.length<=s})}const ut=e=>ye(e,"",1),K=()=>at?process.argv.slice(it?1:2):ve?Deno.args:[];function Be(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 lt=/\s\s+/,Ne=e=>e===f?!0:!(e.startsWith(" ")||e.endsWith(" "))&&!lt.test(e),ht=(e,t)=>t?`[${e}]`:`<${e}>`,dt="<Root>",Se=e=>Array.isArray(e)?e.join(" "):typeof e=="string"?e:dt,Ae=()=>process.env.CLERC_LOCALE?process.env.CLERC_LOCALE:Intl.DateTimeFormat().resolvedOptions().locale,ke=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 Q(e,t,s,r){for(let n=0;n<t.length;n+=1){const{name:o,required:a,spread:i}=t[n],c=nt(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 ft={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 X=(e,t,s)=>{if(!t.has(e))throw TypeError("Cannot "+s)},u=(e,t,s)=>(X(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)=>(X(e,t,"write to private field"),r?r.call(e,s):t.set(e,s),s),m=(e,t,s)=>(X(e,t,"access private method"),s),_,v,M,D,b,T,O,S,P,I,x,R,$,F,Y,Le,ee,We,te,De,g,E,se,be,re,Oe,oe,Pe,G,ne,ae,Ie;const f=Symbol.for("Clerc.Root"),xe=class{constructor(e,t,s){h(this,Y),h(this,ee),h(this,te),h(this,g),h(this,se),h(this,re),h(this,oe),h(this,G),h(this,ae),h(this,_,""),h(this,v,""),h(this,M,""),h(this,D,[]),h(this,b,{}),h(this,T,new Re),h(this,O,{}),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,F,{}),this.i18n={add:r=>{w(this,F,ot(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]?ce(o[r],...n):a[r]?ce(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,$,Ae()),m(this,te,De).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 xe(e,t,s)}name(e){return m(this,g,E).call(this),w(this,_,e),this}description(e){return m(this,g,E).call(this),w(this,v,e),this}version(e){return m(this,g,E).call(this),w(this,M,e),this}locale(e){if(u(this,x))throw new J(this.i18n.t);return w(this,$,e),this}fallbackLocale(e){if(u(this,x))throw new J(this.i18n.t);return w(this,R,e),this}errorHandler(e){return u(this,I).push(e),this}command(e,t,s={}){return m(this,G,ne).call(this,()=>m(this,se,be).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,T).on(e,t),this}use(e){return m(this,g,E).call(this),e.setup(this)}inspector(e){return m(this,g,E).call(this),u(this,D).push(e),this}parse(e=K()){m(this,g,E).call(this);const{argv:t,run:s}=Array.isArray(e)?{argv:e,run:!0}:{argv:K(),...e};return w(this,P,[...t]),m(this,re,Oe).call(this),s&&this.runMatchedCommand(),this}runMatchedCommand(){return m(this,G,ne).call(this,()=>m(this,ae,Ie).call(this)),process.title=u(this,_),this}};let mt=xe;_=new WeakMap,v=new WeakMap,M=new WeakMap,D=new WeakMap,b=new WeakMap,T=new WeakMap,O=new WeakMap,S=new WeakMap,P=new WeakMap,I=new WeakMap,x=new WeakMap,R=new WeakMap,$=new WeakMap,F=new WeakMap,Y=new WeakSet,Le=function(){return u(this,S).has(f)},ee=new WeakSet,We=function(){return Object.prototype.hasOwnProperty.call(this._commands,f)},te=new WeakSet,De=function(){this.i18n.add(ft)},g=new WeakSet,E=function(){w(this,x,!0)},se=new WeakSet,be=function(e,t,s={}){m(this,g,E).call(this);const{t:r}=this.i18n,o=(d=>!(typeof d=="string"||d===f))(e),a=o?e.name:e;if(!Ne(a))throw new _e(a,r);const{handler:i=void 0,...c}=o?e:{name:a,description:t,...s},l=[c.name],p=c.alias?V(c.alias):[];c.alias&&l.push(...p);for(const d of l)if(u(this,S).has(d))throw new fe(Se(d),r);return u(this,b)[a]=c,u(this,S).add(c.name),p.forEach(d=>u(this,S).add(d)),o&&i&&this.on(e.name,i),this},re=new WeakSet,Oe=function(){const{t:e}=this.i18n;if(!u(this,_))throw new we(e);if(!u(this,v))throw new ge(e);if(!u(this,M))throw new Ee(e)},oe=new WeakSet,Pe=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=le(a,[...t]),{_:c,flags:l,unknownFlags:p}=i;let d=!o||r.name===f?c:c.slice(r.name.split(" ").length),C=(r==null?void 0:r.parameters)||[];const y=C.indexOf("--"),A=C.slice(y+1)||[],B=Object.create(null);if(y>-1&&A.length>0){C=C.slice(0,y);const ie=c["--"];d=d.slice(0,-ie.length||void 0),Q(B,Z(C,s),d,s),Q(B,Z(A,s),ie,s)}else Q(B,Z(C,s),d,s);const j={...l,...p};return{name:r==null?void 0:r.name,called:Array.isArray(n)?n.join(" "):n,resolved:o,hasRootOrAlias:u(this,Y,Le),hasRoot:u(this,ee,We),raw:{...i,parameters:d,mergedFlags:j},parameters:B,flags:l,unknownFlags:p,cli:this}},G=new WeakSet,ne=function(e){try{e()}catch(t){if(u(this,I).length>0)u(this,I).forEach(s=>s(t));else throw t}},ae=new WeakSet,Ie=function(){m(this,g,E).call(this);const{t:e}=this.i18n,t=u(this,P);if(!t)throw new Error(e("core.cliParseMustBeCalled"));const s=()=>Fe(u(this,b),t,e),r=()=>m(this,oe,Pe).call(this,s),n={enforce:"post",fn:i=>{const[c]=s(),l=ke(t).join(" ");if(!c)throw l?new me(l,e):new pe(e);u(this,T).emit(c.name,i)}},o=[...u(this,D),n];Be(o)(r())};const pt=e=>e,Ct=(e,t,s)=>s,wt=(e,t)=>t,gt=(e,t)=>({...e,handler:t});export{mt as Clerc,fe as CommandExistsError,Ce as CommandNameConflictError,ge as DescriptionNotSetError,_e as InvalidCommandNameError,J as LocaleNotCalledFirstError,we as NameNotSetError,pe as NoCommandGivenError,me as NoSuchCommandError,f as Root,Ee as VersionNotSetError,Be as compose,gt as defineCommand,Ct as defineHandler,wt as defineInspector,pt as definePlugin,Ae as detectLocale,Se as formatCommandName,Ne as isValidName,K as resolveArgv,Fe as resolveCommand,ct as resolveCommandStrict,z as resolveFlattenCommands,ut as resolveRootCommands,ye as resolveSubcommandsByParent,ke as stripFlags,ht as withBrackets};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.34.1",
|
|
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.34.1"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "puild --minify",
|