github.com/jfrog/jfrog-cli-core/v2@v2.52.0/artifactory/utils/npm/config-list.go (about)

     1  package npm
     2  
     3  import (
     4  	"errors"
     5  	gofrogcmd "github.com/jfrog/gofrog/io"
     6  	npmutils "github.com/jfrog/jfrog-cli-core/v2/utils/npm"
     7  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
     8  	"io"
     9  )
    10  
    11  // This method runs "npm c ls" command and returns the current npm configuration (calculated by all flags and .npmrc files).
    12  // For more info see https://docs.npmjs.com/cli/config
    13  func GetConfigList(npmFlags []string, executablePath string) (data []byte, err error) {
    14  	pipeReader, pipeWriter := io.Pipe()
    15  	defer func(pipeReader *io.PipeReader) {
    16  		err = errors.Join(err, pipeReader.Close())
    17  	}(pipeReader)
    18  
    19  	npmFlags = append(npmFlags, "--json=false")
    20  	configListCmdConfig := createConfigListCmdConfig(executablePath, npmFlags, pipeWriter)
    21  	npmErrorChan := make(chan error, 1)
    22  	go func() {
    23  		npmErrorChan <- gofrogcmd.RunCmd(configListCmdConfig)
    24  	}()
    25  
    26  	data, err = io.ReadAll(pipeReader)
    27  	if err != nil {
    28  		return nil, errorutils.CheckError(err)
    29  	}
    30  	npmError := <-npmErrorChan
    31  	if npmError != nil {
    32  		return nil, errorutils.CheckError(npmError)
    33  	}
    34  	return data, nil
    35  }
    36  
    37  func createConfigListCmdConfig(executablePath string, splitFlags []string, pipeWriter *io.PipeWriter) *npmutils.NpmConfig {
    38  	return &npmutils.NpmConfig{
    39  		Npm:          executablePath,
    40  		Command:      []string{"c", "ls"},
    41  		CommandFlags: splitFlags,
    42  		StrWriter:    pipeWriter,
    43  		ErrWriter:    nil,
    44  	}
    45  }