1- import { Architecture , ExecResult } from "@/types" ;
2- import { ExecOptions , ShellString , exec } from "shelljs" ;
1+ import { Architecture , ProcessCallback } from "@/types" ;
2+ import { ExecOptions , ShellString , exec , which } from "shelljs" ;
33import { ImageResult , RemoveImageCommandFlags } from "@/types/images" ;
44import {
55 LogsCommandFlags ,
@@ -39,10 +39,44 @@ export default abstract class BaseBackend {
3939 return `${ this . vm } shell ${ this . instance } ${ this . runtime } ` ;
4040 }
4141
42+ //#region commons
43+ protected async fork (
44+ command : string ,
45+ callback ?: ProcessCallback
46+ ) : Promise < boolean > {
47+ const child = ( await this . exec ( command ) ) as ChildProcess ;
48+
49+ return await new Promise ( ( resolve ) => {
50+ child ?. stdout ?. on ( "data" , ( data ) => {
51+ callback && callback ( data ) ;
52+ } ) ;
53+ child ?. stdout ?. on ( "close" , ( ) => {
54+ resolve ( true ) ;
55+ } ) ;
56+ child ?. stderr ?. on ( "data" , ( data ) => {
57+ callback && callback ( data ) ;
58+ } ) ;
59+ child ?. stderr ?. on ( "close" , ( ) => {
60+ resolve ( false ) ;
61+ } ) ;
62+ } ) ;
63+ }
64+
65+ protected async execSync (
66+ command : string ,
67+ options ?: Omit < ExecOptions , "async" >
68+ ) : Promise < ShellString > {
69+ return exec ( command , {
70+ silent : true ,
71+ async : false ,
72+ ...options ,
73+ } ) as ShellString ;
74+ }
75+
4276 protected async exec (
4377 command : string ,
44- options ?: ExecOptions
45- ) : Promise < ExecResult > {
78+ options ?: Omit < ExecOptions , "async" >
79+ ) : Promise < ChildProcess > {
4680 return exec ( command , { silent : true , async : true , ...options } ) ;
4781 }
4882
@@ -73,11 +107,22 @@ export default abstract class BaseBackend {
73107
74108 return params ;
75109 }
110+ //#endregion
76111
77- abstract initVM ( ) : Promise < boolean > ;
78- abstract startVM ( ) : Promise < ChildProcess | null > ;
79- abstract stopVM ( ) : Promise < void > ;
80- abstract deleteVM ( ) : Promise < void > ;
112+ //#region VMs
113+ async checkVM ( ) : Promise < boolean > {
114+ return ! ! which ( this . vm ) ;
115+ }
116+ async checkInstance ( ) : Promise < boolean > {
117+ return true ;
118+ }
119+ async initVM ( callback ?: ProcessCallback ) : Promise < boolean > {
120+ return true ;
121+ }
122+ async initInstance ( callback ?: ProcessCallback ) : Promise < boolean > {
123+ return true ;
124+ }
125+ //#endregion
81126
82127 //#region registry
83128 async login (
@@ -88,20 +133,25 @@ export default abstract class BaseBackend {
88133 flags
89134 ) } ${ server } `;
90135
91- return ( await this . exec ( command , { async : false } ) ) as ShellString ;
136+ return await this . execSync ( command ) ;
92137 }
93138
94139 async logout ( server ?: string ) : Promise < ShellString > {
95140 const command = `${ this . container } logout ${ server } ` ;
96141
97- return ( await this . exec ( command , { async : false } ) ) as ShellString ;
142+ return await this . execSync ( command ) ;
98143 }
99144 //#endregion
100145
101146 //#region containers
102- async run ( image : string , flags ?: RunCommandFlags ) : Promise < ChildProcess > {
147+ async run (
148+ image : string ,
149+ flags ?: RunCommandFlags ,
150+ callback ?: ProcessCallback
151+ ) : Promise < boolean > {
103152 const command = `${ this . container } run ${ this . mergeFlags ( flags ) } ${ image } ` ;
104- return ( await this . exec ( command ) ) as ChildProcess ;
153+
154+ return await this . fork ( command , callback ) ;
105155 }
106156
107157 async stop (
@@ -111,10 +161,11 @@ export default abstract class BaseBackend {
111161 const containers = Array . isArray ( container )
112162 ? container . join ( " " )
113163 : container ;
114- return ( await this . exec (
115- `${ this . container } stop ${ this . mergeFlags ( flags ) } ${ containers } ` ,
116- { async : false }
117- ) ) as ShellString ;
164+ const command = `${ this . container } stop ${ this . mergeFlags (
165+ flags
166+ ) } ${ containers } `;
167+
168+ return await this . execSync ( command ) ;
118169 }
119170
120171 async remove (
@@ -124,25 +175,27 @@ export default abstract class BaseBackend {
124175 const containers = Array . isArray ( container )
125176 ? container . join ( " " )
126177 : container ;
127- return ( await this . exec (
128- `${ this . container } rm ${ this . mergeFlags ( flags ) } ${ containers } ` ,
129- { async : false }
130- ) ) as ShellString ;
178+ const command = `${ this . container } rm ${ this . mergeFlags (
179+ flags
180+ ) } ${ containers } `;
181+
182+ return await this . execSync ( command ) ;
131183 }
132184
133- async logs (
134- container : string ,
135- flags ?: LogsCommandFlags
136- ) : Promise < ChildProcess > {
137- return ( await this . exec (
138- `${ this . container } logs ${ this . mergeFlags ( flags ) } ${ container } `
139- ) ) as ChildProcess ;
185+ async logs ( container : string , flags ?: LogsCommandFlags ) : Promise < boolean > {
186+ const command = `${ this . container } logs ${ this . mergeFlags (
187+ flags
188+ ) } ${ container } `;
189+
190+ return await this . fork ( command ) ;
140191 }
141192 //#endregion
142193
143194 //#region images
144- async pullImage ( image : string ) : Promise < ChildProcess > {
145- return ( await this . exec ( `${ this . container } pull ${ image } ` ) ) as ChildProcess ;
195+ async pullImage ( image : string , callback ?: ProcessCallback ) : Promise < boolean > {
196+ const command = `${ this . container } pull ${ image } ` ;
197+
198+ return await this . fork ( command , callback ) ;
146199 }
147200
148201 async getImages ( ) : Promise < ImageResult [ ] > {
@@ -171,11 +224,9 @@ export default abstract class BaseBackend {
171224 flags ?: RemoveImageCommandFlags
172225 ) : Promise < ShellString > {
173226 const images = Array . isArray ( image ) ? image . join ( " " ) : image ;
227+ const command = `${ this . container } rmi ${ this . mergeFlags ( flags ) } ${ images } ` ;
174228
175- return ( await this . exec (
176- `${ this . container } rmi ${ this . mergeFlags ( flags ) } ${ images } ` ,
177- { async : false }
178- ) ) as ShellString ;
229+ return await this . execSync ( command ) ;
179230 }
180231 //#endregion
181232}
0 commit comments