forked from iriscouch/couchjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·85 lines (70 loc) · 2.54 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
#!/usr/bin/env node
//
// couchjs replacement
//
// Copyright 2011 Iris Couch
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var fs = require('fs')
var util = require('util')
var Fiber = require('fibers')
var optimist = require('optimist')
var child_process = require('child_process')
var couchjs = require('./couchjs')
var package_json = require('./package.json')
var couchdb_extra = require('./extra')
var LineStream = require('./stream')
var inspector = require('./inspector')
var log = require('./console').log
var opts = optimist.boolean(['h', 'V', 'H'])
.describe({ 'h': 'display a short help message and exit'
, 'V': 'display version information and exit'
, 'H': 'enable couchjs cURL bindings (not implemented)'
})
.boolean('extra')
.describe('extra', 'Extra features for CouchDB, for os_daemons')
.usage('$0 <path to main.js>')
function main() {
if(opts.argv.extra)
return couchdb_extra()
var main_js = opts.argv._[0]
if(!main_js)
return console.error(opts.help())
log('couchjs/%s %s: %s', package_json.version, process.pid, main_js)
if(process.env.COUCHJS_DEBUG_PORT)
inspector(+process.env.COUCHJS_DEBUG_PORT)
fs.readFile(main_js, 'utf8', function(er, body) {
if(er)
throw er
var stdin = new LineStream.v2
stdin.on('readable', function() {
var buf = stdin.read()
if(buf)
couchjs.stdin(buf)
})
stdin.on('end', function() {
log('Terminate; connection to parent closed')
process.exit(0)
})
process.stdin.setEncoding('utf8')
process.stdin.pipe(stdin)
var main_func = Function(['print', 'readline', 'evalcx', 'gc', 'quit'], body)
log('Call main')
Fiber(function() { main_func(couchjs.print, couchjs.readline, couchjs.evalcx, couchjs.gc) }).run()
})
process.on('uncaughtException', function(er) {
log('Error:\n%s', er.stack)
})
}
if(require.main === module)
main()