Files
GenAIScript provides access to the file system of workspace and to the selected files in the user interface.
The file paths are rooted in the project workspace folder. In Visual Studio Code, this is the root folder opened (multi-root workspaces are not yet supported). Using the command line, the workspace root is the current working directory when launching the CLI.
env.files
The variable env.files
contains an array of files that have been
selected by the user through the user interface or the command line.
You can pass env.files
directly in the def
function and add additional filters to the files.
file output
Use defFileOutput to specify allowed file output paths and the description of the purpose of those files.
workspace
The workspace
object provides access to the file system of the workspace.
findFiles
Performs a search for files under the workspace. Glob patterns are supported.
grep
Performs a regex ‘grep’ search for files under the workspace using ripgrep. The pattern can be a string or a regular expression.
The pattern can also be a regex, in which case sensitivity follows the regex option.
readText
Reads the content of a file as text, relative to the workspace root.
It will automatically convert PDFs and DOCX files to text.
readJSON
Reads the content of a file as JSON (using a JSON5 parser).
readXML
Reads the content of a file as XML format.
readCSV
Reads the content of a file as CSV format.
In Typescript, you can type the output.
writeText
Writes text to a file, relative to the workspace root.
paths
The paths
object contains helper methods to manipulate file names.
Current path vs workspace path
By default, files are resolved relative to the workspace root. You can use the path
object to resolve paths relative to the current specification, env.spec
.
globs
File path “globs” are patterns used to match file and directory names. They are commonly used in Unix-like operating systems and programming languages to specify sets of filenames with wildcard characters. This section covers the basics of using globs in file paths with workspace.findFiles.
Glob patterns can have the following syntax:
*
to match zero or more characters in a path segment?
to match on one character in a path segment**
to match any number of path segments, including none{}
to group conditions (e.g.**/*.{ts,js}
matches all TypeScript and JavaScript files)[]
to declare a range of characters to match in a path segment (e.g.,example.[0-9]
to match on example.0, example.1, …)[!...]
to negate a range of characters to match in a path segment (e.g.,example.[!0-9]
to match on example.a, example.b, but not example.0)
Note: a backslash (\
“) is not valid within a glob pattern. If you have an existing file path to match against, consider to use the relative pattern support that takes care of converting any backslash into slash. Otherwise, make sure to convert any backslash to slash when creating the glob pattern.