-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·96 lines (83 loc) · 1.97 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
#!/usr/bin/env node
// Original source: https://raw.githubusercontent.com/jxnblk/mdx-deck/master/packages/mdx-deck/cli.js
const path = require('path')
const meow = require('meow')
const execa = require('execa')
const chalk = require('chalk')
const fs = require('fs-extra')
const pkg = require('./package.json')
const log = (...args) => {
console.log(chalk.green('[zoetic]'), ...args)
}
log.error = (...args) => {
console.log(chalk.red('[err]'), ...args)
}
const cli = meow(
`
${chalk.gray('Usage')}
$ ${chalk.green('zoetic deck.mdx')}
$ ${chalk.green('zoetic build deck.mdx')}
${chalk.gray('Options')}
-h --host Dev server host
-p --port Dev server port
--no-open Prevent from opening in default browser
`,
{
description: chalk.green('@zoetic/zoetic ') + chalk.gray(pkg.description),
flags: {
port: {
type: 'string',
alias: 'p',
default: '8000',
},
host: {
type: 'string',
alias: 'h',
default: 'localhost',
},
open: {
type: 'boolean',
alias: 'o',
default: true,
},
},
}
)
const [cmd, file] = cli.input
const filename = file || cmd
if (!filename) cli.showHelp(0)
process.env.__SRC__ = path.resolve(filename)
const opts = Object.assign({}, cli.flags)
const gatsby = async (...args) => {
await execa('gatsby', ['clean'], {
cwd: __dirname,
stdio: 'inherit',
preferLocal: true,
})
return execa('gatsby', args.filter(Boolean), {
cwd: __dirname,
stdio: 'inherit',
preferLocal: true,
})
}
switch (cmd) {
case 'build':
gatsby('build').then(() => {
const public_ = path.join(__dirname, 'public')
const dist = path.join(process.cwd(), 'public')
if (public_ === dist) return
fs.copySync(public_, dist)
})
break
case 'dev':
default:
gatsby(
'develop',
'--host',
opts.host,
'--port',
opts.port,
opts.open && '--open'
)
break
}