Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: custom garden commands in dev mode (WIP) #4679

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions core/src/cli/command-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
import type { GlobalOptions, ParameterValues } from "./params"
import { bindActiveContext, withSessionContext } from "../util/tracing/context"
import { wrapActiveSpan } from "../util/tracing/spans"
import { GardenCli } from "./cli"

const defaultMessageDuration = 3000
const commandLinePrefix = chalk.yellow("🌼 > ")
Expand Down Expand Up @@ -133,13 +134,16 @@ export class CommandLine extends TypedEventEmitter<CommandLineEvents> {
private readonly log: Log
private readonly globalOpts: Partial<ParameterValues<GlobalOptions>>

private cli: GardenCli

constructor({
cwd,
manager,
log,
globalOpts,
serveCommand,
extraCommands,
cli,
history = [],
}: {
cwd: string
Expand All @@ -148,6 +152,7 @@ export class CommandLine extends TypedEventEmitter<CommandLineEvents> {
globalOpts: Partial<ParameterValues<GlobalOptions>>
serveCommand: ServeCommand
extraCommands: Command[]
cli: GardenCli
history?: string[]
}) {
super()
Expand All @@ -160,6 +165,7 @@ export class CommandLine extends TypedEventEmitter<CommandLineEvents> {
this.globalOpts = globalOpts
this.extraCommands = extraCommands
this.serveCommand = serveCommand
this.cli = cli

this.enabled = false
this.currentCommand = ""
Expand Down Expand Up @@ -339,7 +345,10 @@ export class CommandLine extends TypedEventEmitter<CommandLineEvents> {
})

// Execute
this.setKeyHandler("return", bindActiveContext(() => this.handleReturn()))
this.setKeyHandler(
"return",
bindActiveContext(() => this.handleReturn())
)

// Autocomplete
this.setKeyHandler("tab", () => {
Expand Down Expand Up @@ -671,6 +680,7 @@ ${chalk.white.underline("Keys:")}
opts,
commandLine: this,
parentCommand: this.serveCommand,
cli: this.cli,
}

const name = command.getFullName()
Expand Down Expand Up @@ -730,15 +740,17 @@ ${chalk.white.underline("Keys:")}
return
}

garden = await wrapActiveSpan("getGardenForRequest", () => this.manager.getGardenForRequest({
command,
projectConfig,
globalConfigStore: this.globalConfigStore,
log: this.log,
args,
opts,
sessionId,
}))
garden = await wrapActiveSpan("getGardenForRequest", () =>
this.manager.getGardenForRequest({
command,
projectConfig,
globalConfigStore: this.globalConfigStore,
log: this.log,
args,
opts,
sessionId,
})
)
} catch (error) {
this.flashError(getCmdFailMsg(name))
this.log.error({ error })
Expand Down
29 changes: 19 additions & 10 deletions core/src/commands/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import React, { FC, useState } from "react"
import { Box, render, Text, useInput, useStdout } from "ink"
import { serveArgs, ServeCommand, serveOpts } from "./serve"
import { LoggerType } from "../logger/logger"
import { ParameterError } from "../exceptions"
import { InternalError, ParameterError } from "../exceptions"
import { InkTerminalWriter } from "../logger/writers/ink-terminal-writer"
import { CommandLine } from "../cli/command-line"
import chalk from "chalk"
Expand Down Expand Up @@ -93,12 +93,15 @@ Use ${chalk.bold("up/down")} arrow keys to scroll through your command history.
if (terminalWriter.type === "ink") {
inkWriter = terminalWriter as InkTerminalWriter
} else {
throw new ParameterError({ message: `This command can only be used with the ink logger type`, detail: {
writerTypes: {
terminalWriter: terminalWriter.type,
fileWriters: logger.getWriters().file.map((w) => w.type),
throw new ParameterError({
message: `This command can only be used with the ink logger type`,
detail: {
writerTypes: {
terminalWriter: terminalWriter.type,
fileWriters: logger.getWriters().file.map((w) => w.type),
},
},
}})
})
}

const commandLine = await this.initCommandHandler(params)
Expand All @@ -123,9 +126,11 @@ Use ${chalk.bold("up/down")} arrow keys to scroll through your command history.
},
})

useInput(bindActiveContext((input, key) => {
commandLine.handleInput(input, key)
}))
useInput(
bindActiveContext((input, key) => {
commandLine.handleInput(input, key)
})
)

const width = stdout ? stdout.columns - 2 : 50

Expand Down Expand Up @@ -190,7 +195,10 @@ Use ${chalk.bold("up/down")} arrow keys to scroll through your command history.

private async initCommandHandler(params: ActionParams) {
const _this = this
const { garden, log, opts } = params
const { garden, log, opts, cli } = params
if (!cli) {
throw new InternalError({ message: `Missing cli argument in dev command.`, detail: {} })
}

// override the session for this manager to ensure we inherit from
// the initial garden dummy instance
Expand All @@ -205,6 +213,7 @@ Use ${chalk.bold("up/down")} arrow keys to scroll through your command history.
globalOpts: pick(opts, Object.keys(globalOptions)),
history: await garden.localConfigStore.get("devCommandHistory"),
serveCommand: this,
cli,
})
this.commandLine = cl

Expand Down