1- const { flags} = require ( '@oclif/command' )
2- const { spawn} = require ( 'child_process' )
1+ const { flags } = require ( '@oclif/command' )
2+ const { spawn } = require ( 'child_process' )
33const http = require ( 'http' )
44const httpProxy = require ( 'http-proxy' )
55const waitPort = require ( 'wait-port' )
66const 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' )
910const Command = require ( '@netlify/cli-utils' )
10- const { getAddons} = require ( 'netlify/src/addons' )
11+ const { getAddons } = require ( 'netlify/src/addons' )
1112
1213function cleanExit ( ) {
1314 process . exit ( )
@@ -26,47 +27,45 @@ function addonUrl(addonUrls, req) {
2627async function startProxy ( settings , addonUrls ) {
2728 const rulesProxy = require ( 'netlify-rules-proxy' )
2829
29- await waitPort ( { port : settings . proxyPort } )
30+ await waitPort ( { port : settings . proxyPort } )
3031 if ( settings . functionsPort ) {
31- await waitPort ( { port : settings . functionsPort } )
32+ await waitPort ( { port : settings . functionsPort } )
3233 }
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
3736
3837 const proxy = httpProxy . createProxyServer ( {
3938 target : {
4039 host : 'localhost' ,
41- port : settings . proxyPort ,
42- } ,
40+ port : settings . proxyPort
41+ }
4342 } )
4443
45- const rewriter = rulesProxy ( { publicFolder : settings . dist } )
44+ const rewriter = rulesProxy ( { publicFolder : settings . dist } )
4645
47- const server = http . createServer ( function ( req , res ) {
46+ const server = http . createServer ( function ( req , res ) {
4847 if ( isFunction ( settings , req ) ) {
49- return proxy . web ( req , res , { target : functionsServer } )
48+ return proxy . web ( req , res , { target : functionsServer } )
5049 }
5150 let url = addonUrl ( addonUrls , req )
5251 if ( url ) {
53- return proxy . web ( req , res , { target : url } )
52+ return proxy . web ( req , res , { target : url } )
5453 }
5554
5655 rewriter ( req , res , ( ) => {
5756 if ( isFunction ( settings , req ) ) {
58- return proxy . web ( req , res , { target : functionsServer } )
57+ return proxy . web ( req , res , { target : functionsServer } )
5958 }
6059 url = addonUrl ( addonUrls , req )
6160 if ( url ) {
62- return proxy . web ( req , res , { target : url } )
61+ return proxy . web ( req , res , { target : url } )
6362 }
6463
65- proxy . web ( req , res , { target : `http://localhost:${ settings . proxyPort } ` } )
64+ proxy . web ( req , res , { target : `http://localhost:${ settings . proxyPort } ` } )
6665 } )
6766 } )
6867
69- server . on ( 'upgrade' , function ( req , socket , head ) {
68+ server . on ( 'upgrade' , function ( req , socket , head ) {
7069 proxy . ws ( req , socket , head )
7170 } )
7271
@@ -89,17 +88,17 @@ function startDevServer(settings, log, error) {
8988 name : 'netlify-dev' ,
9089 port : settings . proxyPort ,
9190 templates : {
92- notFound : '404.html' ,
93- } ,
91+ notFound : '404.html'
92+ }
9493 } )
9594
96- server . start ( function ( ) {
95+ server . start ( function ( ) {
9796 log ( 'Server listening to' , settings . proxyPort )
9897 } )
9998 return
10099 }
101100
102- const ps = spawn ( settings . cmd , settings . args , { env : settings . env } )
101+ const ps = spawn ( settings . cmd , settings . args , { env : settings . env } )
103102
104103 ps . stdout . on ( 'data' , data => {
105104 log ( `${ data } ` . replace ( settings . urlRegexp , `$1$2${ settings . port } $3` ) )
@@ -119,28 +118,24 @@ function startDevServer(settings, log, error) {
119118
120119class DevCommand extends Command {
121120 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
124123 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 )
128125 const addonUrls = { }
129126 if ( site . id && ! flags . offline ) {
130127 const accessToken = await this . authenticate ( )
131128 const addons = await getAddons ( site . id , accessToken )
132129 if ( Array . isArray ( addons ) ) {
133130 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 } `
137132 for ( const key in addon . env ) {
138133 process . env [ key ] = process . env [ key ] || addon . env [ key ]
139134 }
140135 } )
141136 }
142137 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 } )
144139 // TODO: We should move the environment outside of build settings and possibly have a
145140 // `/api/v1/sites/:site_id/environment` endpoint for it that we can also gate access to
146141 // In the future and that we could make context dependend
@@ -154,55 +149,50 @@ class DevCommand extends Command {
154149 let settings = serverSettings ( config . dev )
155150 if ( ! ( settings && settings . cmd ) ) {
156151 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 )
160153 settings = {
161154 noCmd : true ,
162155 port : 8888 ,
163156 proxyPort : 3999 ,
164- dist,
157+ dist
165158 }
166159 }
167160 startDevServer ( settings , this . log , this . error )
168161 if ( functionsDir ) {
169- const fnSettings = await serveFunctions ( { functionsDir} )
162+ const fnSettings = await serveFunctions ( { functionsDir } )
170163 settings . functionsPort = fnSettings . port
171164 }
172165
173166 const url = await startProxy ( settings , addonUrls )
174167 this . log ( `Netlify dev server is now ready on ${ url } ` )
168+ openBrowser ( url )
175169 }
176170}
177171
178172DevCommand . description = `Local dev server
179173The dev command will run a local dev server with Netlify's proxy and redirect rules
180174`
181175
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' ]
187177
188178DevCommand . strict = false
189179
190180DevCommand . flags = {
191- cmd : flags . string ( { char : 'c' , description : 'command to run' } ) ,
181+ cmd : flags . string ( { char : 'c' , description : 'command to run' } ) ,
192182 devport : flags . integer ( {
193183 char : 'd' ,
194- description : 'port of the dev server started by command' ,
184+ description : 'port of the dev server started by command'
195185 } ) ,
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' } ) ,
198188 functions : flags . string ( {
199189 char : 'f' ,
200- description : 'Specify a functions folder to serve' ,
190+ description : 'Specify a functions folder to serve'
201191 } ) ,
202192 offline : flags . boolean ( {
203193 char : 'o' ,
204- description : 'disables any features that require network access' ,
205- } ) ,
194+ description : 'disables any features that require network access'
195+ } )
206196}
207197
208198module . exports = DevCommand
0 commit comments