forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
102 lines (89 loc) · 2.67 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
const program = require('commander')
const packageJson = require('../../package.json')
const fs = require('fs-extra')
const path = require('path')
const _ = require('lodash')
// Copy our load-context function to root of site in a dot file.
const gatsbyFile = `${__dirname}/../utils/load-context.js`
const siteDirectory = path.resolve('.')
const fileName = `${siteDirectory}/.gatsby-context.js`
fs.copy(gatsbyFile, fileName)
const defaultHost = process.platform === 'win32'
? 'localhost'
: '0.0.0.0'
const directory = path.resolve('.')
program
.version(packageJson.version)
.usage('[command] [options]')
program.command('develop')
.description('Start development server. Watches files and rebuilds and hot reloads if something changes') // eslint-disable-line max-len
.option('-H, --host <url>',
`Set host. Defaults to ${defaultHost}`,
defaultHost,
)
.option('-p, --port <port>', 'Set port. Defaults to 8000', '8000')
.option('-o, --open', 'Open the site in your browser for you.')
.action((command) => {
const develop = require('../utils/develop')
const p = {
...command,
directory,
}
develop(p)
})
program.command('build')
.description('Build a Gatsby project.')
.option('--prefix-links', 'Build site with links prefixed (set prefix in your config).')
.action((command) => {
// Set NODE_ENV to 'production'
process.env.NODE_ENV = 'production'
const build = require('../utils/build')
const p = {
...command,
directory,
}
build(p, (err) => {
if (err) {
throw err
} else {
console.log('Done')
}
})
})
program.command('serve-build')
.description('Serve built site.')
.option('-H, --host <url>',
`Set host. Defaults to ${defaultHost}`,
defaultHost,
)
.option('-p, --port <port>', 'Set port. Defaults to 8000', '8000')
.option('-o, --open', 'Open the site in your browser for you.')
.action((command) => {
const serve = require('../utils/serve-build')
const p = {
...command,
directory,
}
serve(p)
})
program
.command('new [rootPath] [starter]')
.description('Create new Gatsby project.')
.action((rootPath, starter) => {
const newCommand = require('../utils/new')
newCommand(rootPath, starter)
})
program.on('--help', () => {
console.log(`To show subcommand help:
gatsby [command] -h
`)
})
// If the user types an unknown sub-command, just display the help.
const subCmd = process.argv.slice(2, 3)[0]
let cmds = _.map(program.commands, '_name')
cmds = cmds.concat(['--version', '-V'])
if (!_.includes(cmds, subCmd)) {
program.help()
} else {
program.parse(process.argv)
}