forked from whyrusleeping/gx-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
210 lines (194 loc) · 4.8 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Package gx-js provides node specific hooks to be called by the gx tool.
*/
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
cli "github.com/codegangsta/cli"
)
var cwd string
var postInstallHookCommand = cli.Command{
Name: "post-install",
Usage: "post install hook for newly installed node packages",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "global",
Usage: "specifies whether or not the install was global",
},
},
Action: func (c *cli.Context) {
if !c.Args().Present() {
log.Fatal("must specify path to newly installed package")
}
path := c.Args().First()
packageJSON := ""
recurse := false
err := filepath.Walk(path, func(path string, fileinfo os.FileInfo, err error) error {
if fileinfo.Name() == "package.json" && fileinfo.IsDir() == false {
fmt.Println(path)
packageJSON = path
recurse = false
}
if recurse == true {
return filepath.SkipDir
}
return err
})
if err != nil {
log.Fatal("post-install could not find package.json")
}
fmt.Println(packageJSON)
fmt.Println(getNodeModulesBinFolder(path, c.Bool("global")))
binFolder := getNodeModulesBinFolder(path, c.Bool("global"))
packageRoot := filepath.Dir(packageJSON)
content, err := ioutil.ReadFile(packageJSON)
if err != nil {
log.Fatal("post-install could not read package.json")
}
var dat map[string]interface{}
if err := json.Unmarshal(content, &dat); err != nil {
log.Fatal("post-install could not parse package.json")
}
fmt.Println(dat)
name := dat["name"].(string)
var bin map[string]interface{}
var binString string
binObject, ok := dat["bin"].(map[string]interface{})
if !ok {
binString, ok = dat["bin"].(string)
if !ok {
}
}
fmt.Println(binObject, binString)
if len(binObject) != 0 {
bin = binObject
}
if len(binString) != 0 {
bin = make(map[string]interface{})
bin[name] = binString
}
fmt.Println(bin)
for executable, location := range bin {
f1 := filepath.Join(packageRoot, location.(string))
f2 := filepath.Join(binFolder, executable)
fmt.Println(f2, " -> ", f1)
var perm os.FileMode = 0755
err := os.MkdirAll(binFolder, perm)
if err != nil {
log.Fatal("install-path could not create bin directory")
}
err = os.Symlink(f1, f2)
if (err != nil) {
log.Fatal("install-path could not create symlink:", err)
}
err = os.Chmod(f2, perm)
if (err != nil) {
log.Fatal("install-path could not change mode on the bin file", err)
}
}
},
}
var installPathHookCommand = cli.Command{
Name: "install-path",
Usage: "prints out the install path",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "global",
Usage: "print the global install directory",
},
},
Action: func(c *cli.Context) {
var npath string
if c.Bool("global") {
n, err := getNodejsExecutablePath()
if err != nil {
log.Fatal("install-path failed to get node path:", err)
}
npath = n
} else {
wd, err := os.Getwd()
if err != nil {
log.Fatal("install-path failed to get cwd:", err)
}
npath = wd
}
npath = getNodeModulesFolderPath(npath, c.Bool("global"))
fmt.Println(npath)
},
}
// HookCommand provides `gx-js hook [hook] [args]`.
var HookCommand = cli.Command{
Name: "hook",
Usage: "node specific hooks to be called by the gx tool",
Subcommands: []cli.Command{
installPathHookCommand,
postInstallHookCommand,
},
Action: func(c *cli.Context) {},
}
func main() {
app := cli.NewApp()
app.Name = "gx-js"
app.Author = "sterpe"
app.Usage = "gx extensions for node.js"
app.Version = "0.1.0"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Usage: "turn on verbose output",
},
}
wd, err := os.Getwd()
if err != nil {
log.Fatal("failed to get cwd:", err)
}
cwd = wd
app.Commands = []cli.Command{
HookCommand,
{
Name: "install-path",
Category: "hook",
},
}
app.Run(os.Args)
}
func getNodeModulesBinFolder(path string, global bool) (string) {
dir := filepath.Dir(path)
base := filepath.Base(dir)
dir = filepath.Join(dir, "..")
for base != "node_modules" && (base != "." && base != "/") {
base = filepath.Base(dir)
dir = filepath.Join(dir, "..")
}
dir = filepath.Join(dir, "node_modules")
if global {
bin := filepath.Join(dir, "..", "..", "bin")
return bin
}
dotbin := filepath.Join(dir, ".bin")
return dotbin
}
func getNodeModulesFolderPath(path string, global bool) (string) {
if global {
path = filepath.Join(path, "..", "lib")
}
return filepath.Join(path, "node_modules")
}
func getNodejsExecutablePath() (string, error) {
out, err := exec.Command("node", "-p", "process.execPath").Output()
node := strings.TrimSpace(string(out[:]))
if err != nil {
return node, err
} else if node == "" {
return node, fmt.Errorf("node binary not found")
}
dir, _ := filepath.Split(node)
return dir, nil
}