Skip to content

Commit

Permalink
Add scope to config
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpangalos committed May 19, 2023
1 parent d6709f3 commit 1fa3897
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
19 changes: 18 additions & 1 deletion server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,24 @@ func (task *BuildTask) Build() (esm *ESMBuild, err error) {
if cfg.NpmToken != "" {
rcFilePath := path.Join(task.wd, ".npmrc")
if !fileExists(rcFilePath) {
err = os.WriteFile(rcFilePath, []byte("_authToken=${ESM_NPM_TOKEN}"), 0644)
var output bytes.Buffer

if cfg.NpmRegistryScope != "" && cfg.NpmRegistry != "" {
output.WriteString(fmt.Sprintf("%s:registry=%s\n", cfg.NpmRegistryScope, cfg.NpmRegistry))
} else if cfg.NpmRegistryScope == "" && cfg.NpmRegistry != "" {
output.WriteString(fmt.Sprintf("registry=%s\n", cfg.NpmRegistry))
}

if cfg.NpmRegistry != "" && cfg.NpmToken != "" {
var tokenReg string
tokenReg, err = removeHttpPrefix(cfg.NpmRegistry)
if err != nil {
log.Errorf("Invalid npm registry in config: %v", err)
return
}
output.WriteString(fmt.Sprintf("%s:_authToken=${ESM_NPM_TOKEN}\n", tokenReg))
}
err = os.WriteFile(rcFilePath, output.Bytes(), 0644)
if err != nil {
log.Errorf("Failed to create .npmrc file: %v", err)
return
Expand Down
1 change: 1 addition & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config struct {
BasePath string `json:"basePath,omitempty"`
NpmRegistry string `json:"npmRegistry,omitempty"`
NpmToken string `json:"npmToken,omitempty"`
NpmRegistryScope string `json:"npmRegistryScope,omitempty"`
AuthSecret string `json:"authSecret,omitempty"`
NoCompress bool `json:"noCompress,omitempty"`
}
Expand Down
8 changes: 7 additions & 1 deletion server/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ func fetchPackageInfo(name string, version string) (info NpmPackage, err error)
}()

url := cfg.NpmRegistry + name
if cfg.NpmRegistryScope != "" {
isInScope := strings.HasPrefix(name, cfg.NpmRegistryScope)
if !isInScope {
url = "https://registry.npmjs.org/" + name
}
}

if isFullVersion {
url += "/" + version
}
Expand Down Expand Up @@ -374,7 +381,6 @@ func pnpmInstall(wd string, packages ...string) (err error) {
args,
"--ignore-scripts",
"--loglevel", "error",
"--registry", cfg.NpmRegistry,
)
start := time.Now()
cmd := exec.Command("pnpm", args...)
Expand Down
10 changes: 10 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"errors"
"fmt"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -285,3 +286,12 @@ func restorePurgeTimers(npmDir string) {
}
log.Debugf("Restored %d purge timers", len(pkgs))
}

func removeHttpPrefix(s string) (string, error) {
for i, v := range s {
if v == ':' {
return s[i+1:], nil
}
}
return "", fmt.Errorf("Colon not found in string: %s", s)
}

0 comments on commit 1fa3897

Please sign in to comment.