Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 43121ab

Browse files
authored
Merge pull request #18 from netlify/read-dev-settings
Detect overrides for dev server from [dev] settings
2 parents 58597d2 + 30ade6e commit 43121ab

File tree

2 files changed

+76
-44
lines changed

2 files changed

+76
-44
lines changed

src/commands/dev/index.js

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const { flags } = require('@oclif/command')
2-
const { spawn } = require('child_process')
1+
const {flags} = require('@oclif/command')
2+
const {spawn} = require('child_process')
33
const http = require('http')
44
const httpProxy = require('http-proxy')
55
const waitPort = require('wait-port')
66
const getPort = require('get-port')
7-
const { serveFunctions } = require('@netlify/zip-it-and-ship-it')
8-
const { serverSettings } = require('../../detect-server')
7+
const {serveFunctions} = require('@netlify/zip-it-and-ship-it')
8+
const {serverSettings} = require('../../detect-server')
99
const Command = require('@netlify/cli-utils')
10-
const { getAddons } = require('netlify/src/addons')
10+
const {getAddons} = require('netlify/src/addons')
1111

1212
function cleanExit() {
1313
process.exit()
@@ -26,45 +26,47 @@ function addonUrl(addonUrls, req) {
2626
async function startProxy(settings, addonUrls) {
2727
const rulesProxy = require('netlify-rules-proxy')
2828

29-
await waitPort({ port: settings.proxyPort })
29+
await waitPort({port: settings.proxyPort})
3030
if (settings.functionsPort) {
31-
await waitPort({ port: settings.functionsPort })
31+
await waitPort({port: settings.functionsPort})
3232
}
33-
const port = await getPort({ port: settings.port })
34-
const functionsServer = settings.functionsPort ? `http://localhost:${settings.functionsPort}` : null
33+
const port = await getPort({port: settings.port})
34+
const functionsServer = settings.functionsPort ?
35+
`http://localhost:${settings.functionsPort}` :
36+
null
3537

3638
const proxy = httpProxy.createProxyServer({
3739
target: {
3840
host: 'localhost',
39-
port: settings.proxyPort
40-
}
41+
port: settings.proxyPort,
42+
},
4143
})
4244

43-
const rewriter = rulesProxy({ publicFolder: settings.dist })
45+
const rewriter = rulesProxy({publicFolder: settings.dist})
4446

45-
const server = http.createServer(function(req, res) {
47+
const server = http.createServer(function (req, res) {
4648
if (isFunction(settings, req)) {
47-
return proxy.web(req, res, { target: functionsServer })
49+
return proxy.web(req, res, {target: functionsServer})
4850
}
4951
let url = addonUrl(addonUrls, req)
5052
if (url) {
51-
return proxy.web(req, res, { target: url })
53+
return proxy.web(req, res, {target: url})
5254
}
5355

5456
rewriter(req, res, () => {
5557
if (isFunction(settings, req)) {
56-
return proxy.web(req, res, { target: functionsServer })
58+
return proxy.web(req, res, {target: functionsServer})
5759
}
5860
url = addonUrl(addonUrls, req)
5961
if (url) {
60-
return proxy.web(req, res, { target: url })
62+
return proxy.web(req, res, {target: url})
6163
}
6264

63-
proxy.web(req, res, { target: `http://localhost:${settings.proxyPort}` })
65+
proxy.web(req, res, {target: `http://localhost:${settings.proxyPort}`})
6466
})
6567
})
6668

67-
server.on('upgrade', function(req, socket, head) {
69+
server.on('upgrade', function (req, socket, head) {
6870
proxy.ws(req, socket, head)
6971
})
7072

@@ -80,17 +82,17 @@ function startDevServer(settings, log, error) {
8082
name: 'netlify-dev',
8183
port: settings.proxyPort,
8284
templates: {
83-
notFound: '404.html'
84-
}
85+
notFound: '404.html',
86+
},
8587
})
8688

87-
server.start(function() {
89+
server.start(function () {
8890
log('Server listening to', settings.proxyPort)
8991
})
9092
return
9193
}
9294

93-
const ps = spawn(settings.cmd, settings.args, { env: settings.env })
95+
const ps = spawn(settings.cmd, settings.args, {env: settings.env})
9496

9597
ps.stdout.on('data', data => {
9698
log(`${data}`.replace(settings.urlRegexp, `$1$2${settings.port}$3`))
@@ -110,23 +112,26 @@ function startDevServer(settings, log, error) {
110112

111113
class DevCommand extends Command {
112114
async run() {
113-
const { flags, args } = this.parse(DevCommand)
114-
const { api, site, config } = this.netlify
115-
const functionsDir = flags.functions || (config.build && config.build.functions)
115+
const {flags, args} = this.parse(DevCommand)
116+
const {api, site, config} = this.netlify
117+
const functionsDir =
118+
flags.functions || (config.build && config.build.functions)
116119
const addonUrls = {}
117120
if (site.id && !flags.offline) {
118121
const accessToken = await this.authenticate()
119122
const addons = await getAddons(site.id, accessToken)
120123
if (Array.isArray(addons)) {
121124
addons.forEach(addon => {
122-
addonUrls[addon.slug] = `${addon.config.site_url}/.netlify/${addon.slug}`
125+
addonUrls[addon.slug] = `${addon.config.site_url}/.netlify/${
126+
addon.slug
127+
}`
123128
for (const key in addon.env) {
124129
process.env[key] = process.env[key] || addon.env[key]
125130
}
126131
})
127132
}
128133
const api = this.netlify.api
129-
const apiSite = await api.getSite({ site_id: site.id })
134+
const apiSite = await api.getSite({site_id: site.id})
130135
// TODO: We should move the environment outside of build settings and possibly have a
131136
// `/api/v1/sites/:site_id/environment` endpoint for it that we can also gate access to
132137
// In the future and that we could make context dependend
@@ -137,19 +142,19 @@ class DevCommand extends Command {
137142
}
138143
}
139144
process.env.NETLIFY_DEV = 'true'
140-
let settings = serverSettings()
145+
let settings = serverSettings(config.dev)
141146
if (!(settings && settings.cmd)) {
142147
this.log('No dev server detected, using simple static server')
143148
settings = {
144149
noCmd: true,
145150
port: 8888,
146151
proxyPort: 3999,
147-
dist: config.build && config.build.publish
152+
dist: config.build && config.build.publish,
148153
}
149154
}
150155
startDevServer(settings, this.log, this.error)
151156
if (functionsDir) {
152-
const fnSettings = await serveFunctions({ functionsDir })
157+
const fnSettings = await serveFunctions({functionsDir})
153158
settings.functionsPort = fnSettings.port
154159
}
155160

@@ -162,26 +167,30 @@ DevCommand.description = `Local dev server
162167
The dev command will run a local dev server with Netlify's proxy and redirect rules
163168
`
164169

165-
DevCommand.examples = ['$ netlify dev', '$ netlify dev -c "yarn start"', '$ netlify dev -c hugo']
170+
DevCommand.examples = [
171+
'$ netlify dev',
172+
'$ netlify dev -c "yarn start"',
173+
'$ netlify dev -c hugo',
174+
]
166175

167176
DevCommand.strict = false
168177

169178
DevCommand.flags = {
170-
cmd: flags.string({ char: 'c', description: 'command to run' }),
179+
cmd: flags.string({char: 'c', description: 'command to run'}),
171180
devport: flags.integer({
172181
char: 'd',
173-
description: 'port of the dev server started by command'
182+
description: 'port of the dev server started by command',
174183
}),
175-
port: flags.integer({ char: 'p', description: 'port of netlify dev' }),
176-
dir: flags.integer({ char: 'd', description: 'dir with static files' }),
184+
port: flags.integer({char: 'p', description: 'port of netlify dev'}),
185+
dir: flags.integer({char: 'd', description: 'dir with static files'}),
177186
functions: flags.string({
178187
char: 'f',
179-
description: 'Specify a functions folder to serve'
188+
description: 'Specify a functions folder to serve',
180189
}),
181190
offline: flags.boolean({
182191
char: 'o',
183-
description: 'disables any features that require network access'
184-
})
192+
description: 'disables any features that require network access',
193+
}),
185194
}
186195

187196
module.exports = DevCommand

src/detect-server.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,38 @@ const hugoDetector = require('./detectors/hugo')
55
const eleventyDetector = require('./detectors/eleventy')
66
const jekyllDetector = require('./detectors/jekyll')
77

8-
const detectors = [gatsbyDetector, reactStaticDetector, hugoDetector, jekyllDetector, eleventyDetector, craDetector]
8+
const detectors = [
9+
gatsbyDetector,
10+
reactStaticDetector,
11+
hugoDetector,
12+
jekyllDetector,
13+
eleventyDetector,
14+
craDetector,
15+
]
916

10-
module.exports.serverSettings = () => {
17+
module.exports.serverSettings = devConfig => {
18+
let settings = null
1119
for (const i in detectors) {
12-
const settings = detectors[i]()
20+
settings = detectors[i]()
1321
if (settings) {
14-
return settings
22+
break
1523
}
1624
}
1725

18-
return null
26+
if (devConfig) {
27+
settings = settings || {}
28+
if (devConfig.cmd) {
29+
settings.cmd = devConfig.cmd.split(/\s/)[0]
30+
settings.args = devConfig.cmd.split(/\s/).slice(1)
31+
}
32+
if (devConfig.port) {
33+
settings.proxyPort = devConfig.port
34+
settings.urlRegexp =
35+
devConfig.urlRegexp ||
36+
new RegExp(`(http://)([^:]+:)${devConfig.port}(/)?`, 'g')
37+
}
38+
settings.dist = devConfig.publish || settings.dist
39+
}
40+
41+
return settings
1942
}

0 commit comments

Comments
 (0)