@@ -10,6 +10,7 @@ const Command = require("@netlify/cli-utils");
1010const { getAddons } = require ( "netlify/src/addons" ) ;
1111const { track } = require ( "@netlify/cli-utils/src/utils/telemetry" ) ;
1212const chalk = require ( "chalk" ) ;
13+ const NETLIFYDEV = `[${ chalk . cyan ( "Netlify Dev" ) } ]` ;
1314const boxen = require ( "boxen" ) ;
1415const { createTunnel, connectTunnel } = require ( "../../live-tunnel" ) ;
1516
@@ -24,58 +25,62 @@ function addonUrl(addonUrls, req) {
2425}
2526
2627// Used as an optimization to avoid dual lookups for missing assets
27- const assetExtensionRegExp = / \. ( h t m l ? | p n g | j p g | j s | c s s | s v g | g i f | i c o | w o f f | w o f f 2 ) $ /
28+ const assetExtensionRegExp = / \. ( h t m l ? | p n g | j p g | j s | c s s | s v g | g i f | i c o | w o f f | w o f f 2 ) $ / ;
2829
2930function alternativePathsFor ( url ) {
30- const paths = [ ]
31- if ( url [ url . length - 1 ] === '/' ) {
32- const end = url . length - 1
33- if ( url !== '/' ) {
34- paths . push ( url . slice ( 0 , end ) + ' .html' )
35- paths . push ( url . slice ( 0 , end ) + ' .htm' )
31+ const paths = [ ] ;
32+ if ( url [ url . length - 1 ] === "/" ) {
33+ const end = url . length - 1 ;
34+ if ( url !== "/" ) {
35+ paths . push ( url . slice ( 0 , end ) + " .html" ) ;
36+ paths . push ( url . slice ( 0 , end ) + " .htm" ) ;
3637 }
37- paths . push ( url + ' index.html' )
38- paths . push ( url + ' index.htm' )
38+ paths . push ( url + " index.html" ) ;
39+ paths . push ( url + " index.htm" ) ;
3940 } else if ( ! url . match ( assetExtensionRegExp ) ) {
40- paths . push ( url + ' .html' )
41- paths . push ( url + ' .htm' )
42- paths . push ( url + ' /index.html' )
43- paths . push ( url + ' /index.htm' )
41+ paths . push ( url + " .html" ) ;
42+ paths . push ( url + " .htm" ) ;
43+ paths . push ( url + " /index.html" ) ;
44+ paths . push ( url + " /index.htm" ) ;
4445 }
4546
46- return paths
47+ return paths ;
4748}
4849
4950function initializeProxy ( port ) {
5051 const proxy = httpProxy . createProxyServer ( {
5152 selfHandleResponse : true ,
5253 target : {
53- host : ' localhost' ,
54+ host : " localhost" ,
5455 port : port
5556 }
56- } )
57+ } ) ;
5758
58- proxy . on ( 'proxyRes' , ( proxyRes , req , res ) => {
59- if ( proxyRes . statusCode === 404 && req . alternativePaths && req . alternativePaths . length ) {
60- req . url = req . alternativePaths . shift ( )
61- return proxy . web ( req , res , req . proxyOptions )
59+ proxy . on ( "proxyRes" , ( proxyRes , req , res ) => {
60+ if (
61+ proxyRes . statusCode === 404 &&
62+ req . alternativePaths &&
63+ req . alternativePaths . length
64+ ) {
65+ req . url = req . alternativePaths . shift ( ) ;
66+ return proxy . web ( req , res , req . proxyOptions ) ;
6267 }
63- res . writeHead ( proxyRes . statusCode , proxyRes . headers )
64- proxyRes . on ( ' data' , function ( data ) {
65- res . write ( data )
66- } )
67- proxyRes . on ( ' end' , function ( ) {
68- res . end ( )
69- } )
70- } )
68+ res . writeHead ( proxyRes . statusCode , proxyRes . headers ) ;
69+ proxyRes . on ( " data" , function ( data ) {
70+ res . write ( data ) ;
71+ } ) ;
72+ proxyRes . on ( " end" , function ( ) {
73+ res . end ( ) ;
74+ } ) ;
75+ } ) ;
7176
7277 return {
7378 web : ( req , res , options ) => {
74- req . proxyOptions = options
75- req . alternativePaths = alternativePathsFor ( req . url )
76- return proxy . web ( req , res , options )
79+ req . proxyOptions = options ;
80+ req . alternativePaths = alternativePathsFor ( req . url ) ;
81+ return proxy . web ( req , res , options ) ;
7782 }
78- }
83+ } ;
7984}
8085
8186async function startProxy ( settings , addonUrls ) {
@@ -134,7 +139,7 @@ function startDevServer(settings, log, error) {
134139 const StaticServer = require ( "static-server" ) ;
135140 if ( ! settings . dist ) {
136141 log (
137- " Unable to determine public folder for the dev server.\nSetup a netlify.toml file with a [dev] section to specify your dev server settings."
142+ ` ${ NETLIFYDEV } Unable to determine public folder for the dev server. \n Setup a netlify.toml file with a [dev] section to specify your dev server settings.`
138143 ) ;
139144 process . exit ( 1 ) ;
140145 }
@@ -149,10 +154,10 @@ function startDevServer(settings, log, error) {
149154 } ) ;
150155
151156 server . start ( function ( ) {
152- log ( " Server listening to" , settings . proxyPort ) ;
157+ log ( ` ${ NETLIFYDEV } Server listening to` , settings . proxyPort ) ;
153158 } ) ;
154159 } else {
155- log ( `Starting netlify dev with ${ settings . type } ` ) ;
160+ log ( `${ NETLIFYDEV } Starting Netlify Dev with ${ settings . type } ` ) ;
156161 const ps = execa ( settings . command , settings . args , {
157162 env : settings . env ,
158163 stdio : "inherit"
@@ -171,39 +176,21 @@ class DevCommand extends Command {
171176 flags . functions ||
172177 ( config . dev && config . dev . functions ) ||
173178 ( config . build && config . build . functions ) ;
174- const addonUrls = { } ;
179+ let addonUrls = { } ;
175180
176181 let accessToken = api . accessToken ;
177182 if ( site . id && ! flags . offline ) {
178183 accessToken = await this . authenticate ( ) ;
179- const addons = await getAddons ( site . id , accessToken ) ;
180- if ( Array . isArray ( addons ) ) {
181- addons . forEach ( addon => {
182- addonUrls [ addon . slug ] = `${ addon . config . site_url } /.netlify/${
183- addon . slug
184- } `;
185- for ( const key in addon . env ) {
186- process . env [ key ] = process . env [ key ] || addon . env [ key ] ;
187- }
188- } ) ;
189- }
190- const api = this . netlify . api ;
191- const apiSite = await api . getSite ( { site_id : site . id } ) ;
192- // TODO: We should move the environment outside of build settings and possibly have a
193- // `/api/v1/sites/:site_id/environment` endpoint for it that we can also gate access to
194- // In the future and that we could make context dependend
195- if ( apiSite . build_settings && apiSite . build_settings . env ) {
196- for ( const key in apiSite . build_settings . env ) {
197- process . env [ key ] =
198- process . env [ key ] || apiSite . build_settings . env [ key ] ;
199- }
200- }
184+ const { addEnvVariables } = require ( "../../utils/dev" ) ;
185+ addonUrls = await addEnvVariables ( api , site , accessToken ) ;
201186 }
202187 process . env . NETLIFY_DEV = "true" ;
203188
204189 let settings = serverSettings ( config . dev ) ;
205190 if ( ! ( settings && settings . command ) ) {
206- this . log ( "No dev server detected, using simple static server" ) ;
191+ this . log (
192+ "[Netlify Dev] No dev server detected, using simple static server"
193+ ) ;
207194 const dist =
208195 ( config . dev && config . dev . publish ) ||
209196 ( config . build && config . build . publish ) ;
@@ -253,7 +240,7 @@ class DevCommand extends Command {
253240 live : flags . live || false
254241 } ) ;
255242
256- const banner = chalk . bold ( `Netlify dev server is now ready on ${ url } ` ) ;
243+ const banner = chalk . bold ( `Netlify Dev Server now ready on ${ url } ` ) ;
257244 this . log (
258245 boxen ( banner , {
259246 padding : 1 ,
0 commit comments