@mandujs/core 0.12.2 → 0.13.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.
@@ -1,215 +1,225 @@
1
- import { z, ZodError, ZodIssueCode } from "zod";
2
- import path from "path";
3
- import { pathToFileURL } from "url";
4
- import { CONFIG_FILES, coerceConfig } from "./mandu";
5
- import { readJsonFile } from "../utils/bun";
6
-
7
- /**
8
- * DNA-003: Strict mode schema helper
9
- *
10
- * Creates a schema that warns about unknown keys instead of failing
11
- * This provides the benefits of .strict() while maintaining compatibility
12
- */
13
- function strictWithWarnings<T extends z.ZodRawShape>(
14
- schema: z.ZodObject<T>,
15
- schemaName: string
16
- ): z.ZodObject<T> {
17
- return schema.superRefine((data, ctx) => {
18
- if (typeof data !== "object" || data === null) return;
19
-
20
- const knownKeys = new Set(Object.keys(schema.shape));
21
- const unknownKeys = Object.keys(data).filter((key) => !knownKeys.has(key));
22
-
23
- if (unknownKeys.length > 0 && process.env.MANDU_STRICT !== "0") {
24
- // In strict mode (default), add warnings to issues
25
- for (const key of unknownKeys) {
26
- ctx.addIssue({
27
- code: ZodIssueCode.unrecognized_keys,
28
- keys: [key],
29
- message: `Unknown key '${key}' in ${schemaName}. Did you mean one of: ${[...knownKeys].join(", ")}?`,
30
- });
31
- }
32
- }
33
- });
34
- }
35
-
36
- /**
37
- * Server 설정 스키마 (strict)
38
- */
39
- const ServerConfigSchema = z
40
- .object({
41
- port: z.number().min(1).max(65535).default(3000),
42
- hostname: z.string().default("localhost"),
43
- cors: z
44
- .union([
45
- z.boolean(),
46
- z.object({
47
- origin: z.union([z.string(), z.array(z.string())]).optional(),
48
- methods: z.array(z.string()).optional(),
49
- credentials: z.boolean().optional(),
50
- }).strict(),
51
- ])
52
- .default(false),
53
- streaming: z.boolean().default(false),
54
- })
55
- .strict();
56
-
57
- /**
58
- * Guard 설정 스키마 (strict)
59
- */
60
- const GuardConfigSchema = z
61
- .object({
62
- preset: z.enum(["mandu", "fsd", "clean", "hexagonal", "atomic"]).default("mandu"),
63
- srcDir: z.string().default("src"),
64
- exclude: z.array(z.string()).default([]),
65
- realtime: z.boolean().default(true),
66
- rules: z.record(z.enum(["error", "warn", "warning", "off"])).optional(),
67
- })
68
- .strict();
69
-
70
- /**
71
- * Build 설정 스키마 (strict)
72
- */
73
- const BuildConfigSchema = z
74
- .object({
75
- outDir: z.string().default(".mandu"),
76
- minify: z.boolean().default(true),
77
- sourcemap: z.boolean().default(false),
78
- splitting: z.boolean().default(false),
79
- })
80
- .strict();
81
-
82
- /**
83
- * Dev 설정 스키마 (strict)
84
- */
85
- const DevConfigSchema = z
86
- .object({
87
- hmr: z.boolean().default(true),
88
- watchDirs: z.array(z.string()).default([]),
89
- })
90
- .strict();
91
-
92
- /**
93
- * FS Routes 설정 스키마 (strict)
94
- */
95
- const FsRoutesConfigSchema = z
96
- .object({
97
- routesDir: z.string().default("app"),
98
- extensions: z.array(z.string()).default([".tsx", ".ts", ".jsx", ".js"]),
99
- exclude: z.array(z.string()).default([]),
100
- islandSuffix: z.string().default(".island"),
101
- legacyManifestPath: z.string().optional(),
102
- mergeWithLegacy: z.boolean().default(true),
103
- })
104
- .strict();
105
-
106
- /**
107
- * SEO 설정 스키마 (strict)
108
- */
109
- const SeoConfigSchema = z
110
- .object({
111
- enabled: z.boolean().default(true),
112
- defaultTitle: z.string().optional(),
113
- titleTemplate: z.string().optional(),
114
- })
115
- .strict();
116
-
117
- /**
118
- * Mandu 설정 스키마 (DNA-003: strict mode)
119
- *
120
- * 알 수 없는 키가 있으면 오류 발생 → 오타 즉시 감지
121
- * MANDU_STRICT=0 으로 비활성화 가능
122
- */
123
- export const ManduConfigSchema = z
124
- .object({
125
- server: ServerConfigSchema.default({}),
126
- guard: GuardConfigSchema.default({}),
127
- build: BuildConfigSchema.default({}),
128
- dev: DevConfigSchema.default({}),
129
- fsRoutes: FsRoutesConfigSchema.default({}),
130
- seo: SeoConfigSchema.default({}),
131
- })
132
- .strict();
133
-
134
- export type ValidatedManduConfig = z.infer<typeof ManduConfigSchema>;
135
-
136
- /**
137
- * 검증 결과
138
- */
139
- export interface ValidationResult {
140
- valid: boolean;
141
- config?: ValidatedManduConfig;
142
- errors?: Array<{
143
- path: string;
144
- message: string;
145
- }>;
146
- source?: string;
147
- }
148
-
149
- /**
150
- * 설정 파일 검증
151
- */
152
- export async function validateConfig(rootDir: string): Promise<ValidationResult> {
153
- for (const fileName of CONFIG_FILES) {
154
- const filePath = path.join(rootDir, fileName);
155
- if (!(await Bun.file(filePath).exists())) {
156
- continue;
157
- }
158
-
159
- try {
160
- let raw: unknown;
161
- if (fileName.endsWith(".json")) {
162
- raw = await readJsonFile(filePath);
163
- } else {
164
- const module = await import(pathToFileURL(filePath).href);
165
- raw = module?.default ?? module;
166
- }
167
-
168
- const config = ManduConfigSchema.parse(coerceConfig(raw ?? {}, fileName));
169
- return { valid: true, config, source: fileName };
170
- } catch (error) {
171
- if (error instanceof ZodError) {
172
- const errors = error.errors.map((e) => ({
173
- path: e.path.join("."),
174
- message: e.message,
175
- }));
176
- return { valid: false, errors, source: fileName };
177
- }
178
-
179
- return {
180
- valid: false,
181
- errors: [
182
- {
183
- path: "",
184
- message: `Failed to load config: ${
185
- error instanceof Error ? error.message : String(error)
186
- }`,
187
- },
188
- ],
189
- source: fileName,
190
- };
191
- }
192
- }
193
-
194
- // 설정 파일 없음 - 기본값 사용
195
- return { valid: true, config: ManduConfigSchema.parse({}) };
196
- }
197
-
198
- /**
199
- * CLI용 검증 및 리포트
200
- */
201
- export async function validateAndReport(rootDir: string): Promise<ValidatedManduConfig | null> {
202
- const result = await validateConfig(rootDir);
203
-
204
- if (!result.valid) {
205
- console.error(`\n❌ Invalid config${result.source ? ` (${result.source})` : ""}:\n`);
206
- for (const error of result.errors || []) {
207
- const location = error.path ? ` ${error.path}: ` : " ";
208
- console.error(`${location}${error.message}`);
209
- }
210
- console.error("");
211
- return null;
212
- }
213
-
214
- return result.config!;
215
- }
1
+ import { z, ZodError, ZodIssueCode } from "zod";
2
+ import path from "path";
3
+ import { pathToFileURL } from "url";
4
+ import { CONFIG_FILES, coerceConfig } from "./mandu";
5
+ import { readJsonFile } from "../utils/bun";
6
+
7
+ /**
8
+ * DNA-003: Strict mode schema helper
9
+ *
10
+ * Creates a schema that warns about unknown keys instead of failing
11
+ * This provides the benefits of .strict() while maintaining compatibility
12
+ */
13
+ function strictWithWarnings<T extends z.ZodRawShape>(
14
+ schema: z.ZodObject<T>,
15
+ schemaName: string
16
+ ): z.ZodObject<T> {
17
+ return schema.superRefine((data, ctx) => {
18
+ if (typeof data !== "object" || data === null) return;
19
+
20
+ const knownKeys = new Set(Object.keys(schema.shape));
21
+ const unknownKeys = Object.keys(data).filter((key) => !knownKeys.has(key));
22
+
23
+ if (unknownKeys.length > 0 && process.env.MANDU_STRICT !== "0") {
24
+ // In strict mode (default), add warnings to issues
25
+ for (const key of unknownKeys) {
26
+ ctx.addIssue({
27
+ code: ZodIssueCode.unrecognized_keys,
28
+ keys: [key],
29
+ message: `Unknown key '${key}' in ${schemaName}. Did you mean one of: ${[...knownKeys].join(", ")}?`,
30
+ });
31
+ }
32
+ }
33
+ });
34
+ }
35
+
36
+ /**
37
+ * Server 설정 스키마 (strict)
38
+ */
39
+ const ServerConfigSchema = z
40
+ .object({
41
+ port: z.number().min(1).max(65535).default(3000),
42
+ hostname: z.string().default("localhost"),
43
+ cors: z
44
+ .union([
45
+ z.boolean(),
46
+ z.object({
47
+ origin: z.union([z.string(), z.array(z.string())]).optional(),
48
+ methods: z.array(z.string()).optional(),
49
+ credentials: z.boolean().optional(),
50
+ }).strict(),
51
+ ])
52
+ .default(false),
53
+ streaming: z.boolean().default(false),
54
+ rateLimit: z
55
+ .union([
56
+ z.boolean(),
57
+ z.object({
58
+ windowMs: z.number().int().positive().optional(),
59
+ max: z.number().int().positive().optional(),
60
+ message: z.string().min(1).optional(),
61
+ statusCode: z.number().int().min(400).max(599).optional(),
62
+ headers: z.boolean().optional(),
63
+ }).strict(),
64
+ ])
65
+ .default(false),
66
+ })
67
+ .strict();
68
+
69
+ /**
70
+ * Guard 설정 스키마 (strict)
71
+ */
72
+ const GuardConfigSchema = z
73
+ .object({
74
+ preset: z.enum(["mandu", "fsd", "clean", "hexagonal", "atomic"]).default("mandu"),
75
+ srcDir: z.string().default("src"),
76
+ exclude: z.array(z.string()).default([]),
77
+ realtime: z.boolean().default(true),
78
+ rules: z.record(z.enum(["error", "warn", "warning", "off"])).optional(),
79
+ })
80
+ .strict();
81
+
82
+ /**
83
+ * Build 설정 스키마 (strict)
84
+ */
85
+ const BuildConfigSchema = z
86
+ .object({
87
+ outDir: z.string().default(".mandu"),
88
+ minify: z.boolean().default(true),
89
+ sourcemap: z.boolean().default(false),
90
+ splitting: z.boolean().default(false),
91
+ })
92
+ .strict();
93
+
94
+ /**
95
+ * Dev 설정 스키마 (strict)
96
+ */
97
+ const DevConfigSchema = z
98
+ .object({
99
+ hmr: z.boolean().default(true),
100
+ watchDirs: z.array(z.string()).default([]),
101
+ })
102
+ .strict();
103
+
104
+ /**
105
+ * FS Routes 설정 스키마 (strict)
106
+ */
107
+ const FsRoutesConfigSchema = z
108
+ .object({
109
+ routesDir: z.string().default("app"),
110
+ extensions: z.array(z.string()).default([".tsx", ".ts", ".jsx", ".js"]),
111
+ exclude: z.array(z.string()).default([]),
112
+ islandSuffix: z.string().default(".island"),
113
+ })
114
+ .strict();
115
+
116
+ /**
117
+ * SEO 설정 스키마 (strict)
118
+ */
119
+ const SeoConfigSchema = z
120
+ .object({
121
+ enabled: z.boolean().default(true),
122
+ defaultTitle: z.string().optional(),
123
+ titleTemplate: z.string().optional(),
124
+ })
125
+ .strict();
126
+
127
+ /**
128
+ * Mandu 설정 스키마 (DNA-003: strict mode)
129
+ *
130
+ * 알 수 없는 키가 있으면 오류 발생 → 오타 즉시 감지
131
+ * MANDU_STRICT=0 으로 비활성화 가능
132
+ */
133
+ export const ManduConfigSchema = z
134
+ .object({
135
+ server: ServerConfigSchema.default({}),
136
+ guard: GuardConfigSchema.default({}),
137
+ build: BuildConfigSchema.default({}),
138
+ dev: DevConfigSchema.default({}),
139
+ fsRoutes: FsRoutesConfigSchema.default({}),
140
+ seo: SeoConfigSchema.default({}),
141
+ })
142
+ .strict();
143
+
144
+ export type ValidatedManduConfig = z.infer<typeof ManduConfigSchema>;
145
+
146
+ /**
147
+ * 검증 결과
148
+ */
149
+ export interface ValidationResult {
150
+ valid: boolean;
151
+ config?: ValidatedManduConfig;
152
+ errors?: Array<{
153
+ path: string;
154
+ message: string;
155
+ }>;
156
+ source?: string;
157
+ }
158
+
159
+ /**
160
+ * 설정 파일 검증
161
+ */
162
+ export async function validateConfig(rootDir: string): Promise<ValidationResult> {
163
+ for (const fileName of CONFIG_FILES) {
164
+ const filePath = path.join(rootDir, fileName);
165
+ if (!(await Bun.file(filePath).exists())) {
166
+ continue;
167
+ }
168
+
169
+ try {
170
+ let raw: unknown;
171
+ if (fileName.endsWith(".json")) {
172
+ raw = await readJsonFile(filePath);
173
+ } else {
174
+ const module = await import(pathToFileURL(filePath).href);
175
+ raw = module?.default ?? module;
176
+ }
177
+
178
+ const config = ManduConfigSchema.parse(coerceConfig(raw ?? {}, fileName));
179
+ return { valid: true, config, source: fileName };
180
+ } catch (error) {
181
+ if (error instanceof ZodError) {
182
+ const errors = error.errors.map((e) => ({
183
+ path: e.path.join("."),
184
+ message: e.message,
185
+ }));
186
+ return { valid: false, errors, source: fileName };
187
+ }
188
+
189
+ return {
190
+ valid: false,
191
+ errors: [
192
+ {
193
+ path: "",
194
+ message: `Failed to load config: ${
195
+ error instanceof Error ? error.message : String(error)
196
+ }`,
197
+ },
198
+ ],
199
+ source: fileName,
200
+ };
201
+ }
202
+ }
203
+
204
+ // 설정 파일 없음 - 기본값 사용
205
+ return { valid: true, config: ManduConfigSchema.parse({}) };
206
+ }
207
+
208
+ /**
209
+ * CLI용 검증 및 리포트
210
+ */
211
+ export async function validateAndReport(rootDir: string): Promise<ValidatedManduConfig | null> {
212
+ const result = await validateConfig(rootDir);
213
+
214
+ if (!result.valid) {
215
+ console.error(`\n❌ Invalid config${result.source ? ` (${result.source})` : ""}:\n`);
216
+ for (const error of result.errors || []) {
217
+ const location = error.path ? ` ${error.path}: ` : " ";
218
+ console.error(`${location}${error.message}`);
219
+ }
220
+ console.error("");
221
+ return null;
222
+ }
223
+
224
+ return result.config!;
225
+ }
@@ -95,7 +95,7 @@ export class ErrorClassifier {
95
95
  case "spec":
96
96
  errorType = "SPEC_ERROR";
97
97
  code = ErrorCode.SPEC_VALIDATION_ERROR;
98
- fixFile = blameFrame?.file || "spec/routes.manifest.json";
98
+ fixFile = blameFrame?.file || ".mandu/routes.manifest.json";
99
99
  suggestion = "Spec 파일의 JSON 구문 또는 스키마를 확인하세요";
100
100
  break;
101
101
 
@@ -258,7 +258,7 @@ export class ErrorClassifier {
258
258
  export function createSpecError(
259
259
  code: ErrorCode,
260
260
  message: string,
261
- file: string = "spec/routes.manifest.json",
261
+ file: string = ".mandu/routes.manifest.json",
262
262
  suggestion?: string
263
263
  ): ManduError {
264
264
  return {
@@ -105,14 +105,14 @@ export function createNotFoundResponse(
105
105
  pathname: string,
106
106
  routeContext?: RouteContext
107
107
  ): ManduError {
108
- return {
109
- errorType: "SPEC_ERROR",
110
- code: ErrorCode.SPEC_ROUTE_NOT_FOUND,
111
- httpStatus: 404,
112
- message: `Route not found: ${pathname}`,
113
- summary: "라우트 없음 - spec 파일에 추가 필요",
114
- fix: {
115
- file: "spec/routes.manifest.json",
108
+ return {
109
+ errorType: "SPEC_ERROR",
110
+ code: ErrorCode.SPEC_ROUTE_NOT_FOUND,
111
+ httpStatus: 404,
112
+ message: `Route not found: ${pathname}`,
113
+ summary: "라우트 없음 - spec 파일에 추가 필요",
114
+ fix: {
115
+ file: ".mandu/routes.manifest.json",
116
116
  suggestion: `'${pathname}' 패턴의 라우트를 추가하세요`,
117
117
  },
118
118
  route: routeContext,
@@ -127,14 +127,14 @@ export function createHandlerNotFoundResponse(
127
127
  routeId: string,
128
128
  pattern: string
129
129
  ): ManduError {
130
- return {
131
- errorType: "FRAMEWORK_BUG",
132
- code: ErrorCode.FRAMEWORK_ROUTER_ERROR,
133
- httpStatus: 500,
134
- message: `Handler not registered for route: ${routeId}`,
135
- summary: "핸들러 미등록 - generate 재실행 필요",
136
- fix: {
137
- file: `apps/server/generated/routes/${routeId}.route.ts`,
130
+ return {
131
+ errorType: "FRAMEWORK_BUG",
132
+ code: ErrorCode.FRAMEWORK_ROUTER_ERROR,
133
+ httpStatus: 500,
134
+ message: `Handler not registered for route: ${routeId}`,
135
+ summary: "핸들러 미등록 - generate 재실행 필요",
136
+ fix: {
137
+ file: `.mandu/generated/server/routes/${routeId}.route.ts`,
138
138
  suggestion: "bunx mandu generate를 실행하세요",
139
139
  },
140
140
  route: {
@@ -153,14 +153,14 @@ export function createPageLoadErrorResponse(
153
153
  pattern: string,
154
154
  originalError?: Error
155
155
  ): ManduError {
156
- const error: ManduError = {
157
- errorType: "LOGIC_ERROR",
158
- code: ErrorCode.SLOT_IMPORT_ERROR,
159
- httpStatus: 500,
160
- message: originalError?.message || `Failed to load page module for route: ${routeId}`,
161
- summary: `페이지 모듈 로드 실패 - ${routeId}.route.tsx 확인 필요`,
162
- fix: {
163
- file: `apps/web/generated/routes/${routeId}.route.tsx`,
156
+ const error: ManduError = {
157
+ errorType: "LOGIC_ERROR",
158
+ code: ErrorCode.SLOT_IMPORT_ERROR,
159
+ httpStatus: 500,
160
+ message: originalError?.message || `Failed to load page module for route: ${routeId}`,
161
+ summary: `페이지 모듈 로드 실패 - ${routeId}.route.tsx 확인 필요`,
162
+ fix: {
163
+ file: `.mandu/generated/web/routes/${routeId}.route.tsx`,
164
164
  suggestion: "import 경로와 컴포넌트 export를 확인하세요",
165
165
  },
166
166
  route: {
@@ -189,14 +189,14 @@ export function createSSRErrorResponse(
189
189
  pattern: string,
190
190
  originalError?: Error
191
191
  ): ManduError {
192
- const error: ManduError = {
193
- errorType: "FRAMEWORK_BUG",
194
- code: ErrorCode.FRAMEWORK_SSR_ERROR,
195
- httpStatus: 500,
196
- message: originalError?.message || `SSR rendering failed for route: ${routeId}`,
197
- summary: `SSR 렌더링 실패 - 컴포넌트 확인 필요`,
198
- fix: {
199
- file: `apps/web/generated/routes/${routeId}.route.tsx`,
192
+ const error: ManduError = {
193
+ errorType: "FRAMEWORK_BUG",
194
+ code: ErrorCode.FRAMEWORK_SSR_ERROR,
195
+ httpStatus: 500,
196
+ message: originalError?.message || `SSR rendering failed for route: ${routeId}`,
197
+ summary: `SSR 렌더링 실패 - 컴포넌트 확인 필요`,
198
+ fix: {
199
+ file: `.mandu/generated/web/routes/${routeId}.route.tsx`,
200
200
  suggestion: "React 컴포넌트가 서버에서 렌더링 가능한지 확인하세요 (브라우저 전용 API 사용 금지)",
201
201
  },
202
202
  route: {
@@ -183,6 +183,11 @@ export class StackTraceAnalyzer {
183
183
  isSpecFile(file: string): boolean {
184
184
  const normalized = this.normalizePath(file);
185
185
 
186
+ // .mandu/ 디렉토리 내 생성된 매니페스트/락 파일
187
+ if (normalized.startsWith(".mandu/") && normalized.endsWith(".json")) {
188
+ return true;
189
+ }
190
+
186
191
  // spec/ 디렉토리 내 JSON 파일
187
192
  if (normalized.startsWith("spec/") && normalized.endsWith(".json")) {
188
193
  return true;