-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathoutputs.go
More file actions
65 lines (56 loc) · 1.82 KB
/
outputs.go
File metadata and controls
65 lines (56 loc) · 1.82 KB
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
package devpkg
import (
"strings"
"go.jetify.com/devbox/internal/boxcli/usererr"
)
type Output struct {
Name string
CacheURI string
}
// outputs are the nix package outputs
type outputs struct {
selectedNames []string
defaultNames []string
}
func (out *outputs) GetNames(pkg *Package) ([]string, error) {
if len(out.selectedNames) > 0 {
return out.selectedNames, nil
}
// else, get the default outputs from the lockfile
// if we haven't already
if out.defaultNames == nil {
if err := out.initDefaultNames(pkg); err != nil {
return []string{}, err
}
}
return out.defaultNames, nil
}
// initDefaultNames initializes the defaultNames field of the Outputs object.
// We run this lazily (rather than eagerly in initOutputs) because it depends on the Package,
// and initOutputs is called from the Package constructor, so cannot depend on Package.
func (out *outputs) initDefaultNames(pkg *Package) error {
sysInfo, err := pkg.sysInfoIfExists()
if err != nil {
// Provide better error for npm packages not found in Nixpkgs
errMsg := strings.TrimSpace(err.Error())
if strings.Contains(errMsg, "package not found") && strings.HasPrefix(pkg.Raw, "nodePackages.") {
npmPkg := strings.TrimPrefix(pkg.Raw, `nodePackages."`)
npmPkg = strings.TrimSuffix(npmPkg, `"`)
return usererr.New(
"npm package %s was not found in Nixpkgs.\n"+
"This may mean the package isn't packaged yet. Check https://search.nixos.org/packages for availability.\n"+
"As a workaround, you can install it manually in your devbox shell using npm after running: npm config set prefix '~/.npm-global'",
npmPkg,
)
}
return err
}
out.defaultNames = []string{}
if sysInfo == nil {
return nil
}
for _, output := range sysInfo.DefaultOutputs() {
out.defaultNames = append(out.defaultNames, output.Name)
}
return nil
}