forked from dat-ecosystem/dat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-port.js
42 lines (36 loc) · 957 Bytes
/
get-port.js
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
var fs = require('fs')
var getport = require('getport')
var debug = require('debug')('get-port')
module.exports = getPort
module.exports.readPort = readPort
function getPort(startingPort, portFile, cb) {
readPort(portFile, function(err, port) {
if (err) return newPort()
gotPort(err, port)
})
function newPort() {
getport(startingPort, function(err, port) {
if (err) return gotPort(err)
debug('writing new portFile ' + portFile)
fs.writeFile(portFile, port.toString(), function(err) {
gotPort(err, port)
})
})
}
function gotPort(err, port) {
if (err) {
debug('getport err: ' + err)
return cb(err)
}
debug('getport got port ' + port)
cb(err, port)
}
}
function readPort(portFile, cb) {
fs.readFile(portFile, function(err, buf) {
if (err) return cb(err)
var port = +buf.toString()
debug('read port from portFile: ' + port)
cb(err, port)
})
}