-
Notifications
You must be signed in to change notification settings - Fork 6
/
embedded.go
57 lines (51 loc) · 1.42 KB
/
embedded.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
package embedded
import (
"embed"
"os"
"path"
)
// embeddedRootDir is the name of the root directory for the embeddedFS variable.
const embeddedRootDir string = "resources"
//go:embed resources/*
var embeddedFS embed.FS
// entrypointName is the path to the entry point relative to the embeddedRootDir.
var entrypointName = path.Join("bin", "jsii-runtime.js")
// ExtractRuntime extracts a copy of the embedded runtime library into
// the designated directory, and returns the fully qualified path to the entry
// point to be used when starting the child process.
func ExtractRuntime(into string) (entrypoint string, err error) {
err = extractRuntime(into, embeddedRootDir)
if err == nil {
entrypoint = path.Join(into, entrypointName)
}
return
}
// extractRuntime copies the contents of embeddedFS at "from" to the provided
// "into" directory, recursively.
func extractRuntime(into string, from string) error {
files, err := embeddedFS.ReadDir(from)
if err != nil {
return err
}
for _, file := range files {
src := path.Join(from, file.Name())
dest := path.Join(into, file.Name())
if file.IsDir() {
if err = os.Mkdir(dest, 0o700); err != nil {
return err
}
if err = extractRuntime(dest, src); err != nil {
return err
}
} else {
data, err := embeddedFS.ReadFile(src)
if err != nil {
return err
}
if err = os.WriteFile(dest, data, 0o600); err != nil {
return err
}
}
}
return nil
}