-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
560 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ | |
dist/ | ||
node_modules | ||
.env | ||
.npmrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,44 @@ | ||
package npm | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func FetchNpmRegistry(dependency string) (*http.Response, error) { | ||
resp, err := http.Get(fmt.Sprintf("https://registry.npmjs.org/%s", dependency)) | ||
func FetchNpmRegistry(dependency string, token string) (map[string]interface{}, error) { | ||
|
||
client := &http.Client{} | ||
|
||
req, err := http.NewRequest("GET", fmt.Sprintf("https://registry.npmjs.org/%s", dependency), nil) | ||
if err != nil { | ||
fmt.Println(err) | ||
return nil, err | ||
} | ||
|
||
if token != "" { | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
fmt.Println(err) | ||
return nil, err | ||
} | ||
|
||
return resp, err | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("package %s, status code: %d", dependency, resp.StatusCode) | ||
} | ||
|
||
var result map[string]interface{} | ||
err = json.NewDecoder(resp.Body).Decode(&result) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package npmrc | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
const npmrcFilename = ".npmrc" | ||
|
||
type NpmrcTokens struct { | ||
Exists bool | ||
Project string | ||
User string | ||
Global string | ||
Builtin string | ||
} | ||
|
||
func GetNpmrcTokens() (NpmrcTokens, error) { | ||
|
||
/* | ||
The four relevant files are: | ||
· per-project config file (/path/to/my/project/.npmrc) | ||
· per-user config file (~/.npmrc) | ||
· [TODO] global config file ($PREFIX/etc/npmrc) | ||
· [TODO] npm builtin config file (/path/to/npm/npmrc) | ||
*/ | ||
|
||
npmrcTokens := NpmrcTokens{ | ||
Exists: false, | ||
Project: "", | ||
User: "", | ||
Global: "", | ||
Builtin: "", | ||
} | ||
|
||
// per-project config file (/path/to/my/project/.npmrc) | ||
if _, err := os.Stat(npmrcFilename); err == nil { | ||
|
||
fileContent, err := os.ReadFile(npmrcFilename) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
if token, err := ParseNpmrc(string(fileContent)); err == nil { | ||
npmrcTokens.Exists = true | ||
npmrcTokens.Project = token | ||
} | ||
} | ||
|
||
// per-user config file (~/.npmrc) | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
userNpmrcFilename := filepath.Join(homeDir, npmrcFilename) | ||
if _, err := os.Stat(userNpmrcFilename); err == nil { | ||
|
||
fileContent, err := os.ReadFile(userNpmrcFilename) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
if token, err := ParseNpmrc(string(fileContent)); err == nil { | ||
npmrcTokens.Exists = true | ||
npmrcTokens.User = token | ||
} | ||
} | ||
|
||
return npmrcTokens, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package npmrc | ||
|
||
type NpmrcTokenLevel string | ||
|
||
const ( | ||
Project NpmrcTokenLevel = "project" | ||
User NpmrcTokenLevel = "user" | ||
Global NpmrcTokenLevel = "global" | ||
Builtin NpmrcTokenLevel = "builtin" | ||
) | ||
|
||
// Gets the most relevant npmrc token using order preference from https://docs.npmjs.com/cli/v10/configuring-npm/npmrc#files | ||
func GetRelevantNpmrcToken(npmrcTokens NpmrcTokens) (string, NpmrcTokenLevel) { | ||
|
||
if npmrcTokens.Project != "" { | ||
return npmrcTokens.Project, Project | ||
} | ||
|
||
if npmrcTokens.User != "" { | ||
return npmrcTokens.User, User | ||
} | ||
|
||
if npmrcTokens.Global != "" { | ||
return npmrcTokens.Global, Global | ||
} | ||
|
||
if npmrcTokens.Builtin != "" { | ||
return npmrcTokens.Builtin, Builtin | ||
} | ||
|
||
return "", "" | ||
|
||
} |
Oops, something went wrong.