cdd-cli 3.1.7 → 3.1.9
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/.eslintrc.json +1 -1
- package/.github/workflows/ci.yml +3 -0
- package/.github/workflows/docs.yml +8 -1
- package/CHANGELOG.md +23 -0
- package/CONTRIBUTING.md +28 -15
- package/README.md +56 -88
- package/babel.config.cjs +18 -1
- package/dist/components/ContainerRow.js +5 -78
- package/dist/components/LogViewer.js +4 -9
- package/dist/helpers/containerOptionsBuilder.js +58 -0
- package/dist/hooks/creation/useContainerCreation.js +16 -1
- package/dist/hooks/debug/useDebugLogs.js +46 -0
- package/dist/hooks/navigation/useContainerSelection.js +36 -0
- package/dist/hooks/useContainerCommandRouter.js +151 -0
- package/dist/{helpers/actionHelpers.js → hooks/useContainerStats.js} +76 -78
- package/dist/hooks/useControls.js +83 -342
- package/dist/hooks/useEraseConfirmation.js +47 -0
- package/dist/hooks/useExitHandler.js +68 -0
- package/jest.config.cjs +1 -1
- package/package.json +2 -3
- package/src/components/ContainerRow.jsx +3 -38
- package/src/components/LogViewer.jsx +4 -10
- package/src/helpers/containerOptionsBuilder.js +42 -0
- package/src/hooks/creation/useContainerCreation.js +15 -0
- package/src/hooks/debug/useDebugLogs.js +29 -0
- package/src/hooks/navigation/useContainerSelection.js +25 -0
- package/src/hooks/useContainerCommandRouter.js +110 -0
- package/src/hooks/useContainerStats.js +44 -0
- package/src/hooks/useControls.js +68 -272
- package/src/hooks/useEraseConfirmation.js +39 -0
- package/src/hooks/useExitHandler.js +66 -0
- package/test/useContainerCreation.dom.test.js +3 -0
- package/.babelrc +0 -6
- package/FIXES_APPLIED.md +0 -419
- package/SECURITY.md +0 -5
- package/dist/hooks/useLogsStream.js +0 -41
- package/docs/_config.yml +0 -13
- package/docs/components_ContainerCreationPrompt.jsx.html +0 -114
- package/docs/components_ContainerRow.jsx.html +0 -151
- package/docs/components_ContainerSection.jsx.html +0 -75
- package/docs/components_Footer.jsx.html +0 -66
- package/docs/components_Header.jsx.html +0 -84
- package/docs/components_LogViewer.jsx.html +0 -91
- package/docs/components_MessageFeedback.jsx.html +0 -76
- package/docs/components_PromptField.jsx.html +0 -99
- package/docs/components_StatsBar.jsx.html +0 -105
- package/docs/components_UsageMenu.jsx.html +0 -73
- package/docs/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/docs/fonts/OpenSans-Bold-webfont.svg +0 -1830
- package/docs/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/docs/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/docs/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
- package/docs/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/docs/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/docs/fonts/OpenSans-Italic-webfont.svg +0 -1830
- package/docs/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/docs/fonts/OpenSans-Light-webfont.eot +0 -0
- package/docs/fonts/OpenSans-Light-webfont.svg +0 -1831
- package/docs/fonts/OpenSans-Light-webfont.woff +0 -0
- package/docs/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/docs/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
- package/docs/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/docs/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/docs/fonts/OpenSans-Regular-webfont.svg +0 -1831
- package/docs/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/docs/global.html +0 -6145
- package/docs/helpers_actionHelpers.js.html +0 -94
- package/docs/helpers_constants.js.html +0 -81
- package/docs/helpers_dockerService_dockerService.js.html +0 -79
- package/docs/helpers_dockerService_serviceComponents_containerActions.js.html +0 -153
- package/docs/helpers_dockerService_serviceComponents_containerList.js.html +0 -81
- package/docs/helpers_dockerService_serviceComponents_containerLogs.js.html +0 -85
- package/docs/helpers_dockerService_serviceComponents_containerStats.js.html +0 -107
- package/docs/helpers_dockerService_serviceComponents_imageUtils.js.html +0 -82
- package/docs/helpers_exitWithMessage.js.html +0 -77
- package/docs/helpers_safeCall.js.html +0 -68
- package/docs/helpers_validationHelpers.js.html +0 -96
- package/docs/hooks_creation_useContainerActions.js.html +0 -120
- package/docs/hooks_creation_useContainerCreation.js.html +0 -152
- package/docs/hooks_creation_useLogsViewer.js.html +0 -102
- package/docs/hooks_useControls.js.html +0 -298
- package/docs/hooks_useLogsStream.js.html +0 -82
- package/docs/index.html +0 -65
- package/docs/index.js.html +0 -65
- package/docs/module-index.html +0 -171
- package/docs/scripts/linenumber.js +0 -25
- package/docs/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/docs/scripts/prettify/lang-css.js +0 -2
- package/docs/scripts/prettify/prettify.js +0 -28
- package/docs/styles/jsdoc-default.css +0 -358
- package/docs/styles/prettify-jsdoc.css +0 -111
- package/docs/styles/prettify-tomorrow.css +0 -132
- package/esbuild.config.cjs +0 -11
- package/src/helpers/actionHelpers.js +0 -52
- package/src/hooks/useLogsStream.js +0 -31
- package/test/actionHelpers.test.js +0 -106
package/src/hooks/useControls.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
+
import { useInput } from "ink";
|
|
2
3
|
import { useContainerActions } from "./creation/useContainerActions";
|
|
3
4
|
import { useContainerCreation } from "./creation/useContainerCreation";
|
|
4
5
|
import { useLogsViewer } from "./creation/useLogsViewer";
|
|
5
|
-
import {
|
|
6
|
+
import { useContainerSelection } from "./navigation/useContainerSelection";
|
|
7
|
+
import { useDebugLogs } from "./debug/useDebugLogs";
|
|
8
|
+
import { useEraseConfirmation } from "./useEraseConfirmation";
|
|
9
|
+
import { useExitHandler } from "./useExitHandler";
|
|
10
|
+
import { useContainerCommandRouter } from "./useContainerCommandRouter";
|
|
6
11
|
import { getLogsStream } from "../helpers/dockerService/serviceComponents/containerLogs.js";
|
|
7
|
-
import { EXIT_DELAY } from "../helpers/constants.js";
|
|
8
12
|
import { createContainer as svcCreateContainer } from "../helpers/dockerService/serviceComponents/containerActions.js";
|
|
9
|
-
import {
|
|
13
|
+
import { buildContainerOptions } from "../helpers/containerOptionsBuilder.js";
|
|
10
14
|
|
|
11
15
|
// Principal hook to manage user inputs and control the app state
|
|
12
16
|
/**
|
|
@@ -15,63 +19,18 @@ import { logger } from "../helpers/logger.js";
|
|
|
15
19
|
*
|
|
16
20
|
* @param {Array<Object>} containers - Current list of Docker containers
|
|
17
21
|
* @returns {Object} controls - API for the App component
|
|
18
|
-
* @property {number} selected - Index of the currently selected container
|
|
19
|
-
* @property {function} setSelected - Setter for selected index
|
|
20
|
-
* @property {string} message - Current feedback message (creation or actions)
|
|
21
|
-
* @property {string} messageColor - Color to show for the feedback message
|
|
22
|
-
* @property {boolean} showLogs - Whether the logs viewer is active
|
|
23
|
-
* @property {Array<string>} logs - Array of log lines currently collected
|
|
24
|
-
* @property {function} exitLogs - Helper to close the logs viewer
|
|
25
|
-
* @property {boolean} creatingContainer - Whether the create-container prompt is open
|
|
26
|
-
* @property {number} creationStep - Current step in the creation flow
|
|
27
|
-
* @property {string} imageNameInput - Current value of the image name field
|
|
28
|
-
* @property {string} containerNameInput - Current value of the container name field
|
|
29
|
-
* @property {string} portInput - Current value of the port input field
|
|
30
|
-
* @property {string} envInput - Current value of the env input field
|
|
31
|
-
* @property {Object} creation - The creation hook API (setters and helpers)
|
|
32
|
-
* @property {Object} actions - The actions hook API (helpers to start/stop/remove)
|
|
33
|
-
* @property {Object} logsViewer - The logs viewer hook API
|
|
34
|
-
* @property {boolean} showDebugLogs - Whether the debug log panel is visible
|
|
35
|
-
* @property {Array<string>} debugLogs - Buffered log lines captured from the shared logger
|
|
36
|
-
* @property {boolean} confirmErase - Whether the erase confirmation dialog is active
|
|
37
22
|
*/
|
|
38
23
|
export function useControls(containers = []) {
|
|
39
|
-
const [selected, setSelected] = React.useState(0);
|
|
40
24
|
const [creatingContainer, setCreatingContainer] = React.useState(false);
|
|
41
|
-
const [confirmErase, setConfirmErase] = React.useState(false);
|
|
42
|
-
const [showDebugLogs, setShowDebugLogs] = React.useState(false);
|
|
43
|
-
const [debugLogs, setDebugLogs] = React.useState([]);
|
|
44
|
-
const total = containers.length;
|
|
45
|
-
const { exit } = useApp();
|
|
46
25
|
|
|
47
|
-
// Modular hooks
|
|
26
|
+
// — Modular hooks —
|
|
48
27
|
const actions = useContainerActions({ containers });
|
|
28
|
+
|
|
49
29
|
const creation = useContainerCreation({
|
|
50
30
|
onCreate: async ({ imageName, containerName, portInput, envInput }) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const ExposedPorts = {};
|
|
55
|
-
const PortBindings = {};
|
|
56
|
-
ports.forEach(pair => {
|
|
57
|
-
const [host, cont] = pair.split(":");
|
|
58
|
-
if (!host || !cont) return;
|
|
59
|
-
const key = `${cont}/tcp`;
|
|
60
|
-
ExposedPorts[key] = {};
|
|
61
|
-
PortBindings[key] = PortBindings[key] || [];
|
|
62
|
-
PortBindings[key].push({ HostPort: `${host}` });
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
const options = {
|
|
66
|
-
Tty: true,
|
|
67
|
-
};
|
|
68
|
-
if (Object.keys(ExposedPorts).length) options.ExposedPorts = ExposedPorts;
|
|
69
|
-
if (Object.keys(PortBindings).length) options.HostConfig = { PortBindings };
|
|
70
|
-
if (env.length) options.Env = env;
|
|
71
|
-
if (containerName) options.name = containerName;
|
|
72
|
-
|
|
73
|
-
actions.setMessage(`Creating container ${imageName}...`);
|
|
74
|
-
actions.setMessageColor("yellow");
|
|
31
|
+
const options = buildContainerOptions({ imageName, containerName, portInput, envInput });
|
|
32
|
+
actions.setMessage(`Creating container ${imageName}...`);
|
|
33
|
+
actions.setMessageColor("yellow");
|
|
75
34
|
try {
|
|
76
35
|
const id = await svcCreateContainer(imageName, options);
|
|
77
36
|
actions.setMessage(`Created container ${id}`);
|
|
@@ -84,12 +43,27 @@ export function useControls(containers = []) {
|
|
|
84
43
|
}
|
|
85
44
|
},
|
|
86
45
|
onCancel: () => setCreatingContainer(false),
|
|
87
|
-
dbImages: ["mysql", "mariadb", "postgres", "mongo", "mssql", "redis"]
|
|
46
|
+
dbImages: ["mysql", "mariadb", "postgres", "mongo", "mssql", "redis"],
|
|
88
47
|
});
|
|
48
|
+
|
|
89
49
|
const logsViewer = useLogsViewer();
|
|
50
|
+
const selection = useContainerSelection(containers.length);
|
|
51
|
+
const debugLogs = useDebugLogs();
|
|
90
52
|
|
|
91
|
-
|
|
92
|
-
|
|
53
|
+
const eraseConfirmation = useEraseConfirmation({
|
|
54
|
+
onConfirm: () => {
|
|
55
|
+
actions.handleAction({
|
|
56
|
+
actionFn: async (id) => await actions.removeContainer(id),
|
|
57
|
+
actionLabel: "Erasing",
|
|
58
|
+
selected: selection.selected,
|
|
59
|
+
});
|
|
60
|
+
actions.setMessageColor("yellow");
|
|
61
|
+
},
|
|
62
|
+
onCancel: () => {
|
|
63
|
+
actions.setMessage("");
|
|
64
|
+
actions.setMessageColor("");
|
|
65
|
+
},
|
|
66
|
+
});
|
|
93
67
|
|
|
94
68
|
/**
|
|
95
69
|
* Pipe container logs into the viewer while applying a hard limit.
|
|
@@ -107,40 +81,30 @@ export function useControls(containers = []) {
|
|
|
107
81
|
);
|
|
108
82
|
}, [logsViewer]);
|
|
109
83
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
setConfirmErase(false);
|
|
84
|
+
const commandRouter = useContainerCommandRouter({
|
|
85
|
+
actions,
|
|
86
|
+
containers,
|
|
87
|
+
selected: selection.selected,
|
|
88
|
+
creation,
|
|
89
|
+
logsViewer,
|
|
90
|
+
startLogsStream,
|
|
91
|
+
onStartErase: () => {
|
|
92
|
+
eraseConfirmation.startErase();
|
|
93
|
+
actions.setMessage("Are you sure you want to erase this container? (y/n)");
|
|
121
94
|
actions.setMessageColor("yellow");
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
setConfirmErase(false);
|
|
127
|
-
actions.setMessage("");
|
|
128
|
-
actions.setMessageColor("");
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
actions.setMessage("Are you sure you want to erase this container? (y/n)");
|
|
133
|
-
actions.setMessageColor("yellow");
|
|
134
|
-
}, [actions, selected]);
|
|
95
|
+
},
|
|
96
|
+
onToggleDebug: () => debugLogs.setShowDebugLogs((prev) => !prev),
|
|
97
|
+
onStartCreate: () => setCreatingContainer(true),
|
|
98
|
+
});
|
|
135
99
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (input === "q" || key.escape) {
|
|
100
|
+
const exitHandler = useExitHandler({
|
|
101
|
+
onBeforeExit: () => {
|
|
102
|
+
actions.setMessage("Exiting...");
|
|
103
|
+
actions.setMessageColor("yellow");
|
|
141
104
|
logsViewer.closeLogs();
|
|
142
|
-
|
|
143
|
-
|
|
105
|
+
debugLogs.setShowDebugLogs(false);
|
|
106
|
+
},
|
|
107
|
+
});
|
|
144
108
|
|
|
145
109
|
/**
|
|
146
110
|
* Route keystrokes to the container creation wizard.
|
|
@@ -178,164 +142,17 @@ export function useControls(containers = []) {
|
|
|
178
142
|
}
|
|
179
143
|
}, [creation, setCreatingContainer]);
|
|
180
144
|
|
|
181
|
-
|
|
182
|
-
* Navigate the container list using arrow keys.
|
|
183
|
-
* @returns {boolean} True when the input was consumed.
|
|
184
|
-
*/
|
|
185
|
-
const handleNavigation = React.useCallback((input, key) => {
|
|
186
|
-
if (total === 0) {
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (key.upArrow) {
|
|
191
|
-
setSelected((i) => (i === 0 ? total - 1 : i - 1));
|
|
192
|
-
return true;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (key.downArrow) {
|
|
196
|
-
setSelected((i) => (i === total - 1 ? 0 : i + 1));
|
|
197
|
-
return true;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return false;
|
|
201
|
-
}, [total]);
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Handle the quit command.
|
|
205
|
-
* @returns {boolean} True when the input was consumed.
|
|
206
|
-
*/
|
|
207
|
-
const handleExitCommand = React.useCallback((input) => {
|
|
208
|
-
if (input !== "q") {
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
actions.setMessage("Exiting...");
|
|
213
|
-
actions.setMessageColor("yellow");
|
|
214
|
-
logsViewer.closeLogs();
|
|
215
|
-
setShowDebugLogs(false);
|
|
216
|
-
|
|
217
|
-
const clearTerminal = () => {
|
|
218
|
-
try {
|
|
219
|
-
if (process.stdout && process.stdout.isTTY) {
|
|
220
|
-
process.stdout.write("\u001Bc");
|
|
221
|
-
} else {
|
|
222
|
-
console.clear();
|
|
223
|
-
}
|
|
224
|
-
} catch (err) {
|
|
225
|
-
// Ignore clear errors to avoid blocking exit.
|
|
226
|
-
}
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
setTimeout(() => {
|
|
230
|
-
clearTerminal();
|
|
231
|
-
exit();
|
|
232
|
-
if (process.env.NODE_ENV !== "test") {
|
|
233
|
-
setTimeout(() => process.exit(0), 50);
|
|
234
|
-
}
|
|
235
|
-
}, EXIT_DELAY);
|
|
236
|
-
return true;
|
|
237
|
-
}, [actions, exit, logsViewer]);
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Execute Docker-related commands and prompt transitions.
|
|
241
|
-
* @returns {boolean} True when the input was consumed.
|
|
242
|
-
*/
|
|
243
|
-
const handleDockerCommands = React.useCallback((input) => {
|
|
244
|
-
const container = containers[selected];
|
|
245
|
-
|
|
246
|
-
if (input === "i") {
|
|
247
|
-
actions.handleAction({
|
|
248
|
-
actionFn: async (id) => await actions.startContainer(id),
|
|
249
|
-
actionLabel: "Starting",
|
|
250
|
-
selected,
|
|
251
|
-
stateCheck: (c) => (c.state === "running" || c.status === "running") && "Container is already running."
|
|
252
|
-
});
|
|
253
|
-
return true;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
if (input === "p") {
|
|
257
|
-
actions.handleAction({
|
|
258
|
-
actionFn: async (id) => await actions.stopContainer(id),
|
|
259
|
-
actionLabel: "Stopping",
|
|
260
|
-
selected,
|
|
261
|
-
stateCheck: (c) => (c.state === "exited" || c.status === "exited" || c.state === "stopped" || c.status === "stopped") && "Container is already stopped."
|
|
262
|
-
});
|
|
263
|
-
return true;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (input === "r") {
|
|
267
|
-
actions.handleAction({
|
|
268
|
-
actionFn: async (id) => await actions.restartContainer(id),
|
|
269
|
-
actionLabel: "Restarting",
|
|
270
|
-
selected,
|
|
271
|
-
});
|
|
272
|
-
return true;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
if (input === "e") {
|
|
276
|
-
if (!container) {
|
|
277
|
-
return true;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
setConfirmErase(true);
|
|
281
|
-
actions.setMessage("Are you sure you want to erase this container? (y/n)");
|
|
282
|
-
actions.setMessageColor("yellow");
|
|
283
|
-
return true;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
if (input === "l") {
|
|
287
|
-
if (!container) {
|
|
288
|
-
return true;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
logsViewer.openLogs();
|
|
292
|
-
startLogsStream(container.id);
|
|
293
|
-
return true;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (input === "c") {
|
|
297
|
-
setCreatingContainer(true);
|
|
298
|
-
creation.setStep(0);
|
|
299
|
-
creation.setImageName("");
|
|
300
|
-
creation.setContainerName("");
|
|
301
|
-
creation.setPortInput("");
|
|
302
|
-
creation.setEnvInput("");
|
|
303
|
-
creation.setMessage("Insert the name of the image to create: ");
|
|
304
|
-
creation.setMessageColor("yellow");
|
|
305
|
-
return true;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
if (input === "d") {
|
|
309
|
-
setShowDebugLogs((prev) => !prev);
|
|
310
|
-
return true;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
return false;
|
|
314
|
-
}, [actions, containers, creation, logsViewer, selected, setConfirmErase, setCreatingContainer, startLogsStream, setShowDebugLogs]);
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Default handler executed when no special mode is active.
|
|
318
|
-
*/
|
|
319
|
-
const processGlobalInput = React.useCallback((input, key) => {
|
|
320
|
-
if (handleNavigation(input, key)) {
|
|
321
|
-
return;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
if (handleExitCommand(input)) {
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
handleDockerCommands(input);
|
|
329
|
-
}, [handleDockerCommands, handleExitCommand, handleNavigation]);
|
|
330
|
-
|
|
145
|
+
// Single keyboard entry point
|
|
331
146
|
useInput((input, key) => {
|
|
332
|
-
if (confirmErase) {
|
|
333
|
-
processEraseConfirmation(input, key);
|
|
147
|
+
if (eraseConfirmation.confirmErase) {
|
|
148
|
+
eraseConfirmation.processEraseConfirmation(input, key);
|
|
334
149
|
return;
|
|
335
150
|
}
|
|
336
151
|
|
|
337
152
|
if (logsViewer.showLogs) {
|
|
338
|
-
|
|
153
|
+
if (input === "q" || key.escape) {
|
|
154
|
+
logsViewer.closeLogs();
|
|
155
|
+
}
|
|
339
156
|
return;
|
|
340
157
|
}
|
|
341
158
|
|
|
@@ -344,46 +161,25 @@ export function useControls(containers = []) {
|
|
|
344
161
|
return;
|
|
345
162
|
}
|
|
346
163
|
|
|
347
|
-
if (showDebugLogs && key.escape) {
|
|
348
|
-
setShowDebugLogs(false);
|
|
164
|
+
if (debugLogs.showDebugLogs && key.escape) {
|
|
165
|
+
debugLogs.setShowDebugLogs(false);
|
|
349
166
|
return;
|
|
350
167
|
}
|
|
351
168
|
|
|
352
|
-
|
|
169
|
+
commandRouter.handleDockerCommands(input);
|
|
170
|
+
exitHandler.handleExitCommand(input);
|
|
171
|
+
selection.handleNavigation(input, key);
|
|
353
172
|
});
|
|
354
173
|
|
|
355
|
-
// Mirror global logger entries so the D shortcut can surface them without leaving the CLI.
|
|
356
|
-
React.useEffect(() => {
|
|
357
|
-
const unsubscribe = logger.subscribe((entry) => {
|
|
358
|
-
setDebugLogs((prev) => {
|
|
359
|
-
const extras = (entry.args || []).map((arg) => {
|
|
360
|
-
if (typeof arg === "string") return arg;
|
|
361
|
-
if (typeof arg === "number" || typeof arg === "boolean") return String(arg);
|
|
362
|
-
try {
|
|
363
|
-
return JSON.stringify(arg);
|
|
364
|
-
} catch (err) {
|
|
365
|
-
return "[unserializable]";
|
|
366
|
-
}
|
|
367
|
-
}).filter(Boolean);
|
|
368
|
-
const line = extras.length ? `${entry.formatted} ${extras.join(" ")}` : entry.formatted;
|
|
369
|
-
const next = [...prev, line];
|
|
370
|
-
return next.slice(-200);
|
|
371
|
-
});
|
|
372
|
-
});
|
|
373
|
-
return unsubscribe;
|
|
374
|
-
}, []);
|
|
375
|
-
|
|
376
174
|
return {
|
|
377
|
-
selected,
|
|
378
|
-
setSelected,
|
|
379
|
-
// Map messaging to creation when creating, otherwise to actions
|
|
175
|
+
selected: selection.selected,
|
|
176
|
+
setSelected: selection.setSelected,
|
|
380
177
|
message: creatingContainer ? creation.message : actions.message,
|
|
381
178
|
messageColor: creatingContainer ? creation.messageColor : actions.messageColor,
|
|
382
179
|
showLogs: logsViewer.showLogs,
|
|
383
180
|
logs: logsViewer.logs,
|
|
384
|
-
exitLogs,
|
|
181
|
+
exitLogs: logsViewer.closeLogs,
|
|
385
182
|
creatingContainer,
|
|
386
|
-
// Expose creation fields in the shape App expects
|
|
387
183
|
creationStep: creation.step,
|
|
388
184
|
imageNameInput: creation.imageName,
|
|
389
185
|
containerNameInput: creation.containerName,
|
|
@@ -392,8 +188,8 @@ export function useControls(containers = []) {
|
|
|
392
188
|
creation,
|
|
393
189
|
actions,
|
|
394
190
|
logsViewer,
|
|
395
|
-
confirmErase,
|
|
396
|
-
showDebugLogs,
|
|
397
|
-
debugLogs,
|
|
191
|
+
confirmErase: eraseConfirmation.confirmErase,
|
|
192
|
+
showDebugLogs: debugLogs.showDebugLogs,
|
|
193
|
+
debugLogs: debugLogs.debugLogs,
|
|
398
194
|
};
|
|
399
195
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useState, useCallback } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hook to manage the erase confirmation dialog flow.
|
|
5
|
+
*
|
|
6
|
+
* Encapsulates the `confirmErase` state and the input handler that resolves it.
|
|
7
|
+
* The caller is responsible for what happens on confirm/cancel via the callbacks.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} params
|
|
10
|
+
* @param {Function} params.onConfirm - Called when the user presses y/Y. Should trigger the erase action.
|
|
11
|
+
* @param {Function} params.onCancel - Called when the user presses n/N or Escape. Should clean up messages.
|
|
12
|
+
* @returns {{ confirmErase: boolean, startErase: Function, processEraseConfirmation: Function }}
|
|
13
|
+
*/
|
|
14
|
+
export function useEraseConfirmation({ onConfirm, onCancel }) {
|
|
15
|
+
const [confirmErase, setConfirmErase] = useState(false);
|
|
16
|
+
|
|
17
|
+
const startErase = useCallback(() => {
|
|
18
|
+
setConfirmErase(true);
|
|
19
|
+
}, []);
|
|
20
|
+
|
|
21
|
+
const processEraseConfirmation = useCallback(
|
|
22
|
+
(input, key) => {
|
|
23
|
+
if (input === "y" || input === "Y") {
|
|
24
|
+
onConfirm();
|
|
25
|
+
setConfirmErase(false);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (input === "n" || input === "N" || key.escape) {
|
|
30
|
+
setConfirmErase(false);
|
|
31
|
+
onCancel();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
[onConfirm, onCancel]
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return { confirmErase, startErase, processEraseConfirmation };
|
|
39
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { useApp } from 'ink';
|
|
3
|
+
import { EXIT_DELAY } from '../helpers/constants.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook that encapsulates the application exit sequence triggered by pressing `q`.
|
|
7
|
+
*
|
|
8
|
+
* Responsibilities:
|
|
9
|
+
* - Detect the `q` keypress
|
|
10
|
+
* - Invoke an optional `onBeforeExit` callback for resource cleanup (e.g. closing logs)
|
|
11
|
+
* - Clear the terminal after a short delay
|
|
12
|
+
* - Call Ink's `exit()` and, outside of test environments, `process.exit(0)`
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} [options]
|
|
15
|
+
* @param {Function} [options.onBeforeExit] - Optional callback invoked synchronously
|
|
16
|
+
* before the exit delay starts. Use it to close logs, reset UI state, etc.
|
|
17
|
+
* @returns {{ handleExitCommand: Function }}
|
|
18
|
+
*/
|
|
19
|
+
export function useExitHandler({ onBeforeExit } = {}) {
|
|
20
|
+
const { exit } = useApp();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Handle the quit command.
|
|
24
|
+
*
|
|
25
|
+
* Mirrors the exact logic of `handleExitCommand` in `useControls.js`:
|
|
26
|
+
* 1. Guard: return false if input is not 'q'
|
|
27
|
+
* 2. Call onBeforeExit (replaces inline logsViewer.closeLogs + setShowDebugLogs(false))
|
|
28
|
+
* 3. After EXIT_DELAY: clear terminal, call exit(), then process.exit(0) outside tests
|
|
29
|
+
*
|
|
30
|
+
* @param {string} input - Raw character input from Ink's useInput
|
|
31
|
+
* @returns {boolean} True when the input was consumed, false otherwise
|
|
32
|
+
*/
|
|
33
|
+
const handleExitCommand = useCallback((input) => {
|
|
34
|
+
if (input !== 'q') {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (typeof onBeforeExit === 'function') {
|
|
39
|
+
onBeforeExit();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const clearTerminal = () => {
|
|
43
|
+
try {
|
|
44
|
+
if (process.stdout && process.stdout.isTTY) {
|
|
45
|
+
process.stdout.write('\u001Bc');
|
|
46
|
+
} else {
|
|
47
|
+
console.clear();
|
|
48
|
+
}
|
|
49
|
+
} catch (err) {
|
|
50
|
+
// Ignore clear errors to avoid blocking exit.
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
setTimeout(() => {
|
|
55
|
+
clearTerminal();
|
|
56
|
+
exit();
|
|
57
|
+
if (process.env.NODE_ENV !== 'test') {
|
|
58
|
+
setTimeout(() => process.exit(0), 50);
|
|
59
|
+
}
|
|
60
|
+
}, EXIT_DELAY);
|
|
61
|
+
|
|
62
|
+
return true;
|
|
63
|
+
}, [exit, onBeforeExit]);
|
|
64
|
+
|
|
65
|
+
return { handleExitCommand };
|
|
66
|
+
}
|