This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
nodejs_detector.go
66 lines (55 loc) · 1.85 KB
/
nodejs_detector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*******************************************************************************
* Copyright (c) 2021 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc.
******************************************************************************/
package enricher
import (
"path/filepath"
"regexp"
"github.com/redhat-developer/alizer/go/pkg/schema"
"github.com/redhat-developer/alizer/go/pkg/utils"
)
type packageScriptFunc func(schema.PackageJson) string
// hasFramework uses the package.json to check for framework
func hasFramework(configFile string, tag string) bool {
return utils.IsTagInPackageJsonFile(configFile, tag)
}
func getPortFromStartScript(root string, regexes []string) int {
getStartScript := func(packageJson schema.PackageJson) string {
return packageJson.Scripts.Start
}
return getPortFromScript(root, getStartScript, regexes)
}
func getPortFromDevScript(root string, regexes []string) int {
getDevScript := func(packageJson schema.PackageJson) string {
return packageJson.Scripts.Dev
}
return getPortFromScript(root, getDevScript, regexes)
}
func getPortFromScript(root string, getScript packageScriptFunc, regexes []string) int {
packageJson, err := getPackageJson(root)
if err != nil {
return -1
}
for _, regex := range regexes {
re, err := regexp.Compile(regex)
if err != nil {
continue
}
port := utils.FindPortSubmatch(re, getScript(packageJson), 1)
if port != -1 {
return port
}
}
return -1
}
func getPackageJson(root string) (schema.PackageJson, error) {
packageJsonPath := filepath.Join(root, "package.json")
return utils.GetPackageJsonSchemaFromFile(packageJsonPath)
}