2
2
// SPDX-License-Identifier: MPL-2.0
3
3
4
4
import { ArtifactResolutionConfig } from "./gen/interfaces/metatype-typegraph-core.js" ;
5
+ import { BasicAuth , tgRemove } from "./tg_deploy.js" ;
5
6
import { TypegraphOutput } from "./typegraph.js" ;
6
7
import { getEnvVariable } from "./utils/func_utils.js" ;
7
8
@@ -10,21 +11,37 @@ const PORT_SOURCE = "META_CLI_SERVER_PORT";
10
11
11
12
type Command = "serialize" | "deploy" | "undeploy" | "unpack_migration" ;
12
13
14
+ // Types for CLI => SDK
13
15
type CLIServerResponse = {
14
16
command : Command ;
15
17
config : CLIConfigRequest ;
16
18
} ;
17
19
18
20
type CLIConfigRequest = {
19
- typegates : Record < string , string > ;
21
+ typegate : {
22
+ endpoint : string ;
23
+ auth : {
24
+ username : string ;
25
+ password : string ;
26
+ } ;
27
+ } ;
20
28
secrets : Record < string , string > ;
21
29
artifactsConfig : ArtifactResolutionConfig ;
22
30
} ;
23
31
24
- type SDKSuccess < T > = {
32
+ type CLISuccess < T > = {
25
33
data : T ;
26
34
} ;
27
35
36
+ // Types for SDK => CLi
37
+ type SDKResponse < T > = {
38
+ command : Command ;
39
+ data : T ;
40
+ } | {
41
+ command : Command ;
42
+ error : T ;
43
+ } ;
44
+
28
45
export class Manager {
29
46
#port: number ;
30
47
#typegraph: TypegraphOutput ;
@@ -60,7 +77,8 @@ export class Manager {
60
77
this . #deploy( config ) ;
61
78
break ;
62
79
case "undeploy" :
63
- this . #undeploy( ) ;
80
+ await this . #undeploy( config ) ;
81
+ break ;
64
82
case "unpack_migration" :
65
83
this . #unpackMigration( config ) ;
66
84
break ;
@@ -71,37 +89,59 @@ export class Manager {
71
89
72
90
async #requestCommands( ) : Promise < CLIServerResponse > {
73
91
const { data : config } = await this . #requestConfig( ) ;
74
-
75
92
const { data : command } =
76
93
await ( await fetch ( new URL ( "command" , this . #endpoint) ) )
77
- . json ( ) as SDKSuccess < Command > ;
94
+ . json ( ) as CLISuccess < Command > ;
78
95
79
96
console . error ( "Command" , command ) ;
80
97
81
98
return { command, config } ;
82
99
}
83
100
84
- async #requestConfig( ) : Promise < SDKSuccess < CLIConfigRequest > > {
101
+ async #requestConfig( ) : Promise < CLISuccess < CLIConfigRequest > > {
85
102
const response = await fetch ( new URL ( "config" , this . #endpoint) ) ;
86
- return ( await response . json ( ) ) as SDKSuccess < CLIConfigRequest > ;
103
+ return ( await response . json ( ) ) as CLISuccess < CLIConfigRequest > ;
87
104
}
88
105
89
106
#serialize( config : CLIConfigRequest ) : void {
90
107
const ret = this . #typegraph. serialize ( config . artifactsConfig ) ;
91
- // TODO:
92
- // send back through http
93
108
console . log ( ret ) ;
94
109
}
95
110
96
111
#deploy( config : CLIConfigRequest ) : void {
97
112
// TODO
98
113
}
99
114
100
- #undeploy( ) : void {
101
- // TODO
115
+ async #undeploy( { typegate } : CLIConfigRequest ) : Promise < void > {
116
+ const { endpoint, auth : { username, password } } = typegate ;
117
+ await this . #relayResultToCLI(
118
+ "undeploy" ,
119
+ async ( ) =>
120
+ await tgRemove ( this . #typegraph, {
121
+ baseUrl : endpoint ,
122
+ auth : new BasicAuth ( username , password ) ,
123
+ } ) ,
124
+ ) ;
102
125
}
103
126
104
127
#unpackMigration( config : CLIConfigRequest ) : void {
105
128
// TODO
106
129
}
130
+
131
+ async #relayResultToCLI< T > ( initiator : Command , fn : ( ) => Promise < T > ) {
132
+ let response : SDKResponse < any > ;
133
+ try {
134
+ const data = await fn ( ) ;
135
+ response = { command : initiator , data } ;
136
+ } catch ( err ) {
137
+ const msg = err instanceof Error ? err . message : err ;
138
+ response = { command : initiator , error : msg } ;
139
+ }
140
+
141
+ await fetch ( new URL ( "response" , this . #endpoint) , {
142
+ method : "POST" ,
143
+ headers : { "Content-Type" : "application/json" } ,
144
+ body : JSON . stringify ( response ) ,
145
+ } ) ;
146
+ }
107
147
}
0 commit comments