Skip to content

Commit 4ba3d91

Browse files
committed
feat: command scoped middleware and devOnly function
1 parent dbed887 commit 4ba3d91

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

packages/commandkit/src/app/router/CommandsRouter.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface Middleware {
2020
relativePath: string;
2121
parentPath: string;
2222
global: boolean;
23+
command: string | null;
2324
}
2425

2526
export interface ParsedCommandData {
@@ -32,6 +33,8 @@ export interface CommandsRouterOptions {
3233
}
3334

3435
const MIDDLEWARE_PATTERN = /^\+middleware\.(m|c)?(j|t)sx?$/;
36+
const COMMAND_MIDDLEWARE_PATTERN =
37+
/^\+([^+().][^().]*)\.middleware\.(m|c)?(j|t)sx?$/;
3538
const GLOBAL_MIDDLEWARE_PATTERN = /^\+global-middleware\.(m|c)?(j|t)sx?$/;
3639
const COMMAND_PATTERN = /^([^+().][^().]*)\.(m|c)?(j|t)sx?$/;
3740
const CATEGORY_PATTERN = /^\(.+\)$/;
@@ -62,7 +65,9 @@ export class CommandsRouter {
6265

6366
private isMiddleware(name: string): boolean {
6467
return (
65-
MIDDLEWARE_PATTERN.test(name) || GLOBAL_MIDDLEWARE_PATTERN.test(name)
68+
MIDDLEWARE_PATTERN.test(name) ||
69+
GLOBAL_MIDDLEWARE_PATTERN.test(name) ||
70+
COMMAND_MIDDLEWARE_PATTERN.test(name)
6671
);
6772
}
6873

@@ -159,6 +164,9 @@ export class CommandsRouter {
159164
relativePath: this.replaceEntrypoint(path),
160165
parentPath: entry.parentPath,
161166
global: GLOBAL_MIDDLEWARE_PATTERN.test(name),
167+
command: COMMAND_MIDDLEWARE_PATTERN.test(name)
168+
? name.split('.')[0] || null
169+
: null,
162170
};
163171

164172
this.middlewares.set(middleware.id, middleware);
@@ -170,9 +178,9 @@ export class CommandsRouter {
170178
const commandPath = command.parentPath;
171179
const samePathMiddlewares = Array.from(this.middlewares.values())
172180
.filter((middleware) => {
173-
// if middleware's parent path is the same as command's parent path
174-
// or if the middleware is global
175-
return middleware.parentPath === commandPath || middleware.global;
181+
if (middleware.global) return true;
182+
if (middleware.command) return middleware.command === command.name;
183+
return middleware.parentPath === commandPath;
176184
})
177185
.map((middleware) => middleware.id);
178186

packages/commandkit/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ export * from './app/router/index';
1616
export type * from './types';
1717
export * from './version';
1818
export * from './plugins/index';
19-
export { getCurrentDirectory, getSourceDirectories } from './utils/utilities';
19+
export {
20+
getCurrentDirectory,
21+
getSourceDirectories,
22+
devOnly,
23+
debounce,
24+
} from './utils/utilities';
2025
export type { CommandKitHMREvent } from './utils/dev-hooks';
2126
export * from './utils/constants';
2227

packages/commandkit/src/utils/utilities.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,25 @@ export function debounce<R, F extends (...args: any[]) => R>(
8888
});
8989
}) as F;
9090
}
91+
92+
/**
93+
* Creates a function from the given function that runs only in development mode.
94+
* @param fn The function to run in development mode.
95+
* @returns The function that runs only in development mode.
96+
* @example
97+
* ```ts
98+
* const devOnlyFn = devOnly(() => {
99+
* console.log('This function runs only in development mode');
100+
* });
101+
* devOnlyFn(); // This will log the message only in development mode
102+
* ```
103+
*/
104+
export function devOnly<T extends (...args: any[]) => any>(fn: T): T {
105+
const f = (...args: Parameters<T>) => {
106+
if (COMMANDKIT_IS_DEV) {
107+
return fn(...args);
108+
}
109+
};
110+
111+
return f as T;
112+
}

0 commit comments

Comments
 (0)