-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
develop.js
162 lines (142 loc) · 4.07 KB
/
develop.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
/* @flow */
const express = require(`express`)
const graphqlHTTP = require(`express-graphql`)
const request = require(`request`)
const bootstrap = require(`../bootstrap`)
const webpack = require(`webpack`)
const webpackConfig = require(`./webpack.config`)
const rl = require(`readline`)
const parsePath = require(`parse-filepath`)
const _ = require(`lodash`)
const { store } = require(`../redux`)
const copyStaticDirectory = require(`./copy-static-directory`)
const developHtml = require(`./develop-html`)
// Watch the static directory and copy files to public as they're added or
// changed. Wait 10 seconds so copying doesn't interfer with the regular
// bootstrap.
setTimeout(() => {
copyStaticDirectory()
}, 10000)
const rlInterface = rl.createInterface({
input: process.stdin,
output: process.stdout,
})
// Quit immediately on hearing ctrl-c
rlInterface.on(`SIGINT`, () => {
process.exit()
})
async function startServer(program) {
const directory = program.directory
// Start bootstrap process.
await bootstrap(program)
await developHtml(program).catch(err => {
console.log(err)
process.exit(1)
})
const compilerConfig = await webpackConfig(
program,
directory,
`develop`,
program.port
)
const devConfig = compilerConfig.resolve()
const compiler = webpack(devConfig)
const app = express()
app.use(
require(`webpack-hot-middleware`)(compiler, {
log: () => {},
path: `/__webpack_hmr`,
heartbeat: 10 * 1000,
})
)
app.use(
`/___graphql`,
graphqlHTTP({
schema: store.getState().schema,
graphiql: true,
})
)
app.use(express.static(__dirname + `/public`))
app.use(
require(`webpack-dev-middleware`)(compiler, {
noInfo: true,
quiet: true,
publicPath: devConfig.output.publicPath,
})
)
// Set up API proxy.
const { proxy } = store.getState().config
if (proxy) {
const { prefix, url } = proxy
app.use(`${prefix}/*`, (req, res) => {
const proxiedUrl = url + req.originalUrl
req.pipe(request(proxiedUrl)).pipe(res)
})
}
// Render an HTML page and serve it.
app.use((req, res, next) => {
const parsedPath = parsePath(req.originalUrl)
if (parsedPath.extname === `` || parsedPath.extname === `.html`) {
res.sendFile(`${program.directory}/public/index.html`, err => {
if (err) {
res.status(500).end()
}
})
} else {
next()
}
})
// As last step, check if the file exists in the public folder.
app.get(`*`, (req, res) => {
// Load file but ignore errors.
res.sendFile(`${program.directory}/public/${req.url}`, err => {
if (err) {
res.status(404).end()
}
})
})
const listener = app.listen(program.port, program.host, e => {
if (e) {
if (e.code === `EADDRINUSE`) {
// eslint-disable-next-line max-len
console.log(
`Unable to start Gatsby on port ${program.port} as there's already a process listing on that port.`
)
} else {
console.log(e)
}
process.exit()
} else {
if (program.open) {
const host =
listener.address().address === `127.0.0.1`
? `localhost`
: listener.address().address
const opn = require(`opn`)
opn(`http://${host}:${listener.address().port}`)
}
}
})
}
module.exports = (program: any) => {
const detect = require(`detect-port`)
const port =
typeof program.port === `string` ? parseInt(program.port, 10) : program.port
detect(port, (err, _port) => {
if (err) {
console.error(err)
process.exit()
}
if (port !== _port) {
// eslint-disable-next-line max-len
const question = `Something is already running at port ${port} \nWould you like to run the app at another port instead? [Y/n] `
return rlInterface.question(question, answer => {
if (answer.length === 0 || answer.match(/^yes|y$/i)) {
program.port = _port // eslint-disable-line no-param-reassign
}
return startServer(program)
})
}
return startServer(program)
})
}