@@ -2,7 +2,8 @@ const path = require('path')
2
2
const fs = require ( 'fs-extra' )
3
3
const router = require ( 'koa-router' ) ( )
4
4
const { useResponse } = require ( '../utils' )
5
- const { exec } = require ( 'child_process' )
5
+ const { exec, spawn } = require ( 'child_process' )
6
+ const commandList = { }
6
7
7
8
router . get ( '/' , async ( ctx ) => {
8
9
await ctx . render ( 'index' , { } )
@@ -48,4 +49,88 @@ router.post('/open', async (ctx) => {
48
49
useResponse ( ctx , { } , 500 , '文件目录不存在' )
49
50
} )
50
51
52
+ router . post ( '/stop' , async ( ctx ) => {
53
+ data = ctx . request . body
54
+ console . log ( commandList [ data . uuid ] )
55
+ if ( data . uuid && Array . isArray ( commandList [ data . uuid ] ) ) {
56
+ commandList [ data . uuid ] . forEach ( list => {
57
+ list . forEach ( item => {
58
+ if ( ! item . close ) {
59
+ item . child . kill ( 'SIGKILL' )
60
+ }
61
+ } )
62
+ } )
63
+ delete commandList [ data . uuid ]
64
+ return useResponse ( ctx , 1 )
65
+ }
66
+ useResponse ( ctx , 0 )
67
+ } )
68
+
69
+ router . post ( '/exec' , async ( ctx ) => {
70
+ const current = [ ]
71
+ const commands = [ ]
72
+ const data = ctx . request . body
73
+ if ( data . uuid ) {
74
+ commandList [ data . uuid ] = commandList [ data . uuid ] || [ ]
75
+ commandList [ data . uuid ] . push ( current )
76
+ }
77
+ if ( data . nodeVersion ) {
78
+ commands . push ( 'nvm use ' + data . nodeVersion )
79
+ }
80
+ if ( typeof data . commandLines === 'string' || Array . isArray ( data . commandLines ) ) {
81
+ const commandLines = ( Array . isArray ( data . commandLines ) ? data . commandLines : data . commandLines . split ( '\n' ) )
82
+ commandLines . map ( command => {
83
+ commands . push ( command . replace ( / ( ^ \s + | \s + $ ) / g, '' ) )
84
+ } )
85
+ const result = [ ]
86
+ const promises = commands . map ( ( command , index ) => {
87
+ return new Promise ( ( resolve , reject ) => {
88
+ if ( ! command ) resolve ( )
89
+ const child = spawn ( 'cmd.exe' , [ '/c' , command ] , {
90
+ cwd : data . path
91
+ } )
92
+ const item = {
93
+ command,
94
+ child
95
+ }
96
+ current . push ( item )
97
+ child . stdout . on ( 'data' , data => {
98
+ console . log ( `${ command } 输出:\n${ data } \n` )
99
+ } )
100
+ child . stderr . on ( 'data' , data => {
101
+ console . error ( `${ command } 错误:\n${ data } \n` )
102
+ } )
103
+ child . on ( 'close' , code => {
104
+ Object . assign ( item , {
105
+ close : true ,
106
+ code
107
+ } )
108
+ result [ index ] = {
109
+ command,
110
+ code
111
+ }
112
+ if ( code === 0 ) resolve ( )
113
+ else reject ( code )
114
+ } )
115
+ } )
116
+ } )
117
+ console . log ( commands )
118
+ const execPromise = ( ) => Promise . all ( promises ) . then ( ( ) => {
119
+ useResponse ( ctx , result . slice ( data . nodeVersion ? 3 : 2 ) )
120
+ } ) . catch ( error => {
121
+ useResponse ( ctx , { } , 500 , error . toString ( ) )
122
+ } ) . finally ( ( ) => {
123
+ if ( data . uuid ) {
124
+ commandList [ data . uuid ] = commandList [ data . uuid ] . filter ( item => item !== current )
125
+ }
126
+ } )
127
+ if ( data . await ) {
128
+ return execPromise ( )
129
+ } else {
130
+ execPromise ( )
131
+ }
132
+ }
133
+ useResponse ( ctx , { } )
134
+ } )
135
+
51
136
module . exports = router
0 commit comments