2021-07-15 14:46:04 +02:00
|
|
|
package npm
|
|
|
|
|
|
|
|
import (
|
2022-03-04 11:26:46 +02:00
|
|
|
"encoding/json"
|
2021-07-15 14:46:04 +02:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2022-03-04 11:26:46 +02:00
|
|
|
"regexp"
|
2021-07-15 14:46:04 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
CredentialUtils "github.com/SAP/jenkins-library/pkg/piperutils"
|
|
|
|
)
|
|
|
|
|
2022-03-04 11:26:46 +02:00
|
|
|
type npmMinimalPackageDescriptor struct {
|
|
|
|
Name string `json:version`
|
|
|
|
Version string `json:version`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *npmMinimalPackageDescriptor) Scope() string {
|
|
|
|
r := regexp.MustCompile(`^(?:(?P<scope>@[^\/]+)\/)?(?P<package>.+)$`)
|
|
|
|
|
|
|
|
matches := r.FindStringSubmatch(pd.Name)
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches[1]
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:46:04 +02:00
|
|
|
// PublishAllPackages executes npm publish for all package.json files defined in packageJSONFiles list
|
2022-01-25 10:52:22 +02:00
|
|
|
func (exec *Execute) PublishAllPackages(packageJSONFiles []string, registry, username, password string, packBeforePublish bool) error {
|
2021-07-15 14:46:04 +02:00
|
|
|
for _, packageJSON := range packageJSONFiles {
|
2022-03-04 11:26:46 +02:00
|
|
|
log.Entry().Infof("triggering publish for %s", packageJSON)
|
|
|
|
|
2021-07-15 14:46:04 +02:00
|
|
|
fileExists, err := exec.Utils.FileExists(packageJSON)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot check if '%s' exists: %w", packageJSON, err)
|
|
|
|
}
|
|
|
|
if !fileExists {
|
|
|
|
return fmt.Errorf("package.json file '%s' not found: %w", packageJSON, err)
|
|
|
|
}
|
|
|
|
|
2022-01-25 10:52:22 +02:00
|
|
|
err = exec.publish(packageJSON, registry, username, password, packBeforePublish)
|
2021-07-15 14:46:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// publish executes npm publish for package.json
|
2022-01-25 10:52:22 +02:00
|
|
|
func (exec *Execute) publish(packageJSON, registry, username, password string, packBeforePublish bool) error {
|
2021-07-15 14:46:04 +02:00
|
|
|
execRunner := exec.Utils.GetExecRunner()
|
|
|
|
|
2022-03-04 11:26:46 +02:00
|
|
|
scope, err := exec.readPackageScope(packageJSON)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error reading package scope from %s", packageJSON)
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:46:04 +02:00
|
|
|
npmignore := NewNPMIgnore(filepath.Dir(packageJSON))
|
2022-03-02 15:06:51 +02:00
|
|
|
if exists, err := exec.Utils.FileExists(npmignore.filepath); exists {
|
2021-07-15 14:46:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to check for existing %s file", npmignore.filepath)
|
|
|
|
}
|
|
|
|
log.Entry().Debugf("loading existing %s file", npmignore.filepath)
|
|
|
|
if err = npmignore.Load(); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to read existing %s file", npmignore.filepath)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Entry().Debug("creating .npmignore file")
|
|
|
|
}
|
|
|
|
log.Entry().Debug("adding **/piper")
|
|
|
|
npmignore.Add("**/piper")
|
|
|
|
log.Entry().Debug("adding **/sap-piper")
|
|
|
|
npmignore.Add("**/sap-piper")
|
2022-01-13 14:08:19 +02:00
|
|
|
|
|
|
|
npmrc := NewNPMRC(filepath.Dir(packageJSON))
|
|
|
|
|
|
|
|
log.Entry().Debugf("adding piper npmrc file %v", npmrc.filepath)
|
|
|
|
npmignore.Add(npmrc.filepath)
|
|
|
|
|
2021-07-15 14:46:04 +02:00
|
|
|
if err := npmignore.Write(); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to update %s file", npmignore.filepath)
|
|
|
|
}
|
|
|
|
|
2022-01-13 14:08:19 +02:00
|
|
|
// update .piperNpmrc
|
2021-07-15 14:46:04 +02:00
|
|
|
if len(registry) > 0 {
|
|
|
|
// check existing .npmrc file
|
2022-03-02 15:06:51 +02:00
|
|
|
if exists, err := exec.Utils.FileExists(npmrc.filepath); exists {
|
2021-07-15 14:46:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to check for existing %s file", npmrc.filepath)
|
|
|
|
}
|
|
|
|
log.Entry().Debugf("loading existing %s file", npmrc.filepath)
|
|
|
|
if err = npmrc.Load(); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to read existing %s file", npmrc.filepath)
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-13 14:08:19 +02:00
|
|
|
log.Entry().Debugf("creating new npmrc file at %s", npmrc.filepath)
|
2021-07-15 14:46:04 +02:00
|
|
|
}
|
|
|
|
// set registry
|
|
|
|
log.Entry().Debugf("adding registry %s", registry)
|
|
|
|
npmrc.Set("registry", registry)
|
2022-03-04 11:26:46 +02:00
|
|
|
|
|
|
|
if len(scope) > 0 {
|
|
|
|
npmrc.Set(fmt.Sprintf("%s:registry", scope), registry)
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:46:04 +02:00
|
|
|
// set registry auth
|
|
|
|
if len(username) > 0 && len(password) > 0 {
|
|
|
|
log.Entry().Debug("adding registry credentials")
|
|
|
|
npmrc.Set("_auth", CredentialUtils.EncodeUsernamePassword(username, password))
|
|
|
|
npmrc.Set("always-auth", "true")
|
|
|
|
}
|
|
|
|
// update .npmrc
|
|
|
|
if err := npmrc.Write(); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to update %s file", npmrc.filepath)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Entry().Debug("no registry provided")
|
|
|
|
}
|
|
|
|
|
2022-01-25 10:52:22 +02:00
|
|
|
if packBeforePublish {
|
2022-03-02 15:06:51 +02:00
|
|
|
tmpDirectory, err := exec.Utils.TempDir(".", "temp-")
|
2022-01-25 10:52:22 +02:00
|
|
|
|
2022-03-02 15:06:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating temp directory failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer exec.Utils.RemoveAll(tmpDirectory)
|
|
|
|
|
|
|
|
err = execRunner.RunExecutable("npm", "pack", "--pack-destination", tmpDirectory)
|
2022-01-25 10:52:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:06:51 +02:00
|
|
|
_, err = exec.Utils.Copy(npmrc.filepath, filepath.Join(tmpDirectory, ".piperNpmrc"))
|
2022-01-25 10:52:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error copying piperNpmrc file from %v to %v with error: %w",
|
|
|
|
npmrc.filepath, filepath.Join(tmpDirectory, ".piperNpmrc"), err)
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:06:51 +02:00
|
|
|
tarballs, err := exec.Utils.Glob(filepath.Join(tmpDirectory, "*.tgz"))
|
|
|
|
|
2022-01-25 10:52:22 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:06:51 +02:00
|
|
|
if len(tarballs) != 1 {
|
|
|
|
return fmt.Errorf("found more tarballs than expected: %v", tarballs)
|
|
|
|
}
|
|
|
|
|
2022-03-04 11:26:46 +02:00
|
|
|
tarballFilePath, err := exec.Utils.Abs(tarballs[0])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-02 15:06:51 +02:00
|
|
|
|
|
|
|
projectNpmrc := filepath.Join(filepath.Dir(packageJSON), ".npmrc")
|
|
|
|
projectNpmrcExists, _ := exec.Utils.FileExists(projectNpmrc)
|
|
|
|
|
|
|
|
if projectNpmrcExists {
|
|
|
|
// rename the .npmrc file since it interferes with publish
|
|
|
|
err = exec.Utils.FileRename(projectNpmrc, projectNpmrc+".tmp")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error when renaming current .npmrc file : %w", err)
|
|
|
|
}
|
2022-01-25 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
2022-03-04 11:26:46 +02:00
|
|
|
err = execRunner.RunExecutable("npm", "publish", "--tarball", tarballFilePath, "--userconfig", filepath.Join(tmpDirectory, ".piperNpmrc"), "--registry", registry)
|
2022-01-25 10:52:22 +02:00
|
|
|
if err != nil {
|
2022-03-04 11:26:46 +02:00
|
|
|
return errors.Wrap(err, "failed publishing artifact")
|
2022-01-25 10:52:22 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 15:06:51 +02:00
|
|
|
if projectNpmrcExists {
|
|
|
|
// undo the renaming ot the .npmrc to keep the workspace like before
|
|
|
|
err = exec.Utils.FileRename(projectNpmrc+".tmp", projectNpmrc)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().Warnf("unable to rename the .npmrc file : %v", err)
|
|
|
|
}
|
2022-01-25 10:52:22 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := execRunner.RunExecutable("npm", "publish", "--userconfig", npmrc.filepath, "--registry", registry)
|
|
|
|
if err != nil {
|
2022-03-04 11:26:46 +02:00
|
|
|
return errors.Wrap(err, "failed publishing artifact")
|
2022-01-25 10:52:22 +02:00
|
|
|
}
|
2021-07-15 14:46:04 +02:00
|
|
|
}
|
2022-01-25 10:52:22 +02:00
|
|
|
|
2021-07-15 14:46:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-03-04 11:26:46 +02:00
|
|
|
|
|
|
|
func (exec *Execute) readPackageScope(packageJSON string) (string, error) {
|
|
|
|
b, err := exec.Utils.FileRead(packageJSON)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pd npmMinimalPackageDescriptor
|
|
|
|
|
|
|
|
json.Unmarshal(b, &pd)
|
|
|
|
|
|
|
|
return pd.Scope(), nil
|
|
|
|
}
|