Skip to content

Commit ff3410d

Browse files
authored
⭐️ npm scripts (#6210)
* ⭐️ npm scripts * 🧹 add file path for single package
1 parent 11add21 commit ff3410d

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

providers/os/resources/npm.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package resources
55

66
import (
7+
"encoding/json"
78
"errors"
89
"fmt"
910
"path/filepath"
@@ -255,6 +256,7 @@ func (r *mqlNpmPackages) gatherData() error {
255256
if err != nil {
256257
return err
257258
}
259+
filePaths = append(filePaths, path)
258260
root = bom.Root()
259261
directDependencies = bom.Direct()
260262
transitiveDependencies = bom.Transitive()
@@ -320,6 +322,38 @@ func (r *mqlNpmPackages) files() ([]any, error) {
320322
return nil, r.gatherData()
321323
}
322324

325+
func (r *mqlNpmPackages) scripts() (map[string]any, error) {
326+
if r.Path.Error != nil {
327+
return nil, r.Path.Error
328+
}
329+
path := r.Path.Data
330+
331+
f, err := newFile(r.MqlRuntime, path)
332+
if err != nil {
333+
return nil, err
334+
}
335+
content := f.GetContent()
336+
if content.Error != nil {
337+
return nil, content.Error
338+
}
339+
340+
type packageJson struct {
341+
Scripts map[string]string `json:"scripts"`
342+
}
343+
344+
pkgInfo := packageJson{}
345+
err = json.Unmarshal([]byte(content.Data), &pkgInfo)
346+
if err != nil {
347+
return nil, err
348+
}
349+
350+
res := make(map[string]any)
351+
for k, v := range pkgInfo.Scripts {
352+
res[k] = v
353+
}
354+
return res, nil
355+
}
356+
323357
// newNpmPackageList creates a list of npm package resources
324358
func newNpmPackageList(runtime *plugin.Runtime, packages []*languages.Package) ([]any, error) {
325359
resources := []any{}

providers/os/resources/os.lr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,9 @@ npm.packages {
14271427

14281428
// Files used to determine the packages
14291429
files() []pkgFileInfo
1430+
1431+
// scripts defined in package json
1432+
scripts() map[string]string
14301433
}
14311434

14321435
npm.package @defaults("name version") {

providers/os/resources/os.lr.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

providers/os/resources/os.lr.manifest.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,8 @@ resources:
638638
list: {}
639639
path: {}
640640
root: {}
641+
scripts:
642+
min_mondoo_version: 9.0.0
641643
min_mondoo_version: latest
642644
ntp.conf:
643645
fields:

0 commit comments

Comments
 (0)