-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
194 lines (167 loc) · 4.73 KB
/
app.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const path = require('path')
const fs = require('fs')
const os = require('os')
const child_process = require('child_process')
const config = require('./config')
const recdir = require('./recdir')
const pjson = require('./package.json')
const MSG = require('./showMessage')
const Utils = require('./utils')
// searchers
const fuzzy = require('./searchers/fuzzy')
const simple = require('./searchers/simple')
const lunr = require('./searchers/lunr')
const { COPYFILE_EXCL } = fs.constants
// presentation styles
// const pTerm = require('./presentations/term')
const pBlessed = require('./presentations/blessed')
const subCmdTable = {
fuzzy,
f: fuzzy,
simple,
s: simple,
lunr,
l: lunr,
default: lunr
}
const bookformats = (() => {
return (
!config.bookformats ||
typeof config.bookformats !== 'array' ||
config.bookformats.length === 0
) ? ["pdf", "epub", "mobi"] :
config.bookformats
})()
function isBook (fileName) {
const splits = fileName.split('.')
const fileFormat = splits[splits.length - 1]
return bookformats.indexOf(fileFormat) !== -1
}
function runApp () {
const ARGS = parseArgs()
if (!ARGS.subCmd || ARGS.subCmd === '-h' || ARGS.subCmd === '--help') {
return showHelp()
}
if (ARGS.subCmd === '-v' || ARGS.subCmd === '--version') {
return MSG.show(MSG.NAME.VERSION, { version: pjson.version })
}
if (ARGS.subCmd === 'collect' || ARGS.subCmd === 'c') {
return moveBooksFromTo()
}
const booklist = createList()
// const filtered = fuzzy.search(searchTerm, booklist);
// const filtered = simple.search(searchTerm, booklist);
// const filtered = lunr.search(searchTerm, booklist);
let filtered
if (subCmdTable[ARGS.subCmd]) {
filtered = subCmdTable[ARGS.subCmd].search(ARGS.searchTerm, booklist)
} else {
const searchTerm = ARGS.subCmd
filtered = subCmdTable['simple'].search(searchTerm, booklist)
const filteredLunr = subCmdTable['lunr'].search(searchTerm, booklist)
filtered = mergeResult(filteredLunr, filtered)
}
// pTerm.p(filtered)
pBlessed.p(filtered)
}
function showHelp () {
MSG.show(MSG.NAME.HELP, { userConfigFilePath: config.userConfigFilePath })
}
function parseArgs () {
// command line args
const subCmd = process.argv[2]
const args = process.argv.splice(3);
const searchTerm = args.join(' ') || ''
return {
subCmd,
searchTerm
}
}
function createList () {
const booklist = []
const booksdir = config.booksdir
checkBooksdir() || process.exit(1)
try {
recdir(booksdir, function (fileName) {
if (isBook(fileName)) {
booklist.push({
name: path.basename(fileName),
path: `file://${fileName}`
})
}
})
} catch (e) {
console.log(e)
// Exit directly
process.exit(1)
}
return booklist
}
function mergeResult (listA, listB) {
const existsInA = {}
for (let e of listA) {
existsInA[e.url] = true
}
const result = listA.slice()
for (let e of listB) {
if (!existsInA[e.url]) {
result.push(e)
}
}
return result
}
function moveBooksFromTo () {
const { collectfrom, booksdir } = config
checkBooksdir() || process.exit(1)
checkCollectfrom() || process.exit(1)
for (let i = 0; i < collectfrom.length; i += 1) {
try {
recdir(collectfrom[i], function (fileName) {
if (isBook(fileName)) {
try {
fs.copyFileSync(fileName, path.join(booksdir, path.basename(fileName)), COPYFILE_EXCL)
child_process.execSync(`rm "${fileName}"`)
} catch (e) {
// console.log(e)
MSG.show(MSG.NAME.MOVE_BOOK_FAIL, { fileName })
return
}
// child_process.execSync(`yes n |mv -i "${fileName}" "${booksdir}"`)
MSG.show(MSG.NAME.MOVE_BOOK, { fileName })
}
})
} catch (e) {
console.log(e)
// Exit directly
process.exit(1)
}
}
}
function checkBooksdir () {
if (!config.booksdir) {
MSG.show(MSG.NAME.USER_CONF_INVALID, { userConfigFilePath: config.userConfigFilePath })
return false
}
if (!Utils.isDir(config.booksdir)) {
MSG.show(MSG.NAME.BOOKSDIR_NOT_EXIST, { booksdir: config.booksdir, userConfigFilePath: config.userConfigFilePath })
return false
}
return true
}
function checkCollectfrom () {
if (!config.collectfrom || !(config.collectfrom instanceof Array)) {
MSG.show(MSG.NAME.USER_CONF_COLLECTFROM_INVALIED, { userConfigFilePath: config.userConfigFilePath })
return false
}
const cf = config.collectfrom
for (let i = 0; i < cf.length; i += 1) {
if (!Utils.isDir(cf[i])) {
MSG.show(MSG.NAME.COLLECTFROM_NOT_EXIST, { collectfromDir: cf[i], userConfigFilePath: config.userConfigFilePath })
return false
}
}
return true
}
module.exports = {
run: runApp
}