Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
983 additions
and
667 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ | |
/yarn.lock | ||
/public/js/xepub.min.js | ||
|
||
/app/*.js | ||
/app/*.d.ts | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
/.gitignore | ||
/webpack.config.js | ||
/yarn.lock | ||
/app/*.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,41 @@ | ||
'use strict'; | ||
|
||
require('colors'); | ||
import 'colors'; | ||
|
||
const flags = { | ||
unstable: false, | ||
broken: false, | ||
debug: false, | ||
} | ||
|
||
const error = (message = '') => { | ||
export const error = (message = '') => { | ||
console.error('[ERRO]'.red, message); | ||
} | ||
const warn = (message = '') => { | ||
export const warn = (message = '') => { | ||
console.warn('[WARN]'.yellow, message); | ||
} | ||
const info = (message = '') => { | ||
export const info = (message = '') => { | ||
console.log('[INFO]'.white, message); | ||
} | ||
const debug = (message = '') => { | ||
export const debug = (message = '') => { | ||
if (flags.debug) { | ||
console.log('[DBUG]'.green, message); | ||
} | ||
} | ||
const unstable = () => { | ||
export const unstable = () => { | ||
if (!flags.unstable) { | ||
warn('This book has some problems while parsing...'); | ||
warn('I can not ensure whether xepub can deal it properly or not.'); | ||
flags.unstable = true; | ||
} | ||
} | ||
const broken = () => { | ||
export const broken = () => { | ||
if (!flags.broken) { | ||
error('This book is almost broken!!!'); | ||
error('Xepub may crash.'); | ||
flags.broken = true; | ||
} | ||
} | ||
const debugMode = () => { | ||
export const debugMode = () => { | ||
flags.debug = true; | ||
} | ||
|
||
module.exports = { | ||
error, warn, info, unstable, broken, debug, debugMode | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { promises as fsp, existsSync } from 'fs'; | ||
import opn from 'opn'; | ||
import pug from 'pug'; | ||
import * as path from 'path'; | ||
import * as http from 'http'; | ||
import * as express from 'express'; | ||
import * as alert from './alert'; | ||
import * as packages from '../package.json'; | ||
import { spawn } from 'child_process'; | ||
import options, { XepubArguments } from './options'; | ||
import parseEpub from './parse-epub'; | ||
import parseFolder from './parse-folder'; | ||
import Zip from './zip'; | ||
import { EpubInfo } from './types'; | ||
import { bindSocket } from './socket'; | ||
import { AddressInfo } from 'net'; | ||
|
||
const here = (...file: string[]) => path.resolve(__dirname, ...file); | ||
|
||
const main = async (option: XepubArguments) => { | ||
if (option.debug) { | ||
alert.debugMode(); | ||
} | ||
|
||
if (option.version) { | ||
alert.info('Xepub v' + packages.version); | ||
alert.info('Node ' + process.version); | ||
process.exit(0); | ||
} | ||
|
||
if (option.help) { | ||
console.log(await fsp.readFile(here('help.txt'), 'utf-8')); | ||
process.exit(0); | ||
} | ||
|
||
const epubname = option._[0]; | ||
if (!epubname) { | ||
alert.error('File name missing'); | ||
process.exit(1); | ||
} | ||
|
||
if (!existsSync(epubname)) { | ||
alert.error('File or folder not found'); | ||
process.exit(1); | ||
} | ||
|
||
const app = express(); | ||
|
||
const lstat = await fsp.lstat(epubname); | ||
let epub: EpubInfo = null; | ||
const zip = new Zip(); | ||
|
||
if (lstat.isDirectory()) { | ||
|
||
// open folder | ||
// since v1.0.0-alpha.9 | ||
|
||
alert.info('Scanning folder...'); | ||
const dirname = path.resolve(process.cwd(), epubname); | ||
const parseResult = await parseFolder(dirname); | ||
epub = parseResult.epub; | ||
alert.debug('Successfully scanned folder'); | ||
|
||
const index = pug.compile(await fsp.readFile(here('folder-index.pug'), 'utf8')); | ||
|
||
app.use('/folder/', (req, res, next) => { | ||
if (req.originalUrl === '/folder/') { | ||
res.header('Content-Type', 'text/html'); | ||
res.end(index({ files: parseResult.files })); | ||
} else { | ||
next(); | ||
} | ||
}) | ||
app.use('/folder', express.static(dirname)); | ||
|
||
} else if (lstat.isFile()) { | ||
|
||
// open epub file | ||
alert.info('Parsing EPUB file...'); | ||
await zip.initialize(epubname); | ||
const mimetype = (await zip.readAsText('mimetype')).unwrapOr(''); | ||
if (mimetype !== 'application/epub+zip') { | ||
alert.error('Not epub file'); | ||
alert.debug('Mimetype: ' + mimetype); | ||
process.exit(1); | ||
} | ||
epub = await parseEpub(zip); | ||
alert.debug('Successfully parsed file'); | ||
app.use(zip.middleware()); | ||
} else { | ||
|
||
alert.error('?'); | ||
alert.error('??'); | ||
alert.error('???'); | ||
alert.error(`What is ${epubname}?`); | ||
} | ||
|
||
// serve public folder | ||
app.use(express.static(here('..', 'public'))); | ||
|
||
const server = http.createServer(app); | ||
bindSocket(server, epub); | ||
|
||
if (option.port === -1) { | ||
option.port = option.electron ? 0 : 23333; | ||
} | ||
server.listen(option.port); | ||
const address = server.address() as AddressInfo; | ||
const url = `http://localhost:${address.port}/`; | ||
alert.info(`Finished, listening on ${url}`); | ||
|
||
const terminate = async (code: number) => { | ||
await zip.clear(); | ||
process.exit(code); | ||
} | ||
|
||
if (option.electron) { | ||
const which = spawn('which', ['electron']); | ||
which.on('close', (code) => { | ||
if (code) { | ||
// not found | ||
alert.error('Electron not found in $PATH'); | ||
alert.error('Please remove -e/--electron flag'); | ||
} else { | ||
alert.info('Opening in electron...'); | ||
const electron = spawn('electron', [ url ]); | ||
electron.on('close', async () => { | ||
alert.info('Electron closed.'); | ||
await terminate(0); | ||
}); | ||
} | ||
}) | ||
} else if (option.open) { | ||
opn(url); | ||
} | ||
|
||
process.on('SIGINT', async () => { | ||
await terminate(0); | ||
}); | ||
|
||
} | ||
|
||
main(options(process.argv.slice(2))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.