-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·56 lines (42 loc) · 1 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
#!/usr/bin/env node
var fs = require('fs')
var prompt = require('prompt')
var JsonObj = require('./')
// not minimist for now
//var minimist = require('minimist')
//var args = minimist(process.argv.slice(2))
var args = process.argv.slice(2)
var filename = args[0]
if (typeof filename !== 'string' || !filename) {
quit(new Error('filename required'))
}
var jsonObj = new JsonObj()
try {
jsonObj.set(require(filename))
} catch(err) {}
// https://github.com/fritx/qqlog/blob/dev/bin/qqlog.js#L12
// for no prompt symbols
prompt.start({
message: ' ',
delimiter: ' '
})
askField()
function askField(){
prompt.get(['key'], function(err, res){
if (err) return quit(err)
var key = res['key']
if (!key) return quit()
prompt.get(['value'], function(err, res){
if (err) return quit(err)
var val = res['value']
jsonObj.set(key, val)
// fixme: not a good way to recurse
askField()
})
})
}
function quit(err){
if (err) throw err
fs.writeFileSync(filename, jsonObj.format())
process.exit(0)
}