Skip to content
This repository was archived by the owner on Dec 10, 2020. It is now read-only.

Commit 45244bc

Browse files
committed
Add client config file and custom logger support
1 parent 8c1cfcc commit 45244bc

File tree

3 files changed

+105
-7
lines changed

3 files changed

+105
-7
lines changed

bin/cli.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@ const { fromName: serverFromName } = require('../lib/net/server')
77
import Node from '../lib/node'
88
import { Server as RPCServer } from 'jayson'
99
import { Config } from '../lib/config'
10+
import {Logger} from 'winston'
1011
const RPCManager = require('../lib/rpc')
1112
const level = require('level')
1213
const os = require('os')
1314
const path = require('path')
1415
const fs = require('fs-extra')
16+
const yargs = require('yargs')
17+
18+
type UserConfig = {
19+
logger? : Logger
20+
}
1521

1622
const networks = Object.entries(chains.names)
17-
const args = require('yargs')
23+
const args = yargs
1824
.options({
25+
config: {
26+
describe: 'Path to ethereumjs.config.js',
27+
default: undefined,
28+
},
1929
network: {
2030
describe: `Network`,
2131
choices: networks.map((n) => n[1]),
@@ -80,7 +90,18 @@ const args = require('yargs')
8090
},
8191
})
8292
.locale('en_EN').argv
83-
const logger = getLogger({ loglevel: args.loglevel })
93+
94+
const config: UserConfig = {}
95+
96+
if (args.config) {
97+
const userConfig: UserConfig = <UserConfig>require(
98+
path.resolve(process.cwd(), args.config)
99+
)
100+
101+
Object.assign(config, userConfig)
102+
}
103+
104+
const logger = config.logger ?? getLogger({ loglevel: args.loglevel })
84105

85106
async function runNode(options: any) {
86107
logger.info('Initializing Ethereumjs client...')
@@ -153,21 +174,24 @@ async function run() {
153174
minPeers: args.minPeers,
154175
maxPeers: args.maxPeers,
155176
}
156-
const node = await runNode(options)
157-
const server = args.rpc ? runRpcServer(node, options) : null
177+
178+
let node: Node|null
179+
let server: RPCServer|null
158180

159181
process.once('SIGINT', async () => {
160182
process.once('SIGINT', () => {
161183
logger.info('Force shutdown. Exit immediately.')
162184
process.exit(1)
163185
})
164-
165186
logger.info('Caught interrupt signal. Shutting down...')
166187
if (server) server.http().close()
167-
await node.stop()
188+
if (node) await node.stop()
168189
logger.info('Exiting.')
169-
process.exit()
190+
process.exit(0)
170191
})
192+
193+
node = await runNode(options)
194+
server = args.rpc ? runRpcServer(node, options) : null
171195
}
172196

173197
run().catch((err) => logger.error(err))

test/cli/cli.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import path from 'path'
2+
import { spawn } from 'child_process'
3+
4+
import tape from 'tape'
5+
6+
tape('[CLI]', (t) => {
7+
t.test('should handle SIGINT', { timeout: 160000 }, (t) => {
8+
t.plan(1)
9+
const file = require.resolve('../../dist/bin/cli.js')
10+
const child = spawn(process.execPath, [
11+
file,
12+
'--config',
13+
path.join(__dirname, '/fixtures/ethereumjs.config.js')
14+
], {
15+
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
16+
})
17+
18+
const timeout = setTimeout(() => {
19+
child.kill('SIGINT')
20+
}, 120000)
21+
22+
const end = () => {
23+
clearTimeout(timeout)
24+
child.kill('SIGINT')
25+
t.end()
26+
}
27+
28+
function onServiceStarted([level, message]: [string, string]) {
29+
if (level === 'info') {
30+
if (message === 'Started eth service.') {
31+
child.removeListener('message', onServiceStarted)
32+
child.on('message', onFirstSigintSent)
33+
child.kill('SIGINT')
34+
}
35+
}
36+
}
37+
38+
function onFirstSigintSent([level, message]: [string, string]) {
39+
if (level === 'info') {
40+
if (message === 'Exiting.') {
41+
child.removeListener('message', onFirstSigintSent)
42+
t.pass('Client exited')
43+
clearTimeout(timeout)
44+
}
45+
}
46+
}
47+
48+
child.on('message', onServiceStarted)
49+
child.on('message', ([level, message]: [string, string]) => console.log(level, message))
50+
51+
child.on('error', (error) => {
52+
t.fail(`Error: ${error}`)
53+
})
54+
55+
child.on('close', (code, signal) => {
56+
if (code !== 0) {
57+
t.fail(`child process exited with code ${code}, signal ${signal}`)
58+
end()
59+
}
60+
})
61+
})
62+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function send(level, message) {
2+
process.send([level, message])
3+
}
4+
5+
module.exports = {
6+
logger: {
7+
warn: send.bind(null, 'warn'),
8+
info: send.bind(null, 'info'),
9+
debug: send.bind(null, 'debug'),
10+
error: send.bind(null, 'error'),
11+
}
12+
}

0 commit comments

Comments
 (0)