Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
05523aceb4 |
53
packer/deps.go
Normal file
53
packer/deps.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package packer
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// JavaScriptExt extension of JavaScript source file
|
||||
const JavaScriptExt = ".js"
|
||||
|
||||
func depsJavaScript(src string) ([]string, error) {
|
||||
deps := []string{}
|
||||
file, err := os.Open(src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if strings.HasPrefix(line, "import") || strings.HasPrefix(line, "require") {
|
||||
reg, err := regexp.Compile(`(\'.*\'|\".*\")`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
str := reg.FindString(line)
|
||||
if str != "" {
|
||||
deps = append(deps, strings.ReplaceAll(strings.ReplaceAll(str, "\"", ""), "'", ""))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return deps, nil
|
||||
}
|
||||
|
||||
// Deps to get the dependencies required from a source code in a file
|
||||
func Deps(src string) ([]string, error) {
|
||||
switch filepath.Ext(src) {
|
||||
case JavaScriptExt:
|
||||
{
|
||||
return depsJavaScript(src)
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("not support language: %s", filepath.Ext(src))
|
||||
}
|
||||
63
packer/deps_test.go
Normal file
63
packer/deps_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package packer
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeps(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
content string
|
||||
expect []string
|
||||
}{
|
||||
{name: "import with singe quote", content: "import 'pkg1'", expect: []string{"pkg1"}},
|
||||
{name: "import with double quote", content: `import "pkg1"`, expect: []string{"pkg1"}},
|
||||
{name: "require with singe quote", content: "require 'pkg1'", expect: []string{"pkg1"}},
|
||||
{name: "require with double quote", content: `require "pkg1"`, expect: []string{"pkg1"}},
|
||||
{
|
||||
name: "require and import with singe quote",
|
||||
content: `
|
||||
require 'pkg1'
|
||||
import 'pkg2'`,
|
||||
expect: []string{"pkg1", "pkg2"},
|
||||
},
|
||||
{
|
||||
name: "require and import with double quote",
|
||||
content: `
|
||||
require "pkg1"
|
||||
import "pkg2"`,
|
||||
expect: []string{"pkg1", "pkg2"},
|
||||
},
|
||||
{
|
||||
name: "require and import with double quote and single quote",
|
||||
content: `
|
||||
require 'pkg1'
|
||||
import "pkg2"`,
|
||||
expect: []string{"pkg1", "pkg2"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
fd, err := ioutil.TempFile("", "dep_pkg_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(c.content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
deps, err := Deps(fd.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(deps, c.expect) {
|
||||
t.Fatalf("%s: should get %s but got %s", c.name, c.expect, deps)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user