-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.go
50 lines (39 loc) · 1.06 KB
/
assets.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
package assets
import (
"fmt"
"os"
"path/filepath"
"github.com/itsliamegan/assets/esbuild"
"github.com/itsliamegan/assets/manifest"
)
type OutputFile = esbuild.OutputFile
func Build(entrypoints []string, outDir string, optimize bool) ([]OutputFile, manifest.Manifest, error) {
files, metadata, err := esbuild.Build(entrypoints, outDir, optimize)
if err != nil {
return nil, nil, err
}
mfest := make(manifest.Manifest)
for outputPath, output := range metadata.Outputs {
if output.Entrypoint == "" {
continue
}
entrypointBasename := filepath.Base(output.Entrypoint)
outputFileBasename := filepath.Base(outputPath)
mfest[entrypointBasename] = outputFileBasename
}
return files, mfest, nil
}
func WriteAll(files []OutputFile) error {
for _, file := range files {
parent := filepath.Dir(file.Path)
err := os.MkdirAll(parent, 0744)
if err != nil {
return fmt.Errorf("writing output file: %w", err)
}
err = os.WriteFile(file.Path, file.Contents, 0644)
if err != nil {
return fmt.Errorf("writing output file: %w", err)
}
}
return nil
}