Skip to content

Commit

Permalink
Add extension to script file name for file commands (#601)
Browse files Browse the repository at this point in the history
Closes #600

---------

Co-authored-by: Sebastian Tiedtke <sebastiantiedtke@gmail.com>
  • Loading branch information
adambabik and sourishkrout authored Jun 4, 2024
1 parent 390020e commit 9b62370
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,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:
Expand Down
45 changes: 42 additions & 3 deletions internal/command/command_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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]
}
19 changes: 19 additions & 0 deletions internal/command/command_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
})
}

0 comments on commit 9b62370

Please sign in to comment.