forked from c8r/x0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·166 lines (143 loc) · 3.46 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/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')
const log = require('@compositor/log')
const chalk = require('chalk')
const clipboard = require('clipboardy')
const config = require('pkg-conf').sync('x0')
const pkg = readPkg().pkg
log.name = 'x0'
const cli = meow(`
${chalk.gray('Usage')}
${chalk.gray('Dev Server')}
${chalk.cyan('x0 pages')}
${chalk.gray('Build')}
${chalk.cyan('x0 build pages')}
${chalk.gray('Options')}
--webpack Path to webpack config file
--match String to match routes against using minimatch
${chalk.gray('Dev Server')}
-o --open Open dev server in default browser
-p --port Port for dev server
--analyze Runs with webpack-bundle-analyzer plugin
${chalk.gray('Build')}
-d --out-dir Output directory (default dist)
-s --static Output static HTML without JS bundle
-t --template Path to custom HTML template
--basename Basename for URL paths
--title Page title
`, {
flags: {
// dev
open: {
type: 'boolean',
alias: 'o'
},
port: {
type: 'string',
alias: 'p'
},
analyze: {},
// build
outDir: {
type: 'string',
alias: 'd'
},
static: {
type: 'boolean',
},
template: {
type: 'string',
alias: 't'
},
// shared
config: {
type: 'string',
alias: 'c'
},
match: {
type: 'string'
},
scope: {
type: 'string',
},
webpack: {
type: 'string',
},
debug: {
type: 'boolean'
}
}
})
const [ cmd, file ] = cli.input
if (!cmd) {
cli.showHelp(0)
}
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',
basename: '',
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)
}
log(chalk.cyan('@compositor/x0'))
switch (cmd) {
case 'build':
log.start('building static site')
const build = require('./lib/build')
build(opts)
.then(res => {
log.stop('site saved to ' + opts.outDir)
})
.catch(handleError)
break
case 'dev':
default:
log.start('starting dev server')
const dev = require('./lib/dev')
dev(opts)
.then(({ server }) => {
const { port } = server.options
const url = `http://localhost:${port}`
log.stop(
'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()