1- import { ChildProcess , spawn } from "child_process"
2- import { err , isErr , ok , Result } from "../utilities/functional/result"
3- import { sqlmesh_exec } from "../utilities/sqlmesh/sqlmesh"
4- import * as vscode from ' vscode'
5- import { isProduction } from "../utilities/isDev"
6- import getPort from "../utilities/getPort"
7- import { ErrorType } from "../utilities/errors"
1+ import { ChildProcess , spawn } from "child_process" ;
2+ import { err , isErr , ok , Result } from "../utilities/functional/result" ;
3+ import { sqlmesh_exec } from "../utilities/sqlmesh/sqlmesh" ;
4+ import * as vscode from " vscode" ;
5+ import { isProduction } from "../utilities/isDev" ;
6+ import getPort from "../utilities/getPort" ;
7+ import { ErrorType } from "../utilities/errors" ;
88
9- export interface Server {
10- stop : ( ) => void ;
11- get_url : ( ) => string
12- }
9+ export type WebServerDetails = {
10+ url : string ;
11+ process : ChildProcess ;
12+ } ;
1313
14- export const startWebServer = async ( outputChannel : vscode . OutputChannel ) : Promise < Result < WebServer , ErrorType > > => {
15- const sqlmesh = await sqlmesh_exec ( )
16- if ( isErr ( sqlmesh ) ) {
17- return sqlmesh
18- }
19- const port = ! isProduction ( ) ? 5174 : await getPort ( )
20- const { bin, workspacePath, env } = sqlmesh . value
21- const process = spawn ( bin , [ 'ui' , '--port' , port . toString ( ) ] , {
22- cwd : workspacePath ,
23- env,
24- } )
25-
26- process . stdout . on ( 'data' , ( data ) => {
27- outputChannel . append ( data . toString ( ) )
28- } )
29- process . stderr . on ( 'data' , ( data ) => {
30- outputChannel . append ( data . toString ( ) )
31- } )
32-
33- return ok ( new WebServer ( port , `http://localhost:${ port } ` , process ) )
34- }
14+ export class WebServer implements vscode . Disposable {
15+ private details : WebServerDetails | undefined ;
16+ private outputChannel : vscode . OutputChannel ;
3517
36- export class WebServer implements Server {
37- port : number
38- url : string
39- process : ChildProcess
18+ constructor ( outputChannel : vscode . OutputChannel ) {
19+ this . details = undefined ;
20+ this . outputChannel = outputChannel ;
21+ }
4022
41- constructor ( port : number , url : string , process : ChildProcess ) {
42- this . port = port
43- this . url = url
44- this . process = process
23+ async start ( ) : Promise < Result < undefined , ErrorType > > {
24+ const sqlmesh = await sqlmesh_exec ( ) ;
25+ if ( isErr ( sqlmesh ) ) {
26+ return sqlmesh ;
4527 }
46-
47- get_url ( ) {
48- return this . url
28+ const port = ! isProduction ( ) ? 5174 : await getPort ( ) ;
29+ const { bin, workspacePath, env } = sqlmesh . value ;
30+ const process = spawn ( bin , [ "ui" , "--port" , port . toString ( ) ] , {
31+ cwd : workspacePath ,
32+ env,
33+ } ) ;
34+ process . stdout . on ( "data" , ( data ) => this . outputChannel . append ( data . toString ( ) ) )
35+ process . stderr . on ( "data" , ( data ) => this . outputChannel . append ( data . toString ( ) ) )
36+ this . details = {
37+ url : `http://localhost:${ port } ` ,
38+ process
4939 }
40+ return ok ( undefined )
41+ }
42+
43+ get_url ( ) : string | undefined {
44+ return this . details ?. url ;
45+ }
5046
51- stop ( ) {
52- this . process . kill ( )
47+ stop ( ) {
48+ if ( this . details ) {
49+ this . details . process . kill ( ) ;
50+ this . details = undefined ;
5351 }
52+ }
53+
54+ dispose ( ) {
55+ this . stop ( ) ;
56+ }
5457}
5558
5659export const queryServer = async (
57- root_url : string ,
58- path : string ,
59- body : any ,
60- method : string
60+ root_url : string ,
61+ path : string ,
62+ body : any | undefined ,
63+ method : string | undefined
6164) : Promise < Result < Object , string > > => {
62- try {
63- const url = `${ root_url } ${ path } `
64- console . log ( 'making request' , url , method , body )
65+ try {
66+ const requestMethod = method ?? "GET" ;
67+ const requestBody = body ?? undefined ;
68+ const url = `${ root_url } ${ path } ` ;
69+ console . log ( "making request" , url , requestMethod , requestBody ) ;
6570 const response = await fetch ( url , {
66- method,
67- body,
68- } )
69- return response . json ( )
70- } catch ( e ) {
71- return err ( "error o" )
72- }
73- }
71+ method : requestMethod ,
72+ body : requestBody ,
73+ } ) ;
74+ return response . json ( ) ;
75+ } catch ( e ) {
76+ return err ( "error o" ) ;
77+ }
78+ } ;
0 commit comments