22 * @Date : 2022-08-09 11:24:59
33 * @Author : Yao guan shou
44 * @LastEditors : Yao guan shou
5- * @LastEditTime : 2022-08-09 13:22:27
5+ * @LastEditTime : 2022-08-10 14:16:33
66 * @FilePath : /react-loading-ssr/bin/cmd.js
77 * @Description :
88 */
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'
1212
1313export default class Cmd {
14- text = '' ;
14+ text = ''
1515
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'
2119 }
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'
2236
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+ }
8985}
9086
9187// let cmd = new Cmd().runNodeModule(
@@ -94,48 +90,51 @@ export default class Cmd {
9490// )
9591
9692export const execute = ( command , options = { stdio : 'inherit' } ) => {
97- command = command . split ( ' ' ) . filter ( ( item ) => item ) ;
93+ command = command . split ( ' ' ) . filter ( ( item ) => item )
9894
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+ }
10298
103- const proc = spawn ( command [ 0 ] , command . slice ( 1 ) , options ) ;
99+ const proc = spawn ( command [ 0 ] , command . slice ( 1 ) , options )
104100
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+ } )
109107
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+ } )
114113
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+ } )
119119
120- return proc ;
121- } ;
120+ return proc
121+ }
122122
123123/**
124124 * 判断端口是否被占用
125125 * @param port 端口号
126126 * @returns 该端口是否被占用
127127 */
128128export 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