github.com/LanceLRQ/deer-common@v0.0.9-0.20210319081233-e8222ac018a8/provider/nodejs.go (about)

     1  /* NodeJS Compiler Provider
     2   * (C) 2019 LanceLRQ
     3   *
     4   * This code is licenced under the GPLv3.
     5   */
     6  package provider
     7  
     8  import (
     9  	"fmt"
    10  	"strings"
    11  )
    12  
    13  type NodeJSCompileProvider struct {
    14  	CodeCompileProvider
    15  }
    16  
    17  func NewNodeJSCompileProvider() *NodeJSCompileProvider {
    18  	return &NodeJSCompileProvider{
    19  		CodeCompileProvider{
    20  			isReady:  false,
    21  			realTime: true,
    22  			Name:     "nodejs",
    23  		},
    24  	}
    25  }
    26  
    27  func (prov *NodeJSCompileProvider) Init(code string, workDir string) error {
    28  	prov.codeContent = code
    29  	prov.workDir = workDir
    30  
    31  	err := prov.checkWorkDir()
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	err = prov.initFiles(".js", "")
    37  	return err
    38  }
    39  
    40  func (prov *NodeJSCompileProvider) Compile() (result bool, errmsg string) {
    41  	result, errmsg = prov.shell(fmt.Sprintf(CompileCommands.NodeJS, prov.codeFilePath))
    42  	if result {
    43  		prov.isReady = true
    44  	}
    45  	return
    46  }
    47  
    48  func (prov *NodeJSCompileProvider) GetRunArgs() (args []string) {
    49  	args = []string{"/usr/bin/node", prov.codeFilePath}
    50  	return
    51  }
    52  
    53  func (prov *NodeJSCompileProvider) IsCompileError(remsg string) bool {
    54  	return strings.Contains(remsg, "SyntaxError") ||
    55  		strings.Contains(remsg, "Error: Cannot find module")
    56  }