Skip to content

Commit e5b1256

Browse files
committed
added npm install
1 parent a54e0b6 commit e5b1256

File tree

12 files changed

+117
-45
lines changed

12 files changed

+117
-45
lines changed

artifactory/commands/npm/common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type CommonArgs struct {
1010
buildConfiguration *build.BuildConfiguration
1111
npmArgs []string
1212
serverDetails *config.ServerDetails
13+
useNative bool
1314
}
1415

1516
func (ca *CommonArgs) SetServerDetails(serverDetails *config.ServerDetails) *CommonArgs {
@@ -31,3 +32,12 @@ func (ca *CommonArgs) SetRepo(repo string) *CommonArgs {
3132
ca.repo = repo
3233
return ca
3334
}
35+
36+
func (ca *CommonArgs) UseNative() bool {
37+
return ca.useNative
38+
}
39+
40+
func (ca *CommonArgs) setUseNative(useNpmRc bool) *CommonArgs {
41+
ca.useNative = useNpmRc
42+
return ca
43+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package npm
2+
3+
type Installer interface {
4+
PrepareInstallPrerequisites(repo string) error
5+
Run() error
6+
RestoreNpmrcFunc() error
7+
}
8+
9+
// Get npm implementation
10+
func NpmInstallStrategy(shouldUseNpmRc bool, npmCommand *NpmCommand) Installer {
11+
12+
if shouldUseNpmRc {
13+
return &npmInstall{npmCommand}
14+
}
15+
16+
return &npmRtInstall{npmCommand}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package npm
2+
3+
import "github.com/jfrog/jfrog-client-go/utils/log"
4+
5+
type npmInstall struct {
6+
*NpmCommand
7+
}
8+
9+
func (ni *npmInstall) PrepareInstallPrerequisites(repo string) error {
10+
log.Debug("Skip Preparing NPM installation for npm command, repo: ", repo)
11+
return nil
12+
}
13+
14+
func (ni *npmInstall) Run() (err error) {
15+
if err = ni.prepareBuildInfoModule(); err != nil {
16+
return
17+
}
18+
err = ni.collectDependencies()
19+
return
20+
}
21+
22+
func (ni *npmInstall) RestoreNpmrcFunc() error {
23+
// No need to restore the npmrc file, since we are using user's npmrc
24+
return nil
25+
}

artifactory/commands/npm/npmPublishHandler.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ type npmPublish struct {
1717
*NpmPublishCommand
1818
}
1919

20-
func (npu *npmPublish) upload() error {
20+
func (npu *npmPublish) upload() (err error) {
2121
for _, packedFilePath := range npu.packedFilePaths {
22-
22+
log.Debug("Deploying npm package using npm upload.")
2323
if err := npu.readPackageInfoFromTarball(packedFilePath); err != nil {
2424
return err
2525
}
26-
2726
repoConfig, err := npu.getRepoConfig()
2827
if err != nil {
2928
return err
@@ -47,7 +46,7 @@ func (npu *npmPublish) upload() error {
4746
}
4847

4948
func (npu *npmPublish) getBuildArtifacts() ([]buildinfo.Artifact, error) {
50-
return specutils.ConvertArtifactsSearchDetailsToBuildInfoArtifacts(npu.artifactsDetailsReader)
49+
return utils.ConvertArtifactsSearchDetailsToBuildInfoArtifacts(npu.artifactsDetailsReader)
5150
}
5251

5352
func (npu *npmPublish) publishPackage(executablePath, filePath string, serverDetails *config.ServerDetails, target string) error {

artifactory/commands/npm/npmcommand.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type NpmCommand struct {
5555
configFilePath string
5656
collectBuildInfo bool
5757
buildInfoModule *build.NpmModule
58+
installHandler Installer
5859
}
5960

6061
func NewNpmCommand(cmdName string, collectBuildInfo bool) *NpmCommand {
@@ -119,7 +120,12 @@ func (nc *NpmCommand) Init() error {
119120
if err != nil {
120121
return err
121122
}
122-
nc.SetRepoConfig(repoConfig).SetArgs(filteredNpmArgs).SetBuildConfiguration(buildConfiguration)
123+
_, useNative, err := coreutils.ExtractUseNativeFromArgs(nc.npmArgs)
124+
if err != nil {
125+
return err
126+
}
127+
nc.SetRepoConfig(repoConfig).SetArgs(filteredNpmArgs).SetBuildConfiguration(buildConfiguration).setUseNative(useNative)
128+
nc.installHandler = NpmInstallStrategy(nc.UseNative(), nc)
123129
return nil
124130
}
125131

@@ -168,15 +174,7 @@ func (nc *NpmCommand) PreparePrerequisites(repo string) error {
168174
return err
169175
}
170176
log.Debug("Working directory set to:", nc.workingDirectory)
171-
if err = nc.setArtifactoryAuth(); err != nil {
172-
return err
173-
}
174-
175-
if err = nc.setNpmAuthRegistry(repo); err != nil {
176-
return err
177-
}
178-
179-
return nc.setRestoreNpmrcFunc()
177+
return nc.installHandler.PrepareInstallPrerequisites(repo)
180178
}
181179

182180
func (nc *NpmCommand) setNpmAuthRegistry(repo string) (err error) {
@@ -309,17 +307,9 @@ func (nc *NpmCommand) Run() (err error) {
309307
return
310308
}
311309
defer func() {
312-
err = errors.Join(err, nc.restoreNpmrcFunc())
310+
err = errors.Join(err, nc.installHandler.RestoreNpmrcFunc())
313311
}()
314-
if err = nc.CreateTempNpmrc(); err != nil {
315-
return
316-
}
317-
318-
if err = nc.prepareBuildInfoModule(); err != nil {
319-
return
320-
}
321-
322-
err = nc.collectDependencies()
312+
err = nc.installHandler.Run()
323313
return
324314
}
325315

artifactory/commands/npm/publish.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type NpmPublishCommandArgs struct {
4545
xrayScan bool
4646
scanOutputFormat format.OutputFormat
4747
distTag string
48-
useNpmRc bool
4948
}
5049

5150
type NpmPublishCommand struct {
@@ -107,15 +106,6 @@ func (npc *NpmPublishCommand) Result() *commandsutils.Result {
107106
return npc.result
108107
}
109108

110-
func (nru *NpmPublishCommand) ShouldUseNpmRc() bool {
111-
return nru.useNpmRc
112-
}
113-
114-
func (npc *NpmPublishCommand) setUseNpmRc(useNpmRc bool) *NpmPublishCommand {
115-
npc.useNpmRc = useNpmRc
116-
return npc
117-
}
118-
119109
func (npc *NpmPublishCommand) IsDetailedSummary() bool {
120110
return npc.detailedSummary
121111
}
@@ -130,7 +120,7 @@ func (npc *NpmPublishCommand) Init() error {
130120
if err != nil {
131121
return err
132122
}
133-
filteredNpmArgs, useNpmRc, err := commandsutils.ExtractNpmConfigType(filteredNpmArgs)
123+
filteredNpmArgs, useNative, err := coreutils.ExtractUseNativeFromArgs(filteredNpmArgs)
134124
if err != nil {
135125
return err
136126
}
@@ -155,7 +145,7 @@ func (npc *NpmPublishCommand) Init() error {
155145
}
156146
npc.SetBuildConfiguration(buildConfiguration).SetRepo(deployerParams.TargetRepo()).SetNpmArgs(filteredNpmArgs).SetServerDetails(rtDetails)
157147
}
158-
npc.SetDetailedSummary(detailedSummary).SetXrayScan(xrayScan).SetScanOutputFormat(scanOutputFormat).SetDistTag(tag).setUseNpmRc(useNpmRc)
148+
npc.SetDetailedSummary(detailedSummary).SetXrayScan(xrayScan).SetScanOutputFormat(scanOutputFormat).SetDistTag(tag).setUseNative(useNative)
159149
return nil
160150
}
161151

@@ -191,7 +181,7 @@ func (npc *NpmPublishCommand) Run() (err error) {
191181
}
192182
}
193183

194-
publishStrategy := NpmPublishStrategy(npc.ShouldUseNpmRc(), npc)
184+
publishStrategy := NpmPublishStrategy(npc.UseNative(), npc)
195185

196186
err = publishStrategy.upload()
197187
if err != nil {

artifactory/commands/npm/publishHandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
99
)
1010

11-
type Publish interface {
11+
type Publisher interface {
1212
upload() error
1313
getBuildArtifacts() ([]buildinfo.Artifact, error)
1414
}
1515

1616
// Get npm implementation
17-
func NpmPublishStrategy(shouldUseNpmRc bool, npmPublishCommand *NpmPublishCommand) Publish {
17+
func NpmPublishStrategy(shouldUseNpmRc bool, npmPublishCommand *NpmPublishCommand) Publisher {
1818

1919
if shouldUseNpmRc {
2020
return &npmPublish{npmPublishCommand}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package npm
2+
3+
import "github.com/jfrog/jfrog-client-go/utils/log"
4+
5+
type npmRtInstall struct {
6+
*NpmCommand
7+
}
8+
9+
func (nri *npmRtInstall) PrepareInstallPrerequisites(repo string) (err error) {
10+
log.Debug("Preparing NPM installation for npm rt command, repo: ", repo)
11+
if err = nri.setArtifactoryAuth(); err != nil {
12+
return err
13+
}
14+
15+
if err = nri.setNpmAuthRegistry(repo); err != nil {
16+
return err
17+
}
18+
19+
return nri.setRestoreNpmrcFunc()
20+
}
21+
22+
func (nri *npmRtInstall) Run() (err error) {
23+
if err = nri.CreateTempNpmrc(); err != nil {
24+
return
25+
}
26+
if err = nri.prepareBuildInfoModule(); err != nil {
27+
return
28+
}
29+
err = nri.collectDependencies()
30+
return
31+
}
32+
33+
func (nri *npmRtInstall) RestoreNpmrcFunc() (err error) {
34+
// Restore the npmrc file, since we are using our own npmrc
35+
if err = nri.restoreNpmrcFunc(); err != nil {
36+
return err
37+
}
38+
return
39+
}

artifactory/commands/npm/rtPublishHandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type npmRtUpload struct {
2020

2121
func (nru *npmRtUpload) upload() (err error) {
2222
for _, packedFilePath := range nru.packedFilePaths {
23-
log.Debug("Deploying npm package.")
23+
log.Debug("Deploying npm package using rt upload.")
2424
if err = nru.readPackageInfoFromTarball(packedFilePath); err != nil {
2525
return
2626
}

cliutils/flagkit/flags.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ const (
341341
// Unique npm flags
342342
npmPrefix = "npm-"
343343
npmDetailedSummary = npmPrefix + detailedSummary
344+
clientNative = "client-native"
344345

345346
// Unique nuget/dotnet config flags
346347
nugetV2 = "nuget-v2"
@@ -656,10 +657,10 @@ var commandFlags = map[string][]string{
656657
global, serverIdResolve, serverIdDeploy, repoResolve, repoDeploy,
657658
},
658659
NpmInstallCi: {
659-
BuildName, BuildNumber, module, Project,
660+
BuildName, BuildNumber, module, Project, clientNative,
660661
},
661662
NpmPublish: {
662-
BuildName, BuildNumber, module, Project, npmDetailedSummary, xrayScan, xrOutput,
663+
BuildName, BuildNumber, module, Project, npmDetailedSummary, xrayScan, xrOutput, clientNative,
663664
},
664665
PnpmConfig: {
665666
global, serverIdResolve, repoResolve,
@@ -800,6 +801,7 @@ var flagsMap = map[string]components.Flag{
800801
bundle: components.NewStringFlag(bundle, "[Optional] If specified, only artifacts of the specified bundle are matched. The value format is bundle-name/bundle-version.", components.SetMandatoryFalse()),
801802
imageFile: components.NewStringFlag(imageFile, "[Mandatory] Path to a file which includes one line in the following format: <IMAGE-TAG>@sha256:<MANIFEST-SHA256>.", components.SetMandatoryTrue()),
802803
ocStartBuildRepo: components.NewStringFlag(repo, "[Mandatory] The name of the repository to which the image was pushed.", components.SetMandatoryTrue()),
804+
clientNative: components.NewBoolFlag(clientNative, "[Default: false] Set to true if you'd like to use the native client configurations. Note: This flag would invoke native client behind the scenes, has performance implications and does not support deployment view and detailed summary.", components.WithBoolDefaultValueFalse()),
803805

804806
// Config specific commands flags
805807
interactive: components.NewBoolFlag(interactive, "[Default: true, unless $CI is true] Set to false if you do not want the config command to be interactive. If true, the --url option becomes optional.", components.WithBoolDefaultValueFalse()),

0 commit comments

Comments
 (0)