-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·77 lines (67 loc) · 2.35 KB
/
index.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
#!/usr/bin/env node
const { getLogger } = require(__dirname + '/utils/utils.logger.js')
const { getChapters, renderChapter, getNCXFile, getRootFile, getTitle } = require('./utils/utils')
const { getDBForMetaContent } = require('./utils/utils.meta.js')
const runCliCommands = require('./ui/runCliCommands')
const argumentParser = require('./utils/argumentParser')
const showHelp = require('./utils/utils.showhelp')
const UI = require('./ui/ui')
const logger = getLogger('debug')
const { filePath, flags } = argumentParser(process.argv)
if (flags.help) {
showHelp()
process.exit(0)
}
if (flags.version || flags.Version) {
console.log(`version: ${require(__dirname + '/package.json').version}`)
process.exit(0)
}
if (!filePath) {
console.warn('please specify a file path')
return process.exit(1)
}
let contentFolder = null
getRootFile(filePath)
.then((mainFileInfo) => {
contentFolder = mainFileInfo.folder
return getNCXFile(filePath, mainFileInfo.filePath)
})
.then(chpFile => {
return new Promise(async (resolve, reject) => {
try {
// location is specified relative to the location of the main file (opf) was found
const chapterList = await getChapters(filePath, `${contentFolder}/${chpFile}`)
const title = await getTitle(filePath, `${contentFolder}/${chpFile}`)
resolve({ chapterList, title})
} catch (ex) {
reject(ex)
}
})
}).then(({chapterList, title}) => {
logger.debug(`chapters: ${JSON.stringify(chapterList, null, 4)}`)
// check the flags and execute any cli commands that may be specified by the flags
runCliCommands(flags, filePath, chapterList, contentFolder, async (err, handled) => {
if (err) {
// we specified a command but could not fulfill it
console.error(err)
process.exit(1)
} else if (handled) {
// we specified and handled a cli command nothing else to do
process.exit(0)
} else {
// we are not performing a cli command, launch the ncurses interface
let db = null
try {
db = await getDBForMetaContent()
} catch(ex) {
console.error(ex)
return process.exit(1)
}
const ui = new UI(title, filePath, chapterList, contentFolder, db)
ui.on('close', () => process.exit(0))
}
})
}).catch(err => {
console.error(err)
process.exit(1)
})