Skip to content

Commit dcb8238

Browse files
committed
add code go and update readme.md
0 parents  commit dcb8238

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Lucas Bomfim
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# npm-checker
2+
npm-checker is a tool to validate dependencies in a package.json file against the npm registry
3+
4+
For more information, run the command below:
5+
6+
```
7+
npm-checker -h or -help
8+
```
9+
10+
Use:
11+
12+
```
13+
npm-checker /path/to/package.json
14+
```

go.mod

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/gri0t/npm-checker
2+
3+
go 1.20
4+
5+
require (
6+
github.com/fatih/color v1.17.0
7+
github.com/olekukonko/tablewriter v0.0.5
8+
)
9+
10+
require (
11+
github.com/mattn/go-colorable v0.1.13 // indirect
12+
github.com/mattn/go-isatty v0.0.20 // indirect
13+
github.com/mattn/go-runewidth v0.0.9 // indirect
14+
golang.org/x/sys v0.18.0 // indirect
15+
)

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
2+
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
3+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
4+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
5+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
6+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
7+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
8+
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
9+
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
10+
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
11+
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
12+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14+
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
15+
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

main.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"os"
9+
"time"
10+
"github.com/fatih/color"
11+
"github.com/olekukonko/tablewriter"
12+
)
13+
14+
type PackageJson struct {
15+
Dependencies map[string]string `json:"dependencies"`
16+
}
17+
18+
func main() {
19+
if len(os.Args) < 2 || os.Args[1] == "-h" || os.Args[1] == "-help" {
20+
displayHelp()
21+
return
22+
}
23+
24+
packageJsonPath := os.Args[1]
25+
color.Cyan("Reading package.json from: %s", packageJsonPath)
26+
27+
packageJson, err := readPackageJson(packageJsonPath)
28+
if err != nil {
29+
color.Red("Error reading package.json: %v", err)
30+
os.Exit(1)
31+
}
32+
33+
color.Green("Successfully parsed package.json")
34+
fmt.Printf("Found %d dependencies\n\n", len(packageJson.Dependencies))
35+
36+
existCount := 0
37+
notExistCount := 0
38+
39+
for name, version := range packageJson.Dependencies {
40+
fmt.Printf("Checking %s (version %s): ", name, version)
41+
exists, err := checkPackageExists(name)
42+
if err != nil {
43+
color.Red("Error: %v", err)
44+
continue
45+
}
46+
if exists {
47+
color.Green("✔ Exists")
48+
existCount++
49+
} else {
50+
color.Red("✘ Does not exist")
51+
notExistCount++
52+
}
53+
time.Sleep(100 * time.Millisecond) // To prevent rate limiting
54+
}
55+
56+
fmt.Printf("\nSummary:\n")
57+
color.Green(" ✔ %d packages exist", existCount)
58+
color.Red(" ✘ %d packages do not exist", notExistCount)
59+
}
60+
61+
func readPackageJson(path string) (PackageJson, error) {
62+
var packageJson PackageJson
63+
64+
data, err := ioutil.ReadFile(path)
65+
if err != nil {
66+
return packageJson, err
67+
}
68+
69+
err = json.Unmarshal(data, &packageJson)
70+
if err != nil {
71+
return packageJson, err
72+
}
73+
74+
return packageJson, nil
75+
}
76+
77+
func checkPackageExists(name string) (bool, error) {
78+
url := fmt.Sprintf("https://registry.npmjs.org/%s", name)
79+
resp, err := http.Get(url)
80+
if err != nil {
81+
return false, err
82+
}
83+
defer resp.Body.Close()
84+
85+
return resp.StatusCode == http.StatusOK, nil
86+
}
87+
88+
func displayHelp() {
89+
logo := `
90+
_ _ ____ __ __ ____ _ _ _____ ____ _ _______ ____
91+
| \ | | _ \| \/ | / ___| | | | ____/ ___| |/ / ____| _ \
92+
| \| | |_) | |\/| | | | | |_| | _|| | | ' /| _| | |_) |
93+
| |\ | __/| | | | | |___| ___ | |__| |___| . \| |___| _ <
94+
|_| \_|_| |_| |_| \____|_| |_|_____\____|_|\_\_____|_| \_\
95+
`
96+
fmt.Println(logo)
97+
color.Cyan("Developed by gri0t")
98+
fmt.Println()
99+
100+
color.Yellow("Description:")
101+
fmt.Println("npm-checker is a tool to validate dependencies in a package.json file against the npm registry.")
102+
fmt.Println()
103+
104+
color.Yellow("Usage:")
105+
table := tablewriter.NewWriter(os.Stdout)
106+
table.SetHeader([]string{"Command", "Description"})
107+
table.SetBorder(false)
108+
table.Append([]string{"npm-checker /path/to/package.json", "Check dependencies in the specified package.json file"})
109+
table.Append([]string{"npm-checker -h, npm-checker -help", "Display this help message"})
110+
table.Render()
111+
fmt.Println()
112+
113+
color.Yellow("Example:")
114+
fmt.Println("npm-checker /path/to/package.json")
115+
fmt.Println()
116+
}

0 commit comments

Comments
 (0)