Skip to content

Commit

Permalink
fix: instruct prebuild-install to download napi builds for dependenci…
Browse files Browse the repository at this point in the history
…es which declare themselves to use napi (#47)
  • Loading branch information
Julusian authored Mar 1, 2021
1 parent a465281 commit d8b8807
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
5 changes: 5 additions & 0 deletions pkg/node-modules/nodeModuleCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import (
"go.uber.org/zap"
)

type DependencyBinary struct {
NapiVersions []uint `json:"napi_versions"`
}

type Dependency struct {
Name string `json:"name"`
Version string `json:"version"`
Dependencies map[string]string `json:"dependencies"`
OptionalDependencies map[string]string `json:"optionalDependencies"`
Binary* DependencyBinary `json:"binary`

dir string
isOptional int
Expand Down
37 changes: 26 additions & 11 deletions pkg/node-modules/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

"github.com/alecthomas/kingpin"
Expand Down Expand Up @@ -39,6 +40,7 @@ type DepInfo struct {
Version string `json:"version"`
Optional bool `json:"optional"`
HasPrebuildInstall bool `json:"hasPrebuildInstall"`
NapiVersions []uint `json:"napiVersions"`

parentDir string
dir string
Expand Down Expand Up @@ -193,7 +195,7 @@ func installUsingPrebuild(dependencies []*DepInfo, configuration *RebuildConfigu
}

return func() error {
logger := log.LOG.With(zap.String("name", dependency.Name), zap.String("version", dependency.Version), zap.String("platform", configuration.Platform), zap.String("arch", configuration.Arch),)
logger := log.LOG.With(zap.String("name", dependency.Name), zap.String("version", dependency.Version), zap.String("platform", configuration.Platform), zap.String("arch", configuration.Arch), zap.Uints("napi", dependency.NapiVersions),)
logger.Info("install prebuilt binary")

parentDir := dependency.parentDir
Expand Down Expand Up @@ -257,14 +259,27 @@ func installUsingPrebuild(dependencies []*DepInfo, configuration *RebuildConfigu
}

func createPrebuildInstallCommand(bin string, extraFlag string, dependency *DepInfo, configuration *RebuildConfiguration) *exec.Cmd {
args := []string{
bin,
"--platform="+configuration.Platform,
"--arch="+configuration.Arch,
"--target="+os.Getenv("npm_config_target"),
"--runtime="+os.Getenv("npm_config_runtime"),
"--verbose",
extraFlag,
var args []string
if len(dependency.NapiVersions) > 0 {
args = []string{
bin,
"--platform=" + configuration.Platform,
"--arch=" + configuration.Arch,
"--target=" + strconv.FormatUint(uint64(dependency.NapiVersions[0]), 10),
"--runtime=napi",
"--verbose",
extraFlag,
}
} else {
args = []string{
bin,
"--platform=" + configuration.Platform,
"--arch=" + configuration.Arch,
"--target=" + os.Getenv("npm_config_target"),
"--runtime=" + os.Getenv("npm_config_runtime"),
"--verbose",
extraFlag,
}
}
command := exec.Command(getNodeExec(configuration), args...)
command.Dir = dependency.dir
Expand Down Expand Up @@ -413,9 +428,9 @@ func readHashBang(path string) (string, error) {
if err != nil {
return "", err
}

defer r.Close()

var header [128]byte
bytesRead, err := io.ReadFull(r, header[:])
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/node-modules/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ func writeDependencyList(jsonWriter *jsoniter.Stream, dependencyMap *map[string]
}
}

if info.Binary != nil {
jsonWriter.WriteMore()
jsonWriter.WriteObjectField("napiVersions")
jsonWriter.WriteArrayStart()

for _, v := range info.Binary.NapiVersions {
jsonWriter.WriteUint(v)
}

jsonWriter.WriteArrayEnd()
}

jsonWriter.WriteObjectEnd()
}
jsonWriter.WriteArrayEnd()
Expand Down

0 comments on commit d8b8807

Please sign in to comment.