Skip to content
Snippets Groups Projects

resolves #6 support client/server adapter

3 files
+ 71
13
Compare changes
  • Side-by-side
  • Inline
Files
3
/* global fetch */
import { exec } from 'node:child_process'
export default class AdapterManager {
constructor (adapterConfig) {
this.adapter = adapterConfig.adapter.cli.command
class AdapterServerManager {
constructor ({ startCommand, stopCommand, url }) {
this.startCommand = startCommand
this.stopCommand = stopCommand
this.url = url
}
async post (data) {
const response = await fetch(this.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
return response.json()
}
async start () {
if (this.startCommand !== undefined) {
const url = await new Promise((resolve, reject) => {
exec(this.startCommand, (err, stdout, _stderr) => {
if (err) return reject(err)
resolve(stdout)
})
})
if (this.url === undefined) {
this.url = url
}
}
}
stop () {
if (this.stopCommand !== undefined) {
return new Promise((resolve, reject) => {
exec(this.stopCommand, (err, stdout, _stderr) => {
if (err) return reject(err)
resolve(stdout)
})
})
}
}
}
class AdapterCliManager {
constructor ({ command }) {
this.command = command
}
post (data) {
return new Promise((resolve, reject) => {
const cp = exec(this.adapter, (err, stdout, _stderr) => {
const cp = exec(this.command, (err, stdout, _stderr) => {
if (err) return reject(err)
resolve(JSON.parse(stdout))
})
@@ -15,7 +60,17 @@ export default class AdapterManager {
})
}
start () {}
start () {
}
stop () {
}
}
stop () {}
export function createAdapterManager (config) {
const adapterConfig = config.adapter
if (adapterConfig.server) {
return new AdapterServerManager(adapterConfig.server)
}
return new AdapterCliManager(adapterConfig.cli)
}
Loading