-
Notifications
You must be signed in to change notification settings - Fork 235
/
node.go
181 lines (140 loc) · 4.21 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
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
package scanner
import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"github.com/superfly/flyctl/helpers"
)
func configureNode(sourceDir string, config *ScannerConfig) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("package.json")) {
return nil, nil
}
remix := checksPass(sourceDir, dirContains("package.json", "remix"))
prisma := checksPass(sourceDir, dirContains("package.json", "prisma"))
data, err := os.ReadFile("package.json")
if err != nil {
return nil, nil
}
var packageJson map[string]interface{}
err = json.Unmarshal(data, &packageJson)
if err != nil {
return nil, nil
}
s := &SourceInfo{
Family: "NodeJS",
Port: 8080,
}
env := map[string]string{
"PORT": "8080",
}
if remix {
s.Family = "Remix"
}
if prisma {
s.Family += "/Prisma"
}
vars := make(map[string]interface{})
var yarnVersion string = "latest"
// node-build requires a version, so either use the same version as install locally,
// or default to an LTS version
var nodeLtsVersion string = "18.16.0"
var nodeVersion string = nodeLtsVersion
out, err := exec.Command("node", "-v").Output()
if err == nil {
nodeVersion = strings.TrimSpace(string(out))
if nodeVersion[:1] == "v" {
nodeVersion = nodeVersion[1:]
}
if nodeVersion < "16" {
s.Notice += fmt.Sprintf("\n[WARNING] It looks like you have NodeJS v%s installed, but it has reached it's end of support. Using NodeJS v%s (LTS) to build your image instead.\n", nodeVersion, nodeLtsVersion)
nodeVersion = nodeLtsVersion
}
}
out, err = exec.Command("yarn", "-v").Output()
if err == nil {
yarnVersion = strings.TrimSpace(string(out))
}
package_files := []string{"package.json"}
_, err = os.Stat("yarn.lock")
vars["yarn"] = !os.IsNotExist(err)
if os.IsNotExist(err) {
vars["packager"] = "npm"
_, err = os.Stat("package-lock.json")
if !os.IsNotExist(err) {
package_files = append(package_files, "package-lock.json")
}
} else {
vars["packager"] = "yarn"
package_files = append(package_files, "yarn.lock")
}
vars["nodeVersion"] = nodeVersion
vars["yarnVersion"] = yarnVersion
vars["package_files"] = strings.Join(package_files, " ")
vars["remix"] = remix
vars["prisma"] = prisma
vars["runtime"] = s.Family
if checksPass(sourceDir+"/prisma", dirContains("*.prisma", "sqlite")) {
env["DATABASE_URL"] = "file:/data/sqlite.db"
s.SkipDatabase = true
s.Volumes = []Volume{
{
Source: "data",
Destination: "/data",
},
}
s.Notice += "\nThis launch configuration uses SQLite on a single, dedicated volume. It will not scale beyond a single VM. Look into 'fly postgres' for a more robust production database."
}
if remix {
bytes, err := helpers.RandBytes(32)
if err == nil {
s.Secrets = []Secret{
{
Key: "SESSION_SECRET",
Help: "Secret key used to verify the integrity of signed cookies",
Value: hex.EncodeToString(bytes),
},
}
}
}
s.SkipDeploy = true
vars["devDependencies"] = packageJson["devDependencies"] != nil
scripts, ok := packageJson["scripts"].(map[string]interface{})
vars["build"] = scripts["build"] != nil
if !ok || scripts["start"] == nil {
s.DeployDocs = `
Your Node app doesn't define a start script in package.json. You will need
to add one before you deploy. Also be sure to set your listen port
to 8080 using code similar to the following:
const port = process.env.PORT || "8080";
`
} else if remix {
s.DeployDocs = `
Your Remix app is prepared for deployment.
`
} else {
s.DeployDocs = `
Your Node app is prepared for deployment. Be sure to set your listen port
to 8080 using code similar to the following:
const port = process.env.PORT || "8080";
`
}
s.DeployDocs += `
If you need custom packages installed, or have problems with your deployment
build, you may need to edit the Dockerfile for app-specific changes. If you
need help, please post on https://community.fly.io.
Now: run 'fly deploy' to deploy your Node app.
`
files := templatesExecute("templates/node", vars)
// only include migration script if this app uses prisma
s.Files = make([]SourceFile, 0)
for _, file := range files {
if prisma || file.Path != "docker-entrypoint" {
s.Files = append(s.Files, file)
}
}
s.Env = env
return s, nil
}