Skip to content

Commit 113234f

Browse files
committed
DRY: Factor common code into a function.
1 parent 9e9b367 commit 113234f

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

build/build.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,11 @@ func (s *Session) BuildDir(packagePath string, importPath string, pkgObj string)
309309
}
310310
pkg := &PackageData{Package: buildPkg}
311311
pkg.ImportPath = "main"
312-
files, err := ioutil.ReadDir(pkg.Dir)
312+
jsFiles, err := jsFilesFromDir(pkg.Dir)
313313
if err != nil {
314314
return err
315315
}
316-
for _, file := range files {
317-
if strings.HasSuffix(file.Name(), ".inc.js") && file.Name()[0] != '_' {
318-
pkg.JsFiles = append(pkg.JsFiles, file.Name())
319-
}
320-
}
316+
pkg.JsFiles = jsFiles
321317
if err := s.BuildPackage(pkg); err != nil {
322318
return err
323319
}
@@ -370,15 +366,11 @@ func (s *Session) ImportPackage(path string) (*compiler.Archive, error) {
370366
}
371367
pkg := &PackageData{Package: buildPkg}
372368

373-
files, err := ioutil.ReadDir(pkg.Dir)
369+
jsFiles, err := jsFilesFromDir(pkg.Dir)
374370
if err != nil {
375371
return nil, err
376372
}
377-
for _, file := range files {
378-
if strings.HasSuffix(file.Name(), ".inc.js") && file.Name()[0] != '_' {
379-
pkg.JsFiles = append(pkg.JsFiles, file.Name())
380-
}
381-
}
373+
pkg.JsFiles = jsFiles
382374

383375
if err := s.BuildPackage(pkg); err != nil {
384376
return nil, err
@@ -583,6 +575,20 @@ func NewMappingCallback(m *sourcemap.Map, goroot, gopath string) func(generatedL
583575
}
584576
}
585577

578+
func jsFilesFromDir(dir string) ([]string, error) {
579+
files, err := ioutil.ReadDir(dir)
580+
if err != nil {
581+
return nil, err
582+
}
583+
var jsFiles []string
584+
for _, file := range files {
585+
if strings.HasSuffix(file.Name(), ".inc.js") && file.Name()[0] != '_' {
586+
jsFiles = append(jsFiles, file.Name())
587+
}
588+
}
589+
return jsFiles, nil
590+
}
591+
586592
// hasGopathPrefix returns true and the length of the matched GOPATH workspace,
587593
// iff file has a prefix that matches one of the GOPATH workspaces.
588594
func hasGopathPrefix(file, gopath string) (hasGopathPrefix bool, prefixLen int) {

0 commit comments

Comments
 (0)