Skip to content

Commit 49b4d53

Browse files
authored
Copy image dir (#119)
1 parent da69636 commit 49b4d53

File tree

2 files changed

+15
-46
lines changed

2 files changed

+15
-46
lines changed

build.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,7 @@ func build(c *modulir.Context) []error {
286286
//
287287
// Copy over remaining images to /content/images
288288
//
289-
if err := mfile.CopyFile(c, c.SourceDir+"/content/images/CoolsterCodes.png", c.TargetDir+"/content/images/CoolsterCodes.png"); err != nil {
290-
return []error{err}
291-
}
292-
if err := mfile.CopyFile(c, c.SourceDir+"/content/images/favicon.png", c.TargetDir+"/content/images/favicon.png"); err != nil {
293-
return []error{err}
294-
}
295-
296-
// Copy over "staticwebapp.config.json" to target build directory
297-
if err := mfile.CopyFile(c, c.SourceDir+"/web/staticwebapp.config.json", c.TargetDir+"/staticwebapp.config.json"); err != nil {
289+
if err := mfile.CopyDirectory(c, c.SourceDir+"/content/images", c.TargetDir+"/content/images"); err != nil {
298290
return []error{err}
299291
}
300292

modules/modulir/mfile/mfile.go

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,27 @@ import (
2424
//
2525
//////////////////////////////////////////////////////////////////////////////
2626

27-
// CopyDirectoryImages is a shortcut for copying over all non-md files into the /public/images/<identifier>/.
28-
func CopyDirectoryImages(c *modulir.Context, source, target string) error {
29-
dirs, err := ReadDirWithOptions(c, source, &ReadDirOptions{ShowDirs: true})
30-
if err != nil {
27+
// CopyDirectory copies all files from source directory directly to target directory (non-recursive).
28+
// This is useful for copying static assets like images without subdirectory structure.
29+
func CopyDirectory(c *modulir.Context, source, target string) error {
30+
// Ensure target directory exists
31+
if err := EnsureDir(c, target); err != nil {
3132
return err
3233
}
3334

34-
for _, dir := range dirs {
35-
// Read the files from that dir ignoring *.md
36-
files, err := ReadDirWithOptions(c, dir, &ReadDirOptions{IgnoreMDs: true})
37-
if err != nil {
38-
return err
39-
}
40-
41-
// Make new target directory path
42-
justNameOfDir := filepath.Base(dir)
43-
targetDir := filepath.Join(target, justNameOfDir)
35+
// Read files from source directory (ignoring subdirectories by default)
36+
files, err := ReadDirWithOptions(c, source, &ReadDirOptions{IgnoreMDs: true})
37+
if err != nil {
38+
return err
39+
}
4440

45-
// Ensure target directory exists
46-
if err = EnsureDir(c, targetDir); err != nil {
41+
// Copy all files to target directory
42+
for _, file := range files {
43+
if err = CopyFileToDir(c, file, target); err != nil {
4744
return err
4845
}
49-
50-
// Copy all files into there
51-
for _, file := range files {
52-
if err = CopyFileToDir(c, file, targetDir); err != nil {
53-
return err
54-
}
55-
}
5646
}
47+
5748
return nil
5849
}
5950

@@ -204,20 +195,6 @@ func MustAbs(path string) string {
204195
return absPath
205196
}
206197

207-
//
208-
// ReadDir
209-
//
210-
211-
// ReadDir reads files in a directory and returns a list of file paths.
212-
//
213-
// Unlike os.ReadDir, this function skips hidden, "meta" (i.e. prefixed by
214-
// an underscore), and Vim backup (i.e. suffixed with a tilde) files, and
215-
// returns a list of full paths (easier to plumb into other functions), and
216-
// sets up a watch on the listed source.
217-
func ReadDir(c *modulir.Context, source string) ([]string, error) {
218-
return ReadDirWithOptions(c, source, nil)
219-
}
220-
221198
// ReadDirOptions are options for ReadDirWithOptions.
222199
type ReadDirOptions struct {
223200
// RecurseDir tells the function to recurse into subdirectories.

0 commit comments

Comments
 (0)