-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathnode.go
More file actions
116 lines (95 loc) · 2.61 KB
/
node.go
File metadata and controls
116 lines (95 loc) · 2.61 KB
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
package langs
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
type NodeLangHelper struct {
BaseHelper
Version string
}
func (h *NodeLangHelper) Handles(lang string) bool {
return defaultHandles(h, lang)
}
func (h *NodeLangHelper) Runtime() string {
return h.LangStrings()[0]
}
func (lh *NodeLangHelper) LangStrings() []string {
return []string{"node", fmt.Sprintf("node%s", lh.Version)}
}
func (lh *NodeLangHelper) Extensions() []string {
// this won't be chosen by default
return []string{".js"}
}
func (lh *NodeLangHelper) BuildFromImage() (string, error) {
return fmt.Sprintf("fnproject/node:%s-dev", lh.Version), nil
}
func (lh *NodeLangHelper) RunFromImage() (string, error) {
return fmt.Sprintf("fnproject/node:%s", lh.Version), nil
}
const funcJsContent = `const fdk=require('@fnproject/fdk');
fdk.handle(function(input){
let name = 'World';
if (input.name) {
name = input.name;
}
return {'message': 'Hello ' + name}
})
`
const packageJsonContent = `{
"name": "hellofn",
"version": "1.0.0",
"description": "example function",
"main": "func.js",
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@fnproject/fdk": ">=0.0.13"
}
}
`
func (h *NodeLangHelper) GenerateBoilerplate(path string) error {
pathToPackageJsonFile := filepath.Join(path, "package.json")
pathToFuncJs := filepath.Join(path, "func.js")
if exists(pathToPackageJsonFile) || exists(pathToFuncJs) {
return ErrBoilerplateExists
}
err := ioutil.WriteFile(pathToPackageJsonFile, []byte(packageJsonContent), os.FileMode(0644))
if err != nil {
return err
}
err = ioutil.WriteFile(pathToFuncJs, []byte(funcJsContent), os.FileMode(0644))
if err != nil {
return err
}
return nil
}
func (h *NodeLangHelper) HasBoilerplate() bool { return true }
// CustomMemory - no memory override here.
func (h *NodeLangHelper) CustomMemory() uint64 { return 0 }
func (h *NodeLangHelper) Entrypoint() (string, error) {
return "node func.js", nil
}
func (h *NodeLangHelper) DockerfileBuildCmds() []string {
r := []string{}
// skip npm -install if node_modules is local - allows local development
if exists("package.json") && !exists("node_modules") {
if exists("package-lock.json") {
r = append(r, "ADD package-lock.json /function/")
}
r = append(r,
"ADD package.json /function/",
"RUN npm install",
)
}
return r
}
func (h *NodeLangHelper) DockerfileCopyCmds() []string {
// excessive but content could be anything really
r := []string{"ADD . /function/"}
if exists("package.json") && !exists("node_modules") {
r = append(r, "COPY --from=build-stage /function/node_modules/ /function/node_modules/")
}
return r
}