forked from ddnlink/ddn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
157 lines (137 loc) · 4.62 KB
/
Copy pathapp.js
File metadata and controls
157 lines (137 loc) · 4.62 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
/**
* 系统启动入口
* wangxm 2018-12-25
*/
const package = require('./package.json');
const command = require('commander');
const path = require('path');
const fs = require('fs');
const { Utils } = require('@ddn/ddn-utils');
const Program = require('./src/kernal/program');
/**
* 整理系统配置文件生成输入参数
*/
function genOptions() {
command
.version(package.version)
.option('-c, --config <path>', 'Config file path')
.option('-p, --port <port>', 'Listening port number')
.option('-a, --address <ip>', 'Listening host name or ip')
.option('-b, --blockchain <path>', 'Blockchain db path')
.option('-g, --genesisblock <path>', 'Genesisblock path')
.option('-x, --peers [peers...]', 'Peers list')
.option('-l, --log <level>', 'Log level')
.option('-d, --daemon', 'Run ddn node as daemon')
.option('-e, --execute <path>', 'exe')
.option('--dapps <dir>', 'DApps directory')
.option('--base <dir>', 'Base directory')
.parse(process.argv);
const baseDir = command.base || path.resolve(__dirname, './');
let configFile = path.join(baseDir, 'config.json');
if (command.config) {
configFile = path.resolve(process.cwd(), command.config);
}
if (!fs.existsSync(configFile)) {
console.error("Failed: DDN config file does not exists.")
process.exit(1);
return;
}
let genesisblockFile = path.join(baseDir, 'genesisBlock.json');
if (command.genesisblock) {
genesisblockFile = path.resolve(process.cwd(), command.genesisblock);
}
if (!fs.existsSync(genesisblockFile)) {
console.error("Failed: DDN genesisblock file does not exists.")
process.exit(1);
return;
}
const protoFile = path.join(baseDir, 'protos', 'ddn.proto');
if (!fs.existsSync(protoFile)) {
console.error('Failed: DDN proto file does not exists.');
process.exit(1);
return;
}
const assetConfigFile = path.resolve(path.join(baseDir, 'config.asset.js'));
if (!fs.existsSync(assetConfigFile)) {
console.error('Failed: DDN asset config file does not exists.');
process.exit(1);
return;
}
const configObject = JSON.parse(fs.readFileSync(configFile, 'utf8'));
const genesisblockObject = JSON.parse(fs.readFileSync(genesisblockFile, 'utf8'));
//wxm 修改config.json文件,自动生成dapp的masterpassword,这个不应该在代码中,应该在外围生成
if (!configObject.dapp.masterpassword) {
const randomstring = require("randomstring");
configObject.dapp.masterpassword = randomstring.generate({
length: 12,
readable: true,
charset: 'alphanumeric'
});
fs.writeFileSync(configFile, JSON.stringify(configObject, null, 2), "utf8");
}
configObject.version = package.version;
configObject.basedir = baseDir;
configObject.buildVersion = 'development';
configObject.netVersion = process.env.NET_VERSION || 'testnet';
configObject.publicDir = path.join(baseDir, 'public', 'dist');
configObject.dappsDir = command.dapps || path.join(baseDir, 'dapps')
if (command.port) {
configObject.port = command.port;
}
if (command.address) {
configObject.address = command.address;
}
if (command.peers) {
if (typeof command.peers === 'string') {
configObject.peers.list = command.peers.split(',').map(peer => {
peer = peer.split(":");
return {
ip: peer.shift(),
port: peer.shift() || configObject.port
};
});
} else {
configObject.peers.list = [];
}
}
if (command.log) {
configObject.logLevel = command.log;
}
if (command.reindex) {
configObject.loading.verifyOnLoading = true;
}
return {
baseDir,
configObject,
genesisblockObject,
assetConfigFile,
protoFile,
isDaemonMode: !!command.daemon
}
}
async function main() {
global._require_runtime_ = function(m) {
if (typeof(_require_native_) == "function") {
return _require_native_(m);
} else {
return require(m);
}
};
var program;
try
{
var options = genOptions();
program = new Program();
await program.run(options);
}
catch (err)
{
console.error(Utils.getErrorMsg(err));
if (program) {
program.destory();
}
process.exit(1);
return;
}
}
main();