@stacksjs/bun-router 0.1.4 → 0.1.5
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/cli.js +1 -1
- package/dist/types/registry.d.ts +20 -4
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -29,7 +29,7 @@ Received ${s}, cleaning up...`),e)try{await e()}catch(r){console.error("Error du
|
|
|
29
29
|
\u2717 Command "${e}" not found.
|
|
30
30
|
`)),this.enableDidYouMean){let t=[];for(let r of this.commands){if(r.displayName)t.push(r.displayName);if(r.aliasNames)t.push(...r.aliasNames)}let s=ue(e,t);if(s.length>0)console.log(E.yellow("Did you mean one of these?")),s.forEach((r)=>console.log(` ${E.dim("\u2022")} ${this.name} ${r}`)),console.log("")}console.log(E.dim("Run"),`${this.name} --help`,E.dim("to see all available commands")),x.exit(1)}async parse(e=oe,t={}){let{run:s=!0,exitOnError:r=!1}=t;if(r)try{return await this.parse(e,{run:s})}catch(i){throw this.handleUsageError(i),i}if(this.rawArgs=e,!this.name)this.name=e[1]?Ve(e[1]):"cli";let n=!0,o=e.slice(2),l=o[0];if(l&&!l.startsWith("-")){for(let i of this.commands)if(i.isMatched(l)){let u=this.mri(o,i);n=!1;let d={...u,args:u.args.slice(1)};this.setParsedInfo(d,i,l),this.emit(`command:${l}`,i);break}}if(n){for(let i of this.commands)if(i.name===""){n=!1;let u=this.mri(o,i);this.setParsedInfo(u,i),this.emit("command:!",i);break}}if(n){let i=this.mri(e.slice(2));this.setParsedInfo(i)}if(this.options.verbose)this.isVerbose=!0;if(this.options.quiet)this.isQuiet=!0;if(this.options.debug)this.isDebug=!0;if(this.options.noInteraction)this.isNoInteraction=!0;if(this.options.env)this.environment=String(this.options.env);if(this.options.dryRun)this.isDryRun=!0;if(this.options.force)this.isForce=!0;if(this.options.noEmoji!==void 0)this.useEmoji=!this.options.noEmoji;if(this.options.theme)this.theme=String(this.options.theme);if(this.options.noCache!==void 0)this.isNoCache=Boolean(this.options.noCache);if(this.options.help&&this.showHelpOnExit)this.outputHelp(),s=!1,this.unsetMatchedCommand();if(this.options.version&&this.showVersionOnExit&&this.matchedCommandName==null)this.outputVersion(),s=!1,this.unsetMatchedCommand();let a={args:this.args,options:this.options};if(s)await this.runMatchedCommand();if(!this.matchedCommand&&this.args[0]){if(this.emit("command:*"),!(this.listenerCount("command:*")>0))this.showCommandNotFound(this.args[0])}return a}mri(e,t){let s=[...this.globalCommand.options,...t?t.options:[]],r=Ae(s),n=[],o=e.indexOf("--");if(o>-1)n=e.slice(o+1),e=e.slice(0,o);let l=ke(e,r),a={_:l._};for(let m of Object.keys(l))if(m!=="_")a[ce(m)]=l[m];let i=a._,u={"--":n},d=t&&t.config.ignoreOptionDefaultValue?t.config.ignoreOptionDefaultValue:this.globalCommand.config.ignoreOptionDefaultValue,h=Object.create(null);for(let m of s){if(!d&&m.config.default!==void 0)for(let f of m.names)u[f]=m.config.default;if(Array.isArray(m.config.type)){if(h[m.name]===void 0)h[m.name]={shouldTransform:!0,transformFunction:m.config.type[0]}}}for(let m of Object.keys(a))if(m!=="_"){let f=m.split(".");Le(u,f,a[m]),Pe(u,h)}return{args:i,options:u}}async run(e=oe){return this.parse(e,{run:!0,exitOnError:!0})}handleUsageError(e){if(!(!!e&&typeof e==="object"&&e.name==="ClappError"&&e.isUsageError!==!1))return;let r=e,n=r.message??"command-line error",o=this.name?`${this.name}: `:"",l=/--help/.test(n)?"":`
|
|
31
31
|
Run \`${this.name??"cli"} --help\` for usage.`;x.stderr.write(`${o}${n}${l}
|
|
32
|
-
`),x.exit(r.exitCode??2)}async runMatchedCommand(){let{args:e,options:t,matchedCommand:s}=this;if(!s||!s.commandAction)return;s.checkUnknownOptions(),s.checkOptionValue(),s.checkRequiredArgs();let r=[];s.args.forEach((a,i)=>{if(a.variadic)r.push(e.slice(i));else r.push(e[i])}),r.push(t);let n={command:s,args:r,options:t};for(let a of s.beforeHooks)await a(n);let o,l=async()=>{let a=s.commandAction.apply(this,r);if(a instanceof Promise)o=await a;else o=a;return o};if(s.middleware.length>0){let a=0,i=async()=>{if(a<s.middleware.length){let u=s.middleware[a++];await u({...n,next:i})}else await l()};await i()}else await l();for(let a of s.afterHooks)await a(n);return o}removeSignalHandlers(){if(!this.signalHandlersSet)return this;return x.removeAllListeners("SIGINT"),x.removeAllListeners("SIGTERM"),this.signalHandlersSet=!1,this}destroy(){this.removeSignalHandlers(),this.commands=[],this.rawArgs=[],this.args=[],this.options={},this.matchedCommand=void 0,this.matchedCommandName=void 0,this.removeAllListeners()}}class he{cache;enabled;cleanupInterval=null;hits=0;misses=0;constructor(){this.cache=new Map,this.enabled=!0,this.startCleanupInterval()}startCleanupInterval(){if(this.cleanupInterval)return;this.cleanupInterval=setInterval(()=>{this.cleanup()},30000),this.cleanupInterval.unref()}stopCleanup(){if(this.cleanupInterval)clearInterval(this.cleanupInterval),this.cleanupInterval=null}isEnabled(){return this.enabled}get(e){if(!this.enabled){this.misses++;return}let t=this.cache.get(e);if(!t){this.misses++;return}if(Date.now()-t.timestamp>t.ttl){this.cache.delete(e),this.misses++;return}return this.hits++,t.value}set(e,t,s=5000){if(!this.enabled)return;this.cache.set(e,{value:t,timestamp:Date.now(),ttl:s})}has(e){if(!this.enabled)return!1;let t=this.cache.get(e);if(!t)return!1;if(Date.now()-t.timestamp>t.ttl)return this.cache.delete(e),!1;return!0}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}disable(){this.enabled=!1,this.clear()}enable(){this.enabled=!0}stats(){return{size:this.cache.size,enabled:this.enabled,hits:this.hits,misses:this.misses}}resetStats(){this.hits=0,this.misses=0}keys(){return Array.from(this.cache.keys())}cleanup(){let e=Date.now();for(let[t,s]of this.cache.entries())if(e-s.timestamp>s.ttl)this.cache.delete(t)}destroy(){this.stopCleanup(),this.clear(),this.resetStats()}}var ns=new he;function qe(){if("FORCE_COLOR"in M.env)return M.env.FORCE_COLOR!=="0";if("NO_COLOR"in M.env||M.env.TERM==="dumb")return!1;if(M.platform==="win32")return!0;return ae.isatty(1)&&ae.isatty(2)}var me=qe();function y(e,t){if(!me)return(s)=>s;return(s)=>e+s+t}var Ke=y("\x1B[0m","\x1B[0m"),Ye=y("\x1B[31m","\x1B[39m"),Je=y("\x1B[32m","\x1B[39m"),Qe=y("\x1B[33m","\x1B[39m"),Xe=y("\x1B[34m","\x1B[39m"),Ze=y("\x1B[35m","\x1B[39m"),et=y("\x1B[36m","\x1B[39m"),tt=y("\x1B[37m","\x1B[39m"),st=y("\x1B[90m","\x1B[39m"),rt=y("\x1B[1m","\x1B[22m"),it=y("\x1B[3m","\x1B[23m"),nt=y("\x1B[4m","\x1B[24m"),ot=y("\x1B[2m","\x1B[22m"),at=y("\x1B[7m","\x1B[27m"),lt=y("\x1B[8m","\x1B[28m"),ct=y("\x1B[9m","\x1B[29m"),ut=y("\x1B[41m","\x1B[49m"),dt=y("\x1B[42m","\x1B[49m"),ht=y("\x1B[43m","\x1B[49m"),mt=y("\x1B[44m","\x1B[49m"),pt=y("\x1B[45m","\x1B[49m"),ft=y("\x1B[46m","\x1B[49m"),gt=y("\x1B[47m","\x1B[49m"),yt=me,$t={reset:Ke,red:Ye,green:Je,yellow:Qe,blue:Xe,magenta:Ze,cyan:et,white:tt,gray:st,bold:rt,italic:it,underline:nt,dim:ot,inverse:at,hidden:lt,strikethrough:ct,bgRed:ut,bgGreen:dt,bgYellow:ht,bgBlue:mt,bgMagenta:pt,bgCyan:ft,bgWhite:gt,isColorSupported:yt},wt=$t,vt=["up","down","left","right","space","enter","cancel"],ds={actions:new Set(vt),aliases:new Map([["k","up"],["j","down"],["h","left"],["l","right"],["\x03","cancel"],["escape","cancel"]]),messages:{cancel:"Canceled",error:"Something went wrong"}};var hs=bt.platform.startsWith("win"),ms=Symbol("clapp:cancel");var xt=De();var g=(e,t)=>xt?e:t,ps=g("\u25C6","*"),fs=g("\u25A0","x"),gs=g("\u25B2","x"),ys=g("\u25C7","o"),$s=g("\u250C","T"),Ct=g("\u2502","|"),ws=g("\u2514","\u2014"),bs=g("\u25CF",">"),vs=g("\u25CB"," "),xs=g("\u25FB","[\u2022]"),Cs=g("\u25FC","[+]"),Rs=g("\u25FB","[ ]"),Os=g("\u25AA","\u2022"),Ss=g("\u2500","-"),Ts=g("\u256E","+"),Is=g("\u251C","+"),Es=g("\u256F","+"),Ms=g("\u25CF","\u2022"),_s=g("\u25C6","*"),js=g("\u25B2","!"),ks=g("\u25A0","x");var Bs={light:g("\u2500","-"),heavy:g("\u2501","="),block:g("\u2588","#")};function Rt(){return`${wt.gray(Ct)} `}var As=Rt();var pe="0.1.
|
|
32
|
+
`),x.exit(r.exitCode??2)}async runMatchedCommand(){let{args:e,options:t,matchedCommand:s}=this;if(!s||!s.commandAction)return;s.checkUnknownOptions(),s.checkOptionValue(),s.checkRequiredArgs();let r=[];s.args.forEach((a,i)=>{if(a.variadic)r.push(e.slice(i));else r.push(e[i])}),r.push(t);let n={command:s,args:r,options:t};for(let a of s.beforeHooks)await a(n);let o,l=async()=>{let a=s.commandAction.apply(this,r);if(a instanceof Promise)o=await a;else o=a;return o};if(s.middleware.length>0){let a=0,i=async()=>{if(a<s.middleware.length){let u=s.middleware[a++];await u({...n,next:i})}else await l()};await i()}else await l();for(let a of s.afterHooks)await a(n);return o}removeSignalHandlers(){if(!this.signalHandlersSet)return this;return x.removeAllListeners("SIGINT"),x.removeAllListeners("SIGTERM"),this.signalHandlersSet=!1,this}destroy(){this.removeSignalHandlers(),this.commands=[],this.rawArgs=[],this.args=[],this.options={},this.matchedCommand=void 0,this.matchedCommandName=void 0,this.removeAllListeners()}}class he{cache;enabled;cleanupInterval=null;hits=0;misses=0;constructor(){this.cache=new Map,this.enabled=!0,this.startCleanupInterval()}startCleanupInterval(){if(this.cleanupInterval)return;this.cleanupInterval=setInterval(()=>{this.cleanup()},30000),this.cleanupInterval.unref()}stopCleanup(){if(this.cleanupInterval)clearInterval(this.cleanupInterval),this.cleanupInterval=null}isEnabled(){return this.enabled}get(e){if(!this.enabled){this.misses++;return}let t=this.cache.get(e);if(!t){this.misses++;return}if(Date.now()-t.timestamp>t.ttl){this.cache.delete(e),this.misses++;return}return this.hits++,t.value}set(e,t,s=5000){if(!this.enabled)return;this.cache.set(e,{value:t,timestamp:Date.now(),ttl:s})}has(e){if(!this.enabled)return!1;let t=this.cache.get(e);if(!t)return!1;if(Date.now()-t.timestamp>t.ttl)return this.cache.delete(e),!1;return!0}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}disable(){this.enabled=!1,this.clear()}enable(){this.enabled=!0}stats(){return{size:this.cache.size,enabled:this.enabled,hits:this.hits,misses:this.misses}}resetStats(){this.hits=0,this.misses=0}keys(){return Array.from(this.cache.keys())}cleanup(){let e=Date.now();for(let[t,s]of this.cache.entries())if(e-s.timestamp>s.ttl)this.cache.delete(t)}destroy(){this.stopCleanup(),this.clear(),this.resetStats()}}var ns=new he;function qe(){if("FORCE_COLOR"in M.env)return M.env.FORCE_COLOR!=="0";if("NO_COLOR"in M.env||M.env.TERM==="dumb")return!1;if(M.platform==="win32")return!0;return ae.isatty(1)&&ae.isatty(2)}var me=qe();function y(e,t){if(!me)return(s)=>s;return(s)=>e+s+t}var Ke=y("\x1B[0m","\x1B[0m"),Ye=y("\x1B[31m","\x1B[39m"),Je=y("\x1B[32m","\x1B[39m"),Qe=y("\x1B[33m","\x1B[39m"),Xe=y("\x1B[34m","\x1B[39m"),Ze=y("\x1B[35m","\x1B[39m"),et=y("\x1B[36m","\x1B[39m"),tt=y("\x1B[37m","\x1B[39m"),st=y("\x1B[90m","\x1B[39m"),rt=y("\x1B[1m","\x1B[22m"),it=y("\x1B[3m","\x1B[23m"),nt=y("\x1B[4m","\x1B[24m"),ot=y("\x1B[2m","\x1B[22m"),at=y("\x1B[7m","\x1B[27m"),lt=y("\x1B[8m","\x1B[28m"),ct=y("\x1B[9m","\x1B[29m"),ut=y("\x1B[41m","\x1B[49m"),dt=y("\x1B[42m","\x1B[49m"),ht=y("\x1B[43m","\x1B[49m"),mt=y("\x1B[44m","\x1B[49m"),pt=y("\x1B[45m","\x1B[49m"),ft=y("\x1B[46m","\x1B[49m"),gt=y("\x1B[47m","\x1B[49m"),yt=me,$t={reset:Ke,red:Ye,green:Je,yellow:Qe,blue:Xe,magenta:Ze,cyan:et,white:tt,gray:st,bold:rt,italic:it,underline:nt,dim:ot,inverse:at,hidden:lt,strikethrough:ct,bgRed:ut,bgGreen:dt,bgYellow:ht,bgBlue:mt,bgMagenta:pt,bgCyan:ft,bgWhite:gt,isColorSupported:yt},wt=$t,vt=["up","down","left","right","space","enter","cancel"],ds={actions:new Set(vt),aliases:new Map([["k","up"],["j","down"],["h","left"],["l","right"],["\x03","cancel"],["escape","cancel"]]),messages:{cancel:"Canceled",error:"Something went wrong"}};var hs=bt.platform.startsWith("win"),ms=Symbol("clapp:cancel");var xt=De();var g=(e,t)=>xt?e:t,ps=g("\u25C6","*"),fs=g("\u25A0","x"),gs=g("\u25B2","x"),ys=g("\u25C7","o"),$s=g("\u250C","T"),Ct=g("\u2502","|"),ws=g("\u2514","\u2014"),bs=g("\u25CF",">"),vs=g("\u25CB"," "),xs=g("\u25FB","[\u2022]"),Cs=g("\u25FC","[+]"),Rs=g("\u25FB","[ ]"),Os=g("\u25AA","\u2022"),Ss=g("\u2500","-"),Ts=g("\u256E","+"),Is=g("\u251C","+"),Es=g("\u256F","+"),Ms=g("\u25CF","\u2022"),_s=g("\u25C6","*"),js=g("\u25B2","!"),ks=g("\u25A0","x");var Bs={light:g("\u2500","-"),heavy:g("\u2501","="),block:g("\u2588","#")};function Rt(){return`${wt.gray(Ct)} `}var As=Rt();var pe="0.1.5";import R from"fs/promises";import Q from"path";import L from"process";var b={reset:"\x1B[0m",bold:"\x1B[1m",dim:"\x1B[2m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",cyan:"\x1B[36m",white:"\x1B[37m",gray:"\x1B[90m"},c={red:(e)=>`${b.red}${e}${b.reset}`,green:(e)=>`${b.green}${e}${b.reset}`,yellow:(e)=>`${b.yellow}${e}${b.reset}`,blue:(e)=>`${b.blue}${e}${b.reset}`,cyan:(e)=>`${b.cyan}${e}${b.reset}`,white:(e)=>`${b.white}${e}${b.reset}`,gray:(e)=>`${b.gray}${e}${b.reset}`,bold:(e)=>`${b.bold}${e}${b.reset}`,dim:(e)=>`${b.dim}${e}${b.reset}`,hex:(e)=>(t)=>t};async function P(e){let t=Object.entries(fe).map(([r,n])=>({name:r,className:n.constructor.name})),s=`/**
|
|
33
33
|
* This file is auto-generated.
|
|
34
34
|
* DO NOT EDIT THIS FILE DIRECTLY.
|
|
35
35
|
* To update, run 'bun router middleware:types'
|
package/dist/types/registry.d.ts
CHANGED
|
@@ -5,7 +5,23 @@ import type { BuiltInMiddleware } from '../types';
|
|
|
5
5
|
* `../index`, which is the module an application can name in a
|
|
6
6
|
* `declare module '@stacksjs/bun-router'` augmentation - see the note there.
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Read a key out of the registry, or fall back.
|
|
10
|
+
*
|
|
11
|
+
* `TKey extends keyof RouterTypeRegistry` is false while the interface is
|
|
12
|
+
* empty, which is what makes every fallback the default.
|
|
13
|
+
*
|
|
14
|
+
* `TShape` is checked separately from `TFallback`, and the distinction is
|
|
15
|
+
* load-bearing. It used to test the declared value against the fallback itself,
|
|
16
|
+
* which sounds like the same thing and is not: the fallback for an action path
|
|
17
|
+
* is the loose pattern `Actions/${string}Action`, and a real application's
|
|
18
|
+
* action list contains names like `'Actions/Dashboard/Library/GetFunctions'`
|
|
19
|
+
* that do not match it. So a correct, complete declaration failed the guard and
|
|
20
|
+
* fell back to the pattern - the feature looked wired up and checked nothing.
|
|
21
|
+
* The guard is meant to catch `actions: number`, so it asks whether the
|
|
22
|
+
* declared value is a string, and nothing more.
|
|
23
|
+
*/
|
|
24
|
+
export type FromRegistry<TKey extends string, TShape, TFallback> = TKey extends keyof RouterTypeRegistry ? (RouterTypeRegistry[TKey] extends TShape ? RouterTypeRegistry[TKey] : TFallback) : TFallback;
|
|
9
25
|
/**
|
|
10
26
|
* The action paths this application has, or the shape of one when it has not
|
|
11
27
|
* said.
|
|
@@ -14,7 +30,7 @@ export type FromRegistry<TKey extends string, TFallback> = TKey extends keyof Ro
|
|
|
14
30
|
* ending in `Action`, or a `Controller@method` reference. It catches a shape
|
|
15
31
|
* mistake and nothing else, which is why declaring the real set is worth doing.
|
|
16
32
|
*/
|
|
17
|
-
export type KnownActionPath = FromRegistry<'actions', `Actions/${string}Action` | `actions/${string}Action` | `${string}Controller@${string}`>;
|
|
33
|
+
export type KnownActionPath = FromRegistry<'actions', string, `Actions/${string}Action` | `actions/${string}Action` | `${string}Controller@${string}`>;
|
|
18
34
|
/**
|
|
19
35
|
* The middleware aliases this application registers, or any string.
|
|
20
36
|
*
|
|
@@ -23,7 +39,7 @@ export type KnownActionPath = FromRegistry<'actions', `Actions/${string}Action`
|
|
|
23
39
|
* to the group - so the union has to contain both or the group form stops
|
|
24
40
|
* type-checking the moment anything is declared.
|
|
25
41
|
*/
|
|
26
|
-
export type KnownMiddlewareName = FromRegistry<'middleware', string>;
|
|
42
|
+
export type KnownMiddlewareName = FromRegistry<'middleware', string, string>;
|
|
27
43
|
/**
|
|
28
44
|
* A middleware reference: an alias, or an alias with parameters.
|
|
29
45
|
*
|
|
@@ -38,7 +54,7 @@ export type KnownMiddlewareName = FromRegistry<'middleware', string>;
|
|
|
38
54
|
*/
|
|
39
55
|
export type MiddlewareReference = BuiltInMiddleware | `${BuiltInMiddleware}:${string}` | KnownMiddlewareName | `${KnownMiddlewareName}:${string}`;
|
|
40
56
|
/** The named routes this application registers, as `name → path`. */
|
|
41
|
-
export type KnownRoutes = FromRegistry<'routes', Record<string, string>>;
|
|
57
|
+
export type KnownRoutes = FromRegistry<'routes', Record<string, string>, Record<string, string>>;
|
|
42
58
|
/** The names of those routes, or any string when none are declared. */
|
|
43
59
|
export type KnownRouteName = keyof KnownRoutes extends never ? string : Extract<keyof KnownRoutes, string>;
|
|
44
60
|
/** The path a declared route name resolves to, or any path when it is not declared. */
|