chore: UPdate types

This commit is contained in:
J H
2024-01-30 17:59:42 -07:00
parent ac51aa1924
commit b24a0145bc
5 changed files with 25 additions and 20 deletions

View File

@@ -194,7 +194,7 @@ export class StartSdk<Manifest extends SDKManifest, Store> {
effects: Effects effects: Effects
started(onTerm: () => void): null started(onTerm: () => void): null
utils: Utils<Manifest, Store, {}> utils: Utils<Manifest, Store, {}>
}) => Promise<Daemons<any>>, }) => Promise<Daemons<Manifest, any>>,
) => setupMain<Manifest, Store>(fn), ) => setupMain<Manifest, Store>(fn),
setupMigrations: <Migrations extends Array<Migration<Store, any>>>( setupMigrations: <Migrations extends Array<Migration<Store, any>>>(
...migrations: EnsureUniqueId<Migrations> ...migrations: EnsureUniqueId<Migrations>

View File

@@ -1,14 +1,21 @@
import { HealthReceipt } from "../health/HealthReceipt" import { HealthReceipt } from "../health/HealthReceipt"
import { CheckResult } from "../health/checkFns" import { CheckResult } from "../health/checkFns"
import { SDKManifest } from "../manifest/ManifestTypes"
import { Trigger } from "../trigger" import { Trigger } from "../trigger"
import { TriggerInput } from "../trigger/TriggerInput" import { TriggerInput } from "../trigger/TriggerInput"
import { defaultTrigger } from "../trigger/defaultTrigger" import { defaultTrigger } from "../trigger/defaultTrigger"
import { DaemonReturned, Effects, ValidIfNoStupidEscape } from "../types" import { DaemonReturned, Effects, ValidIfNoStupidEscape } from "../types"
import { createUtils } from "../util" import { createUtils } from "../util"
import { Signals } from "../util/utils" import { Signals } from "../util/utils"
type Daemon<Ids extends string, Command extends string, Id extends string> = { type Daemon<
Manifest extends SDKManifest,
Ids extends string,
Command extends string,
Id extends string,
> = {
id: "" extends Id ? never : Id id: "" extends Id ? never : Id
command: ValidIfNoStupidEscape<Command> | [string, ...string[]] command: ValidIfNoStupidEscape<Command> | [string, ...string[]]
imageId: Manifest["images"][number]
env?: Record<string, string> env?: Record<string, string>
ready: { ready: {
display: string | null display: string | null
@@ -42,11 +49,11 @@ Daemons.of({
}) })
``` ```
*/ */
export class Daemons<Ids extends string> { export class Daemons<Manifest extends SDKManifest, Ids extends string> {
private constructor( private constructor(
readonly effects: Effects, readonly effects: Effects,
readonly started: (onTerm: () => void) => null, readonly started: (onTerm: () => void) => null,
readonly daemons?: Daemon<Ids, "command", Ids>[], readonly daemons?: Daemon<Manifest, Ids, "command", Ids>[],
) {} ) {}
/** /**
* Returns an empty new Daemons class with the provided config. * Returns an empty new Daemons class with the provided config.
@@ -58,12 +65,12 @@ export class Daemons<Ids extends string> {
* @param config * @param config
* @returns * @returns
*/ */
static of(config: { static of<Manifest extends SDKManifest>(config: {
effects: Effects effects: Effects
started: (onTerm: () => void) => null started: (onTerm: () => void) => null
healthReceipts: HealthReceipt[] healthReceipts: HealthReceipt[]
}) { }) {
return new Daemons<never>(config.effects, config.started) return new Daemons<Manifest, never>(config.effects, config.started)
} }
/** /**
* Returns the complete list of daemons, including the one defined here * Returns the complete list of daemons, including the one defined here
@@ -78,13 +85,13 @@ export class Daemons<Ids extends string> {
ErrorDuplicateId<Id> extends Id ? never : ErrorDuplicateId<Id> extends Id ? never :
Id extends Ids ? ErrorDuplicateId<Id> : Id extends Ids ? ErrorDuplicateId<Id> :
Id, Id,
newDaemon: Omit<Daemon<Ids, Command, Id>, "id">, newDaemon: Omit<Daemon<Manifest, Ids, Command, Id>, "id">,
) { ) {
const daemons = ((this?.daemons ?? []) as any[]).concat({ const daemons = ((this?.daemons ?? []) as any[]).concat({
...newDaemon, ...newDaemon,
id, id,
}) })
return new Daemons<Ids | Id>(this.effects, this.started, daemons) return new Daemons<Manifest, Ids | Id>(this.effects, this.started, daemons)
} }
async build() { async build() {
@@ -96,10 +103,10 @@ export class Daemons<Ids extends string> {
daemon.requires?.map((id) => daemonsStarted[id]) ?? [], daemon.requires?.map((id) => daemonsStarted[id]) ?? [],
) )
daemonsStarted[daemon.id] = requiredPromise.then(async () => { daemonsStarted[daemon.id] = requiredPromise.then(async () => {
const { command } = daemon const { command, imageId } = daemon
const utils = createUtils(effects) const utils = createUtils<Manifest>(effects)
const child = utils.runDaemon(command, { env: daemon.env }) const child = utils.runDaemon(imageId, command, { env: daemon.env })
let currentInput: TriggerInput = {} let currentInput: TriggerInput = {}
const getCurrentInput = () => currentInput const getCurrentInput = () => currentInput
const trigger = (daemon.ready.trigger ?? defaultTrigger)( const trigger = (daemon.ready.trigger ?? defaultTrigger)(

View File

@@ -23,7 +23,7 @@ export const setupMain = <Manifest extends SDKManifest, Store>(
effects: Effects effects: Effects
started(onTerm: () => void): null started(onTerm: () => void): null
utils: Utils<Manifest, Store, {}> utils: Utils<Manifest, Store, {}>
}) => Promise<Daemons<any>>, }) => Promise<Daemons<Manifest, any>>,
): ExpectedExports.main => { ): ExpectedExports.main => {
return async (options) => { return async (options) => {
const result = await fn({ const result = await fn({

View File

@@ -148,7 +148,7 @@ export type CommandType<A extends string> =
| [string, ...string[]] | [string, ...string[]]
export type DaemonReturned = { export type DaemonReturned = {
wait(): Promise<string> wait(): Promise<null>
term(options?: { signal?: Signals; timeout?: number }): Promise<void> term(options?: { signal?: Signals; timeout?: number }): Promise<void>
} }

View File

@@ -234,21 +234,19 @@ export const utils = <
const childProcess = overlay.spawn(commands, { const childProcess = overlay.spawn(commands, {
env: options.env, env: options.env,
}) })
const answer = new Promise<string>((resolve, reject) => { const answer = new Promise<null>((resolve, reject) => {
const output: string[] = []
childProcess.stdout.on("data", (data) => { childProcess.stdout.on("data", (data) => {
output.push(data.toString()) console.log(data.toString())
}) })
const outputError: string[] = []
childProcess.stderr.on("data", (data) => { childProcess.stderr.on("data", (data) => {
outputError.push(data.toString()) console.error(data.toString())
}) })
childProcess.on("close", (code) => { childProcess.on("close", (code) => {
if (code === 0) { if (code === 0) {
return resolve(output.join("")) return resolve(null)
} }
return reject(outputError.join("")) return reject(new Error(`${commands[0]} exited with code ${code}`))
}) })
}) })