Skip to content

Commit

Permalink
Add quoteify utility function and refactor task provider and nodehost…
Browse files Browse the repository at this point in the history
… code
  • Loading branch information
pelikhan committed Sep 13, 2024
1 parent cf74d4d commit d4d8067
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"promptdom",
"promptfoo",
"prompty",
"quoteify",
"stringifying",
"sysr",
"tabletojson",
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/nodehost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
ResponseStatus,
} from "../../core/src/host"
import { AbortSignalOptions, TraceOptions } from "../../core/src/trace"
import { logVerbose, unique } from "../../core/src/util"
import { logVerbose, quoteify, unique } from "../../core/src/util"
import { parseModelIdentifier } from "../../core/src/models"
import {
AuthenticationToken,
Expand Down Expand Up @@ -300,7 +300,6 @@ export class NodeHost implements RuntimeHost {
if (command === "python" && process.platform !== "win32")
command = "python3"

const quoteify = (a: string) => (/\s/.test(a) ? `"${a}"` : a)
logVerbose(
`${cwd ? `${cwd}> ` : ""}${quoteify(command)} ${args.map(quoteify).join(" ")}`
)

Check failure on line 305 in packages/cli/src/nodehost.ts

View workflow job for this annotation

GitHub Actions / build

The `quoteify` function is being used here, but it has been imported from `../../core/src/util`. The local definition of `quoteify` has been removed, so this code should work as expected. However, it's a good practice to avoid redefining functions locally that are already imported. 🧐
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ export function logError(msg: string | Error | SerializedError) {
if (!/^\s*\{\s*\}\s*$/.test(se)) host.log(LogLevel.Verbose, se)
}
}
export function quoteify(a: string) {
return /\s/.test(a) ? `"${a}"` : a
}

export function concatArrays<T>(...arrays: T[][]): T[] {
if (arrays.length == 0) return []
return arrays[0].concat(...arrays.slice(1))
Expand Down
8 changes: 0 additions & 8 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@
"script": {
"type": "string",
"description": "GenAIScript script to run"
},
"files": {
"type": "array",
"description": "Files to include in the script",
"items": {
"type": "string",
"description": "File or glob to include in the script"
}
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions packages/vscode/src/taskprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from "vscode"
import { ExtensionState } from "./state"
import { resolveCli } from "./config"
import { TOOL_ID } from "../../core/src/constants"
import { quoteify } from "../../core/src/util"

export async function activeTaskProvider(state: ExtensionState) {
const { context, host } = state
Expand All @@ -11,7 +12,7 @@ export async function activeTaskProvider(state: ExtensionState) {
provideTasks: async () => {
const { cliPath, cliVersion } = await resolveCli()
const exec = cliPath
? cliPath
? quoteify(cliPath)
: `npx --yes genaiscript@${cliVersion}`
const scripts = state.project.templates.filter((t) => !t.isSystem)
const tasks = scripts.map((script) => {
Expand All @@ -20,13 +21,15 @@ export async function activeTaskProvider(state: ExtensionState) {
script.filename
)
const task = new vscode.Task(
{ type: TOOL_ID, script: script.id },
{ type: TOOL_ID, script: script.filename },
vscode.TaskScope.Workspace,
script.id,
TOOL_ID,
new vscode.ShellExecution(
`${exec} run "${scriptp}" $\{relativeFile\}`
)
new vscode.ShellExecution(exec, [
"run",
scriptp,
"${relativeFile}",
])
)

Check failure on line 33 in packages/vscode/src/taskprovider.ts

View workflow job for this annotation

GitHub Actions / build

The `exec` command is constructed using hardcoded values. This could lead to issues if the command or its arguments change in the future. It would be better to use constants or configuration values for these, to make the code more maintainable and less prone to errors. Also, the use of template literals in the arguments array could lead to issues if the `relativeFile` variable is not defined or not a string. It would be safer to check the type and existence of `relativeFile` before using it. 😇
task.detail = `${script.title ?? script.description} - ${scriptp}`
task.problemMatchers = ["$tsc"]
Expand Down

0 comments on commit d4d8067

Please sign in to comment.