summaryrefslogtreecommitdiff
path: root/runner/js/indexer.go
blob: 8a2a6b21de52e2491d157ada32f5eee3ad88149a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package js

import (
	"encoding/json"
	"errors"
	"github.com/ewy1/pik/model"
	"io/fs"
	"os"
	"path/filepath"
)

type Package struct {
	Scripts map[string]string `json:"scripts"`
}

func (n *js) Index(path string, f fs.FS, runners []model.Runner) ([]model.Target, error) {
	p := &Package{}
	// are there any other package.jsons? i hope not, because i don't know them
	content, err := os.ReadFile(filepath.Join(path, "package.json"))
	if errors.Is(err, fs.ErrNotExist) {
		return nil, nil
	} else if err != nil {
		return nil, err
	}
	err = json.Unmarshal(content, p)
	if err != nil {
		return nil, err
	}

	var targets []model.Target
	if n.Npm == "" {
		return nil, NoNpm
	}
	for k, s := range p.Scripts {
		targets = append(targets, n.CreateRun(k, s))
	}

	return targets, nil
}