Skip to content

Commit

Permalink
Add check for npm dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tylermumford committed May 4, 2023
1 parent 9bf3030 commit def8db9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion checks/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This Go package is for all of the checks that localstatus can perform.
Each check is named with a loose hierarchy.

Checks conform to the `Check` interface, which is defined in
the `check.go` file.
the `interface.go` file.

Eventually, there will be many checks.

Expand Down
44 changes: 44 additions & 0 deletions checks/check.npm.install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package checks

import (
"fmt"
"os"
"os/exec"
"path/filepath"
)

/*
check = "npm.install"
Passes if npm dependencies are installed.
This is implemented with the `npm ls` command,
so npm must be installed and on the PATH already.
- package: A string with the path to a package.json file.
*/
type CheckNpmInstall struct{}

func (n CheckNpmInstall) Run(p Params) CheckResult {
packageFile := p.GetString("package")

_, err := os.Lstat(packageFile)
if os.IsNotExist(err) {
return newBasicResult(false, packageFile+" does not exist")
}

dir := filepath.Dir(packageFile)

cmd := exec.Command("npm", "ls")
cmd.Dir = dir
err = cmd.Start()
if err != nil {
return newBasicResult(false, fmt.Sprintf("npm in %s: %s", dir, err))
}

err = cmd.Wait()
if err != nil {
return newBasicResult(false, packageFile+" dependencies not satisfied")
}

return newBasicResult(true, packageFile+" dependencies")
}
1 change: 1 addition & 0 deletions checks/checkregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ func (c *CheckRegistry) AddAllChecks() {
c.Add("file.exists", CheckFileExists{})
c.Add("git.branch", CheckGitBranch{})
c.Add("http.ok", CheckHttpOk{})
c.Add("npm.install", CheckNpmInstall{})
c.Add("tcp.open", CheckTcpOpen{})
}
10 changes: 10 additions & 0 deletions docs/all.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ Passes if the URL responds with 200 OK. Uses the GET method.

----------

check = "npm.install"

Passes if npm dependencies are installed.
This is implemented with the `npm ls` command,
so npm must be installed and on the PATH already.

- package: A string with the path to a package.json file.

----------

check = "tcp.open"

Passes if a TCP connection can be opened.
Expand Down
7 changes: 7 additions & 0 deletions docs/npm.install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
check = "npm.install"

Passes if npm dependencies are installed.
This is implemented with the `npm ls` command,
so npm must be installed and on the PATH already.

- package: A string with the path to a package.json file.

0 comments on commit def8db9

Please sign in to comment.