forked from dat-ecosystem/dat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·97 lines (80 loc) · 2.62 KB
/
cli.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
var path = require('path')
var Dat = require('./')
var minimist = require('minimist')
var EOL = require('os').EOL
var url = require('url')
var stdout = require('stdout-stream')
var fs = require('fs')
var path = require('path')
var debug = require('debug')('dat.cli')
var onerror = function(err) {
console.error('Error: ' + err.message)
process.exit(2)
}
// rules:
// a 1 part command and a 2 part command can't share the same first part
// e.g. if 'dat cat' exists you can't add 'dat cat dog'
var bin = {
"cat": './bin/cat',
"export": './bin/cat',
"import": './bin/import',
"init": './bin/init',
"help": './bin/help',
"version": './bin/version',
"pull": './bin/pull',
"push": './bin/push',
"clone": './bin/clone',
"listen": './bin/listen',
"serve": "./bin/listen",
"blobs get": "./bin/blobs-get",
"blobs put": "./bin/blobs-put",
"rows get": "./bin/rows-get",
"rows delete": "./bin/rows-delete"
}
var argv = minimist(process.argv.slice(2), {boolean: true})
var first = argv._[0] || ''
var second = argv._[1] || ''
var cmd = first
if (!bin.hasOwnProperty(first)) cmd = first + ' ' + second
var defaultMessage = "Usage: dat <command> [<args>]" + EOL + EOL + "Enter 'dat help' for help"
var badMessage = ['Command not found: ' + cmd, '', defaultMessage].join(EOL)
if (!bin.hasOwnProperty(first) && !bin.hasOwnProperty(cmd)) {
console.error(badMessage)
process.exit(1)
}
var dir = (first === 'clone' && (argv._[2] || toFolder(argv._[1]))) || argv.path || '.' // leaky
var initing = (first === 'init' || first === 'clone')
var dat = Dat(dir, {init: false}, function(err) {
if (err) return onerror(err)
var execCommand = function(err) {
if (err) return onerror(err)
require(bin[cmd])(dat, argv, function(err) {
if (err) return onerror(err)
setImmediate(close)
})
}
if (!dat.db && !initing) return onerror(new Error('There is no dat here'))
if (first !== 'listen' && !dat.rpcClient) return dat.listen(argv.port, argv, execCommand)
execCommand()
})
function toFolder(dir) {
if (!dir) return dir
return dir.replace(/^.*\/\//, '').replace(/[\/:].*$/, '')
}
function close() {
// if _server exists it means dat is the rpc server
if (dat._server) {
// since the server process can't exit yet we must manually close stdout
stdout.end()
// if there aren't any active connections then we can close the server
if (dat.connections.sockets.length === 0) dat.close()
// otherwise wait for the current connections to close
dat.connections.on('idle', function() {
debug('dat close due to idle')
dat.close()
})
} else {
dat.close()
}
}