solforge 0.2.0 ā 0.2.2
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/LICENSE +21 -0
- package/docs/API.md +379 -0
- package/docs/CONFIGURATION.md +407 -0
- package/package.json +67 -45
- package/src/api-server-entry.ts +109 -0
- package/src/commands/add-program.ts +337 -0
- package/src/commands/init.ts +122 -0
- package/src/commands/list.ts +136 -0
- package/src/commands/mint.ts +288 -0
- package/src/commands/start.ts +877 -0
- package/src/commands/status.ts +99 -0
- package/src/commands/stop.ts +406 -0
- package/src/config/manager.ts +157 -0
- package/src/gui/public/build/main.css +1 -0
- package/src/gui/public/build/main.js +303 -0
- package/src/gui/public/build/main.js.txt +231 -0
- package/src/index.ts +188 -0
- package/src/services/api-server.ts +485 -0
- package/src/services/port-manager.ts +177 -0
- package/src/services/process-registry.ts +154 -0
- package/src/services/program-cloner.ts +317 -0
- package/src/services/token-cloner.ts +809 -0
- package/src/services/validator.ts +295 -0
- package/src/types/config.ts +110 -0
- package/src/utils/shell.ts +110 -0
- package/src/utils/token-loader.ts +115 -0
- package/.agi/agi.sqlite +0 -0
- package/.claude/settings.local.json +0 -9
- package/.github/workflows/release-binaries.yml +0 -133
- package/.tmp/.787ebcdbf7b8fde8-00000000.hm +0 -0
- package/.tmp/.bffe6efebdf8aedc-00000000.hm +0 -0
- package/AGENTS.md +0 -271
- package/CLAUDE.md +0 -106
- package/PROJECT_STRUCTURE.md +0 -124
- package/SOLANA_KIT_GUIDE.md +0 -251
- package/SOLFORGE.md +0 -119
- package/biome.json +0 -34
- package/bun.lock +0 -743
- package/drizzle/0000_friendly_millenium_guard.sql +0 -53
- package/drizzle/0001_stale_sentinels.sql +0 -2
- package/drizzle/meta/0000_snapshot.json +0 -329
- package/drizzle/meta/0001_snapshot.json +0 -345
- package/drizzle/meta/_journal.json +0 -20
- package/drizzle.config.ts +0 -12
- package/index.ts +0 -21
- package/mint.sh +0 -47
- package/postcss.config.js +0 -6
- package/rpc-server.ts.backup +0 -519
- package/sf.config.json +0 -38
- package/tailwind.config.js +0 -27
- package/test-client.ts +0 -120
- package/tmp/inspect-html.ts +0 -4
- package/tmp/response-test.ts +0 -5
- package/tmp/test-html.ts +0 -5
- package/tmp/test-server.ts +0 -13
- package/tsconfig.json +0 -29
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { runCommand, checkSolanaTools } from "../utils/shell.js";
|
|
3
|
+
import { configManager } from "../config/manager.js";
|
|
4
|
+
import { processRegistry } from "../services/process-registry.js";
|
|
5
|
+
|
|
6
|
+
export async function statusCommand(): Promise<void> {
|
|
7
|
+
console.log(chalk.blue("š Checking system status...\n"));
|
|
8
|
+
|
|
9
|
+
// Check Solana CLI tools
|
|
10
|
+
console.log(chalk.cyan("š§ Solana CLI Tools:"));
|
|
11
|
+
const tools = await checkSolanaTools();
|
|
12
|
+
|
|
13
|
+
console.log(
|
|
14
|
+
` ${tools.solana ? "ā
" : "ā"} solana CLI: ${
|
|
15
|
+
tools.solana ? "Available" : "Not found"
|
|
16
|
+
}`
|
|
17
|
+
);
|
|
18
|
+
console.log(
|
|
19
|
+
` ${tools.splToken ? "ā
" : "ā"} spl-token CLI: ${
|
|
20
|
+
tools.splToken ? "Available" : "Not found"
|
|
21
|
+
}`
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
if (!tools.solana || !tools.splToken) {
|
|
25
|
+
console.log(chalk.yellow("\nš” Install Solana CLI tools:"));
|
|
26
|
+
console.log(
|
|
27
|
+
chalk.gray(
|
|
28
|
+
' sh -c "$(curl -sSfL https://release.solana.com/v1.18.4/install)"'
|
|
29
|
+
)
|
|
30
|
+
);
|
|
31
|
+
console.log();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Check running validators
|
|
35
|
+
console.log(chalk.cyan("\nšļø Validator Status:"));
|
|
36
|
+
|
|
37
|
+
// Clean up dead processes first
|
|
38
|
+
await processRegistry.cleanup();
|
|
39
|
+
|
|
40
|
+
const validators = processRegistry.getRunning();
|
|
41
|
+
|
|
42
|
+
if (validators.length === 0) {
|
|
43
|
+
console.log(` ā No validators running`);
|
|
44
|
+
console.log(` š” Run 'solforge start' to launch a validator`);
|
|
45
|
+
} else {
|
|
46
|
+
console.log(
|
|
47
|
+
` ā
${validators.length} validator${
|
|
48
|
+
validators.length > 1 ? "s" : ""
|
|
49
|
+
} running`
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
for (const validator of validators) {
|
|
53
|
+
const isRunning = await processRegistry.isProcessRunning(validator.pid);
|
|
54
|
+
if (isRunning) {
|
|
55
|
+
console.log(` š¹ ${validator.name} (${validator.id}):`);
|
|
56
|
+
console.log(` š RPC: ${validator.rpcUrl}`);
|
|
57
|
+
console.log(` š° Faucet: ${validator.faucetUrl}`);
|
|
58
|
+
console.log(` š PID: ${validator.pid}`);
|
|
59
|
+
|
|
60
|
+
// Get current slot for this validator
|
|
61
|
+
try {
|
|
62
|
+
const slotResult = await runCommand(
|
|
63
|
+
"solana",
|
|
64
|
+
["slot", "--url", validator.rpcUrl, "--output", "json"],
|
|
65
|
+
{ silent: true, jsonOutput: true }
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (slotResult.success && typeof slotResult.stdout === "object") {
|
|
69
|
+
console.log(` š Current slot: ${slotResult.stdout}`);
|
|
70
|
+
}
|
|
71
|
+
} catch {
|
|
72
|
+
// Ignore slot check errors
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
processRegistry.updateStatus(validator.id, "stopped");
|
|
76
|
+
console.log(
|
|
77
|
+
` ā ļø ${validator.name} (${validator.id}): Process stopped`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
console.log(` š” Run 'solforge list' for detailed validator information`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Check config file
|
|
86
|
+
console.log(chalk.cyan("\nš Configuration:"));
|
|
87
|
+
try {
|
|
88
|
+
const config = configManager.getConfig();
|
|
89
|
+
console.log(` ā
Config loaded: ${configManager.getConfigPath()}`);
|
|
90
|
+
console.log(` š Project: ${config.name}`);
|
|
91
|
+
console.log(` šŖ Tokens: ${config.tokens.length}`);
|
|
92
|
+
console.log(` š¦ Programs: ${config.programs.length}`);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
console.log(` ā No valid configuration found`);
|
|
95
|
+
console.log(` š” Run 'solforge init' to create one`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
console.log();
|
|
99
|
+
}
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import { select } from "@inquirer/prompts";
|
|
3
|
+
import { processRegistry } from "../services/process-registry.js";
|
|
4
|
+
|
|
5
|
+
import type { RunningValidator } from "../services/process-registry.js";
|
|
6
|
+
|
|
7
|
+
export async function stopCommand(
|
|
8
|
+
validatorId?: string,
|
|
9
|
+
options: { all?: boolean; kill?: boolean } = {}
|
|
10
|
+
): Promise<void> {
|
|
11
|
+
console.log(chalk.blue("š Stopping validator(s)...\n"));
|
|
12
|
+
|
|
13
|
+
// Clean up dead processes first
|
|
14
|
+
await processRegistry.cleanup();
|
|
15
|
+
|
|
16
|
+
const validators = processRegistry.getRunning();
|
|
17
|
+
|
|
18
|
+
if (validators.length === 0) {
|
|
19
|
+
console.log(chalk.yellow("ā ļø No running validators found"));
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let validatorsToStop: RunningValidator[] = [];
|
|
24
|
+
|
|
25
|
+
if (options.all) {
|
|
26
|
+
// Stop all validators
|
|
27
|
+
validatorsToStop = validators;
|
|
28
|
+
console.log(
|
|
29
|
+
chalk.cyan(`š Stopping all ${validators.length} validator(s)...`)
|
|
30
|
+
);
|
|
31
|
+
} else if (validatorId) {
|
|
32
|
+
// Stop specific validator
|
|
33
|
+
const validator = processRegistry.getById(validatorId);
|
|
34
|
+
if (!validator) {
|
|
35
|
+
console.error(
|
|
36
|
+
chalk.red(`ā Validator with ID '${validatorId}' not found`)
|
|
37
|
+
);
|
|
38
|
+
console.log(
|
|
39
|
+
chalk.gray("š” Use `solforge list` to see running validators")
|
|
40
|
+
);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
validatorsToStop = [validator];
|
|
44
|
+
console.log(
|
|
45
|
+
chalk.cyan(
|
|
46
|
+
`š Stopping validator '${validator.name}' (${validatorId})...`
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
} else {
|
|
50
|
+
// No specific validator specified, show available options
|
|
51
|
+
console.log(chalk.yellow("ā ļø Please specify which validator to stop:"));
|
|
52
|
+
console.log(
|
|
53
|
+
chalk.gray("š” Use `solforge stop <id>` to stop a specific validator")
|
|
54
|
+
);
|
|
55
|
+
console.log(
|
|
56
|
+
chalk.gray("š” Use `solforge stop --all` to stop all validators")
|
|
57
|
+
);
|
|
58
|
+
console.log(chalk.gray("š” Use `solforge list` to see running validators"));
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Stop each validator
|
|
63
|
+
let stoppedCount = 0;
|
|
64
|
+
let errorCount = 0;
|
|
65
|
+
|
|
66
|
+
for (const validator of validatorsToStop) {
|
|
67
|
+
try {
|
|
68
|
+
const result = await stopValidator(validator, options.kill);
|
|
69
|
+
if (result.success) {
|
|
70
|
+
console.log(
|
|
71
|
+
chalk.green(`ā
Stopped ${validator.name} (${validator.id})`)
|
|
72
|
+
);
|
|
73
|
+
stoppedCount++;
|
|
74
|
+
} else {
|
|
75
|
+
console.error(
|
|
76
|
+
chalk.red(
|
|
77
|
+
`ā Failed to stop ${validator.name} (${validator.id}): ${result.error}`
|
|
78
|
+
)
|
|
79
|
+
);
|
|
80
|
+
errorCount++;
|
|
81
|
+
}
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.error(
|
|
84
|
+
chalk.red(
|
|
85
|
+
`ā Error stopping ${validator.name} (${validator.id}): ${
|
|
86
|
+
error instanceof Error ? error.message : String(error)
|
|
87
|
+
}`
|
|
88
|
+
)
|
|
89
|
+
);
|
|
90
|
+
errorCount++;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Summary
|
|
95
|
+
console.log();
|
|
96
|
+
if (stoppedCount > 0) {
|
|
97
|
+
console.log(
|
|
98
|
+
chalk.green(`ā
Successfully stopped ${stoppedCount} validator(s)`)
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (errorCount > 0) {
|
|
102
|
+
console.log(chalk.red(`ā Failed to stop ${errorCount} validator(s)`));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function stopValidator(
|
|
107
|
+
validator: RunningValidator,
|
|
108
|
+
forceKill: boolean = false
|
|
109
|
+
): Promise<{ success: boolean; error?: string }> {
|
|
110
|
+
try {
|
|
111
|
+
// Stop the API server first if it has a PID
|
|
112
|
+
if (validator.apiServerPid) {
|
|
113
|
+
try {
|
|
114
|
+
process.kill(validator.apiServerPid, "SIGTERM");
|
|
115
|
+
console.log(
|
|
116
|
+
chalk.gray(`š” Stopped API server (PID: ${validator.apiServerPid})`)
|
|
117
|
+
);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
console.log(
|
|
120
|
+
chalk.yellow(
|
|
121
|
+
`ā ļø Warning: Failed to stop API server: ${
|
|
122
|
+
error instanceof Error ? error.message : String(error)
|
|
123
|
+
}`
|
|
124
|
+
)
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Check if process is still running
|
|
130
|
+
const isRunning = await processRegistry.isProcessRunning(validator.pid);
|
|
131
|
+
if (!isRunning) {
|
|
132
|
+
// Process already stopped, just clean up registry
|
|
133
|
+
processRegistry.unregister(validator.id);
|
|
134
|
+
return { success: true };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const signal = forceKill ? "SIGKILL" : "SIGTERM";
|
|
138
|
+
|
|
139
|
+
// Send termination signal
|
|
140
|
+
process.kill(validator.pid, signal);
|
|
141
|
+
|
|
142
|
+
if (forceKill) {
|
|
143
|
+
// For SIGKILL, process should stop immediately
|
|
144
|
+
processRegistry.unregister(validator.id);
|
|
145
|
+
return { success: true };
|
|
146
|
+
} else {
|
|
147
|
+
// For SIGTERM, wait for graceful shutdown
|
|
148
|
+
const shutdownResult = await waitForProcessShutdown(validator.pid, 10000);
|
|
149
|
+
|
|
150
|
+
if (shutdownResult.success) {
|
|
151
|
+
processRegistry.unregister(validator.id);
|
|
152
|
+
return { success: true };
|
|
153
|
+
} else {
|
|
154
|
+
// Graceful shutdown failed, try force kill
|
|
155
|
+
console.log(
|
|
156
|
+
chalk.yellow(
|
|
157
|
+
`ā ļø Graceful shutdown failed for ${validator.name}, force killing...`
|
|
158
|
+
)
|
|
159
|
+
);
|
|
160
|
+
process.kill(validator.pid, "SIGKILL");
|
|
161
|
+
processRegistry.unregister(validator.id);
|
|
162
|
+
return { success: true };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
} catch (error) {
|
|
166
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
167
|
+
|
|
168
|
+
// If error is "ESRCH" (No such process), the process is already gone
|
|
169
|
+
if (errorMessage.includes("ESRCH")) {
|
|
170
|
+
processRegistry.unregister(validator.id);
|
|
171
|
+
return { success: true };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return { success: false, error: errorMessage };
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function waitForProcessShutdown(
|
|
179
|
+
pid: number,
|
|
180
|
+
timeoutMs: number = 10000
|
|
181
|
+
): Promise<{ success: boolean; error?: string }> {
|
|
182
|
+
const startTime = Date.now();
|
|
183
|
+
|
|
184
|
+
while (Date.now() - startTime < timeoutMs) {
|
|
185
|
+
try {
|
|
186
|
+
// Send signal 0 to check if process exists
|
|
187
|
+
process.kill(pid, 0);
|
|
188
|
+
// If no error thrown, process is still running
|
|
189
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
190
|
+
} catch (error) {
|
|
191
|
+
// Process is gone
|
|
192
|
+
return { success: true };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return { success: false, error: "Process shutdown timeout" };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export async function killCommand(
|
|
200
|
+
validatorId?: string,
|
|
201
|
+
options: { all?: boolean } = {}
|
|
202
|
+
): Promise<void> {
|
|
203
|
+
console.log(chalk.red("š Force killing validator(s)...\n"));
|
|
204
|
+
|
|
205
|
+
// Clean up dead processes first
|
|
206
|
+
await processRegistry.cleanup();
|
|
207
|
+
|
|
208
|
+
const validators = processRegistry.getRunning();
|
|
209
|
+
|
|
210
|
+
if (validators.length === 0) {
|
|
211
|
+
console.log(chalk.yellow("ā ļø No running validators found"));
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
let validatorsToKill: RunningValidator[] = [];
|
|
216
|
+
|
|
217
|
+
if (options.all) {
|
|
218
|
+
// Kill all validators
|
|
219
|
+
validatorsToKill = validators;
|
|
220
|
+
console.log(
|
|
221
|
+
chalk.cyan(`š Force killing all ${validators.length} validator(s)...`)
|
|
222
|
+
);
|
|
223
|
+
} else if (validatorId) {
|
|
224
|
+
// Kill specific validator
|
|
225
|
+
const validator = processRegistry.getById(validatorId);
|
|
226
|
+
if (!validator) {
|
|
227
|
+
console.error(
|
|
228
|
+
chalk.red(`ā Validator with ID '${validatorId}' not found`)
|
|
229
|
+
);
|
|
230
|
+
console.log(
|
|
231
|
+
chalk.gray("š” Use `solforge list` to see running validators")
|
|
232
|
+
);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
validatorsToKill = [validator];
|
|
236
|
+
console.log(
|
|
237
|
+
chalk.cyan(
|
|
238
|
+
`š Force killing validator '${validator.name}' (${validatorId})...`
|
|
239
|
+
)
|
|
240
|
+
);
|
|
241
|
+
} else {
|
|
242
|
+
// No specific validator specified, show interactive selection
|
|
243
|
+
console.log(chalk.cyan("š Select validator(s) to force kill:\n"));
|
|
244
|
+
|
|
245
|
+
// Display current validators
|
|
246
|
+
displayValidatorsTable(validators);
|
|
247
|
+
|
|
248
|
+
const choices = [
|
|
249
|
+
...validators.map((v) => ({
|
|
250
|
+
name: `${v.name} (${v.id}) - PID: ${v.pid}`,
|
|
251
|
+
value: v.id,
|
|
252
|
+
})),
|
|
253
|
+
{
|
|
254
|
+
name: chalk.red("Kill ALL validators"),
|
|
255
|
+
value: "all",
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
name: chalk.gray("Cancel"),
|
|
259
|
+
value: "cancel",
|
|
260
|
+
},
|
|
261
|
+
];
|
|
262
|
+
|
|
263
|
+
const selectedValidator = await select({
|
|
264
|
+
message: "Which validator would you like to force kill?",
|
|
265
|
+
choices,
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
if (selectedValidator === "cancel") {
|
|
269
|
+
console.log(chalk.gray("Operation cancelled"));
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (selectedValidator === "all") {
|
|
274
|
+
validatorsToKill = validators;
|
|
275
|
+
console.log(
|
|
276
|
+
chalk.cyan(`š Force killing all ${validators.length} validator(s)...`)
|
|
277
|
+
);
|
|
278
|
+
} else {
|
|
279
|
+
const validator = processRegistry.getById(selectedValidator);
|
|
280
|
+
if (!validator) {
|
|
281
|
+
console.error(chalk.red("ā Selected validator not found"));
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
validatorsToKill = [validator];
|
|
285
|
+
console.log(
|
|
286
|
+
chalk.cyan(
|
|
287
|
+
`š Force killing validator '${validator.name}' (${selectedValidator})...`
|
|
288
|
+
)
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Kill each validator
|
|
294
|
+
let killedCount = 0;
|
|
295
|
+
let errorCount = 0;
|
|
296
|
+
|
|
297
|
+
for (const validator of validatorsToKill) {
|
|
298
|
+
try {
|
|
299
|
+
const result = await stopValidator(validator, true); // Force kill
|
|
300
|
+
if (result.success) {
|
|
301
|
+
console.log(
|
|
302
|
+
chalk.green(`ā
Killed ${validator.name} (${validator.id})`)
|
|
303
|
+
);
|
|
304
|
+
killedCount++;
|
|
305
|
+
} else {
|
|
306
|
+
console.error(
|
|
307
|
+
chalk.red(
|
|
308
|
+
`ā Failed to kill ${validator.name} (${validator.id}): ${result.error}`
|
|
309
|
+
)
|
|
310
|
+
);
|
|
311
|
+
errorCount++;
|
|
312
|
+
}
|
|
313
|
+
} catch (error) {
|
|
314
|
+
console.error(
|
|
315
|
+
chalk.red(
|
|
316
|
+
`ā Error killing ${validator.name} (${validator.id}): ${
|
|
317
|
+
error instanceof Error ? error.message : String(error)
|
|
318
|
+
}`
|
|
319
|
+
)
|
|
320
|
+
);
|
|
321
|
+
errorCount++;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Summary
|
|
326
|
+
console.log();
|
|
327
|
+
if (killedCount > 0) {
|
|
328
|
+
console.log(
|
|
329
|
+
chalk.green(`ā
Successfully killed ${killedCount} validator(s)`)
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
if (errorCount > 0) {
|
|
333
|
+
console.log(chalk.red(`ā Failed to kill ${errorCount} validator(s)`));
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function displayValidatorsTable(validators: RunningValidator[]): void {
|
|
338
|
+
// Calculate column widths
|
|
339
|
+
const maxIdWidth = Math.max(2, ...validators.map((v) => v.id.length));
|
|
340
|
+
const maxNameWidth = Math.max(4, ...validators.map((v) => v.name.length));
|
|
341
|
+
const maxPidWidth = Math.max(
|
|
342
|
+
3,
|
|
343
|
+
...validators.map((v) => v.pid.toString().length)
|
|
344
|
+
);
|
|
345
|
+
const maxPortWidth = 9; // "8899/9900" format
|
|
346
|
+
const maxUptimeWidth = 7;
|
|
347
|
+
|
|
348
|
+
// Header
|
|
349
|
+
const header =
|
|
350
|
+
chalk.bold("ID".padEnd(maxIdWidth)) +
|
|
351
|
+
" | " +
|
|
352
|
+
chalk.bold("Name".padEnd(maxNameWidth)) +
|
|
353
|
+
" | " +
|
|
354
|
+
chalk.bold("PID".padEnd(maxPidWidth)) +
|
|
355
|
+
" | " +
|
|
356
|
+
chalk.bold("RPC/Faucet".padEnd(maxPortWidth)) +
|
|
357
|
+
" | " +
|
|
358
|
+
chalk.bold("Uptime".padEnd(maxUptimeWidth)) +
|
|
359
|
+
" | " +
|
|
360
|
+
chalk.bold("Status");
|
|
361
|
+
|
|
362
|
+
console.log(header);
|
|
363
|
+
console.log("-".repeat(header.length - 20)); // Subtract ANSI codes length
|
|
364
|
+
|
|
365
|
+
// Rows
|
|
366
|
+
validators.forEach((validator) => {
|
|
367
|
+
const uptime = formatUptime(validator.startTime);
|
|
368
|
+
const ports = `${validator.rpcPort}/${validator.faucetPort}`;
|
|
369
|
+
const status =
|
|
370
|
+
validator.status === "running" ? chalk.green("ā") : chalk.red("ā");
|
|
371
|
+
|
|
372
|
+
const row =
|
|
373
|
+
validator.id.padEnd(maxIdWidth) +
|
|
374
|
+
" | " +
|
|
375
|
+
validator.name.padEnd(maxNameWidth) +
|
|
376
|
+
" | " +
|
|
377
|
+
validator.pid.toString().padEnd(maxPidWidth) +
|
|
378
|
+
" | " +
|
|
379
|
+
ports.padEnd(maxPortWidth) +
|
|
380
|
+
" | " +
|
|
381
|
+
uptime.padEnd(maxUptimeWidth) +
|
|
382
|
+
" | " +
|
|
383
|
+
status;
|
|
384
|
+
|
|
385
|
+
console.log(row);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
console.log(); // Empty line
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function formatUptime(startTime: Date): string {
|
|
392
|
+
const now = new Date();
|
|
393
|
+
const uptimeMs = now.getTime() - startTime.getTime();
|
|
394
|
+
const uptimeSeconds = Math.floor(uptimeMs / 1000);
|
|
395
|
+
|
|
396
|
+
if (uptimeSeconds < 60) {
|
|
397
|
+
return `${uptimeSeconds}s`;
|
|
398
|
+
} else if (uptimeSeconds < 3600) {
|
|
399
|
+
const minutes = Math.floor(uptimeSeconds / 60);
|
|
400
|
+
return `${minutes}m`;
|
|
401
|
+
} else {
|
|
402
|
+
const hours = Math.floor(uptimeSeconds / 3600);
|
|
403
|
+
const minutes = Math.floor((uptimeSeconds % 3600) / 60);
|
|
404
|
+
return `${hours}h${minutes}m`;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { readFileSync, writeFileSync, existsSync } from "fs";
|
|
2
|
+
import { join, resolve } from "path";
|
|
3
|
+
import { ConfigSchema } from "../types/config.js";
|
|
4
|
+
import type { Config, ValidationResult } from "../types/config.js";
|
|
5
|
+
|
|
6
|
+
export class ConfigManager {
|
|
7
|
+
private config: Config | null = null;
|
|
8
|
+
private configPath: string | null = null;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Load configuration from a file path
|
|
12
|
+
*/
|
|
13
|
+
async load(configPath: string): Promise<Config> {
|
|
14
|
+
try {
|
|
15
|
+
const fullPath = resolve(configPath);
|
|
16
|
+
|
|
17
|
+
if (!existsSync(fullPath)) {
|
|
18
|
+
throw new Error(`Configuration file not found: ${fullPath}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const configContent = readFileSync(fullPath, "utf-8");
|
|
22
|
+
const rawConfig = JSON.parse(configContent);
|
|
23
|
+
|
|
24
|
+
// Validate and parse with Zod
|
|
25
|
+
const result = ConfigSchema.safeParse(rawConfig);
|
|
26
|
+
|
|
27
|
+
if (!result.success) {
|
|
28
|
+
const errors = result.error.issues.map((issue) => ({
|
|
29
|
+
path: issue.path.join("."),
|
|
30
|
+
message: issue.message,
|
|
31
|
+
}));
|
|
32
|
+
throw new Error(
|
|
33
|
+
`Configuration validation failed:\n${errors
|
|
34
|
+
.map((e) => ` - ${e.path}: ${e.message}`)
|
|
35
|
+
.join("\n")}`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
this.config = result.data;
|
|
40
|
+
this.configPath = fullPath;
|
|
41
|
+
|
|
42
|
+
return this.config;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error instanceof SyntaxError) {
|
|
45
|
+
throw new Error(`Invalid JSON in configuration file: ${error.message}`);
|
|
46
|
+
}
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Save current configuration to file
|
|
53
|
+
*/
|
|
54
|
+
async save(configPath?: string): Promise<void> {
|
|
55
|
+
if (!this.config) {
|
|
56
|
+
throw new Error("No configuration loaded");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const targetPath = configPath || this.configPath;
|
|
60
|
+
if (!targetPath) {
|
|
61
|
+
throw new Error("No configuration path specified");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const configContent = JSON.stringify(this.config, null, 2);
|
|
66
|
+
writeFileSync(targetPath, configContent, "utf-8");
|
|
67
|
+
this.configPath = targetPath;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`Failed to save configuration: ${
|
|
71
|
+
error instanceof Error ? error.message : String(error)
|
|
72
|
+
}`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Validate a configuration object
|
|
79
|
+
*/
|
|
80
|
+
validate(config: any): ValidationResult {
|
|
81
|
+
const result = ConfigSchema.safeParse(config);
|
|
82
|
+
|
|
83
|
+
if (result.success) {
|
|
84
|
+
return { valid: true, errors: [] };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const errors = result.error.issues.map((issue) => ({
|
|
88
|
+
path: issue.path.join("."),
|
|
89
|
+
message: issue.message,
|
|
90
|
+
}));
|
|
91
|
+
|
|
92
|
+
return { valid: false, errors };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Create a default configuration
|
|
97
|
+
*/
|
|
98
|
+
createDefault(): Config {
|
|
99
|
+
const defaultConfig = ConfigSchema.parse({});
|
|
100
|
+
this.config = defaultConfig;
|
|
101
|
+
return defaultConfig;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Get current configuration
|
|
106
|
+
*/
|
|
107
|
+
getConfig(): Config {
|
|
108
|
+
if (!this.config) {
|
|
109
|
+
throw new Error("No configuration loaded. Call load() first.");
|
|
110
|
+
}
|
|
111
|
+
return this.config;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Update configuration
|
|
116
|
+
*/
|
|
117
|
+
updateConfig(updates: Partial<Config>): Config {
|
|
118
|
+
if (!this.config) {
|
|
119
|
+
throw new Error("No configuration loaded. Call load() first.");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const updated = { ...this.config, ...updates };
|
|
123
|
+
const result = ConfigSchema.safeParse(updated);
|
|
124
|
+
|
|
125
|
+
if (!result.success) {
|
|
126
|
+
const errors = result.error.issues.map((issue) => ({
|
|
127
|
+
path: issue.path.join("."),
|
|
128
|
+
message: issue.message,
|
|
129
|
+
}));
|
|
130
|
+
throw new Error(
|
|
131
|
+
`Configuration update validation failed:\n${errors
|
|
132
|
+
.map((e) => ` - ${e.path}: ${e.message}`)
|
|
133
|
+
.join("\n")}`
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this.config = result.data;
|
|
138
|
+
return this.config;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Get configuration file path
|
|
143
|
+
*/
|
|
144
|
+
getConfigPath(): string | null {
|
|
145
|
+
return this.configPath;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Check if configuration is loaded
|
|
150
|
+
*/
|
|
151
|
+
isLoaded(): boolean {
|
|
152
|
+
return this.config !== null;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Singleton instance
|
|
157
|
+
export const configManager = new ConfigManager();
|