-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathsetup.js
More file actions
170 lines (148 loc) · 5.11 KB
/
setup.js
File metadata and controls
170 lines (148 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Listr } from 'listr2';
import { Args, Flags } from '@oclif/core';
import chalk from 'chalk';
import BaseCommand from '../oclif/command/BaseCommand.js';
import MuteOneLineError from '../oclif/errors/MuteOneLineError.js';
import {
PRESET_LOCAL,
PRESET_MAINNET,
PRESETS,
} from '../constants.js';
export default class SetupCommand extends BaseCommand {
static description = 'Set up a new Dash node';
static args = {
preset: Args.string(
{
name: 'preset',
required: false,
description: 'Node configuration preset',
options: Object.values(PRESETS),
},
),
};
static flags = {
'debug-logs': Flags.boolean({ char: 'd', description: 'enable debug logs', allowNo: true }),
'node-count': Flags.integer({ char: 'c', description: 'number of nodes to setup' }),
'miner-interval': Flags.string({ char: 'm', description: 'interval between blocks' }),
verbose: Flags.boolean({ char: 'v', description: 'use verbose mode for output', default: false }),
};
/**
* @param {Object} args
* @param {Object} flags
* @param {ConfigFile} configFile
* @param {setupLocalPresetTask} setupLocalPresetTask
* @param {setupRegularPresetTask} setupRegularPresetTask
* @param {DockerCompose} dockerCompose
* @param {DefaultConfigs} defaultConfigs
* @return {Promise<void>}
*/
async runWithDependencies(
{
preset,
},
{
'node-count': nodeCount,
'debug-logs': debugLogs,
'miner-interval': minerInterval,
verbose: isVerbose,
},
configFile,
setupLocalPresetTask,
setupRegularPresetTask,
dockerCompose,
defaultConfigs,
) {
if (nodeCount !== null && (nodeCount < 1)) {
throw new Error('node-count flag should be not less than 1');
}
const tasks = new Listr(
[
{
title: 'Configuration preset',
task: async (ctx, task) => {
if (ctx.preset === undefined) {
ctx.preset = await task.prompt([
{
type: 'select',
header: ` Dashmate provides three default configuration presets:
mainnet - Run a node connected to the Dash main network
testnet - Run a node connected to the Dash test network
local - Run a full network environment on your machine for local development\n`,
message: 'Select preset',
choices: Object.values(PRESETS),
initial: PRESET_MAINNET,
},
]);
}
let isAlreadyConfigured;
if (ctx.preset === PRESET_LOCAL) {
isAlreadyConfigured = configFile.isGroupExists(ctx.preset);
} else {
const defaultConfig = defaultConfigs.get(ctx.preset);
isAlreadyConfigured = !configFile.getConfig(ctx.preset).isEqual(defaultConfig);
}
if (isAlreadyConfigured) {
const resetCommand = ctx.preset === PRESET_LOCAL
? `dashmate group reset --group ${ctx.preset} --hard` : `dashmate reset --config ${ctx.preset} --hard`;
// eslint-disable-next-line no-param-reassign
task.output = chalk`Preset {bold ${ctx.preset}} already configured.
To set up a node with this preset from scratch use {bold.cyanBright ${resetCommand}}.
Previous data and configuration for this preset will be lost.
If you want to keep the existing data and configuration, please use the {bold.cyanBright dashmate config create}
command to create a new configuration for this preset.`;
throw new Error(`Preset ${ctx.preset} already configured`);
} else {
// eslint-disable-next-line no-param-reassign
task.output = ctx.preset;
}
},
options: {
persistentOutput: true,
showErrorMessage: false,
},
},
{
task: (ctx) => {
if (ctx.preset === PRESET_LOCAL) {
return setupLocalPresetTask();
}
return setupRegularPresetTask();
},
},
],
{
concurrent: false,
renderer: isVerbose ? 'verbose' : 'default',
rendererOptions: {
showTimer: isVerbose,
clearOutput: false,
collapse: false,
showSubtasks: true,
removeEmptyLines: false,
},
},
);
if (!isVerbose) { // TODO: We need to print it only with default renderer
// eslint-disable-next-line import/extensions
const { begoo } = await import('begoo/index.js'); // don't remove index!
const welcomeText = begoo(
chalk`Hello! I'm your {bold.cyanBright Dash} mate!
I will assist you with setting up a Dash node on mainnet or testnet. I can also help you set up a development network on your local system.`,
{ maxLength: 45 },
);
// eslint-disable-next-line no-console
console.log(welcomeText);
}
try {
await tasks.run({
preset,
nodeCount,
debugLogs,
minerInterval,
isVerbose,
});
} catch (e) {
throw new MuteOneLineError(e);
}
}
}