|
| 1 | +import { accessSync, watch } from 'fs'; |
| 2 | +import path from 'path'; |
1 | 3 | import readline from 'readline'; |
2 | 4 | import { Arguments } from 'yargs'; |
3 | 5 | import { CreateNodeRequest, CreateNodeResponse } from '../../proto/xudrpc_pb'; |
4 | 6 | import { callback, loadXudInitClient } from '../command'; |
| 7 | +import { getDefaultCertPath } from '../utils'; |
5 | 8 |
|
6 | 9 | export const command = 'create'; |
7 | 10 |
|
8 | 11 | export const describe = 'use this to create a new xud instance and set a password'; |
9 | 12 |
|
10 | 13 | export const builder = {}; |
11 | 14 |
|
| 15 | +const waitForCert = (certPath: string) => { |
| 16 | + return new Promise<void>((resolve, reject) => { |
| 17 | + try { |
| 18 | + accessSync(certPath); |
| 19 | + resolve(); |
| 20 | + } catch (err) { |
| 21 | + if (err.code === 'ENOENT') { |
| 22 | + // wait up to 5 seconds for the tls.cert file to be created in case |
| 23 | + // this is the first time xud has been run |
| 24 | + const certDir = path.dirname(certPath); |
| 25 | + const certFilename = path.basename(certPath); |
| 26 | + const fsWatcher = watch(certDir, (event, filename) => { |
| 27 | + if (event === 'change' && filename === certFilename) { |
| 28 | + clearTimeout(timeout); |
| 29 | + fsWatcher.close(); |
| 30 | + resolve(); |
| 31 | + } |
| 32 | + }); |
| 33 | + const timeout = setTimeout(() => { |
| 34 | + fsWatcher.close(); |
| 35 | + reject(`timed out waiting for cert to be created at ${certPath}`); |
| 36 | + }, 5000); |
| 37 | + } else { |
| 38 | + // we handle errors due to file not existing, otherwise reject |
| 39 | + reject(err); |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | +}; |
| 44 | + |
12 | 45 | const formatOutput = (response: CreateNodeResponse.AsObject) => { |
13 | 46 | if (response.seedMnemonicList.length === 24) { |
14 | 47 | const WORDS_PER_ROW = 4; |
@@ -49,19 +82,30 @@ export const handler = (argv: Arguments) => { |
49 | 82 | process.stdout.write('Enter a password: '); |
50 | 83 | rl.question('', (password1) => { |
51 | 84 | process.stdout.write('\nRe-enter password: '); |
52 | | - rl.question('', (password2) => { |
| 85 | + rl.question('', async (password2) => { |
53 | 86 | process.stdout.write('\n\n'); |
54 | 87 | rl.close(); |
55 | 88 | if (password1 === password2) { |
56 | 89 | const request = new CreateNodeRequest(); |
57 | 90 | request.setPassword(password1); |
| 91 | + |
| 92 | + const certPath = argv.tlscertpath ? argv.tlscertpath : getDefaultCertPath(); |
| 93 | + try { |
| 94 | + await waitForCert(certPath); |
| 95 | + } catch (err) { |
| 96 | + console.error(err); |
| 97 | + process.exitCode = 1; |
| 98 | + return; |
| 99 | + } |
| 100 | + |
58 | 101 | const client = loadXudInitClient(argv); |
59 | 102 | // wait up to 3 seconds for rpc server to listen before call in case xud was just started |
60 | 103 | client.waitForReady(Date.now() + 3000, () => { |
61 | 104 | client.createNode(request, callback(argv, formatOutput)); |
62 | 105 | }); |
63 | 106 | } else { |
64 | | - console.log('Passwords do not match, please try again.'); |
| 107 | + process.exitCode = 1; |
| 108 | + console.error('Passwords do not match, please try again'); |
65 | 109 | } |
66 | 110 | }); |
67 | 111 | }); |
|
0 commit comments