forked from clarkmcc/go-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
120 lines (102 loc) · 4.17 KB
/
config.go
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
117
118
119
120
package typescript
import (
"encoding/base64"
"fmt"
"github.com/clarkmcc/go-typescript/utils"
"github.com/clarkmcc/go-typescript/versions"
_ "github.com/clarkmcc/go-typescript/versions/v5.1.6"
"github.com/dop251/goja"
)
// TranspileOptionFunc allows for easy chaining of pre-built config modifiers such as WithVersion.
type TranspileOptionFunc func(*Config)
// Config defines the behavior of the typescript compiler.
type Config struct {
CompileOptions map[string]interface{}
TypescriptSource *goja.Program
Runtime *goja.Runtime
// If a module is exported by the typescript compiler, this is the name the module will be called
ModuleName string
// Verbose enables built-in verbose logging for debugging purposes.
Verbose bool
// PreventCancellation indicates that the transpiler should not handle context cancellation. This
// should be used when external runtimes are configured AND cancellation is handled by those runtimes.
PreventCancellation bool
// decoderName refers to a random generated string assigned to a function in the runtimes
// global scope which is analogous to atob(), or a base64 decoding function. This function
// is needed in the transpile process to ensure that we don't have any issues with string
// interpolation errors when we pass our source code we want transpiled, into the typescript
// transpiler. The reason we use a randomly generated string is to avoid the situation where
// the transpiler caller provides their own runtime with a custom implementation of atob.
decoderName string
// Used only for testing to ensure that the compiler can handle config initialization failures
failOnInitialize bool
}
func (c *Config) Initialize() error {
if c.failOnInitialize {
return fmt.Errorf("intentional error")
}
c.decoderName = utils.RandomString()
return c.Runtime.Set(c.decoderName, utils.ErrorWrapper(c.Runtime, func(call goja.FunctionCall) (interface{}, error) {
bs, err := base64.StdEncoding.DecodeString(call.Argument(0).String())
if err != nil {
return nil, err
}
return string(bs), nil
}))
}
// NewDefaultConfig creates a new instance of the Config struct with default values and the latest
// typescript source code.s
func NewDefaultConfig() *Config {
return &Config{
Runtime: goja.New(),
CompileOptions: nil,
TypescriptSource: versions.DefaultRegistry.MustGet("v5.1.6"),
ModuleName: "default",
}
}
// WithVersion loads the provided tagged typescript source from the default registry
func WithVersion(tag string) TranspileOptionFunc {
return func(config *Config) {
config.TypescriptSource = versions.DefaultRegistry.MustGet(tag)
}
}
// WithTypescriptSource configures a Typescript source from the provided typescript source string which
// is compiled by goja when the config is initialized. This function will panic if the Typescript source
// is invalid.
func WithTypescriptSource(src string) TranspileOptionFunc {
return func(config *Config) {
config.TypescriptSource = goja.MustCompile("", src, true)
}
}
// WithCompileOptions sets the compile options that will be passed to the typescript compiler.
func WithCompileOptions(options map[string]interface{}) TranspileOptionFunc {
return func(config *Config) {
config.CompileOptions = options
}
}
// WithRuntime allows you to over-ride the default runtime
func WithRuntime(runtime *goja.Runtime) TranspileOptionFunc {
return func(config *Config) {
config.Runtime = runtime
}
}
// WithModuleName determines the module name applied to the typescript module if applicable. This is only needed to
// customize the module name if the typescript module mode is AMD or SystemJS.
func WithModuleName(name string) TranspileOptionFunc {
return func(config *Config) {
config.ModuleName = name
}
}
// WithPreventCancellation prevents the transpiler runtime from handling its own context cancellation.
func WithPreventCancellation() TranspileOptionFunc {
return func(config *Config) {
config.PreventCancellation = true
}
}
// withFailOnInitialize used to test a config initialization failure. This is not exported because
// it's used only for testing.
func withFailOnInitialize() TranspileOptionFunc {
return func(config *Config) {
config.failOnInitialize = true
}
}