-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-directory-index-command.js
executable file
·71 lines (55 loc) · 1.33 KB
/
create-directory-index-command.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
#!/usr/local/bin/node
const isSSH = require('./looks-like-ssh-path')
const createSSHOptions = require('./create-ssh-options')
let argv = require('minimist')(process.argv.slice(2), {
boolean: ['f', 'formatted', 's']
});
if(argv.help) {
console.error(`
Creates an index for a directory. The command has the format:
create-directory-index <options> <directory_path>
Options:
-f
--formatted Format JSON to be more human readable
-s stream results Produces a JSON stream instead of a single JSON object
These streams should be save with the extension .ndjson or .jsonl
`)
return
}
if(argv._.length == 0) {
console.log('Must specify a directory to index.')
console.log(JSON.stringify(argv))
return
}
let formatted = false
if(argv.f || argv.formatted) {
formatted = true
}
const path = require('path')
let createIndex = require('./index')
let fullPath = argv._[0]
let options = {
directoryPath: fullPath,
fullPath: fullPath
}
if(isSSH(fullPath)) {
options = createSSHOptions(fullPath)
}
if(argv.s) {
options.outputStream = process.stdout
}
createIndex(options.directoryPath, options).then(data => {
if(options.outputStream) {
options.outputStream.end()
}
else {
if(formatted) {
console.log(JSON.stringify(data, null, '\t'))
}
else {
console.log(JSON.stringify(data))
}
}
}).catch(error => {
console.error(error)
})