-
Notifications
You must be signed in to change notification settings - Fork 215
/
node.go
53 lines (49 loc) · 1.12 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
)
// RunScript runs some node code
func (p *Plugins) RunScript(script string) (cmd *exec.Cmd, done func()) {
cacheTmp := filepath.Join(CacheHome, "tmp")
os.MkdirAll(cacheTmp, 0755)
f, _ := ioutil.TempFile(cacheTmp, "heroku-script-")
if f != nil {
defer f.Close()
if _, err := f.WriteString(script); err != nil {
LogIfError(err)
}
cmd = exec.Command(nodeBinPath(), f.Name())
} else {
cmd = exec.Command(nodeBinPath(), "-e", script)
}
cmd.Env = append([]string{"NODE_PATH=" + p.modulesPath()}, os.Environ()...)
return cmd, func() {
if f != nil {
os.Remove(f.Name())
}
}
}
func nodeBinPath() string {
b := os.Getenv("HEROKU_NODE_PATH")
ext := ""
if runtime.GOOS == WINDOWS {
ext = ".exe"
}
if b == "" {
b = filepath.Join(AppDir, "lib", "node"+ext)
}
if exists, _ := FileExists(b); !exists {
var err error
Debugf("node not found in %s. Using node from PATH\n", b)
b, err = exec.LookPath("node" + ext)
must(err)
}
return b
}
func (p *Plugins) modulesPath() string {
return filepath.Join(p.Path, "node_modules")
}