You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
thumbsup/bin/thumbsup.js

88 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
const Analytics = require('./analytics')
const fs = require('fs')
const messages = require('./messages')
const path = require('path')
const options = require('./options')
const checks = require('./checks')
const tty = require('tty')
console.log('')
// Read all options from the command-line / config file
const args = process.argv.slice(2)
const opts = options.get(args)
// If stdout is non TTY, make sure there is at least some text logging on stderr
if (!opts.log && tty.isatty(process.stdout.fd) === false) {
opts.log = 'info'
}
// Only require the index after logging options have been set
require('./log').init(opts.log)
const index = require('../src/index')
// If this is the first run, display a welcome message
const indexPath = path.join(opts.output, 'thumbsup.db')
const firstRun = fs.existsSync(indexPath) === false
if (firstRun) {
console.log(`${messages.GREETING()}\n`)
}
// Basic usage report (anonymous statistics)
const analytics = new Analytics({
enabled: opts['usageStats']
})
analytics.start()
// Catch all exceptions and exit gracefully
process.on('uncaughtException', handleError)
// Check that all binary dependencies are present
const missingErrors = checks.verify()
if (missingErrors) {
console.log(`${missingErrors}\n`)
exit(1)
}
// Build the gallery!
index.build(opts, (err, album) => {
if (err) {
handleError(err)
} else {
const stats = {
albums: countAlbums(0, album),
photos: album.stats.photos,
videos: album.stats.videos
}
analytics.finish(stats)
const message = messages.SUCCESS(stats)
console.log(`\n${message}\n`)
exit(0)
}
})
// Print an error report and exit
// Note: remove "err.context" (entire data model) which can make the output hard to read
function handleError (err) {
analytics.error()
delete err.context
console.error('\nUnexpected error', err)
console.error(`\n${messages.SORRY()}\n`)
exit(1)
}
// Force a successful or failed exit
// This is required
// - because capturing unhandled errors will make Listr run forever
// - to ensure pending Analytics HTTP requests don't keep the tool running
function exit (code) {
// just some time to ensure analytics has time to fire
setTimeout(() => process.exit(code), 10)
}
// Cound the total number of nested albums
function countAlbums (total, album) {
return 1 + album.albums.reduce(countAlbums, total)
}