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-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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
require.resolve('./lib/loader.js'),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2018-07-29 21:51:32 +03:00
|
|
|
conf.module.rules[1].include.push(
|
|
|
|
path.join(__dirname, './src')
|
|
|
|
)
|
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(`
|
|
|
|
Usage
|
|
|
|
|
|
|
|
$ mdx-deck deck.mdx
|
|
|
|
|
|
|
|
$ mdx-deck build deck.mdx
|
|
|
|
|
|
|
|
Options
|
|
|
|
|
|
|
|
-p --port Dev server port
|
|
|
|
|
|
|
|
--no-open Prevent from opening in default browser
|
|
|
|
|
|
|
|
`, {
|
|
|
|
flags: {
|
|
|
|
port: {
|
|
|
|
type: 'string',
|
|
|
|
alias: 'p'
|
|
|
|
},
|
|
|
|
open: {
|
|
|
|
type: 'boolean',
|
|
|
|
alias: 'o',
|
|
|
|
default: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
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-07-29 21:51:32 +03:00
|
|
|
entry: path.join(__dirname, './src/entry.js'),
|
2018-07-29 03:32:51 +03:00
|
|
|
dirname: path.dirname(path.resolve(doc)),
|
|
|
|
globals: {
|
|
|
|
DOC_FILENAME: JSON.stringify(path.resolve(doc))
|
|
|
|
},
|
|
|
|
config: getConfig
|
2018-07-29 01:32:05 +03:00
|
|
|
}, config, cli.flags)
|
2018-07-28 21:21:36 +03:00
|
|
|
|
2018-07-29 04:39:45 +03:00
|
|
|
switch (cmd) {
|
|
|
|
case 'build':
|
|
|
|
log('exporting')
|
|
|
|
ok.build(opts)
|
|
|
|
.then(res => {
|
|
|
|
log('done')
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
log.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
break
|
|
|
|
case 'dev':
|
|
|
|
default:
|
|
|
|
log('starting dev server')
|
|
|
|
ok(opts)
|
|
|
|
.then(res => {
|
|
|
|
const url = 'http://localhost:' + res.port
|
|
|
|
open(url)
|
|
|
|
log('listening on', chalk.magenta(url))
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
log.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
break
|
|
|
|
}
|