forked from nodists/nodist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshim-npm.go
121 lines (91 loc) · 3.09 KB
/
shim-npm.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"./lib/nodist"
)
import . "github.com/tj/go-debug"
var debug = Debug("nodist:shim-npm")
func main() {
if "" == os.Getenv("NODIST_PREFIX") {
fmt.Println("Please set the path to the nodist directory in the NODIST_PREFIX environment variable.")
os.Exit(40)
}
dir, err := getTargetDirectory()
if err != nil {
fmt.Println("Sorry, there's a problem with nodist. Couldn't determine the target directory. Please report this.")
os.Exit(46)
}
// Determine node version first
spec := nodist.GetCurrentNodeVersionSpec(dir)
if spec == "" {
fmt.Println("Sorry, there's a problem with nodist. Couldn't decide which node version to use. Please set a version.")
os.Exit(41)
}
version, err := nodist.ResolveNodeVersion(spec)
if err != nil {
fmt.Println("Sorry, there's a problem with nodist. Couldn't resolve node version spec %s: %s", spec, err.Error())
os.Exit(44)
}
if version == "" {
fmt.Println("Sorry, there's a problem with nodist. Couldn't find an installed node version that matches version spec ", spec)
os.Exit(45)
}
debug("determined node version: %s", version)
// Determine npm version
npmSpec := nodist.GetCurrentNpmVersionSpec(dir)
debug("current npm version spec: %s", npmSpec)
if npmSpec == "" {
fmt.Println("Sorry, there's a problem with nodist. Couldn't decide which npm version to use. Please set a version.")
os.Exit(41)
}
npmVersion, err := nodist.ResolveNpmVersion(npmSpec, version)
if err != nil {
fmt.Println("Sorry, there's a problem with nodist. Couldn't resolve npm version spec", npmSpec, ":", err.Error())
os.Exit(44)
}
debug("determined npm version: %s", npmVersion)
// Set up binary path
path := os.Getenv("NODIST_PREFIX")+"/npmv"
path = path+"/"+npmVersion
npmbin := path+"/bin/npm-cli.js"
args := []string{npmbin}
args = append(args, os.Args[1:]...)
// Run npm!
cmd := exec.Command("node", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "NODIST_NODE_VERSION="+version)// Lock the node version for all child processes
// Set npm prefix correctly. Can't do this in installer, since npm doesn't know where to look (it looks at /v/x.x.x/ by default, so we'd have to put an npmrc in every version folder, which is overkill)
cmd.Env = append(cmd.Env, "npm_config_prefix="+os.Getenv("NODIST_PREFIX")+"/bin")
// Proxy signals
sigc := make(chan os.Signal, 1)
signal.Notify(sigc)
go func() {
for s := range sigc {
cmd.Process.Signal(s)
}
}()
err = cmd.Run()
signal.Stop(sigc)
if err != nil {
exitError, isExitError := err.(*(exec.ExitError))
if isExitError {
// You know it. Black Magic...
os.Exit(exitError.Sys().(syscall.WaitStatus).ExitStatus())
} else {
fmt.Println("Sorry, there's a problem with nodist.")
fmt.Println("Error: ", err)
os.Exit(42)
}
}
}
func getTargetDirectory() (dir string, returnedError error) {
dir, returnedError = os.Getwd()
return
}