-
Notifications
You must be signed in to change notification settings - Fork 0
/
skeleton.go
66 lines (55 loc) · 1.47 KB
/
skeleton.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
package vss
import (
"embed"
"io/fs"
"os"
"path/filepath"
"strings"
)
//go:embed assets/*
var assets embed.FS
func GenerateSkeleton(distDir string) error {
if err := copyEmbedFiles(&assets, distDir); err != nil {
return err
}
// create static directory
err := os.MkdirAll(filepath.Join(distDir, "static"), os.ModePerm)
if err != nil {
return err
}
return nil
}
// copyEmbedFiles copies all files in the embed.FS to the destination directory.
func copyEmbedFiles(efs *embed.FS, distDir string) error {
// Create the destination directory if it doesn't exist
// TODO(zztkm): add a flag to force overwrite the dist directory
if err := createDistDir(distDir, false); err != nil {
return err
}
// Walk through the embed.FS and copy all files to the destination directory
if err := fs.WalkDir(efs, "assets", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
// Get the destination path by removing the "assets/" prefix
distPath := filepath.Join(distDir, strings.Replace(path, "assets/", "", 1))
// Create the destination directory if it doesn't exist
if err := os.MkdirAll(filepath.Dir(distPath), 0755); err != nil {
return err
}
// Copy the file to the destination directory
data, err := efs.ReadFile(path)
if err != nil {
return err
}
if err := os.WriteFile(distPath, data, 0644); err != nil {
return err
}
}
return nil
}); err != nil {
return err
}
return nil
}