2014-04-18 22:24:20 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2018-06-08 22:20:29 +00:00
|
|
|
const fs = require('fs-extra')
|
2017-12-07 13:04:16 +00:00
|
|
|
const path = require('path')
|
2018-06-07 22:49:03 +00:00
|
|
|
const Analytics = require('./analytics')
|
|
|
|
const dependencies = require('./dependencies')
|
|
|
|
const messages = require('./messages')
|
|
|
|
const options = require('./options')
|
2014-04-18 22:24:20 +00:00
|
|
|
|
2017-03-06 12:46:46 +00:00
|
|
|
console.log('')
|
2016-11-03 02:17:31 +00:00
|
|
|
|
2017-12-07 13:04:16 +00:00
|
|
|
// Read all options from the command-line / config file
|
2017-12-22 11:28:37 +00:00
|
|
|
const args = process.argv.slice(2)
|
|
|
|
const opts = options.get(args)
|
2016-11-03 02:17:31 +00:00
|
|
|
|
2017-12-07 13:06:35 +00:00
|
|
|
// Only require the index after logging options have been set
|
2018-06-08 22:20:29 +00:00
|
|
|
fs.mkdirpSync(opts.output)
|
|
|
|
require('./log').init(opts.log, opts.output)
|
2017-12-07 13:06:35 +00:00
|
|
|
const index = require('../src/index')
|
|
|
|
|
2017-12-07 13:04:16 +00:00
|
|
|
// 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`)
|
|
|
|
}
|
2017-05-25 11:43:03 +00:00
|
|
|
|
2017-12-07 13:04:16 +00:00
|
|
|
// Basic usage report (anonymous statistics)
|
|
|
|
const analytics = new Analytics({
|
2017-12-11 10:56:51 +00:00
|
|
|
enabled: opts['usageStats']
|
2017-12-07 13:04:16 +00:00
|
|
|
})
|
|
|
|
analytics.start()
|
|
|
|
|
|
|
|
// Catch all exceptions and exit gracefully
|
|
|
|
process.on('uncaughtException', handleError)
|
2018-06-08 22:20:29 +00:00
|
|
|
process.on('unhandledRejection', handleError)
|
2017-12-07 13:04:16 +00:00
|
|
|
|
2018-05-07 21:36:11 +00:00
|
|
|
// Check that all binary dependencies are present
|
2018-06-07 22:49:03 +00:00
|
|
|
dependencies.checkOptional()
|
|
|
|
const missingErrors = dependencies.checkRequired()
|
2018-05-07 21:36:11 +00:00
|
|
|
if (missingErrors) {
|
2018-06-07 22:49:03 +00:00
|
|
|
console.log(`${missingErrors}`)
|
2018-05-07 21:36:11 +00:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
2017-12-07 13:04:16 +00:00
|
|
|
// Build the gallery!
|
2018-06-08 22:20:29 +00:00
|
|
|
index.build(opts, (err, result) => {
|
|
|
|
console.log('')
|
2017-12-07 13:04:16 +00:00
|
|
|
if (err) {
|
|
|
|
handleError(err)
|
|
|
|
} else {
|
2018-06-08 22:20:29 +00:00
|
|
|
// Print any problems
|
|
|
|
result.problems.print()
|
|
|
|
// And then a summary of the gallery
|
2017-12-07 13:04:16 +00:00
|
|
|
const stats = {
|
2018-06-08 22:20:29 +00:00
|
|
|
albums: countAlbums(0, result.album),
|
|
|
|
photos: result.album.stats.photos,
|
|
|
|
videos: result.album.stats.videos
|
2014-06-10 02:54:56 +00:00
|
|
|
}
|
2017-12-07 13:04:16 +00:00
|
|
|
analytics.finish(stats)
|
2018-06-08 22:20:29 +00:00
|
|
|
console.log(messages.SUCCESS(stats) + '\n')
|
2017-12-07 13:04:16 +00:00
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
})
|
2017-03-08 10:59:19 +00:00
|
|
|
|
2017-12-07 13:04:16 +00:00
|
|
|
// 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
|
2018-07-04 21:41:57 +00:00
|
|
|
require('debug')('thumbsup:error')(err)
|
|
|
|
console.error('\nUnexpected error', err.message)
|
2017-12-07 13:04:16 +00:00
|
|
|
console.error(`\n${messages.SORRY()}\n`)
|
|
|
|
exit(1)
|
2017-05-25 11:43:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 13:04:16 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2017-05-25 11:43:03 +00:00
|
|
|
|
2017-12-07 13:04:16 +00:00
|
|
|
// Cound the total number of nested albums
|
|
|
|
function countAlbums (total, album) {
|
|
|
|
return 1 + album.albums.reduce(countAlbums, total)
|
|
|
|
}
|