Skip to content

Commit

Permalink
refactor: use top-level await natively
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatWizard committed Oct 10, 2021
1 parent ce31efd commit 8b6835c
Showing 1 changed file with 47 additions and 56 deletions.
103 changes: 47 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
import clear from 'clear'

import build from './lib/build'
import getFormats from './lib/get-formats'
import { installKindlegen } from './lib/kindlegen'
Expand All @@ -8,61 +9,51 @@ import { printTitle, info, success, error } from './lib/message'
import { getOptions, printVersion, printHelp } from './lib/options'
import readConfig from './lib/read-config'

const { log, trace } = console
const { exit } = process

const settings = { kindlegen: false }

;(async () => {
let options = getOptions()

if (options.help) {
printHelp()
return
}

if (options.version) {
printVersion()
return
}

clear()
printTitle()

try {
settings.kindlegen = await installKindlegen(options.mobi)
} catch (e) {
settings.kindlegen = false
}

let config = await readConfig()
info(`Book detected: "${config.filename}"`)

let { formats } = await getFormats(settings, options)

let builds = []
for (let format of formats) {
let currentConfig = mergeConfig(config, format)
builds.push(
new Promise(function (resolve, reject) {
return build(currentConfig)
.then(() => {
success(`Bounded "${format}" file`)
resolve()
})
.catch((e) => {
error(`Error with the format "${format}"`)
reject(e)
})
}),
)
}

await Promise.all(builds)
log('✨ Done.')
})
.call(this)
.catch((e) => {
trace(e)
exit(1)
})
let options = getOptions()

if (options.help) {
printHelp()
return
}

if (options.version) {
printVersion()
return
}

clear()
printTitle()

try {
settings.kindlegen = await installKindlegen(options.mobi)
} catch (e) {
settings.kindlegen = false
}

let config = await readConfig()
info(`Book detected: "${config.filename}"`)

let { formats } = await getFormats(settings, options)

let builds = []
for (let format of formats) {
let currentConfig = mergeConfig(config, format)
builds.push(
new Promise(function (resolve, reject) {
return build(currentConfig)
.then(() => {
success(`Bounded "${format}" file`)
resolve()
})
.catch((e) => {
error(`Error with the format "${format}"`)
reject(e)
})
}),
)
}

await Promise.all(builds)
console.log('✨ Done.')

0 comments on commit 8b6835c

Please sign in to comment.