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
9
const Command = require ( '@netlify/cli-utils' )
10
- const { getAddons} = require ( 'netlify/src/addons' )
10
+ const { getAddons } = require ( 'netlify/src/addons' )
11
11
12
12
function cleanExit ( ) {
13
13
process . exit ( )
@@ -26,47 +26,45 @@ function addonUrl(addonUrls, req) {
26
26
async function startProxy ( settings , addonUrls ) {
27
27
const rulesProxy = require ( 'netlify-rules-proxy' )
28
28
29
- await waitPort ( { port : settings . proxyPort } )
29
+ await waitPort ( { port : settings . proxyPort } )
30
30
if ( settings . functionsPort ) {
31
- await waitPort ( { port : settings . functionsPort } )
31
+ await waitPort ( { port : settings . functionsPort } )
32
32
}
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
37
35
38
36
const proxy = httpProxy . createProxyServer ( {
39
37
target : {
40
38
host : 'localhost' ,
41
- port : settings . proxyPort ,
42
- } ,
39
+ port : settings . proxyPort
40
+ }
43
41
} )
44
42
45
- const rewriter = rulesProxy ( { publicFolder : settings . dist } )
43
+ const rewriter = rulesProxy ( { publicFolder : settings . dist } )
46
44
47
- const server = http . createServer ( function ( req , res ) {
45
+ const server = http . createServer ( function ( req , res ) {
48
46
if ( isFunction ( settings , req ) ) {
49
- return proxy . web ( req , res , { target : functionsServer } )
47
+ return proxy . web ( req , res , { target : functionsServer } )
50
48
}
51
49
let url = addonUrl ( addonUrls , req )
52
50
if ( url ) {
53
- return proxy . web ( req , res , { target : url } )
51
+ return proxy . web ( req , res , { target : url } )
54
52
}
55
53
56
54
rewriter ( req , res , ( ) => {
57
55
if ( isFunction ( settings , req ) ) {
58
- return proxy . web ( req , res , { target : functionsServer } )
56
+ return proxy . web ( req , res , { target : functionsServer } )
59
57
}
60
58
url = addonUrl ( addonUrls , req )
61
59
if ( url ) {
62
- return proxy . web ( req , res , { target : url } )
60
+ return proxy . web ( req , res , { target : url } )
63
61
}
64
62
65
- proxy . web ( req , res , { target : `http://localhost:${ settings . proxyPort } ` } )
63
+ proxy . web ( req , res , { target : `http://localhost:${ settings . proxyPort } ` } )
66
64
} )
67
65
} )
68
66
69
- server . on ( 'upgrade' , function ( req , socket , head ) {
67
+ server . on ( 'upgrade' , function ( req , socket , head ) {
70
68
proxy . ws ( req , socket , head )
71
69
} )
72
70
@@ -82,17 +80,17 @@ function startDevServer(settings, log, error) {
82
80
name : 'netlify-dev' ,
83
81
port : settings . proxyPort ,
84
82
templates : {
85
- notFound : '404.html' ,
86
- } ,
83
+ notFound : '404.html'
84
+ }
87
85
} )
88
86
89
- server . start ( function ( ) {
87
+ server . start ( function ( ) {
90
88
log ( 'Server listening to' , settings . proxyPort )
91
89
} )
92
90
return
93
91
}
94
92
95
- const ps = spawn ( settings . cmd , settings . args , { env : settings . env } )
93
+ const ps = spawn ( settings . cmd , settings . args , { env : settings . env } )
96
94
97
95
ps . stdout . on ( 'data' , data => {
98
96
log ( `${ data } ` . replace ( settings . urlRegexp , `$1$2${ settings . port } $3` ) )
@@ -112,24 +110,23 @@ function startDevServer(settings, log, error) {
112
110
113
111
class DevCommand extends Command {
114
112
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 )
119
116
const addonUrls = { }
120
117
if ( site . id && ! flags . offline ) {
121
118
const accessToken = await this . authenticate ( )
122
119
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
+ }
131
128
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 } )
133
130
// TODO: We should move the environment outside of build settings and possibly have a
134
131
// `/api/v1/sites/:site_id/environment` endpoint for it that we can also gate access to
135
132
// In the future and that we could make context dependend
@@ -147,12 +144,12 @@ class DevCommand extends Command {
147
144
noCmd : true ,
148
145
port : 8888 ,
149
146
proxyPort : 3999 ,
150
- dist : config . build && config . build . publish ,
147
+ dist : config . build && config . build . publish
151
148
}
152
149
}
153
150
startDevServer ( settings , this . log , this . error )
154
151
if ( functionsDir ) {
155
- const fnSettings = await serveFunctions ( { functionsDir} )
152
+ const fnSettings = await serveFunctions ( { functionsDir } )
156
153
settings . functionsPort = fnSettings . port
157
154
}
158
155
@@ -165,30 +162,26 @@ DevCommand.description = `Local dev server
165
162
The dev command will run a local dev server with Netlify's proxy and redirect rules
166
163
`
167
164
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' ]
173
166
174
167
DevCommand . strict = false
175
168
176
169
DevCommand . flags = {
177
- cmd : flags . string ( { char : 'c' , description : 'command to run' } ) ,
170
+ cmd : flags . string ( { char : 'c' , description : 'command to run' } ) ,
178
171
devport : flags . integer ( {
179
172
char : 'd' ,
180
- description : 'port of the dev server started by command' ,
173
+ description : 'port of the dev server started by command'
181
174
} ) ,
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' } ) ,
184
177
functions : flags . string ( {
185
178
char : 'f' ,
186
- description : 'Specify a functions folder to serve' ,
179
+ description : 'Specify a functions folder to serve'
187
180
} ) ,
188
181
offline : flags . boolean ( {
189
182
char : 'o' ,
190
- description : 'disables any features that require network access' ,
191
- } ) ,
183
+ description : 'disables any features that require network access'
184
+ } )
192
185
}
193
186
194
187
module . exports = DevCommand
0 commit comments