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