11#!/usr/bin/env node
22
3- 'use strict'
4-
5- import esmain from 'es-main'
6- import { RuntimeApiClient } from '@platformatic/control'
73import { select } from '@inquirer/prompts'
4+ import { RuntimeApiClient } from '@platformatic/control'
5+ import { parseArgs } from 'node:util'
86import { start } from './lib/start.js'
97
108async function getLocationDetails ( client , runtime ) {
@@ -40,11 +38,22 @@ async function getLocationDetails (client, runtime) {
4038 }
4139}
4240
43- let client
41+ function getSocket ( ) {
42+ const {
43+ values : { socket }
44+ } = parseArgs ( {
45+ options : {
46+ socket : { type : 'string' , short : 'S' }
47+ }
48+ } )
49+
50+ return socket
51+ }
52+
4453export default async function main ( ) {
4554 try {
4655 // Get available runtimes
47- client = new RuntimeApiClient ( )
56+ const client = new RuntimeApiClient ( { socket : getSocket ( ) } )
4857
4958 const runtimes = await client . getRuntimes ( )
5059
@@ -114,8 +123,7 @@ export default async function main () {
114123 console . log ( `PID: ${ selectedRuntime . pid } ` )
115124 console . log ( `Working directory: ${ locationDetails . cwd } ` )
116125
117- if ( locationDetails . configPath !== 'Unknown' &&
118- locationDetails . configPath !== 'Unknown (config not available)' ) {
126+ if ( locationDetails . configPath !== 'Unknown' && locationDetails . configPath !== 'Unknown (config not available)' ) {
119127 console . log ( `Configuration path: ${ locationDetails . configPath } ` )
120128 }
121129
@@ -136,17 +144,25 @@ export default async function main () {
136144 }
137145}
138146
139- // Execute the main function if this script is run directly
140- if ( esmain ( import . meta) ) {
141- main ( ) . then ( ( selectedRuntime ) => {
142- if ( ! selectedRuntime ) {
143- return
147+ export async function cli ( ) {
148+ try {
149+ const runtime = await main ( )
150+
151+ console . log ( runtime )
152+ if ( runtime ) {
153+ const client = new RuntimeApiClient ( { socket : getSocket ( ) } )
154+
155+ console . log ( 'Starting Watt admin...' )
156+ console . log ( '--------' )
157+ return await start ( client , runtime . pid )
144158 }
145- console . log ( 'Starting Watt admin...' )
146- console . log ( '--------' )
147- return start ( client , selectedRuntime . pid )
148- } ) . catch ( error => {
159+ } catch ( error ) {
149160 console . error ( 'Fatal error:' , error )
150161 process . exit ( 1 )
151- } )
162+ }
163+ }
164+
165+ // Execute the main function if this script is run directly
166+ if ( import . meta. main ) {
167+ await cli ( )
152168}
0 commit comments