From bd9c1beac79cb6ea13f26d37697918192772f2cb Mon Sep 17 00:00:00 2001 From: David Vogel Date: Mon, 3 Nov 2025 19:50:24 +0100 Subject: [PATCH] Add a way to change the working directory --- .vscode/settings.json | 1 + cli.go | 5 ++++- cli_test.go | 22 +++++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8c8f5d7..5842e79 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "Dadido", "Foogaloo", "golangci", + "somepath", "typst", "Vogel" ] diff --git a/cli.go b/cli.go index e53af10..89afadb 100644 --- a/cli.go +++ b/cli.go @@ -17,7 +17,8 @@ import ( // TODO: Add an interface for the Typst caller and let CLI (and later docker and WASM) be implementations of that type CLI struct { - ExecutablePath string // The Typst executable path can be overridden here. Otherwise the default path will be used. + ExecutablePath string // The Typst executable path can be overridden here. Otherwise the default path will be used. + WorkingDirectory string // The path where the Typst executable is run in. When left empty, the Typst executable will be run in the process's current directory. } // TODO: Add method for querying the Typst version resulting in a semver object @@ -31,6 +32,7 @@ func (c CLI) VersionString() (string, error) { } cmd := exec.Command(execPath, "--version") + cmd.Dir = c.WorkingDirectory // This doesn't do anything, but we will do it anyways for consistency. var output, errBuffer bytes.Buffer cmd.Stdout = &output @@ -64,6 +66,7 @@ func (c CLI) Compile(input io.Reader, output io.Writer, options *CLIOptions) err } cmd := exec.Command(execPath, args...) + cmd.Dir = c.WorkingDirectory cmd.Stdin = input cmd.Stdout = output diff --git a/cli_test.go b/cli_test.go index 7b93aff..c7158c7 100644 --- a/cli_test.go +++ b/cli_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2024 David Vogel +// Copyright (c) 2024-2025 David Vogel // // This software is released under the MIT License. // https://opensource.org/licenses/MIT @@ -9,6 +9,7 @@ import ( "bytes" "image" _ "image/png" + "path/filepath" "strconv" "testing" @@ -60,3 +61,22 @@ func TestCLI_Compile(t *testing.T) { t.Fatalf("Resulting image height is %d, expected %d.", imgConf.Height, inches*ppi) } } + +// Test basic compile functionality with a given working directory. +func TestCLI_CompileWithWorkingDir(t *testing.T) { + cli := typst.CLI{ + WorkingDirectory: filepath.Join(".", "test-files"), + } + + r := bytes.NewBufferString(`#import "hello-world-template.typ": template +#show: doc => template()`) + + var w bytes.Buffer + err := cli.Compile(r, &w, nil) + if err != nil { + t.Fatalf("Failed to compile document: %v.", err) + } + if w.Available() == 0 { + t.Errorf("No output was written.") + } +}