1
1
'use strict' ;
2
- const path = require ( 'path' ) ;
3
2
const os = require ( 'os' ) ;
4
3
const fs = require ( 'fs' ) ;
4
+ const path = require ( 'path' ) ;
5
+ const chalk = require ( 'chalk' ) ;
5
6
const shell = require ( 'shelljs' ) ;
6
7
const opn = require ( 'opn' ) ;
7
8
const killer = require ( 'cross-port-killer' ) ;
9
+ const httpserver = require ( 'node-http-server' ) ;
10
+ const glob = require ( 'glob' ) ;
11
+
12
+
13
+ exports . resolve = ( filename , baseDir ) => {
14
+ baseDir = baseDir || process . cwd ( ) ;
15
+ if ( filename ) {
16
+ return path . isAbsolute ( filename ) ? filename : path . resolve ( baseDir , filename ) ;
17
+ }
18
+ return baseDir ;
19
+ } ;
20
+
21
+ exports . getIp = position => {
22
+ const interfaces = os . networkInterfaces ( ) ;
23
+ const ips = [ ] ;
24
+
25
+ if ( interfaces . en0 ) {
26
+ for ( let i = 0 ; i < interfaces . en0 . length ; i ++ ) {
27
+ if ( interfaces . en0 [ i ] . family === 'IPv4' ) {
28
+ ips . push ( interfaces . en0 [ i ] . address ) ;
29
+ }
30
+ }
31
+ }
32
+ if ( interfaces . en1 ) {
33
+ for ( let i = 0 ; i < interfaces . en1 . length ; i ++ ) {
34
+ if ( interfaces . en1 [ i ] . family === 'IPv4' ) {
35
+ ips . push ( interfaces . en1 [ i ] . address ) ;
36
+ }
37
+ }
38
+ }
39
+ if ( position > 0 && position <= ips . length ) {
40
+ return ips [ position - 1 ] ;
41
+ } else if ( ips . length ) {
42
+ return ips [ 0 ] ;
43
+ }
44
+ return '127.0.0.1' ;
45
+ } ;
46
+
47
+ exports . getHost = port => {
48
+ const ip = exports . getIp ( ) ;
49
+ if ( port ) {
50
+ return `http://${ ip } :${ port } ` ;
51
+ }
52
+ return `http://${ ip } ` ;
53
+ } ;
54
+
55
+ exports . httpServer = ( cfg , callback ) => {
56
+ const port = cfg . port || 8080 ;
57
+ const root = exports . resolve ( cfg . dist ) ;
58
+ let index = cfg . index ;
59
+ if ( ! index ) {
60
+ const files = glob . sync ( '*.html' , { cwd : root , root } ) ;
61
+ if ( files . length > 0 ) {
62
+ index = files [ 0 ] ;
63
+ }
64
+ }
65
+ const options = {
66
+ port,
67
+ root,
68
+ server : {
69
+ index
70
+ }
71
+ } ;
72
+ httpserver . deploy ( options , server => {
73
+ const url = `http://127.0.0.1:${ server . config . port } ` ;
74
+ const host = exports . getHost ( server . config . port ) ;
75
+ console . log ( chalk . green ( `Http server ${ chalk . yellow ( url ) } or ${ chalk . yellow ( host ) } is serve ${ chalk . blue ( root ) } \r\n` ) ) ;
76
+ callback && callback ( server ) ;
77
+ } ) ;
78
+ } ;
79
+
80
+ exports . exec = cmd => {
81
+ return shell . exec ( cmd ) ;
82
+ } ;
8
83
9
84
exports . rm = filepath => {
10
85
const dirs = Array . isArray ( filepath ) ? filepath : [ filepath ] ;
11
86
dirs . forEach ( dir => {
12
- /* istanbul ignore next */
13
- if ( os . platform ( ) === 'win32' ) {
14
- exports . deleteFile ( dir ) ;
15
- console . log ( `remove [ ${ dir } ] success` ) ;
16
- } else {
17
- const result = shell . exec ( `rm -rf ${ dir } ` ) ;
18
- if ( result . code === 0 ) {
87
+ if ( fs . existsSync ( dir ) ) {
88
+ /* istanbul ignore next */
89
+ if ( os . platform ( ) === 'win32' ) {
90
+ exports . deleteFile ( dir ) ;
19
91
console . log ( `remove [ ${ dir } ] success` ) ;
20
92
} else {
21
- /* istanbul ignore next */
22
- exports . deleteFile ( dir ) ;
93
+ const result = shell . exec ( `rm -rf ${ dir } ` ) ;
94
+ if ( result . code === 0 ) {
95
+ console . log ( `remove [ ${ dir } ] success` ) ;
96
+ } else {
97
+ /* istanbul ignore next */
98
+ exports . deleteFile ( dir ) ;
99
+ }
23
100
}
24
101
}
25
102
} ) ;
@@ -42,7 +119,7 @@ exports.deleteFile = filepath => {
42
119
fs . unlinkSync ( filepath ) ;
43
120
}
44
121
}
45
- }
122
+ } ;
46
123
47
124
/* istanbul ignore next */
48
125
exports . open = filepath => {
@@ -63,12 +140,11 @@ exports.open = filepath => {
63
140
}
64
141
} ;
65
142
66
- exports . openBrowser = ( url , port ) => {
67
- if ( ! url && port ) {
68
- const ip = utils . getIp ( ) ;
69
- url = `http://${ ip } :${ port } ` ;
143
+ exports . openBrowser = ( port , url ) => {
144
+ if ( ! url ) {
145
+ url = exports . getHost ( port ) ;
70
146
}
71
- open ( url ) ;
147
+ opn ( url ) ;
72
148
} ;
73
149
74
150
exports . checkPortUsed = port => {
@@ -98,7 +174,7 @@ exports.checkPortUsed = port => {
98
174
exports . getPort = ( port , count = 10 ) => {
99
175
let newPort = port ;
100
176
let checkPort = port ;
101
- while ( checkPort < port + count ) {
177
+ while ( checkPort < port + count ) {
102
178
const isUsed = exports . checkPortUsed ( checkPort ) ;
103
179
if ( ! isUsed ) {
104
180
newPort = checkPort ;
@@ -110,10 +186,9 @@ exports.getPort = (port, count = 10) => {
110
186
} ;
111
187
112
188
/* istanbul ignore next */
113
- exports . kill = function ( port ) {
189
+ exports . kill = function ( port ) {
114
190
if ( port ) {
115
- port = String ( port ) ;
116
- const ports = port . split ( ',' ) ;
191
+ const ports = Array . isArray ( port ) ? port : String ( port ) . split ( ',' ) ;
117
192
ports . forEach ( p => {
118
193
killer . kill ( p ) . then ( ( ) => {
119
194
console . log ( `kill port ${ p } success` ) ;
@@ -122,35 +197,4 @@ exports.kill = function (port) {
122
197
} ) ;
123
198
} ) ;
124
199
}
125
- } ;
126
-
127
- exports . getIp = position => {
128
- const interfaces = os . networkInterfaces ( ) ;
129
- const ips = [ ] ;
130
-
131
- if ( interfaces . en0 ) {
132
- for ( let i = 0 ; i < interfaces . en0 . length ; i ++ ) {
133
- if ( interfaces . en0 [ i ] . family === 'IPv4' ) {
134
- ips . push ( interfaces . en0 [ i ] . address ) ;
135
- }
136
- }
137
- }
138
- if ( interfaces . en1 ) {
139
- for ( let i = 0 ; i < interfaces . en1 . length ; i ++ ) {
140
- if ( interfaces . en1 [ i ] . family === 'IPv4' ) {
141
- ips . push ( interfaces . en1 [ i ] . address ) ;
142
- }
143
- }
144
- }
145
- if ( position > 0 && position <= ips . length ) {
146
- return ips [ position - 1 ] ;
147
- } else if ( ips . length ) {
148
- return ips [ 0 ] ;
149
- }
150
- return '127.0.0.1' ;
151
- } ;
152
-
153
- exports . getHost = port => {
154
- const ip = exports . getIp ( ) ;
155
- return `http://${ ip } :${ port } ` ;
156
200
} ;
0 commit comments