Skip to content

Commit 763cb28

Browse files
committed
Extract simulator into separate bin module
1 parent 7ce2df5 commit 763cb28

File tree

3 files changed

+64
-43
lines changed

3 files changed

+64
-43
lines changed

bin.js renamed to bin/index.js

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ const { spawn } = require('child_process')
77
const minimist = require('minimist')
88
const ram = require('random-access-memory')
99

10-
const { Server, Client } = require('./')
10+
const { Server, Client } = require('../')
1111
const { migrate: migrateFromDaemon, isMigrated } = require('@hyperspace/migration-tool')
12-
const getNetworkOptions = require('@hyperspace/rpc/socket')
1312

1413
// TODO: Default paths are duplicated here because we need to do the async migration check.
1514
const HYPERSPACE_STORAGE_DIR = p.join(os.homedir(), '.hyperspace', 'storage')
@@ -26,21 +25,15 @@ const argv = minimist(process.argv.slice(2), {
2625
host: 'h',
2726
storage: 's',
2827
bootstrap: 'b'
29-
},
30-
'--': true
28+
}
3129
})
32-
console.log('argv:', argv)
3330

34-
const version = `hyperspace/${require('./package.json').version} ${process.platform}-${process.arch} node-${process.version}`
31+
const version = `hyperspace/${require('../package.json').version} ${process.platform}-${process.arch} node-${process.version}`
3532

3633
const help = `Hypercore, batteries included.
3734
${version}
3835
39-
Usage: hyperspace [command] [options]
40-
Commands:
41-
simulator <script.js> Run script.js using an in-memory Hyperspace instance
42-
43-
Flags:
36+
Usage: hyperspace [options]
4437
--host, -h Set unix socket name
4538
--port -p Set the port (will use TCP)
4639
--storage, -s Overwrite storage folder
@@ -59,10 +52,6 @@ if (argv.help) {
5952
main().catch(onerror)
6053

6154
async function main () {
62-
if (argv._[0] === 'simulator') {
63-
return simulator()
64-
}
65-
6655
console.log('Running ' + version)
6756

6857
// Note: This will be removed in future releases of Hyperspace.
@@ -149,33 +138,6 @@ function createServer (storage, opts) {
149138
})
150139
}
151140

152-
async function simulator () {
153-
if (argv._.length === 1) throw new Error('Must provide a script for the simulator to run.')
154-
const scriptPath = p.resolve(argv._[1])
155-
const simulatorId = `hyperspace-simulator-${process.pid}`
156-
process.env.HYPERSPACE_SOCKET = simulatorId
157-
158-
const server = createServer(ram, {
159-
...argv,
160-
host: simulatorId
161-
})
162-
await server.open()
163-
164-
process.once('SIGINT', close)
165-
process.once('SIGTERM', close)
166-
167-
const childArgs = argv['--'] || []
168-
const child = spawn(process.execPath, [scriptPath, ...childArgs], {
169-
stdio: 'inherit'
170-
})
171-
child.on('close', close)
172-
173-
async function close () {
174-
console.log('Shutting down simulator...')
175-
server.close().catch(onerror)
176-
}
177-
}
178-
179141
async function getStoragePath () {
180142
try {
181143
// If this dir exists, use it.

bin/simulator.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env node
2+
const p = require('path')
3+
const { spawn } = require('child_process')
4+
const minimist = require('minimist')
5+
const ram = require('random-access-memory')
6+
7+
const { Server, Client } = require('../')
8+
9+
const argv = minimist(process.argv.slice(2), {
10+
'--': true
11+
})
12+
const version = `hyperspace/${require('../package.json').version} ${process.platform}-${process.arch} node-${process.version}`
13+
const help = `Hypercore, batteries included.
14+
${version}
15+
16+
Usage: hyperspace-simulator <script.js> -- [script-args]
17+
Run the test script using an in-memory Hyperspace instance.
18+
`
19+
20+
if (argv.help) {
21+
console.error(help)
22+
process.exit(0)
23+
}
24+
main().catch(onerror)
25+
26+
27+
async function main () {
28+
if (!argv._.length) throw new Error('Must provide a script for the simulator to run.')
29+
const scriptPath = p.resolve(argv._[0])
30+
const simulatorId = `hyperspace-simulator-${process.pid}`
31+
process.env.HYPERSPACE_SOCKET = simulatorId
32+
33+
const server = new Server({
34+
host: simulatorId,
35+
storage: ram,
36+
noMigrate: true
37+
})
38+
await server.open()
39+
40+
process.once('SIGINT', close)
41+
process.once('SIGTERM', close)
42+
43+
const childArgs = argv['--'] || []
44+
const child = spawn(process.execPath, [scriptPath, ...childArgs], {
45+
stdio: 'inherit'
46+
})
47+
child.on('close', close)
48+
49+
async function close () {
50+
console.log('Shutting down simulator...')
51+
server.close().catch(onerror)
52+
}
53+
}
54+
55+
function onerror (err) {
56+
console.error(err.stack)
57+
process.exit(1)
58+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Hypercores, batteries included.",
55
"main": "index.js",
66
"bin": {
7-
"hyperspace": "bin.js"
7+
"hyperspace": "bin/index.js",
8+
"hyperspace-simulator": "bin/simulator.js"
89
},
910
"directories": {
1011
"test": "test"

0 commit comments

Comments
 (0)