1
1
mirror of https://github.com/c8r/x0.git synced 2024-09-11 13:45:52 +03:00
x0/cli.js

166 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-05-20 00:06:12 +03:00
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const meow = require('meow')
const findup = require('find-up')
const readPkg = require('read-pkg-up').sync
const openBrowser = require('react-dev-utils/openBrowser')
2018-05-20 22:45:56 +03:00
const log = require('@compositor/log')
2018-05-20 00:06:12 +03:00
const chalk = require('chalk')
const clipboard = require('clipboardy')
const config = require('pkg-conf').sync('x0')
const pkg = readPkg().pkg
2018-05-20 22:45:56 +03:00
log.name = 'x0'
2018-05-20 00:06:12 +03:00
const cli = meow(`
2018-05-26 18:26:09 +03:00
${chalk.gray('Usage')}
2018-05-20 00:06:12 +03:00
2018-05-26 18:26:09 +03:00
${chalk.gray('Dev Server')}
2018-05-20 00:06:12 +03:00
2018-05-26 18:26:09 +03:00
${chalk.cyan('x0 pages')}
2018-05-20 00:06:12 +03:00
2018-05-26 18:26:09 +03:00
${chalk.gray('Build')}
2018-05-20 00:06:12 +03:00
2018-05-26 18:26:09 +03:00
${chalk.cyan('x0 build pages')}
2018-05-20 00:06:12 +03:00
2018-05-26 18:26:09 +03:00
${chalk.gray('Options')}
2018-05-20 00:06:12 +03:00
--webpack Path to webpack config file
2018-06-19 18:41:18 +03:00
--match String to match routes against using minimatch
2018-05-20 00:06:12 +03:00
2018-05-26 18:26:09 +03:00
${chalk.gray('Dev Server')}
2018-05-20 00:06:12 +03:00
-o --open Open dev server in default browser
-p --port Port for dev server
2018-06-24 01:26:36 +03:00
--analyze Runs with webpack-bundle-analyzer plugin
2018-05-20 00:06:12 +03:00
2018-05-26 18:26:09 +03:00
${chalk.gray('Build')}
2018-05-20 00:06:12 +03:00
-d --out-dir Output directory (default dist)
-s --static Output static HTML without JS bundle
-t --template Path to custom HTML template
`, {
flags: {
// dev
open: {
type: 'boolean',
alias: 'o'
},
port: {
type: 'string',
alias: 'p'
},
2018-06-24 01:26:36 +03:00
analyze: {},
2018-05-20 00:06:12 +03:00
// build
outDir: {
type: 'string',
alias: 'd'
},
static: {
type: 'boolean',
},
template: {
type: 'string',
alias: 't'
},
// shared
config: {
type: 'string',
alias: 'c'
},
2018-06-19 18:41:18 +03:00
match: {
type: 'string'
},
2018-05-20 00:06:12 +03:00
scope: {
type: 'string',
},
webpack: {
type: 'string',
},
2018-05-22 06:58:57 +03:00
debug: {
type: 'boolean'
}
2018-05-20 00:06:12 +03:00
}
})
const [ cmd, file ] = cli.input
2018-06-23 01:50:15 +03:00
if (!cmd) {
cli.showHelp(0)
}
2018-05-20 00:06:12 +03:00
const input = path.resolve(file || cmd)
const stats = fs.statSync(input)
const dirname = stats.isDirectory() ? input : path.dirname(input)
const filename = stats.isDirectory() ? null : input
const opts = Object.assign({
input,
dirname,
filename,
stats,
outDir: 'dist',
2018-05-22 07:37:19 +03:00
basename: '',
2018-05-20 00:06:12 +03:00
scope: {},
pkg,
}, config, cli.flags)
opts.outDir = path.resolve(opts.outDir)
if (opts.config) opts.config = path.resolve(opts.config)
if (opts.webpack) {
opts.webpack = require(path.resolve(opts.webpack))
} else {
const webpackConfig = findup.sync('webpack.config.js', { cwd: dirname })
if (webpackConfig) opts.webpack = require(webpackConfig)
}
if (opts.template) {
opts.template = require(path.resolve(opts.template))
}
const handleError = err => {
log.error(err)
process.exit(1)
}
2018-05-23 01:04:14 +03:00
log(chalk.cyan('@compositor/x0'))
2018-05-21 00:23:40 +03:00
2018-05-20 00:06:12 +03:00
switch (cmd) {
case 'build':
2018-05-20 22:45:56 +03:00
log.start('building static site')
2018-06-19 20:14:54 +03:00
const build = require('./lib/build')
2018-05-20 00:06:12 +03:00
build(opts)
.then(res => {
2018-05-20 22:45:56 +03:00
log.stop('site saved to ' + opts.outDir)
2018-05-20 00:06:12 +03:00
})
.catch(handleError)
break
case 'dev':
default:
2018-05-20 22:45:56 +03:00
log.start('starting dev server')
2018-06-19 20:14:54 +03:00
const dev = require('./lib/dev')
2018-05-20 00:06:12 +03:00
dev(opts)
2018-05-25 17:32:58 +03:00
.then(({ server }) => {
const { port } = server.options
2018-05-20 00:06:12 +03:00
const url = `http://localhost:${port}`
2018-05-20 22:45:56 +03:00
log.stop(
2018-05-20 00:06:12 +03:00
'dev server listening on',
chalk.green(url),
chalk.gray('(copied to clipboard)')
)
clipboard.write(url)
if (opts.open) {
openBrowser(url)
}
})
.catch(handleError)
break
}
require('update-notifier')({
pkg: require('./package.json')
}).notify()