github.com/iosif02/goja_nodejs@v1.0.1/process/module.go (about)

     1  package process
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"github.com/iosif02/goja"
     8  	"github.com/iosif02/goja_nodejs/require"
     9  )
    10  
    11  const ModuleName = "process"
    12  
    13  type Process struct {
    14  	env map[string]string
    15  }
    16  
    17  func Require(runtime *goja.Runtime, module *goja.Object) {
    18  	p := &Process{
    19  		env: make(map[string]string),
    20  	}
    21  
    22  	for _, e := range os.Environ() {
    23  		envKeyValue := strings.SplitN(e, "=", 2)
    24  		p.env[envKeyValue[0]] = envKeyValue[1]
    25  	}
    26  
    27  	o := module.Get("exports").(*goja.Object)
    28  	o.Set("env", p.env)
    29  }
    30  
    31  func Enable(runtime *goja.Runtime) {
    32  	runtime.Set("process", require.Require(runtime, ModuleName))
    33  }
    34  
    35  func init() {
    36  	require.RegisterCoreModule(ModuleName, Require)
    37  }