github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/pkg/initializers/node/dockerfile_generator.go (about) 1 /* 2 * Copyright 2018 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package node 18 19 import ( 20 "path/filepath" 21 22 "github.com/projectriff/riff-cli/pkg/initializers/core" 23 "github.com/projectriff/riff-cli/pkg/options" 24 "github.com/projectriff/riff-cli/pkg/osutils" 25 ) 26 27 type NodeDockerFileTokens struct { 28 core.DockerFileTokens 29 PackageJSONExists bool 30 } 31 32 var nodeFunctionDockerfileTemplate = ` 33 FROM projectriff/node-function-invoker:{{.RiffVersion}} 34 {{ if .PackageJSONExists -}} 35 ENV FUNCTION_URI /functions/ 36 COPY . ${FUNCTION_URI} 37 RUN (cd ${FUNCTION_URI} && npm install --production) 38 {{- else -}} 39 ENV FUNCTION_URI /functions/{{.Artifact}} 40 ADD {{.ArtifactBase}} ${FUNCTION_URI} 41 {{- end }} 42 ` 43 44 var nodeFunctionDockerIgnoreTemplate = ` 45 {{ if .PackageJSONExists -}} 46 node_modules 47 {{- end }} 48 ` 49 50 func generateNodeFunctionDockerFile(opts options.InitOptions) (string, error) { 51 dockerFileTokens := generateDockerFileTokens(opts) 52 return core.GenerateFunctionDockerFileContents(nodeFunctionDockerfileTemplate, "docker-node", dockerFileTokens) 53 } 54 55 func generateNodeFunctionDockerIgnore(opts options.InitOptions) (string, error) { 56 dockerFileTokens := generateDockerFileTokens(opts) 57 return core.GenerateFunctionDockerIgnoreContents(nodeFunctionDockerIgnoreTemplate, "docker-node", dockerFileTokens) 58 } 59 60 func generateDockerFileTokens(opts options.InitOptions) NodeDockerFileTokens { 61 dockerFileTokens := NodeDockerFileTokens{} 62 dockerFileTokens.Artifact = opts.Artifact 63 dockerFileTokens.ArtifactBase = filepath.Base(opts.Artifact) 64 dockerFileTokens.RiffVersion = opts.RiffVersion 65 dockerFileTokens.PackageJSONExists = packageJSONExists(opts.FilePath) 66 return dockerFileTokens 67 } 68 69 func packageJSONExists(filePath string) bool { 70 if !osutils.IsDirectory(filePath) { 71 filePath = filepath.Dir(filePath) 72 } 73 return osutils.FileExists(filepath.Join(filePath, "package.json")) 74 }