Skip to content

Commit 3862d02

Browse files
authored
Merge pull request #197 from GeoNet/assets-init
feat: Expose InitAssets to allow projects initialise assets to another directory
2 parents df08ce9 + b7ad607 commit 3862d02

2 files changed

Lines changed: 37 additions & 26 deletions

File tree

weft/assets.go

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ type asset struct {
2727
sri string
2828
}
2929

30+
// debug print helper (to skip `b`)
31+
func (a asset) String() string {
32+
return fmt.Sprintf("asset{path:%s, hashedPath:%s, mime:%s, fileType:%s, sri:%s}", a.path, a.hashedPath, a.mime, a.fileType, a.sri)
33+
}
34+
3035
// assets is populated during init and then is only used for reading.
31-
var assets = make(map[string]*asset)
36+
var assets map[string]*asset
3237

3338
// assetHashes maps asset filename to the corresponding hash-prefixed asset pathname.
34-
var assetHashes = make(map[string]string)
39+
var assetHashes map[string]string
3540
var assetError error
3641

3742
func init() {
38-
assetError = initAssets("assets/assets", "assets")
43+
// optionally, one can call InitAssets() to re-init to another directory
44+
assetError = InitAssets("assets/assets", "assets")
3945
}
4046

4147
// As part of Subresource Integrity we need to calculate the hash of the asset, we do this when the asset is loaded into memory
@@ -320,35 +326,40 @@ func loadAsset(file, prefix string) (*asset, error) {
320326
return &a, nil
321327
}
322328

323-
// initAssets loads all assets below dir into global maps.
324-
func initAssets(dir, prefix string) error {
329+
// InitAssets loads all assets below dir into global maps.
330+
func InitAssets(dir, prefix string) error {
325331
var fileList []string
326332

327-
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
328-
fileList = append(fileList, path)
329-
return nil
330-
})
331-
if err != nil {
332-
return err
333-
}
334-
335-
for _, v := range fileList {
336-
fi, err := os.Stat(v)
333+
assets = make(map[string]*asset)
334+
assetHashes = make(map[string]string)
335+
assetError = func() error {
336+
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
337+
fileList = append(fileList, path)
338+
return nil
339+
})
337340
if err != nil {
338341
return err
339342
}
340343

341-
switch mode := fi.Mode(); {
342-
case mode.IsRegular():
343-
a, err := loadAsset(v, prefix)
344+
for _, v := range fileList {
345+
fi, err := os.Stat(v)
344346
if err != nil {
345347
return err
346348
}
347-
assets[a.hashedPath] = a
348-
assets[a.path] = a
349-
assetHashes[a.path] = a.hashedPath
349+
350+
switch mode := fi.Mode(); {
351+
case mode.IsRegular():
352+
a, err := loadAsset(v, prefix)
353+
if err != nil {
354+
return err
355+
}
356+
assets[a.hashedPath] = a
357+
assets[a.path] = a
358+
assetHashes[a.path] = a.hashedPath
359+
}
350360
}
351-
}
361+
return nil
362+
}()
352363

353-
return nil
364+
return assetError
354365
}

weft/assets_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestLoadAssets(t *testing.T) {
8989
}
9090

9191
func TestCreateSubResourceTag(t *testing.T) {
92-
err := initAssets("testdata", "testdata")
92+
err := InitAssets("testdata", "testdata")
9393
if err != nil {
9494
t.Error(err)
9595
}
@@ -138,7 +138,7 @@ func TestCreateSubResourceTag(t *testing.T) {
138138
}
139139

140140
func TestCreateSubResourcePreloadTag(t *testing.T) {
141-
err := initAssets("testdata", "testdata")
141+
err := InitAssets("testdata", "testdata")
142142
if err != nil {
143143
t.Error(err)
144144
}
@@ -177,7 +177,7 @@ func TestCreateSubResourcePreloadTag(t *testing.T) {
177177
}
178178

179179
func TestCreateImportTag(t *testing.T) {
180-
err := initAssets("testdata", "testdata")
180+
err := InitAssets("testdata", "testdata")
181181
if err != nil {
182182
t.Error(err)
183183
}

0 commit comments

Comments
 (0)