Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPM - Enabled flags for 'install' command during dependency tree build #207

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions build/utils/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"golang.org/x/exp/slices"
"os"
"os/exec"
"path/filepath"
Expand All @@ -16,6 +17,8 @@ import (
"github.com/jfrog/gofrog/version"
)

const npmInstallCommand = "install"

// CalculateNpmDependenciesList gets an npm project's dependencies.
func CalculateNpmDependenciesList(executablePath, srcPath, moduleId string, npmParams NpmTreeDepListParam, calculateChecksums bool, log utils.Log) ([]entities.Dependency, error) {
if log == nil {
Expand Down Expand Up @@ -136,7 +139,7 @@ func runNpmLsWithoutNodeModules(executablePath, srcPath string, npmListParams Np
return nil, isDirExistsErr
}
if !isPackageLockExist || (npmListParams.OverwritePackageLock && checkIfLockFileShouldBeUpdated(srcPath, log)) {
err := installPackageLock(executablePath, srcPath, npmListParams.Args, log, npmVersion)
err := installPackageLock(executablePath, srcPath, npmListParams.InstallCommandArgs, npmListParams.Args, log, npmVersion)
if err != nil {
return nil, err
}
Expand All @@ -151,9 +154,10 @@ func runNpmLsWithoutNodeModules(executablePath, srcPath string, npmListParams Np
return data, nil
}

func installPackageLock(executablePath, srcPath string, npmArgs []string, log utils.Log, npmVersion *version.Version) error {
func installPackageLock(executablePath, srcPath string, npmInstallCommandArgs []string, npmArgs []string, log utils.Log, npmVersion *version.Version) error {
eranturgeman marked this conversation as resolved.
Show resolved Hide resolved
if npmVersion.AtLeast("6.0.0") {
npmArgs = append(npmArgs, "--package-lock-only")
npmArgs = append(npmArgs, filterUniqueArgs(npmInstallCommandArgs, npmArgs)...)
eranturgeman marked this conversation as resolved.
Show resolved Hide resolved
// Installing package-lock to generate the dependencies map.
_, _, err := RunNpmCmd(executablePath, srcPath, AppendNpmCommand(npmArgs, "install"), log)
if err != nil {
Expand All @@ -164,6 +168,20 @@ func installPackageLock(executablePath, srcPath string, npmArgs []string, log ut
return errors.New("it looks like you’re using version " + npmVersion.GetVersion() + " of the npm client. Versions below 6.0.0 require running `npm install` before running this command")
}

// filters out all args from argsToFilter that already in existingArgs. In addition, filters out npm install command and leave only flags within the final returned args
eranturgeman marked this conversation as resolved.
Show resolved Hide resolved
func filterUniqueArgs(argsToFilter []string, existingArgs []string) []string {
eranturgeman marked this conversation as resolved.
Show resolved Hide resolved
var filteredArgs []string
for _, arg := range argsToFilter {
if arg == npmInstallCommand {
continue
}
if !slices.Contains(existingArgs, arg) {
filteredArgs = append(filteredArgs, arg)
}
}
return filteredArgs
}

// Check if package.json has been modified.
// This might indicate the addition of new packages to package.json that haven't been reflected in package-lock.json.
func checkIfLockFileShouldBeUpdated(srcPath string, log utils.Log) bool {
Expand Down Expand Up @@ -192,7 +210,8 @@ func GetNpmVersion(executablePath string, log utils.Log) (*version.Version, erro
}

type NpmTreeDepListParam struct {
Args []string
Args []string
InstallCommandArgs []string
eranturgeman marked this conversation as resolved.
Show resolved Hide resolved
// Ignore the node_modules folder if exists, using the '--package-lock-only' flag
IgnoreNodeModules bool
// Rewrite package-lock.json, if exists.
Expand Down
Loading