This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (122 loc) · 3 KB
/
main.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"embed"
"encoding/json"
"errors"
"github.com/rxliuli/saki/builder"
"github.com/rxliuli/saki/runner"
"github.com/rxliuli/saki/utils/array"
"github.com/urfave/cli/v2"
"log"
"os"
"strings"
)
//go:embed npm/package.json
var packageJson embed.FS
type PackageJson struct {
Version string `json:"version"`
}
func readVersion() string {
file, _ := packageJson.ReadFile("npm/package.json")
var pkgJson PackageJson
_ = json.Unmarshal(file, &pkgJson)
return pkgJson.Version
}
func main() {
version := readVersion()
cwd, _ := os.Getwd()
program := builder.Program{
Cwd: cwd,
}
app := &cli.App{
Commands: cli.Commands{
{
Name: "build",
Usage: "构建命令",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "watch",
Usage: "监听文件变化,自动编译",
Value: false,
Aliases: []string{"w"},
},
},
Subcommands: cli.Commands{
{
Name: "lib",
Usage: "构建 lib",
Action: func(context *cli.Context) error {
program.Watch = context.Bool("watch")
return program.BuildToTargets([]builder.Target{builder.TargetEsm, builder.TargetCjs})
},
},
{
Name: "cli",
Usage: "构建 cli",
Action: func(context *cli.Context) error {
program.Watch = context.Bool("watch")
return program.BuildToTargets([]builder.Target{builder.TargetCli, builder.TargetEsm, builder.TargetCjs})
},
},
{
Name: "single",
Usage: "构建 cli",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "target",
Usage: "输出目标,可选值 esm/cjs/iife/cli",
Required: true,
},
},
Action: func(context *cli.Context) error {
program.Watch = context.Bool("watch")
return program.BuildToTargets(array.StringFlatMap(context.StringSlice("target"), func(s string) []string {
return strings.Split(s, ",")
}))
},
},
},
Action: func(context *cli.Context) error {
program.Watch = context.Bool("watch")
return program.BuildByConfig()
},
},
{
Name: "run",
Usage: "运行命令",
Hidden: true,
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "filter",
Usage: "模块过滤器路径列表,可使用 glob 模式",
Required: false,
},
},
Action: func(context *cli.Context) error {
cmd := context.Args().First()
if cmd == "" {
return errors.New("请输入运行的命令")
}
filters := array.StringFlatMap(context.StringSlice("filter"), func(s string) []string {
return strings.Split(s, ",")
})
//ffmt.Puts("filters: ", cmd, filters, context.StringSlice("filter"))
runner.Program{
Cwd: cwd,
}.Run(runner.Options{
Filter: filters,
Script: cmd,
})
return nil
},
},
},
Name: "saki",
Usage: "基于 esbuild 实现高层次的构建工具",
Version: version,
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}