@@ -8,7 +8,6 @@ const program = require('commander');
8
8
const fs = require ( 'fs' ) ;
9
9
const semver = require ( 'semver' ) ;
10
10
const Step = require ( 'step' ) ;
11
- const shelljs = require ( 'shelljs/global' ) ;
12
11
const path = require ( 'path' ) ;
13
12
14
13
@@ -23,18 +22,94 @@ const packageInfo = require('../../package');
23
22
const latestversionFile = path . join ( __dirname , '../../.latestversion' ) ;
24
23
const createServer = require ( './createserver' ) ;
25
24
25
+ function checkForUpdates ( ) {
26
+ http . get ( 'http://registry.npmjs.org/deployd-cli' , ( err , res , body ) => {
27
+ if ( ! err ) {
28
+ let json ;
29
+ try {
30
+ json = JSON . parse ( body ) ;
31
+ } catch ( ex ) {
32
+ console . log ( 'Could not parse body' , body ) ;
33
+ }
34
+
35
+ if ( json && json [ 'dist-tags' ] && json [ 'dist-tags' ] . latest ) {
36
+ const latest = json [ 'dist-tags' ] . latest ;
37
+ fs . writeFile ( latestversionFile , latest ) ;
38
+ }
39
+ }
40
+ } ) ;
41
+ }
26
42
27
43
/**
28
44
* Start the server
29
45
*/
30
46
const start = function ( file ) {
31
- let port = program . port ,
32
- host = program . host || '127.0.0.1' ,
33
- dbname = program . dbname || '-deployd' ,
34
- mongoPort = program . mongoPort ? Number ( program . mongoPort ) : '27017' ,
35
- env = program . environment || process . env . DPD_ENV || 'development' ,
36
- retries = 0 ,
37
- credentials = { } ;
47
+ let port = program . port ;
48
+ const host = program . host || '127.0.0.1' ;
49
+ const dbname = program . dbname || '-deployd' ;
50
+ const mongoPort = program . mongoPort ? Number ( program . mongoPort ) : '27017' ;
51
+ const env = program . environment || process . env . DPD_ENV || 'development' ;
52
+ let retries = 0 ;
53
+ const credentials = { } ;
54
+
55
+ function startup ( err ) {
56
+ if ( err ) {
57
+ console . log ( "Failed to start MongoDB (Make sure 'mongod' are in your $PATH or use dpd --mongod option. Ref: http://docs.deployd.com/docs/basics/cli.html)" ) ;
58
+ return stop ( 1 ) ;
59
+ }
60
+
61
+ const options = { port, env : 'development' , db : { host, port : mongoPort , name : dbname } } ;
62
+
63
+ options . env = program . environment || process . env . DPD_ENV || options . env ;
64
+ if ( options . env !== 'development' ) console . log ( 'starting in %s mode' , options . env ) ;
65
+
66
+ if ( credentials && credentials . length > 0 ) options . db . credentials = credentials ;
67
+
68
+ let dpd = createServer ( options ) ;
69
+
70
+ function onListening ( ) {
71
+ console . info ( 'listening on port' , options . port ) ;
72
+ const commands = repl ( dpd ) ;
73
+ if ( program . dashboard ) {
74
+ return commands . dashboard ( ) ;
75
+ } else if ( program . open ) {
76
+ return commands . open ( ) ;
77
+ }
78
+
79
+ return true ;
80
+ }
81
+
82
+ function onError ( err2 ) {
83
+ if ( err2 . code === 'EADDRINUSE' ) {
84
+ console . error ( ) ;
85
+ console . error ( `ERROR: port ${ options . port } is already in use` ) ;
86
+ if ( retries > 0 ) {
87
+ options . port += 1 ;
88
+ console . log ( `Trying again on port ${ options . port } ...` ) ;
89
+ console . log ( ) ;
90
+ retries -= 1 ;
91
+ dpd = createServer ( options ) ;
92
+ dpd . on ( 'listening' , onListening ) ;
93
+ dpd . on ( 'error' , onError ) ;
94
+ dpd . listen ( ) ;
95
+ } else {
96
+ return process . exit ( ) ;
97
+ }
98
+ } else {
99
+ console . error ( err2 ) ;
100
+ return process . exit ( ) ;
101
+ }
102
+
103
+ return true ;
104
+ }
105
+
106
+ dpd . on ( 'listening' , onListening ) ;
107
+ dpd . on ( 'error' , onError ) ;
108
+ dpd . listen ( ) ;
109
+ dpd . deploydPath = program . deploydPath ;
110
+
111
+ return true ;
112
+ }
38
113
39
114
40
115
if ( ! port ) {
@@ -94,9 +169,9 @@ const start = function (file) {
94
169
95
170
if ( program . host ) {
96
171
if ( program . auth ) {
97
- const auth = program . auth . split ( ':' ) ,
98
- username = auth [ 0 ] ,
99
- password = auth [ 1 ] ;
172
+ const auth = program . auth . split ( ':' ) ;
173
+ const username = auth [ 0 ] ;
174
+ const password = auth [ 1 ] ;
100
175
setCredentials ( username , password ) ;
101
176
} else if ( program . username || program . password ) {
102
177
setCredentials ( program . username , program . password ) ;
@@ -112,76 +187,6 @@ const start = function (file) {
112
187
console . log ( 'or use "dpd path/to/app.dpd" to start an app in another directory' ) ;
113
188
stop ( 1 ) ;
114
189
}
115
-
116
- function startup ( err ) {
117
- if ( err ) {
118
- console . log ( "Failed to start MongoDB (Make sure 'mongod' are in your $PATH or use dpd --mongod option. Ref: http://docs.deployd.com/docs/basics/cli.html)" ) ;
119
- return stop ( 1 ) ;
120
- }
121
-
122
- const options = { port, env : 'development' , db : { host, port : mongoPort , name : dbname } } ;
123
-
124
- options . env = program . environment || process . env . DPD_ENV || options . env ;
125
- if ( options . env !== 'development' ) console . log ( 'starting in %s mode' , options . env ) ;
126
-
127
- if ( credentials && credentials . length > 0 ) options . db . credentials = credentials ;
128
-
129
- let dpd = createServer ( options ) ;
130
- dpd . on ( 'listening' , onListening ) ;
131
- dpd . on ( 'error' , onError ) ;
132
- dpd . listen ( ) ;
133
- dpd . deploydPath = program . deploydPath ;
134
-
135
- function onListening ( ) {
136
- console . info ( 'listening on port' , options . port ) ;
137
- const commands = repl ( dpd ) ;
138
- if ( program . dashboard ) {
139
- commands . dashboard ( ) ;
140
- } else if ( program . open ) {
141
- commands . open ( ) ;
142
- }
143
- }
144
-
145
- function onError ( err2 ) {
146
- if ( err2 . code === 'EADDRINUSE' ) {
147
- console . error ( ) ;
148
- console . error ( `ERROR: port ${ options . port } is already in use` ) ;
149
- if ( retries > 0 ) {
150
- options . port += 1 ;
151
- console . log ( `Trying again on port ${ options . port } ...` ) ;
152
- console . log ( ) ;
153
- retries -= 1 ;
154
- dpd = createServer ( options ) ;
155
- dpd . on ( 'listening' , onListening ) ;
156
- dpd . on ( 'error' , onError ) ;
157
- dpd . listen ( ) ;
158
- } else {
159
- process . exit ( ) ;
160
- }
161
- } else {
162
- console . error ( err2 ) ;
163
- process . exit ( ) ;
164
- }
165
- }
166
- }
167
190
} ;
168
191
169
- function checkForUpdates ( ) {
170
- http . get ( 'http://registry.npmjs.org/deployd-cli' , ( err , res , body ) => {
171
- if ( ! err ) {
172
- let json ;
173
- try {
174
- json = JSON . parse ( body ) ;
175
- } catch ( ex ) {
176
- console . log ( 'Could not parse body' , body ) ;
177
- }
178
-
179
- if ( json && json [ 'dist-tags' ] && json [ 'dist-tags' ] . latest ) {
180
- const latest = json [ 'dist-tags' ] . latest ;
181
- fs . writeFile ( latestversionFile , latest ) ;
182
- }
183
- }
184
- } ) ;
185
- }
186
-
187
192
module . exports = start ;
0 commit comments