Skip to content

Commit

Permalink
Refactor CLI configuration handling by integrating resolveCli function
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 13, 2024
1 parent d4d8067 commit f6c4240
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
27 changes: 5 additions & 22 deletions packages/vscode/src/fragmentcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@ import { ExtensionState } from "./state"
import { checkDirectoryExists, checkFileExists } from "./fs"
import { registerCommand } from "./commands"
import { templateGroup } from "../../core/src/ast"
import {
GENAI_ANY_REGEX,
TOOL_ID,
TOOL_NAME,
VSCODE_CONFIG_CLI_PATH,
VSCODE_CONFIG_CLI_VERSION,
} from "../../core/src/constants"
import { GENAI_ANY_REGEX, TOOL_ID, TOOL_NAME } from "../../core/src/constants"
import { NotSupportedError } from "../../core/src/error"
import { promptParameterTypeToJSONSchema } from "../../core/src/parameters"
import { Fragment } from "../../core/src/generation"
import { assert, dotGenaiscriptPath, groupBy } from "../../core/src/util"
import { CORE_VERSION } from "../../core/src/version"
import { semverParse, semverSatisfies } from "../../core/src/semver"
import { resolveCli } from "./config"

type TemplateQuickPickItem = {
template?: PromptScript
Expand Down Expand Up @@ -188,28 +181,18 @@ export function activateFragmentCommands(state: ExtensionState) {
files = [file]
}

const config = vscode.workspace.getConfiguration(TOOL_ID)
const program = config.get(VSCODE_CONFIG_CLI_PATH) as string
const { cliPath, cliVersion } = await resolveCli()

Check failure on line 184 in packages/vscode/src/fragmentcommands.ts

View workflow job for this annotation

GitHub Actions / build

The async function `resolveCli()` is called without error handling. If this function fails, it could cause the application to crash. Consider adding a try-catch block to handle potential errors. 😊
const args = [
"run",
vscode.workspace.asRelativePath(template.filename),
...files.map((file) =>
vscode.workspace.asRelativePath(file.fsPath)
),
]
const cliVersion =
(config.get(VSCODE_CONFIG_CLI_VERSION) as string) || CORE_VERSION
const gv = semverParse(CORE_VERSION)
if (!semverSatisfies(cliVersion, ">=" + gv.major + "." + gv.minor))
vscode.window.showWarningMessage(
TOOL_ID +
` - genaiscript cli version (${cliVersion}) outdated, please update to ${CORE_VERSION}`
)

const configuration = program
const configuration = cliPath
? <vscode.DebugConfiguration>{
name: TOOL_NAME,
program,
cliPath,
request: "launch",
skipFiles: ["<node_internals>/**", dotGenaiscriptPath("**")],
type: "node",
Expand Down
14 changes: 3 additions & 11 deletions packages/vscode/src/servermanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import {
OPEN,
TOOL_NAME,
ICON_LOGO_NAME,
CLIENT_RECONNECT_MAX_ATTEMPTS,
TOOL_ID,
VSCODE_CONFIG_CLI_VERSION,
VSCODE_CONFIG_CLI_PATH,
} from "../../core/src/constants"
import { ServerManager, host } from "../../core/src/host"
import { logError, logVerbose } from "../../core/src/util"
import { WebSocketClient } from "../../core/src/server/client"
import { CORE_VERSION } from "../../core/src/version"
import { createChatModelRunner } from "./lmaccess"
import { semverParse, semverSatisfies } from "../../core/src/semver"
import { resolveCli } from "./config"

export class TerminalServerManager implements ServerManager {
private _terminal: vscode.Terminal
Expand Down Expand Up @@ -86,15 +84,9 @@ export class TerminalServerManager implements ServerManager {
isTransient: true,
iconPath: new vscode.ThemeIcon(ICON_LOGO_NAME),
})
const config = vscode.workspace.getConfiguration(TOOL_ID)
const cliPath = config.get(VSCODE_CONFIG_CLI_PATH) as string
const { cliPath, cliVersion } = await resolveCli()
if (cliPath) this._terminal.sendText(`node "${cliPath}" serve`)
else {
const cliVersion =
(config.get(VSCODE_CONFIG_CLI_VERSION) as string) ||
CORE_VERSION
this._terminal.sendText(`npx --yes ${TOOL_ID}@${cliVersion} serve`)
}
else this._terminal.sendText(`npx --yes ${TOOL_ID}@${cliVersion} serve`)

Check failure on line 89 in packages/vscode/src/servermanager.ts

View workflow job for this annotation

GitHub Actions / build

The async function `resolveCli()` is called without error handling. If this function fails, it could cause the application to crash. Consider adding a try-catch block to handle potential errors. 😊
this._terminal.show()
}

Expand Down

0 comments on commit f6c4240

Please sign in to comment.