forked from iris-framework/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typescript.go
241 lines (202 loc) · 6.25 KB
/
typescript.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Package typescript provides a typescript compiler with hot-reloader
// and optionally a cloud-based editor, called 'alm-tools'.
// typescript (by microsoft) and alm-tools (by basarat) have their own (open-source) licenses
// the tools are not used directly by this adaptor, but it's good to know where you can find
// the software.
package typescript
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/adaptors/typescript/npm"
)
type (
// TsAdaptor the struct of the Typescript TsAdaptor, holds all necessary fields & methods
TsAdaptor struct {
Config *Config
// taken from framework
logger func(iris.LogMode, string)
}
)
// New creates & returns a new instnace typescript plugin
func New() *TsAdaptor {
c := DefaultConfig()
if !strings.Contains(c.Ignore, nodeModules) {
c.Ignore += "," + nodeModules
}
return &TsAdaptor{Config: &c}
}
// Adapt addapts a TsAdaptor to the Policies via EventPolicy.
// We use that method instead of direct return EventPolicy from new because
// the user should be able to change its configuration from that public API
func (t *TsAdaptor) Adapt(frame *iris.Policies) {
policy := iris.EventPolicy{
Build: t.build,
}
policy.Adapt(frame)
}
func (t *TsAdaptor) build(s *iris.Framework) {
t.logger = s.Log
t.start()
}
//
// implementation
func (t *TsAdaptor) start() {
if t.hasTypescriptFiles() {
//Can't check if permission denied returns always exists = true....
if !npm.NodeModuleExists(t.Config.Bin) {
t.logger(iris.DevMode, "Installing typescript, please wait...")
res := npm.NodeModuleInstall("typescript")
if res.Error != nil {
t.logger(iris.ProdMode, res.Error.Error())
return
}
t.logger(iris.DevMode, res.Message)
}
projects := t.getTypescriptProjects()
if len(projects) > 0 {
watchedProjects := 0
//typescript project (.tsconfig) found
for _, project := range projects {
cmd := npm.CommandBuilder("node", t.Config.Bin, "-p", project[0:strings.LastIndex(project, npm.PathSeparator)]) //remove the /tsconfig.json)
projectConfig, perr := FromFile(project)
if perr != nil {
t.logger(iris.ProdMode, "error while trying to read tsconfig: "+perr.Error())
continue
}
if projectConfig.CompilerOptions.Watch {
watchedProjects++
// if has watch : true then we have to wrap the command to a goroutine (I don't want to use the .Start here)
go func() {
_, err := cmd.Output()
if err != nil {
t.logger(iris.DevMode, err.Error())
return
}
}()
} else {
_, err := cmd.Output()
if err != nil {
t.logger(iris.DevMode, err.Error())
return
}
}
}
// t.logger(iris.DevMode, fmt.Sprintf("%d Typescript project(s) compiled ( %d monitored by a background file watcher ) ", len(projects), watchedProjects))
} else {
//search for standalone typescript (.ts) files and compile them
files := t.getTypescriptFiles()
if len(files) > 0 {
/* watchedFiles := 0
if t.Config.Tsconfig.CompilerOptions.Watch {
watchedFiles = len(files)
}*/
//it must be always > 0 if we came here, because of if hasTypescriptFiles == true.
for _, file := range files {
absPath, err := filepath.Abs(file)
if err != nil {
continue
}
//these will be used if no .tsconfig found.
// cmd := npm.CommandBuilder("node", t.Config.Bin)
// cmd.Arguments(t.Config.Bin, t.Config.Tsconfig.CompilerArgs()...)
// cmd.AppendArguments(absPath)
compilerArgs := t.Config.Tsconfig.CompilerArgs()
cmd := npm.CommandBuilder("node", t.Config.Bin)
for _, s := range compilerArgs {
cmd.AppendArguments(s)
}
cmd.AppendArguments(absPath)
go func() {
compilerMsgB, _ := cmd.Output()
compilerMsg := string(compilerMsgB)
cmd.Args = cmd.Args[0 : len(cmd.Args)-1] //remove the last, which is the file
if strings.Contains(compilerMsg, "error") {
t.logger(iris.DevMode, compilerMsg)
}
}()
}
// t.logger(iris.DevMode, fmt.Sprintf("%d Typescript file(s) compiled ( %d monitored by a background file watcher )", len(files), watchedFiles))
}
}
}
}
func (t *TsAdaptor) hasTypescriptFiles() bool {
root := t.Config.Dir
ignoreFolders := strings.Split(t.Config.Ignore, ",")
hasTs := false
if !npm.Exists(root) {
t.logger(iris.ProdMode, fmt.Sprintf("Typescript Adaptor Error: Directory '%s' couldn't be found,\nplease specify a valid path for your *.ts files", root))
return false
}
// ignore error
filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
for i := range ignoreFolders {
if strings.Contains(path, ignoreFolders[i]) {
return nil
}
}
if strings.HasSuffix(path, ".ts") {
hasTs = true
return errors.New("Typescript found, hope that will stop here")
}
return nil
})
return hasTs
}
func (t *TsAdaptor) getTypescriptProjects() []string {
var projects []string
ignoreFolders := strings.Split(t.Config.Ignore, ",")
root := t.Config.Dir
//t.logger.Printf("\nSearching for typescript projects in %s", root)
// ignore error
filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
for i := range ignoreFolders {
if strings.Contains(path, ignoreFolders[i]) {
//t.logger.Println(path + " ignored")
return filepath.SkipDir
}
}
if strings.HasSuffix(path, npm.PathSeparator+"tsconfig.json") {
//t.logger.Printf("\nTypescript project found in %s", path)
projects = append(projects, path)
}
return nil
})
return projects
}
// this is being called if getTypescriptProjects return 0 len, then we are searching for files using that:
func (t *TsAdaptor) getTypescriptFiles() []string {
var files []string
ignoreFolders := strings.Split(t.Config.Ignore, ",")
root := t.Config.Dir
// ignore error
filepath.Walk(root, func(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
for i := range ignoreFolders {
if strings.Contains(path, ignoreFolders[i]) {
//t.logger.Println(path + " ignored")
return nil
}
}
if strings.HasSuffix(path, ".ts") {
//t.logger.Printf("\nTypescript file found in %s", path)
files = append(files, path)
}
return nil
})
return files
}
//
//