forked from paketo-buildpacks/npm-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_path_parser.go
35 lines (30 loc) · 970 Bytes
/
project_path_parser.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
package npminstall
import (
"errors"
"fmt"
"os"
"path/filepath"
)
// ProjectPathParser provides a mechanism for determining the proper working
// directory for the build process.
type ProjectPathParser struct{}
// NewProjectPathParser creates an instance of a ProjectPathParser.
func NewProjectPathParser() ProjectPathParser {
return ProjectPathParser{}
}
// Get will resolve the $BP_NODE_PROJECT_PATH environment variable. It
// validates that $BP_NODE_PROJECT_PATH is valid relative to the provided path.
func (p ProjectPathParser) Get(path string) (string, error) {
customProjPath := os.Getenv("BP_NODE_PROJECT_PATH")
if customProjPath == "" {
return "", nil
}
_, err := os.Stat(filepath.Join(path, customProjPath))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("expected value derived from BP_NODE_PROJECT_PATH [%s] to be an existing directory", customProjPath)
}
return "", err
}
return customProjPath, nil
}