From 14d6d898fd8555ee2969de22ff475e7a756ad61c Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Mon, 3 Jun 2024 21:34:19 +0200 Subject: [PATCH 1/5] Add extension to script file name for file commands --- internal/command/command_file.go | 45 +++++++++++++++++++++++++-- internal/command/command_file_test.go | 19 +++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/internal/command/command_file.go b/internal/command/command_file.go index 88b22a012..c345e2b42 100644 --- a/internal/command/command_file.go +++ b/internal/command/command_file.go @@ -13,10 +13,10 @@ import ( type fileCommand struct { internalCommand + logger *zap.Logger + scriptFile *os.File tempDir string - - logger *zap.Logger } func (c *fileCommand) Start(ctx context.Context) error { @@ -71,11 +71,23 @@ func (c *fileCommand) createTempDir() (err error) { } func (c *fileCommand) createScriptFile() (err error) { - c.scriptFile, err = os.CreateTemp(c.tempDir, "runme-script-*") + pattern := "runme-script-*" + if ext := c.scriptFileExt(); ext != "" { + pattern += "." + ext + } + c.scriptFile, err = os.CreateTemp(c.tempDir, pattern) err = errors.WithMessage(err, "failed to create a temporary file for script execution") return } +func (c *fileCommand) scriptFileExt() string { + cfg := c.ProgramConfig() + if ext := cfg.GetFileExtension(); ext != "" { + return ext + } + return inferFileExtension(cfg.GetLanguageId()) +} + func (c *fileCommand) removeTempDir() error { if c.tempDir == "" { return nil @@ -90,3 +102,30 @@ func (c *fileCommand) writeScript(script string) error { } return errors.WithMessage(c.scriptFile.Close(), "failed to close the temporary file") } + +var fileExtensionByLanguageID = map[string]string{ + "js": "js", + "javascript": "js", + "jsx": "jsx", + "javascriptreact": "jsx", + "tsx": "tsx", + "typescriptreact": "tsx", + "typescript": "ts", + "ts": "ts", + "sh": "sh", + "bash": "sh", + "ksh": "sh", + "zsh": "sh", + "fish": "sh", + "powershell": "ps1", + "cmd": "bat", + "dos": "bat", + "py": "py", + "python": "py", + "ruby": "rb", + "rb": "rb", +} + +func inferFileExtension(languageID string) string { + return fileExtensionByLanguageID[languageID] +} diff --git a/internal/command/command_file_test.go b/internal/command/command_file_test.go index a63d21006..f051b9a2e 100644 --- a/internal/command/command_file_test.go +++ b/internal/command/command_file_test.go @@ -40,4 +40,23 @@ func TestFileCommand(t *testing.T) { testExecuteCommand(t, cfg, nil, "test\n", "") }) + + // TypeScript runner requires the file extension to be .ts. + t.Run("TypeScript", func(t *testing.T) { + t.Parallel() + + cfg := &ProgramConfig{ + LanguageId: "ts", + Source: &runnerv2alpha1.ProgramConfig_Script{ + Script: `function print(message: string): void { + console.log(message) +} +print("important message") +`, + }, + Mode: runnerv2alpha1.CommandMode_COMMAND_MODE_FILE, + } + + testExecuteCommand(t, cfg, nil, "important message\n", "") + }) } From 9cc045d6b5c4ee31e56088873ce6d1f294912e25 Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Tue, 4 Jun 2024 16:10:40 +0000 Subject: [PATCH 2/5] Use deno in lieu of ts-node --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88a0aa56d..df6ff0b6c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,9 @@ jobs: uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} + - uses: denoland/setup-deno@v1 + with: + deno-version: v1.x - name: Install dependencies run: make install/dev # It duplicates running linter from pre-commit From f7a10470df8f3e31cb0a8fea436f61df9ad85575 Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Tue, 4 Jun 2024 16:12:57 +0000 Subject: [PATCH 3/5] Incomplete deno --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df6ff0b6c..35d5940fe 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,7 @@ jobs: uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} + - name: Setup deno - uses: denoland/setup-deno@v1 with: deno-version: v1.x From 1e7ad813474ccc5e507aff460b1d1f7673d1556c Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Tue, 4 Jun 2024 16:14:10 +0000 Subject: [PATCH 4/5] Ugh --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 35d5940fe..866ec1292 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,8 +21,8 @@ jobs: uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} - - name: Setup deno - uses: denoland/setup-deno@v1 + - name: Setup deno with: deno-version: v1.x - name: Install dependencies From 34d02cda6c4ae1d3ba4ddaa6d199ab8e4e08c245 Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Tue, 4 Jun 2024 16:16:03 +0000 Subject: [PATCH 5/5] Facepalm --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 866ec1292..17399d150 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,10 +21,6 @@ jobs: uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} - - uses: denoland/setup-deno@v1 - - name: Setup deno - with: - deno-version: v1.x - name: Install dependencies run: make install/dev # It duplicates running linter from pre-commit @@ -49,6 +45,10 @@ jobs: with: fetch-depth: 0 fetch-tags: true + - name: Setup deno + uses: denoland/setup-deno@v1 + with: + deno-version: v1.x - name: Setup go uses: actions/setup-go@v5 with: