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' )
99const Command = require ( '@netlify/cli-utils' )
10- const { getAddons} = require ( 'netlify/src/addons' )
10+ const { getAddons } = require ( 'netlify/src/addons' )
1111
1212function cleanExit ( ) {
1313 process . exit ( )
@@ -26,47 +26,45 @@ function addonUrl(addonUrls, req) {
2626async 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 ?
35- `http://localhost:${ settings . functionsPort } ` :
36- null
33+ const port = await getPort ( { port : settings . port } )
34+ const functionsServer = settings . functionsPort ? `http://localhost:${ settings . functionsPort } ` : null
3735
3836 const proxy = httpProxy . createProxyServer ( {
3937 target : {
4038 host : 'localhost' ,
41- port : settings . proxyPort ,
42- } ,
39+ port : settings . proxyPort
40+ }
4341 } )
4442
45- const rewriter = rulesProxy ( { publicFolder : settings . dist } )
43+ const rewriter = rulesProxy ( { publicFolder : settings . dist } )
4644
47- const server = http . createServer ( function ( req , res ) {
45+ const server = http . createServer ( function ( req , res ) {
4846 if ( isFunction ( settings , req ) ) {
49- return proxy . web ( req , res , { target : functionsServer } )
47+ return proxy . web ( req , res , { target : functionsServer } )
5048 }
5149 let url = addonUrl ( addonUrls , req )
5250 if ( url ) {
53- return proxy . web ( req , res , { target : url } )
51+ return proxy . web ( req , res , { target : url } )
5452 }
5553
5654 rewriter ( req , res , ( ) => {
5755 if ( isFunction ( settings , req ) ) {
58- return proxy . web ( req , res , { target : functionsServer } )
56+ return proxy . web ( req , res , { target : functionsServer } )
5957 }
6058 url = addonUrl ( addonUrls , req )
6159 if ( url ) {
62- return proxy . web ( req , res , { target : url } )
60+ return proxy . web ( req , res , { target : url } )
6361 }
6462
65- proxy . web ( req , res , { target : `http://localhost:${ settings . proxyPort } ` } )
63+ proxy . web ( req , res , { target : `http://localhost:${ settings . proxyPort } ` } )
6664 } )
6765 } )
6866
69- server . on ( 'upgrade' , function ( req , socket , head ) {
67+ server . on ( 'upgrade' , function ( req , socket , head ) {
7068 proxy . ws ( req , socket , head )
7169 } )
7270
@@ -82,17 +80,17 @@ function startDevServer(settings, log, error) {
8280 name : 'netlify-dev' ,
8381 port : settings . proxyPort ,
8482 templates : {
85- notFound : '404.html' ,
86- } ,
83+ notFound : '404.html'
84+ }
8785 } )
8886
89- server . start ( function ( ) {
87+ server . start ( function ( ) {
9088 log ( 'Server listening to' , settings . proxyPort )
9189 } )
9290 return
9391 }
9492
95- const ps = spawn ( settings . cmd , settings . args , { env : settings . env } )
93+ const ps = spawn ( settings . cmd , settings . args , { env : settings . env } )
9694
9795 ps . stdout . on ( 'data' , data => {
9896 log ( `${ data } ` . replace ( settings . urlRegexp , `$1$2${ settings . port } $3` ) )
@@ -112,24 +110,23 @@ function startDevServer(settings, log, error) {
112110
113111class DevCommand extends Command {
114112 async run ( ) {
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 )
113+ const { flags, args } = this . parse ( DevCommand )
114+ const { api, site, config } = this . netlify
115+ const functionsDir = flags . functions || ( config . build && config . build . functions )
119116 const addonUrls = { }
120117 if ( site . id && ! flags . offline ) {
121118 const accessToken = await this . authenticate ( )
122119 const addons = await getAddons ( site . id , accessToken )
123- addons . forEach ( addon => {
124- addonUrls [ addon . slug ] = ` ${ addon . config . site_url } /.netlify/ $ {
125- addon . slug
126- } `
127- for ( const key in addon . env ) {
128- process . env [ key ] = process . env [ key ] || addon . env [ key ]
129- }
130- } )
120+ if ( Array . isArray ( addons ) ) {
121+ addons . forEach ( addon => {
122+ addonUrls [ addon . slug ] = ` ${ addon . config . site_url } /.netlify/ ${ addon . slug } `
123+ for ( const key in addon . env ) {
124+ process . env [ key ] = process . env [ key ] || addon . env [ key ]
125+ }
126+ } )
127+ }
131128 const api = this . netlify . api
132- const apiSite = await api . getSite ( { site_id : site . id } )
129+ const apiSite = await api . getSite ( { site_id : site . id } )
133130 // TODO: We should move the environment outside of build settings and possibly have a
134131 // `/api/v1/sites/:site_id/environment` endpoint for it that we can also gate access to
135132 // In the future and that we could make context dependend
@@ -147,12 +144,12 @@ class DevCommand extends Command {
147144 noCmd : true ,
148145 port : 8888 ,
149146 proxyPort : 3999 ,
150- dist : config . build && config . build . publish ,
147+ dist : config . build && config . build . publish
151148 }
152149 }
153150 startDevServer ( settings , this . log , this . error )
154151 if ( functionsDir ) {
155- const fnSettings = await serveFunctions ( { functionsDir} )
152+ const fnSettings = await serveFunctions ( { functionsDir } )
156153 settings . functionsPort = fnSettings . port
157154 }
158155
@@ -165,30 +162,26 @@ DevCommand.description = `Local dev server
165162The dev command will run a local dev server with Netlify's proxy and redirect rules
166163`
167164
168- DevCommand . examples = [
169- '$ netlify dev' ,
170- '$ netlify dev -c "yarn start"' ,
171- '$ netlify dev -c hugo' ,
172- ]
165+ DevCommand . examples = [ '$ netlify dev' , '$ netlify dev -c "yarn start"' , '$ netlify dev -c hugo' ]
173166
174167DevCommand . strict = false
175168
176169DevCommand . flags = {
177- cmd : flags . string ( { char : 'c' , description : 'command to run' } ) ,
170+ cmd : flags . string ( { char : 'c' , description : 'command to run' } ) ,
178171 devport : flags . integer ( {
179172 char : 'd' ,
180- description : 'port of the dev server started by command' ,
173+ description : 'port of the dev server started by command'
181174 } ) ,
182- port : flags . integer ( { char : 'p' , description : 'port of netlify dev' } ) ,
183- dir : flags . integer ( { char : 'd' , description : 'dir with static files' } ) ,
175+ port : flags . integer ( { char : 'p' , description : 'port of netlify dev' } ) ,
176+ dir : flags . integer ( { char : 'd' , description : 'dir with static files' } ) ,
184177 functions : flags . string ( {
185178 char : 'f' ,
186- description : 'Specify a functions folder to serve' ,
179+ description : 'Specify a functions folder to serve'
187180 } ) ,
188181 offline : flags . boolean ( {
189182 char : 'o' ,
190- description : 'disables any features that require network access' ,
191- } ) ,
183+ description : 'disables any features that require network access'
184+ } )
192185}
193186
194187module . exports = DevCommand
0 commit comments