Skip to content

Commit 7f571f7

Browse files
authored
Merge pull request #138 from platformatic/custom-socket
feat: Support custom sockets.
2 parents 084c70c + 8db6609 commit 7f571f7

9 files changed

Lines changed: 747 additions & 686 deletions

File tree

cli.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import type { Runtime } from '@platformatic/control'
22

3+
export declare function cli (): Promise<Runtime>
34
export default function main (): Promise<Runtime | null>

cli.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#!/usr/bin/env node
22

3-
'use strict'
4-
5-
import esmain from 'es-main'
6-
import { RuntimeApiClient } from '@platformatic/control'
73
import { select } from '@inquirer/prompts'
4+
import { RuntimeApiClient } from '@platformatic/control'
5+
import { parseArgs } from 'node:util'
86
import { start } from './lib/start.js'
97

108
async 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+
4453
export 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
}

lib/start.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function start (client, selectedRuntime) {
5050

5151
// We move to the project root to start the server
5252
// so amaro will load correctly
53-
const cwd = process.cwd()
53+
const cwd = process.cwd()
5454
const root = join(import.meta.dirname, '..')
5555
process.chdir(root)
5656
const server = await create('watt.json', undefined, {
@@ -97,4 +97,6 @@ export async function start (client, selectedRuntime) {
9797
console.log(`Failure triggering the start command: ${await body.text()}`)
9898
}
9999
}
100+
101+
return server
100102
}

0 commit comments

Comments
 (0)