diff --git a/cmd/npmExecuteScripts.go b/cmd/npmExecuteScripts.go index c7be2a669..c20251084 100644 --- a/cmd/npmExecuteScripts.go +++ b/cmd/npmExecuteScripts.go @@ -73,7 +73,7 @@ func runNpmExecuteScripts(npmExecutor npm.Executor, config *npmExecuteScriptsOpt return err } - err = npmExecutor.PublishAllPackages(packageJSONFiles, config.RepositoryURL, config.RepositoryUsername, config.RepositoryPassword) + err = npmExecutor.PublishAllPackages(packageJSONFiles, config.RepositoryURL, config.RepositoryUsername, config.RepositoryPassword, config.PackBeforePublish) if err != nil { return err } diff --git a/cmd/npmExecuteScripts_generated.go b/cmd/npmExecuteScripts_generated.go index 8e02992bc..e922a4779 100644 --- a/cmd/npmExecuteScripts_generated.go +++ b/cmd/npmExecuteScripts_generated.go @@ -31,6 +31,7 @@ type npmExecuteScriptsOptions struct { RepositoryPassword string `json:"repositoryPassword,omitempty"` RepositoryUsername string `json:"repositoryUsername,omitempty"` BuildSettingsInfo string `json:"buildSettingsInfo,omitempty"` + PackBeforePublish bool `json:"packBeforePublish,omitempty"` } type npmExecuteScriptsCommonPipelineEnvironment struct { @@ -167,6 +168,7 @@ func addNpmExecuteScriptsFlags(cmd *cobra.Command, stepConfig *npmExecuteScripts cmd.Flags().StringVar(&stepConfig.RepositoryPassword, "repositoryPassword", os.Getenv("PIPER_repositoryPassword"), "Password for the repository to which the project artifacts should be published.") cmd.Flags().StringVar(&stepConfig.RepositoryUsername, "repositoryUsername", os.Getenv("PIPER_repositoryUsername"), "Username for the repository to which the project artifacts should be published.") cmd.Flags().StringVar(&stepConfig.BuildSettingsInfo, "buildSettingsInfo", os.Getenv("PIPER_buildSettingsInfo"), "build settings info is typically filled by the step automatically to create information about the build settings that were used during the npm build . This information is typically used for compliance related processes.") + cmd.Flags().BoolVar(&stepConfig.PackBeforePublish, "packBeforePublish", false, "used for pack") } @@ -321,6 +323,15 @@ func npmExecuteScriptsMetadata() config.StepData { Aliases: []config.Alias{}, Default: os.Getenv("PIPER_buildSettingsInfo"), }, + { + Name: "packBeforePublish", + ResourceRef: []config.ResourceReference{}, + Scope: []string{"STEPS", "STAGES", "PARAMETERS"}, + Type: "bool", + Mandatory: false, + Aliases: []config.Alias{}, + Default: false, + }, }, }, Containers: []config.Container{ diff --git a/pkg/npm/mock.go b/pkg/npm/mock.go index 95b876775..f16d61edb 100644 --- a/pkg/npm/mock.go +++ b/pkg/npm/mock.go @@ -122,6 +122,6 @@ func (n *NpmExecutorMock) CreateBOM(packageJSONFiles []string) error { } // CreateBOM mock implementation -func (n *NpmExecutorMock) PublishAllPackages(packageJSONFiles []string, registry, username, password string) error { +func (n *NpmExecutorMock) PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool) error { return nil } diff --git a/pkg/npm/npm.go b/pkg/npm/npm.go index 67b2eecf9..016292eaf 100644 --- a/pkg/npm/npm.go +++ b/pkg/npm/npm.go @@ -26,7 +26,7 @@ type Executor interface { FindPackageJSONFilesWithScript(packageJSONFiles []string, script string) ([]string, error) RunScriptsInAllPackages(runScripts []string, runOptions []string, scriptOptions []string, virtualFrameBuffer bool, excludeList []string, packagesList []string) error InstallAllDependencies(packageJSONFiles []string) error - PublishAllPackages(packageJSONFiles []string, registry, username, password string) error + PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool) error SetNpmRegistries() error CreateBOM(packageJSONFiles []string) error } diff --git a/pkg/npm/publish.go b/pkg/npm/publish.go index 8019ef78c..b03126560 100644 --- a/pkg/npm/publish.go +++ b/pkg/npm/publish.go @@ -2,6 +2,8 @@ package npm import ( "fmt" + "io/ioutil" + "os" "path/filepath" "github.com/pkg/errors" @@ -12,7 +14,7 @@ import ( ) // PublishAllPackages executes npm publish for all package.json files defined in packageJSONFiles list -func (exec *Execute) PublishAllPackages(packageJSONFiles []string, registry, username, password string) error { +func (exec *Execute) PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool) error { for _, packageJSON := range packageJSONFiles { fileExists, err := exec.Utils.FileExists(packageJSON) if err != nil { @@ -22,7 +24,7 @@ func (exec *Execute) PublishAllPackages(packageJSONFiles []string, registry, use return fmt.Errorf("package.json file '%s' not found: %w", packageJSON, err) } - err = exec.publish(packageJSON, registry, username, password) + err = exec.publish(packageJSON, registry, username, password, packBeforePublish) if err != nil { return err } @@ -31,7 +33,7 @@ func (exec *Execute) PublishAllPackages(packageJSONFiles []string, registry, use } // publish executes npm publish for package.json -func (exec *Execute) publish(packageJSON, registry, username, password string) error { +func (exec *Execute) publish(packageJSON, registry, username, password string, packBeforePublish bool) error { execRunner := exec.Utils.GetExecRunner() npmignore := NewNPMIgnore(filepath.Dir(packageJSON)) @@ -91,9 +93,63 @@ func (exec *Execute) publish(packageJSON, registry, username, password string) e log.Entry().Debug("no registry provided") } - err := execRunner.RunExecutable("npm", "publish", "--userconfig", npmrc.filepath, "--registry", registry) - if err != nil { - return err + if packBeforePublish { + tmpDirectory := getTempDirForNpmTarBall() + defer os.RemoveAll(tmpDirectory) + + err := execRunner.RunExecutable("npm", "pack", "--pack-destination", tmpDirectory) + if err != nil { + return err + } + + _, err = FileUtils.Copy(npmrc.filepath, filepath.Join(tmpDirectory, ".piperNpmrc")) + if err != nil { + return fmt.Errorf("error copying piperNpmrc file from %v to %v with error: %w", + npmrc.filepath, filepath.Join(tmpDirectory, ".piperNpmrc"), err) + } + + tarballFileName := "" + err = filepath.Walk(tmpDirectory, func(path string, info os.FileInfo, err error) error { + if filepath.Ext(path) == ".tgz" { + tarballFileName = "." + string(filepath.Separator) + path + log.Entry().Debugf("found tarball file at %v", tarballFileName) + } + return nil + }) + if err != nil { + return err + } + + // rename the .npmrc file since it interferes with publish + err = os.Rename(filepath.Join(filepath.Dir(packageJSON), ".npmrc"), filepath.Join(filepath.Dir(packageJSON), ".tmpNpmrc")) + if err != nil { + return fmt.Errorf("error when renaming current .npmrc file : %w", err) + } + + err = execRunner.RunExecutable("npm", "publish", "--tarball", tarballFileName, "--userconfig", filepath.Join(tmpDirectory, ".piperNpmrc"), "--registry", registry) + if err != nil { + return err + } + + // undo the renaming ot the .npmrc to keep the workspace like before + err = os.Rename(filepath.Join(filepath.Dir(packageJSON), ".tmpNpmrc"), filepath.Join(filepath.Dir(packageJSON), ".npmrc")) + if err != nil { + log.Entry().Warnf("unable to rename the .npmrc file : %v", err) + } + } else { + err := execRunner.RunExecutable("npm", "publish", "--userconfig", npmrc.filepath, "--registry", registry) + if err != nil { + return err + } } + return nil } + +func getTempDirForNpmTarBall() string { + tmpFolder, err := ioutil.TempDir(".", "temp-") + if err != nil { + log.Entry().WithError(err).WithField("path", tmpFolder).Debug("Creating temp directory failed") + } + return tmpFolder +} diff --git a/resources/metadata/npmExecuteScripts.yaml b/resources/metadata/npmExecuteScripts.yaml index 877e96b1c..d2365d3fb 100644 --- a/resources/metadata/npmExecuteScripts.yaml +++ b/resources/metadata/npmExecuteScripts.yaml @@ -126,6 +126,14 @@ spec: resourceRef: - name: commonPipelineEnvironment param: custom/buildSettingsInfo + - name: packBeforePublish + type: bool + default: false + description: used for pack + scope: + - STEPS + - STAGES + - PARAMETERS outputs: resources: - name: commonPipelineEnvironment