2
2
* @Date : 2022-08-09 11:24:59
3
3
* @Author : Yao guan shou
4
4
* @LastEditors : Yao guan shou
5
- * @LastEditTime : 2022-08-09 13:22:27
5
+ * @LastEditTime : 2022-08-10 14:16:33
6
6
* @FilePath : /react-loading-ssr/bin/cmd.js
7
7
* @Description :
8
8
*/
9
- import os from 'os' ;
10
- import { spawn , SpawnOptions , exec , execSync } from 'child_process' ;
11
- import moment from 'moment' ;
9
+ import os from 'os'
10
+ import { spawn , SpawnOptions , exec , execSync } from 'child_process'
11
+ import moment from 'moment'
12
12
13
13
export default class Cmd {
14
- text = '' ;
14
+ text = ''
15
15
16
- runNodeModule ( moduleName , params , options ) {
17
- if ( os . type ( ) == 'Windows_NT' && ! moduleName . match ( / \. c m d $ / ) ) {
18
- moduleName += '.cmd' ;
19
- }
20
- return this . run ( moduleName , params , options ) ;
16
+ runNodeModule ( moduleName , params , options ) {
17
+ if ( os . type ( ) == 'Windows_NT' && ! moduleName . match ( / \. c m d $ / ) ) {
18
+ moduleName += '.cmd'
21
19
}
20
+ return this . run ( moduleName , params , options )
21
+ }
22
+
23
+ run ( command , params , options ) {
24
+ this . text = ''
25
+ // options = Object.assign(options || {}, { cwd: this.cfg.cwd });
26
+ return new Promise ( ( resolve , reject ) => {
27
+ console . log ( `run command: ${ command } , params:` , params , options )
28
+
29
+ if ( ! options ) {
30
+ options = {
31
+ stdio : 'inherit' ,
32
+ }
33
+ }
34
+ if ( ! params ) params = [ ]
35
+ options . stdio = 'pipe'
22
36
23
- run ( command , params , options ) {
24
- this . text = '' ;
25
- // options = Object.assign(options || {}, { cwd: this.cfg.cwd });
26
- return new Promise ( ( resolve , reject ) => {
27
- console . log ( `run command: ${ command } , params:` , params , options ) ;
28
-
29
- if ( ! options ) {
30
- options = {
31
- stdio : 'inherit' ,
32
- } ;
33
- }
34
- if ( ! params ) params = [ ] ;
35
- options . stdio = 'pipe' ;
36
-
37
- let proc = spawn ( command , params , options ) ;
38
- // console.log('proc===', proc)
39
-
40
- proc . stdout . on ( 'data' , ( data ) => {
41
- let dataStr = String ( data ) ;
42
- if ( options . logPrefix ) {
43
- dataStr = options . logPrefix + dataStr ;
44
- }
45
- this . text += dataStr ;
46
- if ( ! options ?. silent )
47
- process . stdout . write (
48
- moment ( ) . format ( 'HH:mm:ss:SSS ' ) + dataStr
49
- ) ;
50
- } ) ;
51
-
52
- proc . stderr . on ( 'data' , ( data ) => {
53
- // 不一定代表进程exitcode != 0,可能只是进程调用了console.error
54
- let dataStr = String ( data ) ;
55
- if ( options ?. logPrefix ) {
56
- dataStr = options . logPrefix + dataStr ;
57
- }
58
- if ( ! options ?. silent )
59
- process . stderr . write (
60
- moment ( ) . format ( 'HH:mm:ss:SSS ' ) + dataStr
61
- ) ;
62
- } ) ;
63
-
64
- // 进程错误
65
- proc . on ( 'error' , ( error ) => {
66
- if ( ! options ?. silent ) console . error ( error ) ;
67
- reject ( error ) ;
68
- } ) ;
69
-
70
- // 进程关闭
71
- proc . on ( 'close' , ( code ) => {
72
- console . log ( `process closed with exit code: ${ code } ` ) ;
73
- if ( code == 0 ) {
74
- resolve ( this . text || '' ) ;
75
- } else {
76
- let errMsg = `process closed with exit code: ${ code } ` ;
77
- if ( options ?. logPrefix ) {
78
- errMsg = options . logPrefix + errMsg ;
79
- }
80
- reject ( new Error ( errMsg ) ) ;
81
- }
82
- } ) ;
83
-
84
- proc . on ( 'exit' , ( code , signal ) => {
85
- console . log ( `process exits` ) ;
86
- } ) ;
87
- } ) ;
88
- }
37
+ let proc = spawn ( command , params , options )
38
+ // console.log('proc===', proc)
39
+
40
+ proc . stdout . on ( 'data' , ( data ) => {
41
+ let dataStr = String ( data )
42
+ if ( options . logPrefix ) {
43
+ dataStr = options . logPrefix + dataStr
44
+ }
45
+ this . text += dataStr
46
+ if ( ! options ?. silent )
47
+ process . stdout . write ( moment ( ) . format ( 'HH:mm:ss:SSS ' ) + dataStr )
48
+ } )
49
+
50
+ proc . stderr . on ( 'data' , ( data ) => {
51
+ // 不一定代表进程exitcode != 0,可能只是进程调用了console.error
52
+ let dataStr = String ( data )
53
+ if ( options ?. logPrefix ) {
54
+ dataStr = options . logPrefix + dataStr
55
+ }
56
+ if ( ! options ?. silent )
57
+ process . stderr . write ( moment ( ) . format ( 'HH:mm:ss:SSS ' ) + dataStr )
58
+ } )
59
+
60
+ // 进程错误
61
+ proc . on ( 'error' , ( error ) => {
62
+ if ( ! options ?. silent ) console . error ( error )
63
+ reject ( error )
64
+ } )
65
+
66
+ // 进程关闭
67
+ proc . on ( 'close' , ( code ) => {
68
+ console . log ( `process closed with exit code: ${ code } ` )
69
+ if ( code == 0 ) {
70
+ resolve ( this . text || '' )
71
+ } else {
72
+ let errMsg = `process closed with exit code: ${ code } `
73
+ if ( options ?. logPrefix ) {
74
+ errMsg = options . logPrefix + errMsg
75
+ }
76
+ reject ( new Error ( errMsg ) )
77
+ }
78
+ } )
79
+
80
+ proc . on ( 'exit' , ( code , signal ) => {
81
+ console . log ( `process exits` )
82
+ } )
83
+ } )
84
+ }
89
85
}
90
86
91
87
// let cmd = new Cmd().runNodeModule(
@@ -94,48 +90,51 @@ export default class Cmd {
94
90
// )
95
91
96
92
export const execute = ( command , options = { stdio : 'inherit' } ) => {
97
- command = command . split ( ' ' ) . filter ( ( item ) => item ) ;
93
+ command = command . split ( ' ' ) . filter ( ( item ) => item )
98
94
99
- if ( os . type ( ) === 'Windows_NT' && ! command [ 0 ] . match ( / \. c m d $ / ) ) {
100
- command [ 0 ] += '.cmd' ;
101
- }
95
+ if ( os . type ( ) === 'Windows_NT' && ! command [ 0 ] . match ( / \. c m d $ / ) ) {
96
+ command [ 0 ] += '.cmd'
97
+ }
102
98
103
- const proc = spawn ( command [ 0 ] , command . slice ( 1 ) , options ) ;
99
+ const proc = spawn ( command [ 0 ] , command . slice ( 1 ) , options )
104
100
105
- // 进程错误
106
- proc . on ( 'error' , ( error ) => {
107
- if ( error ) console . error ( error ) ;
108
- } ) ;
101
+ // 进程错误
102
+ proc . on ( 'error' , ( error ) => {
103
+ if ( error ) {
104
+ console . error ( 'process error:' , error )
105
+ }
106
+ } )
109
107
110
- // 进程关闭
111
- proc . on ( 'close' , ( code ) => {
112
- console . log ( `process closed with exit code: ${ code } ` ) ;
113
- } ) ;
108
+ // 进程关闭
109
+ proc . on ( 'close' , ( code ) => {
110
+ console . log ( `process closed with exit code: ${ code } ` )
111
+ // process.exit(code);
112
+ } )
114
113
115
- // 退出
116
- proc . on ( 'exit' , ( code , signal ) => {
117
- console . log ( `process exits` ) ;
118
- } ) ;
114
+ // 退出
115
+ proc . on ( 'exit' , ( code , signal ) => {
116
+ console . log ( `process exits` )
117
+ // process.exit(code);
118
+ } )
119
119
120
- return proc ;
121
- } ;
120
+ return proc
121
+ }
122
122
123
123
/**
124
124
* 判断端口是否被占用
125
125
* @param port 端口号
126
126
* @returns 该端口是否被占用
127
127
*/
128
128
export const iSportTake = ( port ) => {
129
- const cmd =
130
- process . platform === 'win32'
131
- ? `netstat -aon|findstr ${ port } `
132
- : `lsof -i:${ port } ` ;
133
- try {
134
- const res = execSync ( cmd ) ;
135
- return true ;
136
- } catch ( error ) {
137
- // console.log('error:', error);
138
- return false ;
139
- }
140
-
141
- } ;
129
+ const cmd =
130
+ process . platform === 'win32'
131
+ ? `netstat -aon|findstr ${ port } `
132
+ : `lsof -i:${ port } `
133
+ try {
134
+ const res = execSync ( cmd )
135
+ return true
136
+ } catch ( error ) {
137
+ // console.log('error:', error);
138
+ return false
139
+ }
140
+ }
0 commit comments