@mokup/cli 1.0.11 → 1.1.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.cjs +145 -12
- package/dist/index.d.cts +35 -17
- package/dist/index.d.mts +35 -17
- package/dist/index.d.ts +35 -17
- package/dist/index.mjs +144 -13
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -131,12 +131,16 @@ async function loadConfig(file) {
|
|
|
131
131
|
if (!mod) {
|
|
132
132
|
return null;
|
|
133
133
|
}
|
|
134
|
-
const
|
|
134
|
+
const raw = mod?.default ?? mod;
|
|
135
|
+
const value = isPromise$1(raw) ? await raw : raw;
|
|
135
136
|
if (!value || typeof value !== "object") {
|
|
136
137
|
return null;
|
|
137
138
|
}
|
|
138
139
|
return value;
|
|
139
140
|
}
|
|
141
|
+
function isPromise$1(value) {
|
|
142
|
+
return !!value && typeof value.then === "function";
|
|
143
|
+
}
|
|
140
144
|
function normalizeMiddlewares(value, source, log, position) {
|
|
141
145
|
if (!value) {
|
|
142
146
|
return [];
|
|
@@ -790,12 +794,90 @@ async function buildManifest(options = {}) {
|
|
|
790
794
|
}
|
|
791
795
|
|
|
792
796
|
const middlewareSymbol = Symbol.for("mokup.config.middlewares");
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
+
const contextStack = [];
|
|
798
|
+
function getActiveContext() {
|
|
799
|
+
const context = contextStack[contextStack.length - 1];
|
|
800
|
+
if (!context) {
|
|
801
|
+
throw new Error("onBeforeAll/onAfterAll must be called inside defineConfig()");
|
|
802
|
+
}
|
|
803
|
+
return context;
|
|
804
|
+
}
|
|
805
|
+
function runWithContext(context, fn) {
|
|
806
|
+
contextStack.push(context);
|
|
807
|
+
try {
|
|
808
|
+
const result = fn();
|
|
809
|
+
if (isPromise(result)) {
|
|
810
|
+
return result.finally(() => {
|
|
811
|
+
contextStack.pop();
|
|
812
|
+
});
|
|
813
|
+
}
|
|
814
|
+
contextStack.pop();
|
|
815
|
+
return result;
|
|
816
|
+
} catch (error) {
|
|
817
|
+
contextStack.pop();
|
|
818
|
+
throw error;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
function isPromise(value) {
|
|
822
|
+
return !!value && typeof value.then === "function";
|
|
823
|
+
}
|
|
824
|
+
function normalizeHookError(policy) {
|
|
825
|
+
if (policy === "throw" || policy === "silent") {
|
|
826
|
+
return policy;
|
|
827
|
+
}
|
|
828
|
+
return "warn";
|
|
829
|
+
}
|
|
830
|
+
function reportHookError(error, policy) {
|
|
831
|
+
if (policy === "silent") {
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
if (policy === "warn") {
|
|
835
|
+
console.warn("[@mokup/cli] defineConfig hook failed:", error);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
function runHookSequence(stage, hooks, policy, setStage) {
|
|
839
|
+
if (hooks.length === 0) {
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
setStage(stage);
|
|
843
|
+
let chain = null;
|
|
844
|
+
const runHook = (hook) => {
|
|
845
|
+
try {
|
|
846
|
+
const result = hook();
|
|
847
|
+
if (isPromise(result)) {
|
|
848
|
+
return result.catch((error) => {
|
|
849
|
+
if (policy === "throw") {
|
|
850
|
+
throw error;
|
|
851
|
+
}
|
|
852
|
+
reportHookError(error, policy);
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
return void 0;
|
|
856
|
+
} catch (error) {
|
|
857
|
+
if (policy === "throw") {
|
|
858
|
+
throw error;
|
|
859
|
+
}
|
|
860
|
+
reportHookError(error, policy);
|
|
861
|
+
return void 0;
|
|
797
862
|
}
|
|
798
863
|
};
|
|
864
|
+
for (const hook of hooks) {
|
|
865
|
+
if (chain) {
|
|
866
|
+
chain = chain.then(() => runHook(hook));
|
|
867
|
+
continue;
|
|
868
|
+
}
|
|
869
|
+
const result = runHook(hook);
|
|
870
|
+
if (isPromise(result)) {
|
|
871
|
+
chain = result;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
if (!chain) {
|
|
875
|
+
setStage("normal");
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
return chain.finally(() => {
|
|
879
|
+
setStage("normal");
|
|
880
|
+
});
|
|
799
881
|
}
|
|
800
882
|
function attachMetadata(config, meta) {
|
|
801
883
|
Object.defineProperty(config, middlewareSymbol, {
|
|
@@ -804,21 +886,70 @@ function attachMetadata(config, meta) {
|
|
|
804
886
|
});
|
|
805
887
|
return config;
|
|
806
888
|
}
|
|
889
|
+
function normalizeConfig(value) {
|
|
890
|
+
return value && typeof value === "object" ? value : {};
|
|
891
|
+
}
|
|
892
|
+
function onBeforeAll(handler) {
|
|
893
|
+
if (typeof handler !== "function") {
|
|
894
|
+
throw new TypeError("onBeforeAll expects a function");
|
|
895
|
+
}
|
|
896
|
+
const context = getActiveContext();
|
|
897
|
+
context.hooks.pre.push(handler);
|
|
898
|
+
}
|
|
899
|
+
function onAfterAll(handler) {
|
|
900
|
+
if (typeof handler !== "function") {
|
|
901
|
+
throw new TypeError("onAfterAll expects a function");
|
|
902
|
+
}
|
|
903
|
+
const context = getActiveContext();
|
|
904
|
+
context.hooks.post.push(handler);
|
|
905
|
+
}
|
|
807
906
|
function defineConfig(input) {
|
|
808
907
|
if (typeof input === "function") {
|
|
809
908
|
const pre = [];
|
|
810
909
|
const normal = [];
|
|
811
910
|
const post = [];
|
|
911
|
+
let stage = "normal";
|
|
912
|
+
const app = {
|
|
913
|
+
use: (...handlers) => {
|
|
914
|
+
if (stage === "pre") {
|
|
915
|
+
pre.push(...handlers);
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
if (stage === "post") {
|
|
919
|
+
post.push(...handlers);
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
normal.push(...handlers);
|
|
923
|
+
}
|
|
924
|
+
};
|
|
812
925
|
const context = {
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
926
|
+
app,
|
|
927
|
+
hooks: { pre: [], post: [] },
|
|
928
|
+
setStage: (next) => {
|
|
929
|
+
stage = next;
|
|
930
|
+
}
|
|
816
931
|
};
|
|
817
|
-
const result =
|
|
818
|
-
const
|
|
819
|
-
|
|
932
|
+
const result = runWithContext(context, () => input({ app }));
|
|
933
|
+
const finalize = (value) => {
|
|
934
|
+
const config2 = normalizeConfig(value);
|
|
935
|
+
const policy = normalizeHookError(config2.hookError);
|
|
936
|
+
const preResult = runHookSequence("pre", context.hooks.pre, policy, context.setStage);
|
|
937
|
+
const runPost = () => runHookSequence("post", context.hooks.post, policy, context.setStage);
|
|
938
|
+
if (isPromise(preResult)) {
|
|
939
|
+
return preResult.then(runPost).then(() => attachMetadata(config2, { pre, normal, post }));
|
|
940
|
+
}
|
|
941
|
+
const postResult = runPost();
|
|
942
|
+
if (isPromise(postResult)) {
|
|
943
|
+
return postResult.then(() => attachMetadata(config2, { pre, normal, post }));
|
|
944
|
+
}
|
|
945
|
+
return attachMetadata(config2, { pre, normal, post });
|
|
946
|
+
};
|
|
947
|
+
if (isPromise(result)) {
|
|
948
|
+
return result.then(finalize);
|
|
949
|
+
}
|
|
950
|
+
return finalize(result);
|
|
820
951
|
}
|
|
821
|
-
const config = input
|
|
952
|
+
const config = normalizeConfig(input);
|
|
822
953
|
return attachMetadata(config, { pre: [], normal: [], post: [] });
|
|
823
954
|
}
|
|
824
955
|
|
|
@@ -965,4 +1096,6 @@ async function runCli(argv = process__default.argv) {
|
|
|
965
1096
|
exports.buildManifest = buildManifest;
|
|
966
1097
|
exports.createCli = createCli;
|
|
967
1098
|
exports.defineConfig = defineConfig;
|
|
1099
|
+
exports.onAfterAll = onAfterAll;
|
|
1100
|
+
exports.onBeforeAll = onBeforeAll;
|
|
968
1101
|
exports.runCli = runCli;
|
package/dist/index.d.cts
CHANGED
|
@@ -164,6 +164,12 @@ interface RouteDirectoryConfig {
|
|
|
164
164
|
* @default undefined
|
|
165
165
|
*/
|
|
166
166
|
middleware?: MiddlewareHandler | MiddlewareHandler[];
|
|
167
|
+
/**
|
|
168
|
+
* Error handling policy for defineConfig hooks.
|
|
169
|
+
*
|
|
170
|
+
* @default "warn"
|
|
171
|
+
*/
|
|
172
|
+
hookError?: HookErrorPolicy;
|
|
167
173
|
}
|
|
168
174
|
/**
|
|
169
175
|
* Middleware execution position.
|
|
@@ -174,6 +180,10 @@ interface RouteDirectoryConfig {
|
|
|
174
180
|
* const position: MiddlewarePosition = 'pre'
|
|
175
181
|
*/
|
|
176
182
|
type MiddlewarePosition = 'pre' | 'normal' | 'post';
|
|
183
|
+
/**
|
|
184
|
+
* Error handling policy for config hooks.
|
|
185
|
+
*/
|
|
186
|
+
type HookErrorPolicy = 'throw' | 'warn' | 'silent';
|
|
177
187
|
/**
|
|
178
188
|
* Middleware registry used by defineConfig.
|
|
179
189
|
*
|
|
@@ -202,39 +212,47 @@ declare function buildManifest(options?: BuildOptions): Promise<{
|
|
|
202
212
|
manifestPath: string;
|
|
203
213
|
}>;
|
|
204
214
|
|
|
215
|
+
type HookHandler = () => void | Promise<void>;
|
|
216
|
+
interface ConfigApp {
|
|
217
|
+
use: (...handlers: MiddlewareHandler[]) => void;
|
|
218
|
+
}
|
|
205
219
|
type DefineConfigFactory = (context: {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
220
|
+
app: ConfigApp;
|
|
221
|
+
}) => RouteDirectoryConfig | void | Promise<RouteDirectoryConfig | void>;
|
|
222
|
+
declare function onBeforeAll(handler: HookHandler): void;
|
|
223
|
+
declare function onAfterAll(handler: HookHandler): void;
|
|
210
224
|
/**
|
|
211
|
-
* Define a directory config with
|
|
225
|
+
* Define a directory config with hook-based middleware registration.
|
|
212
226
|
*
|
|
213
227
|
* @param input - Config object or factory callback.
|
|
214
228
|
* @returns Route directory config with middleware metadata.
|
|
215
229
|
*
|
|
216
230
|
* @example
|
|
217
|
-
* import { defineConfig } from '@mokup/cli'
|
|
231
|
+
* import { defineConfig, onBeforeAll, onAfterAll } from '@mokup/cli'
|
|
218
232
|
*
|
|
219
|
-
* export default defineConfig(({
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
233
|
+
* export default defineConfig(({ app }) => {
|
|
234
|
+
* onBeforeAll(() => {
|
|
235
|
+
* app.use(async (c, next) => {
|
|
236
|
+
* c.header('x-before', '1')
|
|
237
|
+
* await next()
|
|
238
|
+
* })
|
|
223
239
|
* })
|
|
224
240
|
*
|
|
225
|
-
*
|
|
241
|
+
* app.use(async (_c, next) => {
|
|
226
242
|
* await next()
|
|
227
243
|
* })
|
|
228
244
|
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
245
|
+
* onAfterAll(() => {
|
|
246
|
+
* app.use(async (c, next) => {
|
|
247
|
+
* await next()
|
|
248
|
+
* c.header('x-after', '1')
|
|
249
|
+
* })
|
|
232
250
|
* })
|
|
233
251
|
*
|
|
234
252
|
* return { delay: 120 }
|
|
235
253
|
* })
|
|
236
254
|
*/
|
|
237
|
-
declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig
|
|
255
|
+
declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig | Promise<RouteDirectoryConfig>;
|
|
238
256
|
|
|
239
257
|
/**
|
|
240
258
|
* Create the mokup CLI program instance.
|
|
@@ -260,5 +278,5 @@ declare function createCli(): Command;
|
|
|
260
278
|
*/
|
|
261
279
|
declare function runCli(argv?: string[]): Promise<void>;
|
|
262
280
|
|
|
263
|
-
export { buildManifest, createCli, defineConfig, runCli };
|
|
264
|
-
export type { BuildOptions, MiddlewarePosition, MiddlewareRegistry, RouteDirectoryConfig, RouteRule };
|
|
281
|
+
export { buildManifest, createCli, defineConfig, onAfterAll, onBeforeAll, runCli };
|
|
282
|
+
export type { BuildOptions, HookErrorPolicy, MiddlewarePosition, MiddlewareRegistry, RouteDirectoryConfig, RouteRule };
|
package/dist/index.d.mts
CHANGED
|
@@ -164,6 +164,12 @@ interface RouteDirectoryConfig {
|
|
|
164
164
|
* @default undefined
|
|
165
165
|
*/
|
|
166
166
|
middleware?: MiddlewareHandler | MiddlewareHandler[];
|
|
167
|
+
/**
|
|
168
|
+
* Error handling policy for defineConfig hooks.
|
|
169
|
+
*
|
|
170
|
+
* @default "warn"
|
|
171
|
+
*/
|
|
172
|
+
hookError?: HookErrorPolicy;
|
|
167
173
|
}
|
|
168
174
|
/**
|
|
169
175
|
* Middleware execution position.
|
|
@@ -174,6 +180,10 @@ interface RouteDirectoryConfig {
|
|
|
174
180
|
* const position: MiddlewarePosition = 'pre'
|
|
175
181
|
*/
|
|
176
182
|
type MiddlewarePosition = 'pre' | 'normal' | 'post';
|
|
183
|
+
/**
|
|
184
|
+
* Error handling policy for config hooks.
|
|
185
|
+
*/
|
|
186
|
+
type HookErrorPolicy = 'throw' | 'warn' | 'silent';
|
|
177
187
|
/**
|
|
178
188
|
* Middleware registry used by defineConfig.
|
|
179
189
|
*
|
|
@@ -202,39 +212,47 @@ declare function buildManifest(options?: BuildOptions): Promise<{
|
|
|
202
212
|
manifestPath: string;
|
|
203
213
|
}>;
|
|
204
214
|
|
|
215
|
+
type HookHandler = () => void | Promise<void>;
|
|
216
|
+
interface ConfigApp {
|
|
217
|
+
use: (...handlers: MiddlewareHandler[]) => void;
|
|
218
|
+
}
|
|
205
219
|
type DefineConfigFactory = (context: {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
220
|
+
app: ConfigApp;
|
|
221
|
+
}) => RouteDirectoryConfig | void | Promise<RouteDirectoryConfig | void>;
|
|
222
|
+
declare function onBeforeAll(handler: HookHandler): void;
|
|
223
|
+
declare function onAfterAll(handler: HookHandler): void;
|
|
210
224
|
/**
|
|
211
|
-
* Define a directory config with
|
|
225
|
+
* Define a directory config with hook-based middleware registration.
|
|
212
226
|
*
|
|
213
227
|
* @param input - Config object or factory callback.
|
|
214
228
|
* @returns Route directory config with middleware metadata.
|
|
215
229
|
*
|
|
216
230
|
* @example
|
|
217
|
-
* import { defineConfig } from '@mokup/cli'
|
|
231
|
+
* import { defineConfig, onBeforeAll, onAfterAll } from '@mokup/cli'
|
|
218
232
|
*
|
|
219
|
-
* export default defineConfig(({
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
233
|
+
* export default defineConfig(({ app }) => {
|
|
234
|
+
* onBeforeAll(() => {
|
|
235
|
+
* app.use(async (c, next) => {
|
|
236
|
+
* c.header('x-before', '1')
|
|
237
|
+
* await next()
|
|
238
|
+
* })
|
|
223
239
|
* })
|
|
224
240
|
*
|
|
225
|
-
*
|
|
241
|
+
* app.use(async (_c, next) => {
|
|
226
242
|
* await next()
|
|
227
243
|
* })
|
|
228
244
|
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
245
|
+
* onAfterAll(() => {
|
|
246
|
+
* app.use(async (c, next) => {
|
|
247
|
+
* await next()
|
|
248
|
+
* c.header('x-after', '1')
|
|
249
|
+
* })
|
|
232
250
|
* })
|
|
233
251
|
*
|
|
234
252
|
* return { delay: 120 }
|
|
235
253
|
* })
|
|
236
254
|
*/
|
|
237
|
-
declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig
|
|
255
|
+
declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig | Promise<RouteDirectoryConfig>;
|
|
238
256
|
|
|
239
257
|
/**
|
|
240
258
|
* Create the mokup CLI program instance.
|
|
@@ -260,5 +278,5 @@ declare function createCli(): Command;
|
|
|
260
278
|
*/
|
|
261
279
|
declare function runCli(argv?: string[]): Promise<void>;
|
|
262
280
|
|
|
263
|
-
export { buildManifest, createCli, defineConfig, runCli };
|
|
264
|
-
export type { BuildOptions, MiddlewarePosition, MiddlewareRegistry, RouteDirectoryConfig, RouteRule };
|
|
281
|
+
export { buildManifest, createCli, defineConfig, onAfterAll, onBeforeAll, runCli };
|
|
282
|
+
export type { BuildOptions, HookErrorPolicy, MiddlewarePosition, MiddlewareRegistry, RouteDirectoryConfig, RouteRule };
|
package/dist/index.d.ts
CHANGED
|
@@ -164,6 +164,12 @@ interface RouteDirectoryConfig {
|
|
|
164
164
|
* @default undefined
|
|
165
165
|
*/
|
|
166
166
|
middleware?: MiddlewareHandler | MiddlewareHandler[];
|
|
167
|
+
/**
|
|
168
|
+
* Error handling policy for defineConfig hooks.
|
|
169
|
+
*
|
|
170
|
+
* @default "warn"
|
|
171
|
+
*/
|
|
172
|
+
hookError?: HookErrorPolicy;
|
|
167
173
|
}
|
|
168
174
|
/**
|
|
169
175
|
* Middleware execution position.
|
|
@@ -174,6 +180,10 @@ interface RouteDirectoryConfig {
|
|
|
174
180
|
* const position: MiddlewarePosition = 'pre'
|
|
175
181
|
*/
|
|
176
182
|
type MiddlewarePosition = 'pre' | 'normal' | 'post';
|
|
183
|
+
/**
|
|
184
|
+
* Error handling policy for config hooks.
|
|
185
|
+
*/
|
|
186
|
+
type HookErrorPolicy = 'throw' | 'warn' | 'silent';
|
|
177
187
|
/**
|
|
178
188
|
* Middleware registry used by defineConfig.
|
|
179
189
|
*
|
|
@@ -202,39 +212,47 @@ declare function buildManifest(options?: BuildOptions): Promise<{
|
|
|
202
212
|
manifestPath: string;
|
|
203
213
|
}>;
|
|
204
214
|
|
|
215
|
+
type HookHandler = () => void | Promise<void>;
|
|
216
|
+
interface ConfigApp {
|
|
217
|
+
use: (...handlers: MiddlewareHandler[]) => void;
|
|
218
|
+
}
|
|
205
219
|
type DefineConfigFactory = (context: {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
220
|
+
app: ConfigApp;
|
|
221
|
+
}) => RouteDirectoryConfig | void | Promise<RouteDirectoryConfig | void>;
|
|
222
|
+
declare function onBeforeAll(handler: HookHandler): void;
|
|
223
|
+
declare function onAfterAll(handler: HookHandler): void;
|
|
210
224
|
/**
|
|
211
|
-
* Define a directory config with
|
|
225
|
+
* Define a directory config with hook-based middleware registration.
|
|
212
226
|
*
|
|
213
227
|
* @param input - Config object or factory callback.
|
|
214
228
|
* @returns Route directory config with middleware metadata.
|
|
215
229
|
*
|
|
216
230
|
* @example
|
|
217
|
-
* import { defineConfig } from '@mokup/cli'
|
|
231
|
+
* import { defineConfig, onBeforeAll, onAfterAll } from '@mokup/cli'
|
|
218
232
|
*
|
|
219
|
-
* export default defineConfig(({
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
233
|
+
* export default defineConfig(({ app }) => {
|
|
234
|
+
* onBeforeAll(() => {
|
|
235
|
+
* app.use(async (c, next) => {
|
|
236
|
+
* c.header('x-before', '1')
|
|
237
|
+
* await next()
|
|
238
|
+
* })
|
|
223
239
|
* })
|
|
224
240
|
*
|
|
225
|
-
*
|
|
241
|
+
* app.use(async (_c, next) => {
|
|
226
242
|
* await next()
|
|
227
243
|
* })
|
|
228
244
|
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
*
|
|
245
|
+
* onAfterAll(() => {
|
|
246
|
+
* app.use(async (c, next) => {
|
|
247
|
+
* await next()
|
|
248
|
+
* c.header('x-after', '1')
|
|
249
|
+
* })
|
|
232
250
|
* })
|
|
233
251
|
*
|
|
234
252
|
* return { delay: 120 }
|
|
235
253
|
* })
|
|
236
254
|
*/
|
|
237
|
-
declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig
|
|
255
|
+
declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig | Promise<RouteDirectoryConfig>;
|
|
238
256
|
|
|
239
257
|
/**
|
|
240
258
|
* Create the mokup CLI program instance.
|
|
@@ -260,5 +278,5 @@ declare function createCli(): Command;
|
|
|
260
278
|
*/
|
|
261
279
|
declare function runCli(argv?: string[]): Promise<void>;
|
|
262
280
|
|
|
263
|
-
export { buildManifest, createCli, defineConfig, runCli };
|
|
264
|
-
export type { BuildOptions, MiddlewarePosition, MiddlewareRegistry, RouteDirectoryConfig, RouteRule };
|
|
281
|
+
export { buildManifest, createCli, defineConfig, onAfterAll, onBeforeAll, runCli };
|
|
282
|
+
export type { BuildOptions, HookErrorPolicy, MiddlewarePosition, MiddlewareRegistry, RouteDirectoryConfig, RouteRule };
|
package/dist/index.mjs
CHANGED
|
@@ -124,12 +124,16 @@ async function loadConfig(file) {
|
|
|
124
124
|
if (!mod) {
|
|
125
125
|
return null;
|
|
126
126
|
}
|
|
127
|
-
const
|
|
127
|
+
const raw = mod?.default ?? mod;
|
|
128
|
+
const value = isPromise$1(raw) ? await raw : raw;
|
|
128
129
|
if (!value || typeof value !== "object") {
|
|
129
130
|
return null;
|
|
130
131
|
}
|
|
131
132
|
return value;
|
|
132
133
|
}
|
|
134
|
+
function isPromise$1(value) {
|
|
135
|
+
return !!value && typeof value.then === "function";
|
|
136
|
+
}
|
|
133
137
|
function normalizeMiddlewares(value, source, log, position) {
|
|
134
138
|
if (!value) {
|
|
135
139
|
return [];
|
|
@@ -783,12 +787,90 @@ async function buildManifest(options = {}) {
|
|
|
783
787
|
}
|
|
784
788
|
|
|
785
789
|
const middlewareSymbol = Symbol.for("mokup.config.middlewares");
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
+
const contextStack = [];
|
|
791
|
+
function getActiveContext() {
|
|
792
|
+
const context = contextStack[contextStack.length - 1];
|
|
793
|
+
if (!context) {
|
|
794
|
+
throw new Error("onBeforeAll/onAfterAll must be called inside defineConfig()");
|
|
795
|
+
}
|
|
796
|
+
return context;
|
|
797
|
+
}
|
|
798
|
+
function runWithContext(context, fn) {
|
|
799
|
+
contextStack.push(context);
|
|
800
|
+
try {
|
|
801
|
+
const result = fn();
|
|
802
|
+
if (isPromise(result)) {
|
|
803
|
+
return result.finally(() => {
|
|
804
|
+
contextStack.pop();
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
contextStack.pop();
|
|
808
|
+
return result;
|
|
809
|
+
} catch (error) {
|
|
810
|
+
contextStack.pop();
|
|
811
|
+
throw error;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
function isPromise(value) {
|
|
815
|
+
return !!value && typeof value.then === "function";
|
|
816
|
+
}
|
|
817
|
+
function normalizeHookError(policy) {
|
|
818
|
+
if (policy === "throw" || policy === "silent") {
|
|
819
|
+
return policy;
|
|
820
|
+
}
|
|
821
|
+
return "warn";
|
|
822
|
+
}
|
|
823
|
+
function reportHookError(error, policy) {
|
|
824
|
+
if (policy === "silent") {
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
if (policy === "warn") {
|
|
828
|
+
console.warn("[@mokup/cli] defineConfig hook failed:", error);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
function runHookSequence(stage, hooks, policy, setStage) {
|
|
832
|
+
if (hooks.length === 0) {
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
setStage(stage);
|
|
836
|
+
let chain = null;
|
|
837
|
+
const runHook = (hook) => {
|
|
838
|
+
try {
|
|
839
|
+
const result = hook();
|
|
840
|
+
if (isPromise(result)) {
|
|
841
|
+
return result.catch((error) => {
|
|
842
|
+
if (policy === "throw") {
|
|
843
|
+
throw error;
|
|
844
|
+
}
|
|
845
|
+
reportHookError(error, policy);
|
|
846
|
+
});
|
|
847
|
+
}
|
|
848
|
+
return void 0;
|
|
849
|
+
} catch (error) {
|
|
850
|
+
if (policy === "throw") {
|
|
851
|
+
throw error;
|
|
852
|
+
}
|
|
853
|
+
reportHookError(error, policy);
|
|
854
|
+
return void 0;
|
|
790
855
|
}
|
|
791
856
|
};
|
|
857
|
+
for (const hook of hooks) {
|
|
858
|
+
if (chain) {
|
|
859
|
+
chain = chain.then(() => runHook(hook));
|
|
860
|
+
continue;
|
|
861
|
+
}
|
|
862
|
+
const result = runHook(hook);
|
|
863
|
+
if (isPromise(result)) {
|
|
864
|
+
chain = result;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
if (!chain) {
|
|
868
|
+
setStage("normal");
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
return chain.finally(() => {
|
|
872
|
+
setStage("normal");
|
|
873
|
+
});
|
|
792
874
|
}
|
|
793
875
|
function attachMetadata(config, meta) {
|
|
794
876
|
Object.defineProperty(config, middlewareSymbol, {
|
|
@@ -797,21 +879,70 @@ function attachMetadata(config, meta) {
|
|
|
797
879
|
});
|
|
798
880
|
return config;
|
|
799
881
|
}
|
|
882
|
+
function normalizeConfig(value) {
|
|
883
|
+
return value && typeof value === "object" ? value : {};
|
|
884
|
+
}
|
|
885
|
+
function onBeforeAll(handler) {
|
|
886
|
+
if (typeof handler !== "function") {
|
|
887
|
+
throw new TypeError("onBeforeAll expects a function");
|
|
888
|
+
}
|
|
889
|
+
const context = getActiveContext();
|
|
890
|
+
context.hooks.pre.push(handler);
|
|
891
|
+
}
|
|
892
|
+
function onAfterAll(handler) {
|
|
893
|
+
if (typeof handler !== "function") {
|
|
894
|
+
throw new TypeError("onAfterAll expects a function");
|
|
895
|
+
}
|
|
896
|
+
const context = getActiveContext();
|
|
897
|
+
context.hooks.post.push(handler);
|
|
898
|
+
}
|
|
800
899
|
function defineConfig(input) {
|
|
801
900
|
if (typeof input === "function") {
|
|
802
901
|
const pre = [];
|
|
803
902
|
const normal = [];
|
|
804
903
|
const post = [];
|
|
904
|
+
let stage = "normal";
|
|
905
|
+
const app = {
|
|
906
|
+
use: (...handlers) => {
|
|
907
|
+
if (stage === "pre") {
|
|
908
|
+
pre.push(...handlers);
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
if (stage === "post") {
|
|
912
|
+
post.push(...handlers);
|
|
913
|
+
return;
|
|
914
|
+
}
|
|
915
|
+
normal.push(...handlers);
|
|
916
|
+
}
|
|
917
|
+
};
|
|
805
918
|
const context = {
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
919
|
+
app,
|
|
920
|
+
hooks: { pre: [], post: [] },
|
|
921
|
+
setStage: (next) => {
|
|
922
|
+
stage = next;
|
|
923
|
+
}
|
|
809
924
|
};
|
|
810
|
-
const result =
|
|
811
|
-
const
|
|
812
|
-
|
|
925
|
+
const result = runWithContext(context, () => input({ app }));
|
|
926
|
+
const finalize = (value) => {
|
|
927
|
+
const config2 = normalizeConfig(value);
|
|
928
|
+
const policy = normalizeHookError(config2.hookError);
|
|
929
|
+
const preResult = runHookSequence("pre", context.hooks.pre, policy, context.setStage);
|
|
930
|
+
const runPost = () => runHookSequence("post", context.hooks.post, policy, context.setStage);
|
|
931
|
+
if (isPromise(preResult)) {
|
|
932
|
+
return preResult.then(runPost).then(() => attachMetadata(config2, { pre, normal, post }));
|
|
933
|
+
}
|
|
934
|
+
const postResult = runPost();
|
|
935
|
+
if (isPromise(postResult)) {
|
|
936
|
+
return postResult.then(() => attachMetadata(config2, { pre, normal, post }));
|
|
937
|
+
}
|
|
938
|
+
return attachMetadata(config2, { pre, normal, post });
|
|
939
|
+
};
|
|
940
|
+
if (isPromise(result)) {
|
|
941
|
+
return result.then(finalize);
|
|
942
|
+
}
|
|
943
|
+
return finalize(result);
|
|
813
944
|
}
|
|
814
|
-
const config = input
|
|
945
|
+
const config = normalizeConfig(input);
|
|
815
946
|
return attachMetadata(config, { pre: [], normal: [], post: [] });
|
|
816
947
|
}
|
|
817
948
|
|
|
@@ -955,4 +1086,4 @@ async function runCli(argv = process.argv) {
|
|
|
955
1086
|
await program.parseAsync(argv);
|
|
956
1087
|
}
|
|
957
1088
|
|
|
958
|
-
export { buildManifest, createCli, defineConfig, runCli };
|
|
1089
|
+
export { buildManifest, createCli, defineConfig, onAfterAll, onBeforeAll, runCli };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mokup/cli",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0
|
|
4
|
+
"version": "1.1.0",
|
|
5
5
|
"description": "CLI for building mokup manifests and handlers.",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://mokup.icebreaker.top",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"commander": "^14.0.0",
|
|
31
31
|
"@mokup/runtime": "1.0.6",
|
|
32
|
-
"@mokup/server": "1.
|
|
32
|
+
"@mokup/server": "1.2.0",
|
|
33
33
|
"@mokup/shared": "1.1.1"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|