@simplysm/sd-cli 14.0.19 → 14.0.22

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.
Files changed (114) hide show
  1. package/dist/angular/vite-postcss-inline-plugin.d.ts.map +1 -1
  2. package/dist/angular/vite-postcss-inline-plugin.js +4 -1
  3. package/dist/angular/vite-postcss-inline-plugin.js.map +1 -1
  4. package/dist/capacitor/capacitor-android.d.ts +16 -0
  5. package/dist/capacitor/capacitor-android.d.ts.map +1 -0
  6. package/dist/capacitor/capacitor-android.js +289 -0
  7. package/dist/capacitor/capacitor-android.js.map +1 -0
  8. package/dist/capacitor/capacitor.d.ts +0 -49
  9. package/dist/capacitor/capacitor.d.ts.map +1 -1
  10. package/dist/capacitor/capacitor.js +4 -244
  11. package/dist/capacitor/capacitor.js.map +1 -1
  12. package/dist/commands/check.js +2 -2
  13. package/dist/commands/check.js.map +1 -1
  14. package/dist/commands/lint.d.ts +1 -42
  15. package/dist/commands/lint.d.ts.map +1 -1
  16. package/dist/commands/lint.js +1 -151
  17. package/dist/commands/lint.js.map +1 -1
  18. package/dist/commands/publish.d.ts.map +1 -1
  19. package/dist/commands/publish.js +2 -1
  20. package/dist/commands/publish.js.map +1 -1
  21. package/dist/commands/typecheck.d.ts +3 -40
  22. package/dist/commands/typecheck.d.ts.map +1 -1
  23. package/dist/commands/typecheck.js +3 -232
  24. package/dist/commands/typecheck.js.map +1 -1
  25. package/dist/electron/electron.js +11 -4
  26. package/dist/electron/electron.js.map +1 -1
  27. package/dist/engines/ViteEngine.js +1 -1
  28. package/dist/engines/ViteEngine.js.map +1 -1
  29. package/dist/engines/types.d.ts +2 -0
  30. package/dist/engines/types.d.ts.map +1 -1
  31. package/dist/infra/ResultCollector.d.ts +2 -2
  32. package/dist/infra/ResultCollector.d.ts.map +1 -1
  33. package/dist/infra/ResultCollector.js +1 -1
  34. package/dist/orchestrators/DevWatchOrchestrator.d.ts +2 -1
  35. package/dist/orchestrators/DevWatchOrchestrator.d.ts.map +1 -1
  36. package/dist/orchestrators/DevWatchOrchestrator.js +28 -16
  37. package/dist/orchestrators/DevWatchOrchestrator.js.map +1 -1
  38. package/dist/orchestrators/TypecheckOrchestrator.d.ts +74 -0
  39. package/dist/orchestrators/TypecheckOrchestrator.d.ts.map +1 -0
  40. package/dist/orchestrators/TypecheckOrchestrator.js +285 -0
  41. package/dist/orchestrators/TypecheckOrchestrator.js.map +1 -0
  42. package/dist/sd-cli.js +6 -1
  43. package/dist/sd-cli.js.map +1 -1
  44. package/dist/utils/lint-core.d.ts +43 -0
  45. package/dist/utils/lint-core.d.ts.map +1 -0
  46. package/dist/utils/lint-core.js +154 -0
  47. package/dist/utils/lint-core.js.map +1 -0
  48. package/dist/utils/lint-utils.d.ts +1 -1
  49. package/dist/utils/lint-utils.d.ts.map +1 -1
  50. package/dist/utils/output-utils.d.ts +2 -2
  51. package/dist/utils/output-utils.d.ts.map +1 -1
  52. package/dist/utils/output-utils.js.map +1 -1
  53. package/dist/utils/server-production-files.d.ts +22 -0
  54. package/dist/utils/server-production-files.d.ts.map +1 -0
  55. package/dist/utils/server-production-files.js +162 -0
  56. package/dist/utils/server-production-files.js.map +1 -0
  57. package/dist/utils/vite-config.js.map +1 -1
  58. package/dist/workers/client.worker.d.ts.map +1 -1
  59. package/dist/workers/client.worker.js +7 -1
  60. package/dist/workers/client.worker.js.map +1 -1
  61. package/dist/workers/lint.worker.d.ts +1 -1
  62. package/dist/workers/lint.worker.d.ts.map +1 -1
  63. package/dist/workers/lint.worker.js +1 -1
  64. package/dist/workers/lint.worker.js.map +1 -1
  65. package/dist/workers/server-build.worker.d.ts.map +1 -1
  66. package/dist/workers/server-build.worker.js +12 -161
  67. package/dist/workers/server-build.worker.js.map +1 -1
  68. package/package.json +4 -4
  69. package/src/angular/vite-postcss-inline-plugin.ts +5 -1
  70. package/src/capacitor/capacitor-android.ts +368 -0
  71. package/src/capacitor/capacitor.ts +4 -317
  72. package/src/commands/check.ts +2 -2
  73. package/src/commands/lint.ts +1 -201
  74. package/src/commands/publish.ts +2 -1
  75. package/src/commands/typecheck.ts +7 -292
  76. package/src/electron/electron.ts +4 -4
  77. package/src/engines/ViteEngine.ts +1 -1
  78. package/src/engines/types.ts +3 -0
  79. package/src/infra/ResultCollector.ts +2 -2
  80. package/src/orchestrators/DevWatchOrchestrator.ts +35 -20
  81. package/src/orchestrators/TypecheckOrchestrator.ts +364 -0
  82. package/src/sd-cli.ts +6 -1
  83. package/src/utils/lint-core.ts +205 -0
  84. package/src/utils/lint-utils.ts +1 -1
  85. package/src/utils/output-utils.ts +3 -3
  86. package/src/utils/server-production-files.ts +186 -0
  87. package/src/utils/vite-config.ts +1 -1
  88. package/src/workers/client.worker.ts +7 -1
  89. package/src/workers/lint.worker.ts +1 -1
  90. package/src/workers/server-build.worker.ts +11 -185
  91. package/tests/angular/vite-postcss-inline-plugin.spec.ts +10 -0
  92. package/tests/capacitor/capacitor-android-exports.verify.md +11 -0
  93. package/tests/capacitor/capacitor-android.spec.ts +219 -0
  94. package/tests/capacitor/capacitor-build.spec.ts +17 -21
  95. package/tests/capacitor/capacitor-icon.spec.ts +17 -19
  96. package/tests/capacitor/capacitor-init.spec.ts +18 -14
  97. package/tests/capacitor/capacitor-run.spec.ts +10 -24
  98. package/tests/capacitor/capacitor-workspace.spec.ts +10 -15
  99. package/tests/commands/check.spec.ts +2 -2
  100. package/tests/commands/lint.spec.ts +33 -194
  101. package/tests/commands/publish-set.verify.md +7 -0
  102. package/tests/electron/electron-symlink-cleanup.verify.md +8 -0
  103. package/tests/engines/vite-engine.spec.ts +29 -0
  104. package/tests/infra/result-collector.spec.ts +11 -0
  105. package/tests/orchestrators/dev-watch-orchestrator.spec.ts +70 -0
  106. package/tests/orchestrators/dist-delete-watcher.verify.md +10 -0
  107. package/tests/orchestrators/typecheck-orchestrator.spec.ts +180 -0
  108. package/tests/sd-cli-catch-all.verify.md +7 -0
  109. package/tests/utils/lint-core-import-paths.verify.md +10 -0
  110. package/tests/utils/lint-core.spec.ts +188 -0
  111. package/tests/utils/server-production-files-import-paths.verify.md +14 -0
  112. package/tests/workers/client-worker.spec.ts +92 -0
  113. package/tests/workers/server-build-context-dispose.verify.md +8 -0
  114. package/tests/workers/server-build-worker.spec.ts +39 -0
@@ -0,0 +1,364 @@
1
+ import ts from "typescript";
2
+ import { err as errNs } from "@simplysm/core-common";
3
+ import { pathx } from "@simplysm/core-node";
4
+ import { consola } from "consola";
5
+ import { loadSdConfig } from "../utils/sd-config";
6
+ import { deserializeDiagnostic } from "../utils/typecheck-serialization";
7
+ import { createBuildEngine } from "../engines/index";
8
+ import { typecheckNonPackageFiles } from "../utils/typecheck-non-package";
9
+ import { runWithConcurrency, getMaxConcurrency } from "../utils/concurrency";
10
+ import { discoverWorkspacePackages, mergeTestsPackagesIntoConfig } from "../utils/package-utils";
11
+ import type { EngineResult } from "../engines/types";
12
+ import type { TypecheckEnv } from "../utils/tsconfig";
13
+ import { toTypecheckEnvs } from "../utils/tsconfig";
14
+
15
+ //#region Types
16
+
17
+ /**
18
+ * TypeScript 타입체크 옵션
19
+ */
20
+ export interface TypecheckOptions {
21
+ /** 타입체크 대상 경로 필터 (예: `packages/core-common`, `tests/orm`). 빈 배열이면 전체 대상. */
22
+ targets: string[];
23
+ /** sd.config.ts에 전달할 추가 옵션 */
24
+ options: string[];
25
+ /** true이면 엔진에 lint: true를 전달하고 lint 결과를 수집한다 */
26
+ lint?: boolean;
27
+ }
28
+
29
+ /**
30
+ * TypeScript 타입체크 실행 결과
31
+ */
32
+ export interface TypecheckResult {
33
+ success: boolean;
34
+ errorCount: number;
35
+ warningCount: number;
36
+ formattedOutput: string;
37
+ /** lint 결과 (TypecheckOptions.lint가 true일 때 존재) */
38
+ lint?: {
39
+ success: boolean;
40
+ errorCount: number;
41
+ warningCount: number;
42
+ formattedOutput: string;
43
+ };
44
+ /** 건너뛴 scripts 패키지 경로 (별도 lint용) */
45
+ scriptsPackagePaths?: string[];
46
+ }
47
+
48
+ //#endregion
49
+
50
+ //#region Utilities
51
+
52
+ const TARGET_PATH_PATTERN = /^(?:packages|tests)\/([^/]+)/;
53
+
54
+ /**
55
+ * 대상 경로에서 패키지명을 추출한다.
56
+ * "packages/core-common" → "core-common"
57
+ * "tests/orm" → "orm"
58
+ */
59
+ function extractTargetPackageNames(targets: string[]): Set<string> {
60
+ const names = new Set<string>();
61
+ for (const target of targets) {
62
+ const match = target.match(TARGET_PATH_PATTERN);
63
+ if (match) names.add(match[1]);
64
+ }
65
+ return names;
66
+ }
67
+
68
+ //#endregion
69
+
70
+ //#region TypecheckOrchestrator
71
+
72
+ /**
73
+ * 타입체크를 조율하는 Orchestrator
74
+ *
75
+ * BuildOrchestrator/DevWatchOrchestrator와 동일한 initialize() → start() → shutdown() 생명주기를 따른다.
76
+ * sd.config.ts 기반으로 패키지를 분류하고, BuildEngine을 사용하여 타입체크를 실행한다.
77
+ */
78
+ export class TypecheckOrchestrator {
79
+ private readonly _cwd: string;
80
+ private readonly _options: TypecheckOptions;
81
+ private readonly _logger = consola.withTag("sd:cli:typecheck");
82
+
83
+ // initialize()에서 설정되는 내부 상태
84
+ private readonly _typecheckTasks: Array<{ name: string; dir: string; config: any; env: TypecheckEnv }> =
85
+ [];
86
+ private readonly _scriptsPackagePaths: string[] = [];
87
+ private _includeNonPackage = false;
88
+
89
+ constructor(options: TypecheckOptions) {
90
+ this._cwd = process.cwd();
91
+ this._options = options;
92
+ }
93
+
94
+ /**
95
+ * Initialize Orchestrator
96
+ * - Load sd.config.ts
97
+ * - Discover workspace packages and merge tests
98
+ * - Classify packages into typecheck tasks
99
+ * - Collect scripts package paths
100
+ */
101
+ async initialize(): Promise<void> {
102
+ const { targets } = this._options;
103
+ const phaseLabel = this._options.lint === true ? "타입체크/린트" : "타입체크";
104
+
105
+ this._logger.debug(`${phaseLabel} 시작`, { targets, lint: this._options.lint });
106
+
107
+ // sd.config.ts 로드
108
+ const sdConfig = await loadSdConfig({ cwd: this._cwd, dev: false, opt: this._options.options });
109
+ this._logger.debug("sd.config.ts 로드 완료");
110
+
111
+ // 워크스페이스 패키지 탐색 및 tests/를 설정에 병합
112
+ const workspacePackages = discoverWorkspacePackages(this._cwd);
113
+ const { merged, pathMap } = mergeTestsPackagesIntoConfig(
114
+ sdConfig.packages,
115
+ workspacePackages,
116
+ );
117
+
118
+ // 경로 기반 대상에서 패키지명 결정
119
+ const targetNames = extractTargetPackageNames(targets);
120
+
121
+ // 타입체크할 패키지 수집 (scripts 제외), env별로 확장
122
+ for (const [name, config] of Object.entries(merged)) {
123
+ if (config == null) continue;
124
+ if (config.target === "scripts") {
125
+ if (targets.length === 0 || targetNames.has(name)) {
126
+ const relPath = pathMap.get(name) ?? `packages/${name}`;
127
+ this._scriptsPackagePaths.push(relPath);
128
+ }
129
+ continue;
130
+ }
131
+ if (targets.length > 0 && !targetNames.has(name)) continue;
132
+
133
+ const relPath = pathMap.get(name) ?? `packages/${name}`;
134
+ // 클라이언트 패키지의 경우 browser 타겟을 사용하여 createBuildEngine이 ViteEngine 대신 NgtscEngine으로 라우팅되도록 함
135
+ const typecheckConfig =
136
+ config.target === "client" ? { target: "browser" as const } : config;
137
+ const envs = toTypecheckEnvs(config.target);
138
+ for (const env of envs) {
139
+ this._typecheckTasks.push({
140
+ name,
141
+ dir: pathx.posixResolve(this._cwd, relPath),
142
+ config: typecheckConfig,
143
+ env,
144
+ });
145
+ }
146
+ }
147
+
148
+ // 비패키지 타입체크: 대상이 지정되지 않은 경우에만 (= 전체 검사)
149
+ this._includeNonPackage = targets.length === 0;
150
+ }
151
+
152
+ /**
153
+ * 타입체크 실행
154
+ * - 엔진 생성 및 동시성 제어된 실행
155
+ * - 비패키지 타입체크
156
+ * - 결과 집계 및 포맷 출력 생성
157
+ *
158
+ * @returns 타입체크 결과
159
+ */
160
+ async start(): Promise<TypecheckResult> {
161
+ const phaseLabel = this._options.lint === true ? "타입체크/린트" : "타입체크";
162
+ const formatHost: ts.FormatDiagnosticsHost = {
163
+ getCanonicalFileName: (f) => f,
164
+ getCurrentDirectory: () => this._cwd,
165
+ getNewLine: () => ts.sys.newLine,
166
+ };
167
+
168
+ if (this._typecheckTasks.length === 0 && !this._includeNonPackage) {
169
+ this._logger.info(`${phaseLabel} 대상 없음`);
170
+ return {
171
+ success: true,
172
+ errorCount: 0,
173
+ warningCount: 0,
174
+ formattedOutput: `✔ ${phaseLabel} 대상 없음.\n`,
175
+ scriptsPackagePaths:
176
+ this._scriptsPackagePaths.length > 0 ? this._scriptsPackagePaths : undefined,
177
+ };
178
+ }
179
+
180
+ // 동시성 제한이 있는 BuildEngine 작업 생성
181
+ const allDiagnostics: ts.Diagnostic[] = [];
182
+ let totalErrorCount = 0;
183
+ let totalWarningCount = 0;
184
+ const fileCache = new Map<string, string>();
185
+
186
+ // Lint 결과 집계
187
+ let lintErrorCount = 0;
188
+ let lintWarningCount = 0;
189
+ let lintSuccess = true;
190
+ const lintOutputs: string[] = [];
191
+
192
+ if (this._typecheckTasks.length > 0) {
193
+ const tasks = this._typecheckTasks.map((task) => async (): Promise<EngineResult> => {
194
+ const label = `${task.name}:${task.env}`;
195
+ const engine = createBuildEngine(
196
+ { name: task.name, dir: task.dir, config: task.config },
197
+ { cwd: this._cwd },
198
+ );
199
+ try {
200
+ this._logger.debug(`[${label}] 타입체크 시작됨`);
201
+ const result = await engine.run({
202
+ js: false,
203
+ dts: false,
204
+ env: task.env,
205
+ includeTests: true,
206
+ ...(this._options.lint === true ? { lint: true } : {}),
207
+ });
208
+ this._logger.debug(
209
+ `[${label}] 타입체크 ${result.build.success ? "완료" : "실패"}`,
210
+ );
211
+ return result;
212
+ } catch (err) {
213
+ const message = errNs.message(err);
214
+ const stack = err instanceof Error ? err.stack : undefined;
215
+ this._logger.error(`[${label}] 엔진 작업 실패: ${message}`);
216
+ if (stack != null) {
217
+ this._logger.debug(`[${label}] 스택 트레이스:\n${stack}`);
218
+ }
219
+ return {
220
+ build: {
221
+ success: false,
222
+ errors: [`[${label}] ${message}`],
223
+ warnings: [],
224
+ diagnostics: [],
225
+ },
226
+ };
227
+ } finally {
228
+ await engine.stop();
229
+ }
230
+ });
231
+
232
+ const concurrency = getMaxConcurrency();
233
+ this._logger.start(
234
+ `${phaseLabel} 실행 중... (${tasks.length}개 작업, 동시성: ${concurrency})`,
235
+ );
236
+ const results = await runWithConcurrency(tasks, concurrency);
237
+ this._logger.success(`${phaseLabel} 실행 완료`);
238
+
239
+ // 엔진 결과 집계 (모든 task는 catch로 인해 항상 fulfilled)
240
+ for (const settled of results) {
241
+ if (settled.status !== "fulfilled") continue;
242
+ const engineResult = settled.value;
243
+ const buildDiags = engineResult.build.diagnostics.map((d) =>
244
+ deserializeDiagnostic(d, fileCache),
245
+ );
246
+ allDiagnostics.push(...buildDiags);
247
+ // 역직렬화된 진단 정보에서 에러/경고 수 집계
248
+ // 숫자 카테고리 값 사용 (ts.DiagnosticCategory: Error=1, Warning=0)
249
+ totalErrorCount += buildDiags.filter((d) => d.category === 1).length;
250
+ totalWarningCount += buildDiags.filter((d) => d.category === 0).length;
251
+ if (!engineResult.build.success && buildDiags.length === 0) {
252
+ for (const errMsg of engineResult.build.errors) {
253
+ allDiagnostics.push({
254
+ category: 1,
255
+ code: 0,
256
+ messageText: errMsg,
257
+ file: undefined,
258
+ start: undefined,
259
+ length: undefined,
260
+ });
261
+ }
262
+ totalErrorCount += engineResult.build.errors.length || 1;
263
+ }
264
+
265
+ // Lint 결과 수집
266
+ if (engineResult.lint != null) {
267
+ lintErrorCount += engineResult.lint.errorCount;
268
+ lintWarningCount += engineResult.lint.warningCount;
269
+ if (!engineResult.lint.success) lintSuccess = false;
270
+ if (engineResult.lint.formattedOutput !== "") {
271
+ lintOutputs.push(engineResult.lint.formattedOutput);
272
+ }
273
+ }
274
+ }
275
+ }
276
+
277
+ // 비패키지 타입체크
278
+ if (this._includeNonPackage) {
279
+ this._logger.debug("비패키지 타입체크 실행 중");
280
+ const nonPkgResult = typecheckNonPackageFiles(this._cwd);
281
+ totalErrorCount += nonPkgResult.errorCount;
282
+ totalWarningCount += nonPkgResult.warningCount;
283
+ const nonPkgDiags = nonPkgResult.diagnostics.map((d) =>
284
+ deserializeDiagnostic(d, fileCache),
285
+ );
286
+ allDiagnostics.push(...nonPkgDiags);
287
+ }
288
+
289
+ // 요약 로그
290
+ const resultMeta: Record<string, number> = {
291
+ errorCount: totalErrorCount,
292
+ warningCount: totalWarningCount,
293
+ };
294
+ if (this._options.lint === true) {
295
+ resultMeta["lintErrorCount"] = lintErrorCount;
296
+ resultMeta["lintWarningCount"] = lintWarningCount;
297
+ }
298
+ if (totalErrorCount > 0) {
299
+ this._logger.error(`${phaseLabel} 에러 발생`, resultMeta);
300
+ } else {
301
+ this._logger.info(`${phaseLabel} 완료`, resultMeta);
302
+ }
303
+
304
+ // 진단 출력 포매팅
305
+ let formattedOutput = "";
306
+ if (allDiagnostics.length > 0) {
307
+ const uniqueDiagnostics = ts.sortAndDeduplicateDiagnostics(allDiagnostics);
308
+ formattedOutput = ts.formatDiagnosticsWithColorAndContext(uniqueDiagnostics, formatHost);
309
+ }
310
+
311
+ // lint가 요청된 경우 lint 결과 생성
312
+ const lintResult =
313
+ this._options.lint === true
314
+ ? {
315
+ success: lintSuccess,
316
+ errorCount: lintErrorCount,
317
+ warningCount: lintWarningCount,
318
+ formattedOutput: lintOutputs.join("\n"),
319
+ }
320
+ : undefined;
321
+
322
+ return {
323
+ success: totalErrorCount === 0,
324
+ errorCount: totalErrorCount,
325
+ warningCount: totalWarningCount,
326
+ formattedOutput,
327
+ lint: lintResult,
328
+ scriptsPackagePaths:
329
+ this._scriptsPackagePaths.length > 0 ? this._scriptsPackagePaths : undefined,
330
+ };
331
+ }
332
+
333
+ /**
334
+ * Orchestrator 종료 (현재 정리할 리소스 없음)
335
+ */
336
+ async shutdown(): Promise<void> {
337
+ // 타입체크는 일회성 작업이므로 종료 시 정리할 리소스가 없음
338
+ // 엔진은 각 작업 내에서 stop()으로 정리됨
339
+ await Promise.resolve();
340
+ }
341
+ }
342
+
343
+ //#endregion
344
+
345
+ //#region Convenience Function
346
+
347
+ /**
348
+ * TypecheckOrchestrator의 전체 생명주기를 래핑한 편의 함수.
349
+ * initialize() → start() → shutdown() 순서로 호출한다.
350
+ *
351
+ * @param options - 타입체크 실행 옵션
352
+ * @returns 타입체크 결과
353
+ */
354
+ export async function executeTypecheck(options: TypecheckOptions): Promise<TypecheckResult> {
355
+ const orchestrator = new TypecheckOrchestrator(options);
356
+ await orchestrator.initialize();
357
+ try {
358
+ return await orchestrator.start();
359
+ } finally {
360
+ await orchestrator.shutdown();
361
+ }
362
+ }
363
+
364
+ //#endregion
package/src/sd-cli.ts CHANGED
@@ -8,6 +8,7 @@
8
8
  */
9
9
 
10
10
  import { cpx } from "@simplysm/core-node";
11
+ import { consola } from "consola";
11
12
  import os from "os";
12
13
  import path from "path";
13
14
  import { fileURLToPath } from "url";
@@ -34,8 +35,12 @@ if (isDev) {
34
35
  if (process.argv[2] !== "replace-deps" && sdConfig.replaceDeps != null) {
35
36
  await setupReplaceDeps(process.cwd(), sdConfig.replaceDeps);
36
37
  }
37
- } catch {
38
+ } catch (err: unknown) {
38
39
  // sd.config.ts가 없거나 replaceDeps가 설정되지 않으면 건너뜀
40
+ const code = err instanceof Error && "code" in err ? (err as NodeJS.ErrnoException).code : undefined;
41
+ if (code !== "MODULE_NOT_FOUND" && code !== "ERR_MODULE_NOT_FOUND") {
42
+ consola.warn("[sd-cli] replaceDeps 사전 설정 실패:", err instanceof Error ? err.message : err);
43
+ }
39
44
  }
40
45
 
41
46
  // Phase 2: 실제 CLI를 새 프로세스로 실행 (모듈 캐시 초기화)
@@ -0,0 +1,205 @@
1
+ import { ESLint } from "eslint";
2
+ import { createJiti } from "jiti";
3
+ import path from "path";
4
+ import { fsx, pathx } from "@simplysm/core-node";
5
+ import { env, SdError } from "@simplysm/core-common";
6
+ import { consola } from "consola";
7
+
8
+ //#region Types
9
+
10
+ /**
11
+ * ESLint 실행 옵션
12
+ */
13
+ export interface LintOptions {
14
+ /** 린트 대상 경로 필터 (예: `packages/core-common`). 빈 배열이면 전체 대상 */
15
+ targets: string[];
16
+ /** 자동 수정 활성화 */
17
+ fix: boolean;
18
+ /** ESLint 규칙별 실행 시간 측정 활성화 (TIMING 환경변수 설정) */
19
+ timing: boolean;
20
+ }
21
+
22
+ /**
23
+ * executeLint()의 반환 타입
24
+ */
25
+ export interface LintResult {
26
+ /** 린트 에러가 없으면 true */
27
+ success: boolean;
28
+ /** ESLint 전체 에러 수 */
29
+ errorCount: number;
30
+ /** ESLint 전체 경고 수 */
31
+ warningCount: number;
32
+ /** 포매터 출력 문자열 (stdout에 출력할 내용) */
33
+ formattedOutput: string;
34
+ }
35
+
36
+ //#endregion
37
+
38
+ //#region Utilities
39
+
40
+ /** ESLint 설정 파일 검색 순서 */
41
+ const ESLINT_CONFIG_FILES = [
42
+ "eslint.config.ts",
43
+ "eslint.config.mts",
44
+ "eslint.config.js",
45
+ "eslint.config.mjs",
46
+ ] as const;
47
+
48
+ /**
49
+ * ESLint 설정 객체가 ignores 속성만 가지고 있는지 확인하는 타입 가드
50
+ */
51
+ function isGlobalIgnoresConfig(item: unknown): item is { ignores: string[] } {
52
+ if (item == null || typeof item !== "object") return false;
53
+ if (!("ignores" in item)) return false;
54
+ if ("files" in item) return false; // files가 있으면 globalIgnores가 아님
55
+ const ignores = (item as { ignores: unknown }).ignores;
56
+ if (!Array.isArray(ignores)) return false;
57
+ return ignores.every((i) => typeof i === "string");
58
+ }
59
+
60
+ /**
61
+ * eslint.config.ts/js에서 globalIgnores 패턴을 추출한다.
62
+ * ignores만 있고 files가 없는 설정 객체가 globalIgnores이다.
63
+ * @internal 테스트용으로 export
64
+ */
65
+ export async function loadIgnorePatterns(cwd: string): Promise<string[]> {
66
+ let configPath: string | undefined;
67
+ for (const f of ESLINT_CONFIG_FILES) {
68
+ const p = path.join(cwd, f);
69
+ if (await fsx.exists(p)) {
70
+ configPath = p;
71
+ break;
72
+ }
73
+ }
74
+
75
+ if (configPath == null) {
76
+ throw new SdError(
77
+ `ESLint 설정 파일을 찾을 수 없습니다 (cwd: ${cwd}): ${ESLINT_CONFIG_FILES.join(", ")}`,
78
+ );
79
+ }
80
+
81
+ const jiti = createJiti(import.meta.url);
82
+ const configModule = await jiti.import<{ default: Record<string, unknown>[] } | undefined>(
83
+ configPath,
84
+ );
85
+
86
+ let configs: unknown;
87
+ if (Array.isArray(configModule)) {
88
+ configs = configModule;
89
+ } else if (
90
+ configModule != null &&
91
+ typeof configModule === "object" &&
92
+ "default" in configModule
93
+ ) {
94
+ configs = configModule.default;
95
+ } else {
96
+ throw new SdError(`ESLint 설정 파일의 형식이 올바르지 않습니다: ${configPath}`);
97
+ }
98
+
99
+ if (!Array.isArray(configs)) {
100
+ throw new SdError(`ESLint 설정이 배열이 아닙니다: ${configPath}`);
101
+ }
102
+
103
+ return configs.filter(isGlobalIgnoresConfig).flatMap((item) => item.ignores);
104
+ }
105
+
106
+ //#endregion
107
+
108
+ //#region Main
109
+
110
+ /**
111
+ * ESLint를 실행하고 결과를 반환한다.
112
+ *
113
+ * - `eslint.config.ts/js`에서 globalIgnores 패턴을 추출하여 glob 필터링에 적용
114
+ * - consola를 사용하여 진행 상황 표시
115
+ * - 캐시 활성화 (`.cache/eslint.cache`에 저장, 설정 변경 시 자동 무효화)
116
+ * - stdout 출력이나 process.exitCode 설정 없음 (호출자가 결정)
117
+ *
118
+ * @param options - 린트 실행 옵션
119
+ * @returns 린트 결과 (성공 여부, 에러/경고 수, 포매터 출력)
120
+ */
121
+ export async function executeLint(options: LintOptions): Promise<LintResult> {
122
+ const { targets, fix, timing } = options;
123
+ const cwd = process.cwd();
124
+ const logger = consola.withTag("sd:cli:lint");
125
+
126
+ logger.debug("린트 시작", { targets, fix, timing });
127
+
128
+ // TIMING 환경변수 설정
129
+ if (timing) {
130
+ env("TIMING", "1");
131
+ }
132
+
133
+ // ESLint 설정 로드
134
+ logger.start("ESLint 설정 로딩 중");
135
+ const ignorePatterns = await loadIgnorePatterns(cwd);
136
+ logger.debug("무시 패턴 로드 완료", { ignorePatternCount: ignorePatterns.length });
137
+ logger.success(`ESLint 설정 로드 완료 (${ignorePatterns.length}개 무시 패턴)`);
138
+
139
+ // 린트 대상 파일 수집
140
+ logger.start("린트 대상 파일 수집 중");
141
+ let files = await fsx.glob("**/*.{ts,js,mjs,cjs}", {
142
+ cwd,
143
+ ignore: ignorePatterns,
144
+ nodir: true,
145
+ absolute: true,
146
+ });
147
+ files = pathx.filterByTargets(files, targets, cwd);
148
+ logger.debug("파일 수집 완료", { fileCount: files.length });
149
+ logger.success(`린트 대상 파일 수집 완료 (${files.length}개 파일)`);
150
+
151
+ // 린트 실행
152
+ let eslint: ESLint | undefined;
153
+ let eslintResults: ESLint.LintResult[] | undefined;
154
+ if (files.length > 0) {
155
+ logger.start(`린트 실행 중... (${files.length}개 파일)`);
156
+ eslint = new ESLint({
157
+ cwd,
158
+ fix,
159
+ cache: true,
160
+ cacheLocation: path.join(cwd, ".cache", "eslint.cache"),
161
+ });
162
+ eslintResults = await eslint.lintFiles(files);
163
+ logger.success("린트 실행 완료");
164
+
165
+ // 자동 수정 적용
166
+ if (fix) {
167
+ logger.debug("자동 수정 적용 중...");
168
+ await ESLint.outputFixes(eslintResults);
169
+ logger.debug("자동 수정 적용 완료");
170
+ }
171
+ }
172
+
173
+ // 파일이 없거나 린트가 실행되지 않은 경우 성공 결과 반환
174
+ if (files.length === 0 || eslintResults == null || eslint == null) {
175
+ logger.info("린트할 파일 없음");
176
+ return { success: true, errorCount: 0, warningCount: 0, formattedOutput: "" };
177
+ }
178
+
179
+ // 결과 집계
180
+ let errorCount = eslintResults.sum((r) => r.errorCount);
181
+ let warningCount = eslintResults.sum((r) => r.warningCount);
182
+
183
+ if (errorCount > 0) {
184
+ logger.error("린트 에러 발생", { errorCount, warningCount });
185
+ } else {
186
+ logger.info("린트 완료", { errorCount, warningCount });
187
+ }
188
+
189
+ // 포매터 출력 수집
190
+ let formattedOutput = "";
191
+ const formatter = await eslint.loadFormatter("stylish");
192
+ const resultText = await formatter.format(eslintResults);
193
+ if (resultText) {
194
+ formattedOutput += resultText;
195
+ }
196
+
197
+ return {
198
+ success: errorCount === 0,
199
+ errorCount,
200
+ warningCount,
201
+ formattedOutput,
202
+ };
203
+ }
204
+
205
+ //#endregion
@@ -1,5 +1,5 @@
1
1
  import { Worker } from "@simplysm/core-node";
2
- import type { LintOptions, LintResult } from "../commands/lint";
2
+ import type { LintOptions, LintResult } from "./lint-core";
3
3
  import type * as LintWorkerModule from "../workers/lint.worker";
4
4
 
5
5
  /**
@@ -18,7 +18,7 @@ export function formatBuildMessages(name: string, label: string, messages: strin
18
18
  * 에러만 출력한다.
19
19
  * @param results 패키지별 빌드 결과 상태
20
20
  */
21
- export function printErrors(results: Map<string, BuildResult>): void {
21
+ export function printErrors(results: ReadonlyMap<string, BuildResult>): void {
22
22
  for (const result of results.values()) {
23
23
  if (result.status === "error") {
24
24
  const typeLabel = result.type === "lint" ? "lint" : result.target;
@@ -37,8 +37,8 @@ export function printErrors(results: Map<string, BuildResult>): void {
37
37
  * @param serverClientsMap 서버별 연결된 클라이언트 목록
38
38
  */
39
39
  export function printServers(
40
- results: Map<string, BuildResult>,
41
- serverClientsMap?: Map<string, string[]>,
40
+ results: ReadonlyMap<string, BuildResult>,
41
+ serverClientsMap?: ReadonlyMap<string, string[]>,
42
42
  ): void {
43
43
  // 서버 정보 수집
44
44
  const servers = [...results.values()].filter((r) => r.status === "running" && r.port != null);