-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcli.js
executable file
·85 lines (68 loc) · 1.9 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
var program = require('commander')
var yuno = require('.')
var exists = require('path-exists').sync
var JSONStream = require('JSONStream')
var fs = require('fs')
var _path = require('path')
program
.version(require('./package.json').version)
program
.command('create <path> <input>')
.description('create a database from JSON objects')
.option('-o, --opts <file>', 'JSON file containing database options')
.action(function (path, input, options) {
checkFile(input, 'input JSON')
var opts = {}
if (options.opts) {
checkFile(input, 'options JSON')
opts = require(_path.resolve('.', options.opts))
}
console.log('creating database at', path, 'from file', input)
opts.location = path
function load (err, db) {
if (err) throw err
populate(db, input)
}
yuno(opts, load)
})
program
.parse(process.argv)
function populate (db, file) {
var json = JSONStream.parse()
var read = fs.createReadStream(file)
var n = 0
var chunk = []
json.on('data', function (entry) {
chunk.push(entry)
if (chunk.length === 10000) {
var thischunk = chunk
db.add(thischunk, {}, function (err) {
if (err) return console.log(err)
n += 10000
console.log('written:', n)
})
chunk = []
}
})
json.on('end', function () {
if (chunk.length === 0) return
db.add(chunk, {}, function (err) {
if (err) throw console.log(err)
db.index.tellMeAboutMySearchIndex(function (err, info) {
if (err) throw err
console.log('done! added', info.totalDocs, 'docs to index')
})
})
})
read.pipe(json)
}
function checkFile (file, name) {
if (!file) {
console.log('ERROR: you must provide an', name)
process.exit(1)
} else if (/^input/.test(name) && !exists(file)) {
console.log('ERROR:', name, "file doesn't exist at path", file)
process.exit(1)
}
}