Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension to script file name for file commands #601

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", "")
})
}
Loading