|
1 | 1 | const p = require('path')
|
2 |
| -const { URL } = require('url') |
3 |
| -const request = require('request-promise-native') |
4 | 2 | const chalk = require('chalk')
|
5 | 3 | const forever = require('forever')
|
6 | 4 |
|
7 |
| -const { loadMetadata, createMetadata } = require('../lib/metadata') |
| 5 | +const { createMetadata } = require('../lib/metadata') |
| 6 | +const { HyperdriveClient } = require('hyperdrive-daemon-client') |
8 | 7 |
|
9 | 8 | exports.command = 'start'
|
10 |
| -exports.desc = 'Start the Hypermount daemon.' |
| 9 | +exports.desc = 'Start the Hyperdrive daemon.' |
11 | 10 | exports.builder = {
|
12 | 11 | port: {
|
13 |
| - description: 'The HTTP port that the daemon will bind to.', |
| 12 | + description: 'The gRPC port that the daemon will bind to.', |
14 | 13 | type: 'number',
|
15 | 14 | default: 3101
|
16 | 15 | },
|
17 |
| - replicationPort: { |
18 |
| - description: 'The port that the hypercore replicator will bind to.', |
19 |
| - type: 'number', |
20 |
| - default: 3102 |
| 16 | + storage: { |
| 17 | + description: 'The storage directory for hyperdrives and associated metadata.', |
| 18 | + type: 'string', |
| 19 | + default: './storage' |
21 | 20 | }
|
22 | 21 | }
|
23 | 22 | exports.handler = async function (argv) {
|
24 |
| - let metadata = await loadMetadata() |
25 |
| - if (metadata) { |
26 |
| - try { |
27 |
| - await request.get(new URL('/status', metadata.endpoint).toString(), { |
28 |
| - auth: { |
29 |
| - bearer: metadata.token |
30 |
| - } |
31 |
| - }) |
32 |
| - } catch (err) { |
33 |
| - await start(argv) |
34 |
| - } |
35 |
| - } else { |
36 |
| - await start(argv) |
| 23 | + const client = new HyperdriveClient(`localhost:${argv.port}`) |
| 24 | + console.log(0) |
| 25 | + client.ready(err => { |
| 26 | + console.log('err:', err) |
| 27 | + if (err) return onerror(err) |
| 28 | + console.log(chalk.green('The Hyperdrive daemon is already running.')) |
| 29 | + }) |
| 30 | + |
| 31 | + function onerror (err) { |
| 32 | + if (!err.disconnected) return showError(err) |
| 33 | + console.log('starting here') |
| 34 | + start(argv).catch(showError) |
| 35 | + } |
| 36 | + |
| 37 | + function showError (err) { |
| 38 | + console.error(chalk.red('Could not start the Hyperdrive daemon:')) |
| 39 | + console.error(chalk.red(` ${err}`)) |
37 | 40 | }
|
38 | 41 | }
|
39 | 42 |
|
40 | 43 | async function start (argv) {
|
41 |
| - let endpoint = `http://localhost:${argv.port}` |
| 44 | + console.log('in start') |
| 45 | + let endpoint = `localhost:${argv.port}` |
42 | 46 | await createMetadata(endpoint)
|
43 | 47 | forever.startDaemon(p.join(__dirname, '..', 'index.js'), {
|
44 |
| - uid: 'hypermount', |
| 48 | + uid: 'hyperdrive', |
45 | 49 | max: 1,
|
46 |
| - logFile: './hypermount.log', |
47 |
| - outFile: './hypermount.log', |
48 |
| - errFile: './hypermount.log', |
49 |
| - args: ['--replicationPort', argv.replicationPort, '--port', argv.port] |
| 50 | + logFile: './hyperdrive.log', |
| 51 | + outFile: './hyperdrive.log', |
| 52 | + errFile: './hyperdrive.log', |
| 53 | + args: ['--port', argv.port, '--storage', argv.storage] |
50 | 54 | })
|
51 | 55 | console.log(chalk.green(`Daemon started at ${endpoint}`))
|
52 | 56 | }
|
0 commit comments