1
+ import { spawn , exec , ChildProcessWithoutNullStreams } from 'child_process' ;
2
+ import Process from '../decorators/process' ;
3
+ import { dialog } from 'electron' ;
4
+
5
+ interface Command {
6
+ command : string ,
7
+ arguments : string [ ] ,
8
+ }
9
+
10
+ export const CMD_LS : Command = { command : 'ls' , arguments : [ '-lh' ] } ;
11
+
12
+ export const CMD_MKDIR : Command = { command : 'mkdir' , arguments : [ ] } ;
13
+
14
+ export const CMD_RMDIR : Command = { command : 'rmdir' , arguments : [ ] } ;
15
+
16
+ export const CMD_PWD : Command = { command : 'pwd' , arguments : [ ] } ;
17
+
18
+ export const POWERSHELL : Command = { command : 'cmd.exe' , arguments : [ 'mvn' , '--help' ] } ;
19
+
20
+ const MAX_BUFFER = 1024 * 500 ; /* 500 KB */
21
+
22
+ export class TerminalService {
23
+
24
+ private standardHandler ( spawn : ChildProcessWithoutNullStreams ) : Promise < { } > {
25
+ return new Promise ( ( resolve , reject ) => {
26
+ let result = '' ;
27
+ let error = ''
28
+ spawn . stdout . on ( 'data' , data => { result += data } ) ;
29
+ spawn . stderr . on ( 'data' , data => { error += data } ) ;
30
+ spawn . on ( 'close' , ( ) => {
31
+ if ( error ) {
32
+ reject ( result + error )
33
+ } else {
34
+ resolve ( result + error )
35
+ }
36
+ } ) ;
37
+ spawn . stdin . end ( )
38
+ } ) ;
39
+ }
40
+
41
+ @Process ( 'terminal/ls' )
42
+ async ls ( ) {
43
+ const ls = spawn ( CMD_LS . command , CMD_LS . arguments )
44
+ const result = await this . standardHandler ( ls ) ;
45
+
46
+ return result ;
47
+ }
48
+
49
+ @Process ( 'terminal/mkdir' )
50
+ async mkdir ( dirname : string ) {
51
+ const mkdir = spawn ( CMD_MKDIR . command , [ dirname ] ) ;
52
+ try {
53
+ const result = await this . standardHandler ( mkdir ) ;
54
+
55
+ return result ;
56
+ } catch ( error ) {
57
+ return error ;
58
+ }
59
+ }
60
+
61
+ @Process ( 'terminal/rmdir' )
62
+ async rmdir ( dirname : string ) {
63
+ const rmdir = spawn ( CMD_RMDIR . command , [ dirname ] ) ;
64
+ try {
65
+ const result = await this . standardHandler ( rmdir ) ;
66
+ return result ;
67
+
68
+ } catch ( error ) {
69
+ return error ;
70
+ }
71
+ }
72
+
73
+ @Process ( 'terminal/pwd' )
74
+ async pwd ( ) {
75
+ const pwd = spawn ( CMD_PWD . command , CMD_PWD . arguments ) ;
76
+
77
+ try {
78
+ const result = await this . standardHandler ( pwd ) ;
79
+ return result ;
80
+
81
+ } catch ( error ) {
82
+ return error ;
83
+ }
84
+ }
85
+
86
+ @Process ( 'terminal/open-dialog' )
87
+ async openDialog ( ) {
88
+ const res = await dialog . showOpenDialog ( { properties : [ 'openDirectory' ] } ) ;
89
+ return res ;
90
+ }
91
+
92
+
93
+ @Process ( 'terminal/mvn-install' )
94
+ async mvnInstall ( cwd ?: string ) {
95
+ const options = cwd ? { cwd, maxBuffer : MAX_BUFFER } : undefined
96
+ let mvn = exec ( 'mvn --help' , options ) ;
97
+
98
+ try {
99
+ const result = await this . standardHandler ( mvn ) ;
100
+ return result ;
101
+
102
+ } catch ( error ) {
103
+ return error ;
104
+ }
105
+ }
106
+
107
+ @Process ( 'terminal/all-commands' )
108
+ async allCommands ( command : string , cwd ?: string ) {
109
+ if ( ! command ) return '' ;
110
+
111
+ const options = cwd ? { cwd, maxBuffer : MAX_BUFFER } : undefined
112
+ let mvn = exec ( command , options ) ;
113
+
114
+ try {
115
+ const result = await this . standardHandler ( mvn ) ;
116
+ return new ReturnMessage ( false , result ) ;
117
+
118
+ } catch ( error ) {
119
+ return new ReturnMessage ( true , error ) ;
120
+ }
121
+ }
122
+ }
123
+
124
+ class ReturnMessage {
125
+ error : boolean ;
126
+ body : any ;
127
+
128
+ constructor ( error : boolean , body : any ) {
129
+ this . error = error ;
130
+ this . body = body ;
131
+ }
132
+ }
0 commit comments