1
1
mirror of https://github.com/jxnblk/mdx-deck.git synced 2024-11-30 00:44:20 +03:00
mdx-deck/cli.js

184 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-07-28 21:21:36 +03:00
#!/usr/bin/env node
2018-07-29 03:32:51 +03:00
const path = require('path')
2018-07-29 01:32:05 +03:00
const meow = require('meow')
2018-07-28 21:21:36 +03:00
const open = require('react-dev-utils/openBrowser')
2018-07-29 01:32:05 +03:00
const chalk = require('chalk')
2018-07-29 04:39:45 +03:00
const ok = require('ok-cli')
2018-08-01 01:24:46 +03:00
const remark = {
2018-08-04 19:56:26 +03:00
emoji: require('remark-emoji'),
unwrapImages: require('remark-unwrap-images')
2018-08-01 01:24:46 +03:00
}
2018-08-01 02:12:55 +03:00
const pkg = require('./package.json')
2018-07-28 21:21:36 +03:00
2018-07-29 01:32:05 +03:00
const config = require('pkg-conf').sync('mdx-deck')
const log = (...args) => {
console.log(
chalk.magenta('[mdx-deck]'),
...args
)
2018-07-28 21:21:36 +03:00
}
2018-07-29 01:32:05 +03:00
log.error = (...args) => {
console.log(
chalk.red('[err]'),
...args
)
}
2018-07-29 03:32:51 +03:00
const getConfig = conf => {
conf.module.rules = [
...conf.module.rules
.filter(rule => !rule.test.test('.mdx')),
{
test: /\.mdx?$/,
exclude: /node_modules/,
use: [
{
loader: require.resolve('babel-loader'),
options: {
presets: [
'babel-preset-env',
'babel-preset-stage-0',
'babel-preset-react',
].map(require.resolve)
}
},
2018-08-01 01:24:46 +03:00
{
loader: require.resolve('./lib/loader.js'),
options: {
mdPlugins: [
2018-08-04 19:56:26 +03:00
remark.emoji,
remark.unwrapImages
2018-08-01 01:24:46 +03:00
]
}
}
2018-07-29 03:32:51 +03:00
]
}
]
2018-07-29 21:38:37 +03:00
2018-07-29 03:32:51 +03:00
return conf
}
2018-07-29 01:32:05 +03:00
const cli = meow(`
2018-07-29 23:19:12 +03:00
${chalk.gray('Usage')}
2018-07-29 01:32:05 +03:00
2018-07-29 23:19:12 +03:00
$ ${chalk.magenta('mdx-deck deck.mdx')}
2018-07-29 01:32:05 +03:00
2018-07-29 23:19:12 +03:00
$ ${chalk.magenta('mdx-deck build deck.mdx')}
2018-08-01 02:12:55 +03:00
$ ${chalk.magenta('mdx-deck pdf deck.mdx')}
2018-07-31 21:44:57 +03:00
2018-07-29 23:19:12 +03:00
${chalk.gray('Options')}
2018-07-29 01:32:05 +03:00
2018-08-01 02:12:55 +03:00
--title Title for the HTML document
2018-07-29 01:32:05 +03:00
2018-08-01 02:12:55 +03:00
${chalk.gray('Dev server options')}
2018-07-29 01:32:05 +03:00
2018-08-01 02:12:55 +03:00
-p --port Dev server port
--no-open Prevent from opening in default browser
2018-07-29 23:33:26 +03:00
2018-08-01 02:12:55 +03:00
${chalk.gray('Build options')}
2018-07-29 23:17:23 +03:00
2018-08-01 02:12:55 +03:00
-d --out-dir Output directory for exporting
${chalk.gray('PDF options')}
--out-file Filename for PDF export
--width Width in pixels
--heigh Height in pixels
2018-07-31 21:44:57 +03:00
2018-07-29 01:32:05 +03:00
`, {
2018-08-01 02:12:55 +03:00
description: chalk.magenta('[mdx-deck] ') + chalk.gray(pkg.description),
2018-07-29 01:32:05 +03:00
flags: {
port: {
type: 'string',
alias: 'p'
},
open: {
type: 'boolean',
alias: 'o',
default: true
2018-07-29 23:17:23 +03:00
},
2018-07-29 23:33:26 +03:00
outDir: {
type: 'string',
alias: 'd'
},
2018-07-29 23:17:23 +03:00
title: {
type: 'string'
2018-07-31 21:44:57 +03:00
},
outFile: {
type: 'string',
2018-07-29 01:32:05 +03:00
}
}
})
2018-07-29 04:39:45 +03:00
const [ cmd, file ] = cli.input
const doc = file || cmd
2018-07-29 01:32:05 +03:00
2018-07-29 03:32:51 +03:00
if (!doc) cli.showHelp(0)
2018-07-29 01:32:05 +03:00
const opts = Object.assign({
2018-08-04 18:31:08 +03:00
entry: path.join(__dirname, './dist/entry.js'),
2018-07-29 03:32:51 +03:00
dirname: path.dirname(path.resolve(doc)),
globals: {
DOC_FILENAME: JSON.stringify(path.resolve(doc))
},
2018-07-29 23:17:23 +03:00
config: getConfig,
2018-07-29 23:33:26 +03:00
title: 'mdx-deck',
2018-08-01 02:12:55 +03:00
port: 8080,
outDir: 'dist',
outFile: 'presentation.pdf'
2018-07-29 01:32:05 +03:00
}, config, cli.flags)
2018-07-28 21:21:36 +03:00
2018-07-29 23:33:26 +03:00
opts.outDir = path.resolve(opts.outDir)
2018-07-29 04:39:45 +03:00
switch (cmd) {
case 'build':
2018-07-31 21:44:57 +03:00
log('building')
2018-07-29 04:39:45 +03:00
ok.build(opts)
.then(res => {
log('done')
})
.catch(err => {
log.error(err)
process.exit(1)
})
break
2018-08-01 02:12:55 +03:00
case 'pdf':
log('exporting to PDF')
2018-07-31 21:44:57 +03:00
const pdf = require('./lib/pdf')
ok(opts)
.then(({ server }) => {
log('rendering PDF')
pdf(opts)
.then(filename => {
server.close()
2018-08-01 02:12:55 +03:00
log('done', filename)
2018-07-31 21:44:57 +03:00
process.exit(0)
})
.catch(err => {
log.error(err)
process.exit(1)
})
})
.catch(err => {
log.error(err)
process.exit(1)
})
break
2018-07-29 04:39:45 +03:00
case 'dev':
default:
log('starting dev server')
ok(opts)
.then(res => {
const url = 'http://localhost:' + res.port
2018-07-31 20:19:10 +03:00
if (opts.open) open(url)
2018-07-29 04:39:45 +03:00
log('listening on', chalk.magenta(url))
})
.catch(err => {
log.error(err)
process.exit(1)
})
break
}