1
- const { flags} = require ( '@oclif/command' )
2
- const { spawn} = require ( 'child_process' )
1
+ const { flags } = require ( '@oclif/command' )
2
+ const { spawn } = require ( 'child_process' )
3
3
const http = require ( 'http' )
4
4
const httpProxy = require ( 'http-proxy' )
5
5
const waitPort = require ( 'wait-port' )
6
6
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' )
9
+ const openBrowser = require ( './openBrowser' )
9
10
const Command = require ( '@netlify/cli-utils' )
10
- const { getAddons} = require ( 'netlify/src/addons' )
11
+ const { getAddons } = require ( 'netlify/src/addons' )
11
12
12
13
function cleanExit ( ) {
13
14
process . exit ( )
@@ -26,47 +27,45 @@ function addonUrl(addonUrls, req) {
26
27
async function startProxy ( settings , addonUrls ) {
27
28
const rulesProxy = require ( 'netlify-rules-proxy' )
28
29
29
- await waitPort ( { port : settings . proxyPort } )
30
+ await waitPort ( { port : settings . proxyPort } )
30
31
if ( settings . functionsPort ) {
31
- await waitPort ( { port : settings . functionsPort } )
32
+ await waitPort ( { port : settings . functionsPort } )
32
33
}
33
- const port = await getPort ( { port : settings . port } )
34
- const functionsServer = settings . functionsPort ?
35
- `http://localhost:${ settings . functionsPort } ` :
36
- null
34
+ const port = await getPort ( { port : settings . port } )
35
+ const functionsServer = settings . functionsPort ? `http://localhost:${ settings . functionsPort } ` : null
37
36
38
37
const proxy = httpProxy . createProxyServer ( {
39
38
target : {
40
39
host : 'localhost' ,
41
- port : settings . proxyPort ,
42
- } ,
40
+ port : settings . proxyPort
41
+ }
43
42
} )
44
43
45
- const rewriter = rulesProxy ( { publicFolder : settings . dist } )
44
+ const rewriter = rulesProxy ( { publicFolder : settings . dist } )
46
45
47
- const server = http . createServer ( function ( req , res ) {
46
+ const server = http . createServer ( function ( req , res ) {
48
47
if ( isFunction ( settings , req ) ) {
49
- return proxy . web ( req , res , { target : functionsServer } )
48
+ return proxy . web ( req , res , { target : functionsServer } )
50
49
}
51
50
let url = addonUrl ( addonUrls , req )
52
51
if ( url ) {
53
- return proxy . web ( req , res , { target : url } )
52
+ return proxy . web ( req , res , { target : url } )
54
53
}
55
54
56
55
rewriter ( req , res , ( ) => {
57
56
if ( isFunction ( settings , req ) ) {
58
- return proxy . web ( req , res , { target : functionsServer } )
57
+ return proxy . web ( req , res , { target : functionsServer } )
59
58
}
60
59
url = addonUrl ( addonUrls , req )
61
60
if ( url ) {
62
- return proxy . web ( req , res , { target : url } )
61
+ return proxy . web ( req , res , { target : url } )
63
62
}
64
63
65
- proxy . web ( req , res , { target : `http://localhost:${ settings . proxyPort } ` } )
64
+ proxy . web ( req , res , { target : `http://localhost:${ settings . proxyPort } ` } )
66
65
} )
67
66
} )
68
67
69
- server . on ( 'upgrade' , function ( req , socket , head ) {
68
+ server . on ( 'upgrade' , function ( req , socket , head ) {
70
69
proxy . ws ( req , socket , head )
71
70
} )
72
71
@@ -89,17 +88,17 @@ function startDevServer(settings, log, error) {
89
88
name : 'netlify-dev' ,
90
89
port : settings . proxyPort ,
91
90
templates : {
92
- notFound : '404.html' ,
93
- } ,
91
+ notFound : '404.html'
92
+ }
94
93
} )
95
94
96
- server . start ( function ( ) {
95
+ server . start ( function ( ) {
97
96
log ( 'Server listening to' , settings . proxyPort )
98
97
} )
99
98
return
100
99
}
101
100
102
- const ps = spawn ( settings . cmd , settings . args , { env : settings . env } )
101
+ const ps = spawn ( settings . cmd , settings . args , { env : settings . env } )
103
102
104
103
ps . stdout . on ( 'data' , data => {
105
104
log ( `${ data } ` . replace ( settings . urlRegexp , `$1$2${ settings . port } $3` ) )
@@ -119,28 +118,24 @@ function startDevServer(settings, log, error) {
119
118
120
119
class DevCommand extends Command {
121
120
async run ( ) {
122
- const { flags, args} = this . parse ( DevCommand )
123
- const { api, site, config} = this . netlify
121
+ const { flags, args } = this . parse ( DevCommand )
122
+ const { api, site, config } = this . netlify
124
123
const functionsDir =
125
- flags . functions ||
126
- ( config . dev && config . dev . functions ) ||
127
- ( config . build && config . build . functions )
124
+ flags . functions || ( config . dev && config . dev . functions ) || ( config . build && config . build . functions )
128
125
const addonUrls = { }
129
126
if ( site . id && ! flags . offline ) {
130
127
const accessToken = await this . authenticate ( )
131
128
const addons = await getAddons ( site . id , accessToken )
132
129
if ( Array . isArray ( addons ) ) {
133
130
addons . forEach ( addon => {
134
- addonUrls [ addon . slug ] = `${ addon . config . site_url } /.netlify/${
135
- addon . slug
136
- } `
131
+ addonUrls [ addon . slug ] = `${ addon . config . site_url } /.netlify/${ addon . slug } `
137
132
for ( const key in addon . env ) {
138
133
process . env [ key ] = process . env [ key ] || addon . env [ key ]
139
134
}
140
135
} )
141
136
}
142
137
const api = this . netlify . api
143
- const apiSite = await api . getSite ( { site_id : site . id } )
138
+ const apiSite = await api . getSite ( { site_id : site . id } )
144
139
// TODO: We should move the environment outside of build settings and possibly have a
145
140
// `/api/v1/sites/:site_id/environment` endpoint for it that we can also gate access to
146
141
// In the future and that we could make context dependend
@@ -154,55 +149,50 @@ class DevCommand extends Command {
154
149
let settings = serverSettings ( config . dev )
155
150
if ( ! ( settings && settings . cmd ) ) {
156
151
this . log ( 'No dev server detected, using simple static server' )
157
- const dist =
158
- ( config . dev && config . dev . publish ) ||
159
- ( config . build && config . build . publish )
152
+ const dist = ( config . dev && config . dev . publish ) || ( config . build && config . build . publish )
160
153
settings = {
161
154
noCmd : true ,
162
155
port : 8888 ,
163
156
proxyPort : 3999 ,
164
- dist,
157
+ dist
165
158
}
166
159
}
167
160
startDevServer ( settings , this . log , this . error )
168
161
if ( functionsDir ) {
169
- const fnSettings = await serveFunctions ( { functionsDir} )
162
+ const fnSettings = await serveFunctions ( { functionsDir } )
170
163
settings . functionsPort = fnSettings . port
171
164
}
172
165
173
166
const url = await startProxy ( settings , addonUrls )
174
167
this . log ( `Netlify dev server is now ready on ${ url } ` )
168
+ openBrowser ( url )
175
169
}
176
170
}
177
171
178
172
DevCommand . description = `Local dev server
179
173
The dev command will run a local dev server with Netlify's proxy and redirect rules
180
174
`
181
175
182
- DevCommand . examples = [
183
- '$ netlify dev' ,
184
- '$ netlify dev -c "yarn start"' ,
185
- '$ netlify dev -c hugo' ,
186
- ]
176
+ DevCommand . examples = [ '$ netlify dev' , '$ netlify dev -c "yarn start"' , '$ netlify dev -c hugo' ]
187
177
188
178
DevCommand . strict = false
189
179
190
180
DevCommand . flags = {
191
- cmd : flags . string ( { char : 'c' , description : 'command to run' } ) ,
181
+ cmd : flags . string ( { char : 'c' , description : 'command to run' } ) ,
192
182
devport : flags . integer ( {
193
183
char : 'd' ,
194
- description : 'port of the dev server started by command' ,
184
+ description : 'port of the dev server started by command'
195
185
} ) ,
196
- port : flags . integer ( { char : 'p' , description : 'port of netlify dev' } ) ,
197
- dir : flags . integer ( { char : 'd' , description : 'dir with static files' } ) ,
186
+ port : flags . integer ( { char : 'p' , description : 'port of netlify dev' } ) ,
187
+ dir : flags . integer ( { char : 'd' , description : 'dir with static files' } ) ,
198
188
functions : flags . string ( {
199
189
char : 'f' ,
200
- description : 'Specify a functions folder to serve' ,
190
+ description : 'Specify a functions folder to serve'
201
191
} ) ,
202
192
offline : flags . boolean ( {
203
193
char : 'o' ,
204
- description : 'disables any features that require network access' ,
205
- } ) ,
194
+ description : 'disables any features that require network access'
195
+ } )
206
196
}
207
197
208
198
module . exports = DevCommand
0 commit comments