Skip to content

Commit 4101477

Browse files
committed
fix: execution of html pages
1 parent 5b2aa79 commit 4101477

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

cmd/main.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ func generate(
115115
return buf.String(), nil
116116
}
117117

118-
// TODO return correct exit code when -run or -exec fails
119-
func runBlock(block *parser.CodeBlock) error {
118+
// TODO return correct exit code when -run or -exec fails - is this fixed?
119+
func runBlock(block *parser.CodeBlock, tempDir string) error {
120120
switch block.Lang {
121121
case parser.Go:
122122
code := block.Code
123123
// TODO remove when prompt engineering is there to add "make it runnable"
124124
if !strings.HasPrefix(block.Code, "package") {
125125
code = fmt.Sprintf("package main\n\n%s", block.Code)
126126
}
127-
path := fmt.Sprintf("%smain.go", os.TempDir())
127+
path := fmt.Sprintf("%smain.go", tempDir)
128128
if err := os.WriteFile(path, []byte(code), 0644); err != nil {
129129
return err
130130
}
@@ -136,15 +136,27 @@ func runBlock(block *parser.CodeBlock) error {
136136
return err
137137
}
138138
case parser.HTML:
139-
path := fmt.Sprintf("%sindex.html", os.TempDir())
139+
path := fmt.Sprintf("%sindex.html", tempDir)
140140
if err := os.WriteFile(path, []byte(block.Code), 0644); err != nil {
141141
return err
142142
}
143143
if err := runCmd("open", fmt.Sprintf("file://%s", path)); err != nil {
144144
return err
145145
}
146+
case parser.JavaScript:
147+
path := fmt.Sprintf("%sscript.js", tempDir) // TODO can this be parsed from model output? sometimes changes
148+
if err := os.WriteFile(path, []byte(block.Code), 0644); err != nil {
149+
return err
150+
}
151+
// assumed to be part of html, so not executed separately
152+
case parser.CSS:
153+
path := fmt.Sprintf("%sstyle.css", tempDir) // TODO can this be parsed from model output? sometimes changes
154+
if err := os.WriteFile(path, []byte(block.Code), 0644); err != nil {
155+
return err
156+
}
157+
// assumed to be part of html, so not executed separately
146158
case parser.Python:
147-
path := fmt.Sprintf("%smain.py", os.TempDir())
159+
path := fmt.Sprintf("%smain.py", tempDir)
148160
if err := os.WriteFile(path, []byte(block.Code), 0644); err != nil {
149161
return err
150162
}
@@ -280,8 +292,9 @@ func cmd(ctx context.Context, usrMsg string, flagVals *flagValues) error {
280292
codeW, blockCh := parser.NewCode()
281293
go func() {
282294
defer close(done)
295+
tempDir := os.TempDir()
283296
for block := range blockCh {
284-
_ = runBlock(block)
297+
_ = runBlock(block, tempDir)
285298
}
286299
}()
287300
// no output to the user, we just execute the code
@@ -310,8 +323,9 @@ func cmd(ctx context.Context, usrMsg string, flagVals *flagValues) error {
310323
out.Close()
311324
}
312325
<-done
326+
tempDir := os.TempDir()
313327
for _, block := range blocks {
314-
if err := runBlock(block); err != nil {
328+
if err := runBlock(block, tempDir); err != nil {
315329
return "", err
316330
}
317331
}
@@ -338,13 +352,14 @@ func cmd(ctx context.Context, usrMsg string, flagVals *flagValues) error {
338352
return fmt.Errorf("failed to open /dev/tty: %w", err)
339353
}
340354
}
341-
if usrMsg == "" && pipeContent == "" {
355+
if usrMsg == "" && pipeContent == "" && !flagVals.Interactive {
342356
return fmt.Errorf("what's your command?")
343357
}
344358
if pipeContent != "" {
345359
usrMsg = fmt.Sprintf(CONTEXT_TEMPLATE, pipeContent, usrMsg)
346360
}
347361

362+
// TODO require -f flag for file input, use it for image too
348363
if _, err := os.Stat(usrMsg); err == nil {
349364
f, err := os.Open(usrMsg)
350365
if err != nil {

parser/code.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const (
1414
Go
1515
Bash
1616
HTML
17+
JavaScript
18+
CSS
1719
Python
1820
)
1921

@@ -25,6 +27,10 @@ func language(s string) Language {
2527
return Bash
2628
case "html":
2729
return HTML
30+
case "javascript", "js":
31+
return JavaScript
32+
case "css":
33+
return CSS
2834
case "python", "python3":
2935
return Python
3036
default:

0 commit comments

Comments
 (0)