1
0
mirror of https://github.com/comatory/fb2iCal synced 2024-12-11 16:16:14 +01:00
Facebook-Events-iCal-Converter/lib/cli.js
2020-07-19 08:14:34 +02:00

48 lines
1.2 KiB
JavaScript
Executable File

#!/usr/bin/env node
const parseUsingLDJSONData = require('./services/ldjson-parser')
const parseUsingDOM = require('./services/dom-parser')
const { createParserError, createAppLogger } = require('./utils')
const parseHTMLString = (html, { verbose }) => {
const logger = verbose ?
createAppLogger({ dev: true }) :
null
try {
const LDJSONEventData = parseUsingLDJSONData(html, { logger })
const eventData = LDJSONEventData || parseUsingDOM(html, { logger })
if (!eventData) {
throw createParserError()
return
}
process.stdout.write(JSON.stringify(eventData))
} catch (err) {
console.error(err)
process.stderr.write(err.toString())
process.exit(1)
}
}
const argv = require('yargs')
.command('$0 <html> [options]', 'Parse Facebook event HTML page into JSON data.', (yargs) => {
yargs.positional('html', {
describe: 'valid HTML string',
type: 'string',
})
}, (argv) => {
const { html, verbose } = argv
parseHTMLString(html, { verbose })
})
.option('verbose', {
alias: 'v',
description: 'Add logging to output.',
type: 'boolean',
})
.alias('h', 'help')
.help('h')
.argv