@mandujs/cli 0.12.2 → 0.13.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/README.ko.md +234 -234
- package/README.md +354 -354
- package/package.json +2 -2
- package/src/commands/contract.ts +173 -173
- package/src/commands/dev.ts +8 -68
- package/src/commands/doctor.ts +27 -27
- package/src/commands/guard-arch.ts +303 -303
- package/src/commands/guard-check.ts +3 -3
- package/src/commands/monitor.ts +300 -300
- package/src/commands/openapi.ts +107 -107
- package/src/commands/registry.ts +367 -357
- package/src/commands/routes.ts +228 -228
- package/src/commands/start.ts +184 -0
- package/src/errors/codes.ts +35 -35
- package/src/errors/index.ts +2 -2
- package/src/errors/messages.ts +143 -143
- package/src/hooks/index.ts +17 -17
- package/src/hooks/preaction.ts +256 -256
- package/src/main.ts +37 -34
- package/src/terminal/banner.ts +166 -166
- package/src/terminal/help.ts +306 -306
- package/src/terminal/index.ts +71 -71
- package/src/terminal/output.ts +295 -295
- package/src/terminal/palette.ts +30 -30
- package/src/terminal/progress.ts +327 -327
- package/src/terminal/stream-writer.ts +214 -214
- package/src/terminal/table.ts +354 -354
- package/src/terminal/theme.ts +142 -142
- package/src/util/bun.ts +6 -6
- package/src/util/fs.ts +23 -23
- package/src/util/handlers.ts +96 -0
- package/src/util/manifest.ts +52 -52
- package/src/util/output.ts +22 -22
- package/src/util/port.ts +71 -71
- package/templates/default/AGENTS.md +96 -96
- package/templates/default/app/api/health/route.ts +13 -13
- package/templates/default/app/globals.css +49 -49
- package/templates/default/app/layout.tsx +27 -27
- package/templates/default/app/page.tsx +38 -38
- package/templates/default/package.json +1 -0
- package/templates/default/src/client/shared/lib/utils.ts +16 -16
- package/templates/default/src/client/shared/ui/button.tsx +57 -57
- package/templates/default/src/client/shared/ui/card.tsx +78 -78
- package/templates/default/src/client/shared/ui/index.ts +21 -21
- package/templates/default/src/client/shared/ui/input.tsx +24 -24
- package/templates/default/tests/example.test.ts +58 -58
- package/templates/default/tests/helpers.ts +52 -52
- package/templates/default/tests/setup.ts +9 -9
- package/templates/default/tsconfig.json +12 -14
- package/templates/default/apps/server/main.ts +0 -67
- package/templates/default/apps/web/entry.tsx +0 -35
package/src/terminal/progress.ts
CHANGED
|
@@ -1,327 +1,327 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DNA-012: Multi-fallback Progress
|
|
3
|
-
*
|
|
4
|
-
* 다단계 폴백 프로그레스 시스템
|
|
5
|
-
* - TTY: 스피너 (ora) → 라인 → 로그
|
|
6
|
-
* - Non-TTY: 로그만
|
|
7
|
-
* - withProgress() 패턴으로 자동 정리
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { theme } from "./theme.js";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 프로그레스 옵션
|
|
14
|
-
*/
|
|
15
|
-
export interface ProgressOptions {
|
|
16
|
-
/** 레이블 */
|
|
17
|
-
label: string;
|
|
18
|
-
/** 전체 단계 수 (기본: 100) */
|
|
19
|
-
total?: number;
|
|
20
|
-
/** 출력 스트림 (기본: stderr) */
|
|
21
|
-
stream?: NodeJS.WriteStream;
|
|
22
|
-
/** 폴백 모드 */
|
|
23
|
-
fallback?: "spinner" | "line" | "log" | "none";
|
|
24
|
-
/** 성공 메시지 */
|
|
25
|
-
successMessage?: string;
|
|
26
|
-
/** 실패 메시지 */
|
|
27
|
-
failMessage?: string;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 프로그레스 리포터
|
|
32
|
-
*/
|
|
33
|
-
export interface ProgressReporter {
|
|
34
|
-
/** 레이블 변경 */
|
|
35
|
-
setLabel: (label: string) => void;
|
|
36
|
-
/** 퍼센트 설정 (0-100) */
|
|
37
|
-
setPercent: (percent: number) => void;
|
|
38
|
-
/** 진행 (delta 만큼 증가) */
|
|
39
|
-
tick: (delta?: number) => void;
|
|
40
|
-
/** 성공 완료 */
|
|
41
|
-
done: (message?: string) => void;
|
|
42
|
-
/** 실패 완료 */
|
|
43
|
-
fail: (message?: string) => void;
|
|
44
|
-
/** 현재 퍼센트 */
|
|
45
|
-
getPercent: () => number;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 스피너 문자
|
|
50
|
-
*/
|
|
51
|
-
const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* 간단한 스피너 구현 (ora 대체)
|
|
55
|
-
*/
|
|
56
|
-
function createSpinner(stream: NodeJS.WriteStream) {
|
|
57
|
-
let frameIndex = 0;
|
|
58
|
-
let intervalId: ReturnType<typeof setInterval> | null = null;
|
|
59
|
-
let text = "";
|
|
60
|
-
let isRunning = false;
|
|
61
|
-
|
|
62
|
-
const render = () => {
|
|
63
|
-
if (!isRunning) return;
|
|
64
|
-
const frame = SPINNER_FRAMES[frameIndex % SPINNER_FRAMES.length];
|
|
65
|
-
stream.write(`\r${theme.accent(frame)} ${text}`);
|
|
66
|
-
frameIndex++;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
start: (initialText: string) => {
|
|
71
|
-
text = initialText;
|
|
72
|
-
isRunning = true;
|
|
73
|
-
render();
|
|
74
|
-
intervalId = setInterval(render, 80);
|
|
75
|
-
},
|
|
76
|
-
setText: (newText: string) => {
|
|
77
|
-
text = newText;
|
|
78
|
-
},
|
|
79
|
-
succeed: (successText: string) => {
|
|
80
|
-
isRunning = false;
|
|
81
|
-
if (intervalId) clearInterval(intervalId);
|
|
82
|
-
stream.write(`\r${theme.success("✓")} ${successText}\n`);
|
|
83
|
-
},
|
|
84
|
-
fail: (failText: string) => {
|
|
85
|
-
isRunning = false;
|
|
86
|
-
if (intervalId) clearInterval(intervalId);
|
|
87
|
-
stream.write(`\r${theme.error("✗")} ${failText}\n`);
|
|
88
|
-
},
|
|
89
|
-
stop: () => {
|
|
90
|
-
isRunning = false;
|
|
91
|
-
if (intervalId) clearInterval(intervalId);
|
|
92
|
-
stream.write("\r" + " ".repeat(text.length + 5) + "\r");
|
|
93
|
-
},
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* 프로그레스 바 렌더링
|
|
99
|
-
*/
|
|
100
|
-
function renderProgressBar(percent: number, width: number = 20): string {
|
|
101
|
-
const filled = Math.round((percent / 100) * width);
|
|
102
|
-
const empty = width - filled;
|
|
103
|
-
const bar = "█".repeat(filled) + "░".repeat(empty);
|
|
104
|
-
return `[${bar}]`;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* CLI 프로그레스 생성
|
|
109
|
-
*
|
|
110
|
-
* @example
|
|
111
|
-
* ```ts
|
|
112
|
-
* const progress = createCliProgress({ label: "Building...", total: 4 });
|
|
113
|
-
*
|
|
114
|
-
* progress.setLabel("Scanning routes...");
|
|
115
|
-
* await scanRoutes();
|
|
116
|
-
* progress.tick();
|
|
117
|
-
*
|
|
118
|
-
* progress.setLabel("Bundling...");
|
|
119
|
-
* await bundle();
|
|
120
|
-
* progress.tick();
|
|
121
|
-
*
|
|
122
|
-
* progress.done("Build complete!");
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
|
-
export function createCliProgress(options: ProgressOptions): ProgressReporter {
|
|
126
|
-
const {
|
|
127
|
-
label: initialLabel,
|
|
128
|
-
total = 100,
|
|
129
|
-
stream = process.stderr,
|
|
130
|
-
fallback = "spinner",
|
|
131
|
-
successMessage,
|
|
132
|
-
failMessage,
|
|
133
|
-
} = options;
|
|
134
|
-
|
|
135
|
-
const isTty = stream.isTTY;
|
|
136
|
-
|
|
137
|
-
let label = initialLabel;
|
|
138
|
-
let completed = 0;
|
|
139
|
-
|
|
140
|
-
// TTY: 스피너 사용 (stdout이 pipe여도 stderr TTY면 동작)
|
|
141
|
-
const spinner = isTty && fallback === "spinner" ? createSpinner(stream) : null;
|
|
142
|
-
|
|
143
|
-
if (spinner) {
|
|
144
|
-
spinner.start(label);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const getPercent = () => Math.round((completed / total) * 100);
|
|
148
|
-
|
|
149
|
-
const render = () => {
|
|
150
|
-
const percent = getPercent();
|
|
151
|
-
const text =
|
|
152
|
-
total > 1 ? `${label} ${renderProgressBar(percent)} ${percent}%` : label;
|
|
153
|
-
|
|
154
|
-
if (spinner) {
|
|
155
|
-
spinner.setText(text);
|
|
156
|
-
} else if (isTty && fallback === "line") {
|
|
157
|
-
stream.write(`\r${text}`);
|
|
158
|
-
}
|
|
159
|
-
// "log" 모드는 상태 변경 시마다 로그하지 않음
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
return {
|
|
163
|
-
setLabel: (next: string) => {
|
|
164
|
-
label = next;
|
|
165
|
-
render();
|
|
166
|
-
},
|
|
167
|
-
|
|
168
|
-
setPercent: (percent: number) => {
|
|
169
|
-
completed = (Math.max(0, Math.min(100, percent)) / 100) * total;
|
|
170
|
-
render();
|
|
171
|
-
},
|
|
172
|
-
|
|
173
|
-
tick: (delta = 1) => {
|
|
174
|
-
completed = Math.min(total, completed + delta);
|
|
175
|
-
render();
|
|
176
|
-
},
|
|
177
|
-
|
|
178
|
-
getPercent,
|
|
179
|
-
|
|
180
|
-
done: (message?: string) => {
|
|
181
|
-
const finalMessage =
|
|
182
|
-
message ?? successMessage ?? `${initialLabel} completed`;
|
|
183
|
-
|
|
184
|
-
if (spinner) {
|
|
185
|
-
spinner.succeed(finalMessage);
|
|
186
|
-
} else if (isTty) {
|
|
187
|
-
stream.write(`\r${theme.success("✓")} ${finalMessage}\n`);
|
|
188
|
-
} else if (fallback !== "none") {
|
|
189
|
-
stream.write(`[OK] ${finalMessage}\n`);
|
|
190
|
-
}
|
|
191
|
-
},
|
|
192
|
-
|
|
193
|
-
fail: (message?: string) => {
|
|
194
|
-
const finalMessage =
|
|
195
|
-
message ?? failMessage ?? `${initialLabel} failed`;
|
|
196
|
-
|
|
197
|
-
if (spinner) {
|
|
198
|
-
spinner.fail(finalMessage);
|
|
199
|
-
} else if (isTty) {
|
|
200
|
-
stream.write(`\r${theme.error("✗")} ${finalMessage}\n`);
|
|
201
|
-
} else if (fallback !== "none") {
|
|
202
|
-
stream.write(`[FAIL] ${finalMessage}\n`);
|
|
203
|
-
}
|
|
204
|
-
},
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* 프로그레스 컨텍스트 패턴
|
|
210
|
-
*
|
|
211
|
-
* 작업 완료 후 자동으로 프로그레스 정리
|
|
212
|
-
*
|
|
213
|
-
* @example
|
|
214
|
-
* ```ts
|
|
215
|
-
* const result = await withProgress(
|
|
216
|
-
* { label: "Building...", total: 4 },
|
|
217
|
-
* async (progress) => {
|
|
218
|
-
* progress.setLabel("Step 1");
|
|
219
|
-
* await step1();
|
|
220
|
-
* progress.tick();
|
|
221
|
-
*
|
|
222
|
-
* progress.setLabel("Step 2");
|
|
223
|
-
* await step2();
|
|
224
|
-
* progress.tick();
|
|
225
|
-
*
|
|
226
|
-
* return { success: true };
|
|
227
|
-
* }
|
|
228
|
-
* );
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
export async function withProgress<T>(
|
|
232
|
-
options: ProgressOptions,
|
|
233
|
-
work: (progress: ProgressReporter) => Promise<T>
|
|
234
|
-
): Promise<T> {
|
|
235
|
-
const progress = createCliProgress(options);
|
|
236
|
-
|
|
237
|
-
try {
|
|
238
|
-
const result = await work(progress);
|
|
239
|
-
progress.done();
|
|
240
|
-
return result;
|
|
241
|
-
} catch (error) {
|
|
242
|
-
progress.fail(
|
|
243
|
-
error instanceof Error ? error.message : String(error)
|
|
244
|
-
);
|
|
245
|
-
throw error;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* 단순 스피너 (진행률 없음)
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* ```ts
|
|
254
|
-
* const stop = startSpinner("Loading...");
|
|
255
|
-
* await loadData();
|
|
256
|
-
* stop("Loaded!");
|
|
257
|
-
* ```
|
|
258
|
-
*/
|
|
259
|
-
export function startSpinner(
|
|
260
|
-
label: string,
|
|
261
|
-
stream: NodeJS.WriteStream = process.stderr
|
|
262
|
-
): (successMessage?: string) => void {
|
|
263
|
-
const isTty = stream.isTTY;
|
|
264
|
-
|
|
265
|
-
if (!isTty) {
|
|
266
|
-
stream.write(`${label}\n`);
|
|
267
|
-
return (msg) => {
|
|
268
|
-
if (msg) stream.write(`${msg}\n`);
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const spinner = createSpinner(stream);
|
|
273
|
-
spinner.start(label);
|
|
274
|
-
|
|
275
|
-
return (successMessage?: string) => {
|
|
276
|
-
if (successMessage) {
|
|
277
|
-
spinner.succeed(successMessage);
|
|
278
|
-
} else {
|
|
279
|
-
spinner.stop();
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* 다중 단계 프로그레스
|
|
286
|
-
*
|
|
287
|
-
* @example
|
|
288
|
-
* ```ts
|
|
289
|
-
* await runSteps([
|
|
290
|
-
* { label: "Installing dependencies", fn: installDeps },
|
|
291
|
-
* { label: "Building", fn: build },
|
|
292
|
-
* { label: "Testing", fn: test },
|
|
293
|
-
* ]);
|
|
294
|
-
* ```
|
|
295
|
-
*/
|
|
296
|
-
export async function runSteps<T>(
|
|
297
|
-
steps: Array<{
|
|
298
|
-
label: string;
|
|
299
|
-
fn: () => T | Promise<T>;
|
|
300
|
-
}>,
|
|
301
|
-
options: Omit<ProgressOptions, "label" | "total"> = {}
|
|
302
|
-
): Promise<T[]> {
|
|
303
|
-
const results: T[] = [];
|
|
304
|
-
const total = steps.length;
|
|
305
|
-
let current = 0;
|
|
306
|
-
|
|
307
|
-
const progress = createCliProgress({
|
|
308
|
-
...options,
|
|
309
|
-
label: steps[0]?.label ?? "Processing...",
|
|
310
|
-
total,
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
try {
|
|
314
|
-
for (const step of steps) {
|
|
315
|
-
progress.setLabel(step.label);
|
|
316
|
-
const result = await step.fn();
|
|
317
|
-
results.push(result);
|
|
318
|
-
current++;
|
|
319
|
-
progress.tick();
|
|
320
|
-
}
|
|
321
|
-
progress.done();
|
|
322
|
-
return results;
|
|
323
|
-
} catch (error) {
|
|
324
|
-
progress.fail();
|
|
325
|
-
throw error;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* DNA-012: Multi-fallback Progress
|
|
3
|
+
*
|
|
4
|
+
* 다단계 폴백 프로그레스 시스템
|
|
5
|
+
* - TTY: 스피너 (ora) → 라인 → 로그
|
|
6
|
+
* - Non-TTY: 로그만
|
|
7
|
+
* - withProgress() 패턴으로 자동 정리
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { theme } from "./theme.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 프로그레스 옵션
|
|
14
|
+
*/
|
|
15
|
+
export interface ProgressOptions {
|
|
16
|
+
/** 레이블 */
|
|
17
|
+
label: string;
|
|
18
|
+
/** 전체 단계 수 (기본: 100) */
|
|
19
|
+
total?: number;
|
|
20
|
+
/** 출력 스트림 (기본: stderr) */
|
|
21
|
+
stream?: NodeJS.WriteStream;
|
|
22
|
+
/** 폴백 모드 */
|
|
23
|
+
fallback?: "spinner" | "line" | "log" | "none";
|
|
24
|
+
/** 성공 메시지 */
|
|
25
|
+
successMessage?: string;
|
|
26
|
+
/** 실패 메시지 */
|
|
27
|
+
failMessage?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 프로그레스 리포터
|
|
32
|
+
*/
|
|
33
|
+
export interface ProgressReporter {
|
|
34
|
+
/** 레이블 변경 */
|
|
35
|
+
setLabel: (label: string) => void;
|
|
36
|
+
/** 퍼센트 설정 (0-100) */
|
|
37
|
+
setPercent: (percent: number) => void;
|
|
38
|
+
/** 진행 (delta 만큼 증가) */
|
|
39
|
+
tick: (delta?: number) => void;
|
|
40
|
+
/** 성공 완료 */
|
|
41
|
+
done: (message?: string) => void;
|
|
42
|
+
/** 실패 완료 */
|
|
43
|
+
fail: (message?: string) => void;
|
|
44
|
+
/** 현재 퍼센트 */
|
|
45
|
+
getPercent: () => number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 스피너 문자
|
|
50
|
+
*/
|
|
51
|
+
const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 간단한 스피너 구현 (ora 대체)
|
|
55
|
+
*/
|
|
56
|
+
function createSpinner(stream: NodeJS.WriteStream) {
|
|
57
|
+
let frameIndex = 0;
|
|
58
|
+
let intervalId: ReturnType<typeof setInterval> | null = null;
|
|
59
|
+
let text = "";
|
|
60
|
+
let isRunning = false;
|
|
61
|
+
|
|
62
|
+
const render = () => {
|
|
63
|
+
if (!isRunning) return;
|
|
64
|
+
const frame = SPINNER_FRAMES[frameIndex % SPINNER_FRAMES.length];
|
|
65
|
+
stream.write(`\r${theme.accent(frame)} ${text}`);
|
|
66
|
+
frameIndex++;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
start: (initialText: string) => {
|
|
71
|
+
text = initialText;
|
|
72
|
+
isRunning = true;
|
|
73
|
+
render();
|
|
74
|
+
intervalId = setInterval(render, 80);
|
|
75
|
+
},
|
|
76
|
+
setText: (newText: string) => {
|
|
77
|
+
text = newText;
|
|
78
|
+
},
|
|
79
|
+
succeed: (successText: string) => {
|
|
80
|
+
isRunning = false;
|
|
81
|
+
if (intervalId) clearInterval(intervalId);
|
|
82
|
+
stream.write(`\r${theme.success("✓")} ${successText}\n`);
|
|
83
|
+
},
|
|
84
|
+
fail: (failText: string) => {
|
|
85
|
+
isRunning = false;
|
|
86
|
+
if (intervalId) clearInterval(intervalId);
|
|
87
|
+
stream.write(`\r${theme.error("✗")} ${failText}\n`);
|
|
88
|
+
},
|
|
89
|
+
stop: () => {
|
|
90
|
+
isRunning = false;
|
|
91
|
+
if (intervalId) clearInterval(intervalId);
|
|
92
|
+
stream.write("\r" + " ".repeat(text.length + 5) + "\r");
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 프로그레스 바 렌더링
|
|
99
|
+
*/
|
|
100
|
+
function renderProgressBar(percent: number, width: number = 20): string {
|
|
101
|
+
const filled = Math.round((percent / 100) * width);
|
|
102
|
+
const empty = width - filled;
|
|
103
|
+
const bar = "█".repeat(filled) + "░".repeat(empty);
|
|
104
|
+
return `[${bar}]`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* CLI 프로그레스 생성
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* const progress = createCliProgress({ label: "Building...", total: 4 });
|
|
113
|
+
*
|
|
114
|
+
* progress.setLabel("Scanning routes...");
|
|
115
|
+
* await scanRoutes();
|
|
116
|
+
* progress.tick();
|
|
117
|
+
*
|
|
118
|
+
* progress.setLabel("Bundling...");
|
|
119
|
+
* await bundle();
|
|
120
|
+
* progress.tick();
|
|
121
|
+
*
|
|
122
|
+
* progress.done("Build complete!");
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export function createCliProgress(options: ProgressOptions): ProgressReporter {
|
|
126
|
+
const {
|
|
127
|
+
label: initialLabel,
|
|
128
|
+
total = 100,
|
|
129
|
+
stream = process.stderr,
|
|
130
|
+
fallback = "spinner",
|
|
131
|
+
successMessage,
|
|
132
|
+
failMessage,
|
|
133
|
+
} = options;
|
|
134
|
+
|
|
135
|
+
const isTty = stream.isTTY;
|
|
136
|
+
|
|
137
|
+
let label = initialLabel;
|
|
138
|
+
let completed = 0;
|
|
139
|
+
|
|
140
|
+
// TTY: 스피너 사용 (stdout이 pipe여도 stderr TTY면 동작)
|
|
141
|
+
const spinner = isTty && fallback === "spinner" ? createSpinner(stream) : null;
|
|
142
|
+
|
|
143
|
+
if (spinner) {
|
|
144
|
+
spinner.start(label);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const getPercent = () => Math.round((completed / total) * 100);
|
|
148
|
+
|
|
149
|
+
const render = () => {
|
|
150
|
+
const percent = getPercent();
|
|
151
|
+
const text =
|
|
152
|
+
total > 1 ? `${label} ${renderProgressBar(percent)} ${percent}%` : label;
|
|
153
|
+
|
|
154
|
+
if (spinner) {
|
|
155
|
+
spinner.setText(text);
|
|
156
|
+
} else if (isTty && fallback === "line") {
|
|
157
|
+
stream.write(`\r${text}`);
|
|
158
|
+
}
|
|
159
|
+
// "log" 모드는 상태 변경 시마다 로그하지 않음
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
setLabel: (next: string) => {
|
|
164
|
+
label = next;
|
|
165
|
+
render();
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
setPercent: (percent: number) => {
|
|
169
|
+
completed = (Math.max(0, Math.min(100, percent)) / 100) * total;
|
|
170
|
+
render();
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
tick: (delta = 1) => {
|
|
174
|
+
completed = Math.min(total, completed + delta);
|
|
175
|
+
render();
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
getPercent,
|
|
179
|
+
|
|
180
|
+
done: (message?: string) => {
|
|
181
|
+
const finalMessage =
|
|
182
|
+
message ?? successMessage ?? `${initialLabel} completed`;
|
|
183
|
+
|
|
184
|
+
if (spinner) {
|
|
185
|
+
spinner.succeed(finalMessage);
|
|
186
|
+
} else if (isTty) {
|
|
187
|
+
stream.write(`\r${theme.success("✓")} ${finalMessage}\n`);
|
|
188
|
+
} else if (fallback !== "none") {
|
|
189
|
+
stream.write(`[OK] ${finalMessage}\n`);
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
fail: (message?: string) => {
|
|
194
|
+
const finalMessage =
|
|
195
|
+
message ?? failMessage ?? `${initialLabel} failed`;
|
|
196
|
+
|
|
197
|
+
if (spinner) {
|
|
198
|
+
spinner.fail(finalMessage);
|
|
199
|
+
} else if (isTty) {
|
|
200
|
+
stream.write(`\r${theme.error("✗")} ${finalMessage}\n`);
|
|
201
|
+
} else if (fallback !== "none") {
|
|
202
|
+
stream.write(`[FAIL] ${finalMessage}\n`);
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 프로그레스 컨텍스트 패턴
|
|
210
|
+
*
|
|
211
|
+
* 작업 완료 후 자동으로 프로그레스 정리
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```ts
|
|
215
|
+
* const result = await withProgress(
|
|
216
|
+
* { label: "Building...", total: 4 },
|
|
217
|
+
* async (progress) => {
|
|
218
|
+
* progress.setLabel("Step 1");
|
|
219
|
+
* await step1();
|
|
220
|
+
* progress.tick();
|
|
221
|
+
*
|
|
222
|
+
* progress.setLabel("Step 2");
|
|
223
|
+
* await step2();
|
|
224
|
+
* progress.tick();
|
|
225
|
+
*
|
|
226
|
+
* return { success: true };
|
|
227
|
+
* }
|
|
228
|
+
* );
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
export async function withProgress<T>(
|
|
232
|
+
options: ProgressOptions,
|
|
233
|
+
work: (progress: ProgressReporter) => Promise<T>
|
|
234
|
+
): Promise<T> {
|
|
235
|
+
const progress = createCliProgress(options);
|
|
236
|
+
|
|
237
|
+
try {
|
|
238
|
+
const result = await work(progress);
|
|
239
|
+
progress.done();
|
|
240
|
+
return result;
|
|
241
|
+
} catch (error) {
|
|
242
|
+
progress.fail(
|
|
243
|
+
error instanceof Error ? error.message : String(error)
|
|
244
|
+
);
|
|
245
|
+
throw error;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* 단순 스피너 (진행률 없음)
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* const stop = startSpinner("Loading...");
|
|
255
|
+
* await loadData();
|
|
256
|
+
* stop("Loaded!");
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
export function startSpinner(
|
|
260
|
+
label: string,
|
|
261
|
+
stream: NodeJS.WriteStream = process.stderr
|
|
262
|
+
): (successMessage?: string) => void {
|
|
263
|
+
const isTty = stream.isTTY;
|
|
264
|
+
|
|
265
|
+
if (!isTty) {
|
|
266
|
+
stream.write(`${label}\n`);
|
|
267
|
+
return (msg) => {
|
|
268
|
+
if (msg) stream.write(`${msg}\n`);
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const spinner = createSpinner(stream);
|
|
273
|
+
spinner.start(label);
|
|
274
|
+
|
|
275
|
+
return (successMessage?: string) => {
|
|
276
|
+
if (successMessage) {
|
|
277
|
+
spinner.succeed(successMessage);
|
|
278
|
+
} else {
|
|
279
|
+
spinner.stop();
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 다중 단계 프로그레스
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ```ts
|
|
289
|
+
* await runSteps([
|
|
290
|
+
* { label: "Installing dependencies", fn: installDeps },
|
|
291
|
+
* { label: "Building", fn: build },
|
|
292
|
+
* { label: "Testing", fn: test },
|
|
293
|
+
* ]);
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
export async function runSteps<T>(
|
|
297
|
+
steps: Array<{
|
|
298
|
+
label: string;
|
|
299
|
+
fn: () => T | Promise<T>;
|
|
300
|
+
}>,
|
|
301
|
+
options: Omit<ProgressOptions, "label" | "total"> = {}
|
|
302
|
+
): Promise<T[]> {
|
|
303
|
+
const results: T[] = [];
|
|
304
|
+
const total = steps.length;
|
|
305
|
+
let current = 0;
|
|
306
|
+
|
|
307
|
+
const progress = createCliProgress({
|
|
308
|
+
...options,
|
|
309
|
+
label: steps[0]?.label ?? "Processing...",
|
|
310
|
+
total,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
try {
|
|
314
|
+
for (const step of steps) {
|
|
315
|
+
progress.setLabel(step.label);
|
|
316
|
+
const result = await step.fn();
|
|
317
|
+
results.push(result);
|
|
318
|
+
current++;
|
|
319
|
+
progress.tick();
|
|
320
|
+
}
|
|
321
|
+
progress.done();
|
|
322
|
+
return results;
|
|
323
|
+
} catch (error) {
|
|
324
|
+
progress.fail();
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
}
|