-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfig.js
122 lines (95 loc) · 2.73 KB
/
config.js
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
/* eslint-disable import/no-dynamic-require */
import os from 'os';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import Joi from 'joi';
import jsonminify from 'jsonminify';
import chalk from 'chalk';
import {
createKeypairFromPassphrase,
createAddressFromPublicKey,
} from 'adamant-api';
import * as log from './log.js';
const homeDir = os.homedir();
const configPathName = '.adm';
export const configFileName = process.env.ADM_CONFIG_FILENAME || 'config.jsonc';
export const configDirPath =
process.env.ADM_CONFIG_PATH || `${homeDir}/${configPathName}`;
const configFilePath = path.normalize(`${configDirPath}/${configFileName}`);
const localConfigFilePath = path.resolve(configFileName);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const defaultConfigFilePath = path.join(__dirname, '../config.default.jsonc');
function loadConfig(configPath) {
let parsedConfig = {};
try {
const data = fs.readFileSync(configPath, 'utf8');
parsedConfig = JSON.parse(jsonminify(data));
} catch (err) {
console.log(
chalk.red(
'Failed to parse the configuration from:\n' +
`└── ${chalk.yellow(configPath)}.`,
),
);
console.log();
console.log(err);
process.exit(1);
}
return parsedConfig;
}
let config = loadConfig(defaultConfigFilePath);
const configPaths = [configFilePath, localConfigFilePath];
for (const configPath of configPaths) {
let existingConfigPath;
const configWithComments = `${configPath}c`;
if (fs.existsSync(configPath)) {
existingConfigPath = configPath;
} else if (
!process.env.ADM_CONFIG_FILENAME &&
fs.existsSync(configWithComments)
) {
existingConfigPath = configWithComments;
}
if (existingConfigPath) {
const loadedConfig = loadConfig(existingConfigPath);
const keypair = createKeypairFromPassphrase(loadedConfig.passphrase);
const address = createAddressFromPublicKey(keypair.publicKey);
config = {
...config,
...loadedConfig,
configPath: existingConfigPath,
accountAddress: address,
};
break;
}
}
const netSchema = Joi.object({
nodes: Joi.array().items(
Joi.object({
ip: Joi.string(),
protocol: Joi.string(),
port: Joi.number().allow(''),
}),
),
});
const schema = Joi.object({
network: Joi.string(),
rpc: Joi.object({
port: Joi.number(),
}),
networks: Joi.object({
[config.network]: netSchema.required(),
}),
// passphrase can be set later
passphrase: Joi.string().allow(''),
});
const { error, value } = schema.validate(config, {
allowUnknown: true,
});
if (error) {
log.error(
error.toString().replace('ValidationError', 'Config validation error'),
);
}
export default value;