Explorar o código

Extract nodejs scripts from package.json

Mohammed Nafees %!s(int64=4) %!d(string=hai) anos
pai
achega
b810d35fe3

+ 3 - 2
cmd/test-runtime/main.go

@@ -145,8 +145,9 @@ func main() {
 	workingDir = os.Args[1]
 	workingDir = os.Args[1]
 
 
 	nodeRuntime := runtimes.NewNodeRuntime()
 	nodeRuntime := runtimes.NewNodeRuntime()
-	buildpackInfo := nodeRuntime.Detect(workingDir)
-	if buildpackInfo != nil {
+	buildpackInfo, data := nodeRuntime.Detect(workingDir)
+	if data != nil {
+		fmt.Println(data)
 		for i := 0; i < len(buildpackInfo.Packs); i++ {
 		for i := 0; i < len(buildpackInfo.Packs); i++ {
 			fmt.Println(buildpackInfo.Packs[i])
 			fmt.Println(buildpackInfo.Packs[i])
 		}
 		}

+ 1 - 2
cmd/test-runtime/runtimes/common.go

@@ -16,7 +16,6 @@ type BuildpackInfo struct {
 	// FIXME: env vars for https://github.com/paketo-buildpacks/environment-variables
 	// FIXME: env vars for https://github.com/paketo-buildpacks/environment-variables
 	//        and for https://github.com/paketo-buildpacks/image-labels
 	//        and for https://github.com/paketo-buildpacks/image-labels
 	EnvVars map[string]string
 	EnvVars map[string]string
-	Data    interface{}
 }
 }
 
 
 func newBuildpackInfo() *BuildpackInfo {
 func newBuildpackInfo() *BuildpackInfo {
@@ -42,5 +41,5 @@ func getExecPath() string {
 }
 }
 
 
 type Runtime interface {
 type Runtime interface {
-	Detect(string) *BuildpackInfo
+	Detect(string) (BuildpackInfo, map[string]interface{})
 }
 }

+ 30 - 8
cmd/test-runtime/runtimes/nodejs.go

@@ -11,6 +11,7 @@ import (
 	"github.com/paketo-buildpacks/packit"
 	"github.com/paketo-buildpacks/packit"
 	yarninstall "github.com/paketo-buildpacks/yarn-install"
 	yarninstall "github.com/paketo-buildpacks/yarn-install"
 	"github.com/pelletier/go-toml"
 	"github.com/pelletier/go-toml"
+	"k8s.io/apimachinery/pkg/util/json"
 )
 )
 
 
 const (
 const (
@@ -171,7 +172,7 @@ func (runtime *NodeRuntime) detectStandalone(results chan struct {
 	runtime.wg.Done()
 	runtime.wg.Done()
 }
 }
 
 
-func (runtime *NodeRuntime) Detect(workingDir string) *BuildpackInfo {
+func (runtime *NodeRuntime) Detect(workingDir string) (BuildpackInfo, map[string]interface{}) {
 	results := make(chan struct {
 	results := make(chan struct {
 		string
 		string
 		bool
 		bool
@@ -184,18 +185,39 @@ func (runtime *NodeRuntime) Detect(workingDir string) *BuildpackInfo {
 	runtime.wg.Wait()
 	runtime.wg.Wait()
 	close(results)
 	close(results)
 
 
+	atLeastOne := false
 	detected := make(map[string]bool)
 	detected := make(map[string]bool)
 	for result := range results {
 	for result := range results {
+		if result.bool {
+			atLeastOne = true
+		}
 		detected[result.string] = result.bool
 		detected[result.string] = result.bool
 	}
 	}
 
 
-	if detected[yarn] {
-		return runtime.packs[yarn]
-	} else if detected[npm] {
-		return runtime.packs[npm]
-	} else if detected[standalone] {
-		return runtime.packs[standalone]
+	if atLeastOne {
+		// it is safe to assume that the project contains a package.json
+		var packageJSONContents map[string]interface{}
+		packageJSONPath := filepath.Join(workingDir, "package.json")
+		data, err := os.ReadFile(packageJSONPath)
+		if err != nil {
+			fmt.Printf("Error reading %s: %v\n", packageJSONPath, err)
+			os.Exit(1)
+		}
+		err = json.Unmarshal(data, &packageJSONContents)
+		if err != nil {
+			fmt.Printf("Error reading %s: %v\n", packageJSONPath, err)
+			os.Exit(1)
+		}
+		scripts := packageJSONContents["scripts"].(map[string]interface{})
+
+		if detected[yarn] {
+			return *runtime.packs[yarn], map[string]interface{}{"scripts": scripts}
+		} else if detected[npm] {
+			return *runtime.packs[npm], map[string]interface{}{"scripts": scripts}
+		} else if detected[standalone] {
+			return *runtime.packs[standalone], map[string]interface{}{"scripts": scripts}
+		}
 	}
 	}
 
 
-	return nil
+	return BuildpackInfo{}, nil
 }
 }