github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/langs/node.go (about)

     1  package langs
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  )
     8  
     9  type NodeLangHelper struct {
    10  	BaseHelper
    11  }
    12  
    13  func (h *NodeLangHelper) Handles(lang string) bool {
    14  	return defaultHandles(h, lang)
    15  }
    16  func (h *NodeLangHelper) Runtime() string {
    17  	return h.LangStrings()[0]
    18  }
    19  
    20  func (lh *NodeLangHelper) LangStrings() []string {
    21  	return []string{"node"}
    22  }
    23  func (lh *NodeLangHelper) Extensions() []string {
    24  	// this won't be chosen by default
    25  	return []string{".js"}
    26  }
    27  
    28  func (lh *NodeLangHelper) BuildFromImage() (string, error) {
    29  	return "fnproject/node:dev", nil
    30  }
    31  func (lh *NodeLangHelper) RunFromImage() (string, error) {
    32  	return "fnproject/node", nil
    33  }
    34  
    35  const funcJsContent = `const fdk=require('@fnproject/fdk');
    36  
    37  fdk.handle(function(input){
    38    let name = 'World';
    39    if (input.name) {
    40      name = input.name;
    41    }
    42    return {'message': 'Hello ' + name}
    43  })
    44  `
    45  
    46  const packageJsonContent = `{
    47  	"name": "hellofn",
    48      "version": "1.0.0",
    49  	"description": "example function",
    50  	"main": "func.js",
    51  	"author": "",
    52  	"license": "Apache-2.0",
    53  	"dependencies": {
    54  		"@fnproject/fdk": ">=0.0.11"
    55  	}
    56  }
    57  `
    58  
    59  func (lh *NodeLangHelper) GenerateBoilerplate(path string) error {
    60  	pathToPackageJsonFile := filepath.Join(path, "package.json")
    61  	pathToFuncJs := filepath.Join(path, "func.js")
    62  
    63  	if exists(pathToPackageJsonFile) || exists(pathToFuncJs) {
    64  		return ErrBoilerplateExists
    65  	}
    66  
    67  	err := ioutil.WriteFile(pathToPackageJsonFile, []byte(packageJsonContent), os.FileMode(0644))
    68  	if err != nil {
    69  		return err
    70  	}
    71  	err = ioutil.WriteFile(pathToFuncJs, []byte(funcJsContent), os.FileMode(0644))
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  func (lh *NodeLangHelper) HasBoilerplate() bool { return true }
    80  
    81  func (lh *NodeLangHelper) DefaultFormat() string { return "http-stream" }
    82  
    83  func (lh *NodeLangHelper) Entrypoint() (string, error) {
    84  	return "node func.js", nil
    85  }
    86  
    87  func (h *NodeLangHelper) DockerfileBuildCmds() []string {
    88  	r := []string{}
    89  	// skip npm -install if node_modules is local - allows local development
    90  	if exists("package.json") && !exists("node_modules") {
    91  		if exists("package-lock.json") {
    92  			r = append(r, "ADD package-lock.json /function/")
    93  		}
    94  
    95  		r = append(r,
    96  			"ADD package.json /function/",
    97  			"RUN npm install",
    98  		)
    99  	}
   100  	return r
   101  }
   102  
   103  func (h *NodeLangHelper) DockerfileCopyCmds() []string {
   104  	// excessive but content could be anything really
   105  	r := []string{"ADD . /function/"}
   106  	if exists("package.json") && !exists("node_modules") {
   107  		r = append(r, "COPY --from=build-stage /function/node_modules/ /function/node_modules/")
   108  	}
   109  
   110  	return r
   111  }