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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
package js
import (
"errors"
"github.com/ewy1/pik/identity"
"github.com/ewy1/pik/model"
"github.com/ewy1/pik/runner"
"os/exec"
"path/filepath"
"slices"
)
var jsExtensions = []string{
".js",
".cjs",
}
var tsExtensions = []string{
".ts",
}
var extensions = append(jsExtensions, tsExtensions...)
var managers = []string{
"pnpm",
"yarn",
"npm",
}
var jsInterpreters = []string{
"node",
"bun",
}
var tsInterpeters = []string{
"ts",
"ts-node",
"bun",
}
type js struct {
JsInterpreter string
TsInterpreter string
Npm string
}
var Js = &js{}
var UnsupportedFile = errors.New("unsupported file")
var NoJsInterpreter = errors.New("no js interpreter found in $PATH")
var NoTsInterpreter = errors.New("no ts interpreter found in $PATH")
var NoNpm = errors.New("npm not found in $PATH")
func (n *js) Interpreter(file string) (string, error) {
ext := filepath.Ext(file)
if slices.Contains(jsInterpreters, ext) {
if n.JsInterpreter == "" {
return "", NoJsInterpreter
}
return n.JsInterpreter, nil
}
if slices.Contains(tsInterpeters, ext) {
if n.TsInterpreter == "" {
return "", NoTsInterpreter
}
return n.TsInterpreter, nil
}
return "", UnsupportedFile
}
func (n *js) Init() error {
for _, p := range jsInterpreters {
if r, err := exec.LookPath(p); err != nil {
n.JsInterpreter = r
}
}
for _, p := range tsInterpeters {
if r, err := exec.LookPath(p); err != nil {
n.TsInterpreter = r
}
}
for _, m := range managers {
if r, err := exec.LookPath(m); err == nil {
n.Npm = r
}
}
return nil
}
var npmSub = []string{
"npm",
}
func (n *js) CreateRun(name, cmd string) model.Target {
return &Npm{
BaseTarget: runner.BaseTarget{
Identity: identity.New(cmd),
MyTags: model.TagsFromFilename(cmd),
MySub: npmSub,
},
Name: name,
Cmd: cmd,
}
}
|