-
Notifications
You must be signed in to change notification settings - Fork 114
/
project.go
226 lines (187 loc) · 4.24 KB
/
project.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
package project
import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"github.com/sst/ion/internal/fs"
"github.com/sst/ion/pkg/js"
"github.com/sst/ion/pkg/project/provider"
)
type App struct {
Name string `json:"name"`
Stage string `json:"stage"`
RemovalPolicy string `json:"removalPolicy"`
Providers map[string]map[string]string `json:"providers"`
}
type Project struct {
version string
root string
config string
process *js.Process
app *App
backend provider.Backend
Providers map[string]provider.Provider
env map[string]string
Stack *stack
}
func Discover() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
cfgPath, err := fs.FindUp(cwd, "sst.config.ts")
if err != nil {
return "", err
}
err = os.MkdirAll(ResolveWorkingDir(cfgPath), 0755)
if err != nil {
return "", err
}
return cfgPath, nil
}
func ResolveWorkingDir(cfgPath string) string {
return path.Join(filepath.Dir(cfgPath), ".sst")
}
func ResolvePlatformDir(cfgPath string) string {
return path.Join(ResolveWorkingDir(cfgPath), "platform")
}
type ProjectConfig struct {
Version string
Stage string
Config string
}
func New(input *ProjectConfig) (*Project, error) {
rootPath := filepath.Dir(input.Config)
process, err := js.Start(
rootPath,
)
if err != nil {
return nil, err
}
proj := &Project{
version: input.Version,
root: rootPath,
process: process,
config: input.Config,
}
proj.Stack = &stack{
project: proj,
}
tmp := proj.PathWorkingDir()
// platformDir := proj.PathPlatformDir()
_, err = os.Stat(tmp)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
err := os.Mkdir(tmp, 0755)
if err != nil {
return nil, err
}
}
inputBytes, err := json.Marshal(map[string]string{
"stage": input.Stage,
})
err = process.Eval(
js.EvalOptions{
Dir: tmp,
Banner: `
function $config(input) { return input }
`,
Define: map[string]string{
"$input": string(inputBytes),
},
Code: fmt.Sprintf(`
import mod from '%s';
console.log("~j" + JSON.stringify(mod.app({
stage: $input.stage || undefined,
})))`,
input.Config),
},
)
if err != nil {
return nil, err
}
for {
cmd, line := process.Scan()
if cmd == js.CommandDone {
break
}
if cmd != js.CommandJSON {
fmt.Println(line)
continue
}
var parsed App
err = json.Unmarshal([]byte(line), &parsed)
if err != nil {
return nil, err
}
proj.app = &parsed
proj.app.Stage = input.Stage
if proj.app.Providers == nil {
proj.app.Providers = map[string]map[string]string{}
}
if proj.app.Name == "" {
return nil, fmt.Errorf("Project name is required")
}
if proj.app.RemovalPolicy == "" {
proj.app.RemovalPolicy = "retain"
}
if proj.app.RemovalPolicy != "remove" && proj.app.RemovalPolicy != "retain" && proj.app.RemovalPolicy != "retain-all" {
return nil, fmt.Errorf("RemovalPolicy must be one of: remove, retain, retain-all")
}
}
return proj, nil
}
func (proj *Project) LoadProviders() error {
if _, ok := proj.app.Providers["aws"]; !ok {
proj.app.Providers["aws"] = map[string]string{}
}
proj.Providers = map[string]provider.Provider{}
for name, args := range proj.app.Providers {
var p provider.Provider
if name == "aws" {
p = &provider.AwsProvider{}
}
if name == "cloudflare" {
p = &provider.CloudflareProvider{}
}
if p == nil {
continue
}
err := p.Init(proj.app.Name, proj.app.Stage, args)
if err != nil {
return err
}
proj.Providers[name] = p
}
proj.backend = proj.Providers["aws"].(provider.Backend)
return nil
}
func (p *Project) getPath(path ...string) string {
paths := append([]string{p.PathWorkingDir()}, path...)
return filepath.Join(paths...)
}
func (p *Project) PathWorkingDir() string {
return filepath.Join(p.root, ".sst")
}
func (p *Project) PathPlatformDir() string {
return filepath.Join(p.PathWorkingDir(), "platform")
}
func (p *Project) PathRoot() string {
return p.root
}
func (p *Project) PathConfig() string {
return p.config
}
func (p *Project) Version() string {
return p.version
}
func (p *Project) App() *App {
return p.app
}
func (p *Project) Backend() provider.Backend {
return p.backend
}