-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (68 loc) · 1.54 KB
/
main.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
package main
import (
"embed"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/mkideal/next/src/compile"
)
//go:embed builtin/*
var builtin embed.FS
type embedFS struct {
embed.FS
}
func (e embedFS) Abs(name string) (string, error) {
return name, nil
}
type dirFS struct {
baseDir string
fs.FS
}
func (d dirFS) Abs(name string) (string, error) {
return filepath.Join(d.baseDir, name), nil
}
func copyBuiltin() compile.FileSystem {
builtin := embedFS{FS: builtin}
if x := strings.ToLower(os.Getenv(compile.NEXT_NOCOPYBUILTIN)); x == "1" || x == "true" || x == "on" || x == "yes" {
return builtin
}
homeDir, err := os.UserHomeDir()
if err != nil {
return builtin
}
builtinBaseDir := filepath.Join(homeDir, ".next")
builtinDir := filepath.Join(builtinBaseDir, "builtin")
if err := os.MkdirAll(builtinDir, 0755); err != nil {
return builtin
}
entries, err := builtin.ReadDir("builtin")
if err != nil {
return builtin
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
data, err := builtin.ReadFile("builtin/" + entry.Name())
if err != nil {
return builtin
}
path := filepath.Join(builtinDir, entry.Name())
if content, err := os.ReadFile(path); err == nil && string(content) == string(data) {
continue
}
os.Chmod(path, 0644)
if err := os.WriteFile(path, data, 0644); err != nil {
return builtin
}
os.Chmod(path, 0444)
}
return dirFS{
baseDir: builtinBaseDir,
FS: os.DirFS(builtinBaseDir),
}
}
func main() {
compile.Compile(compile.StandardPlatform(), copyBuiltin(), os.Args)
}