-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
65 lines (57 loc) · 2.08 KB
/
component.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
package v2v
import (
"github.com/pubgo/g/pkg/fileutil"
"github.com/pubgo/g/xcmds"
"github.com/pubgo/g/xerror"
"github.com/pubgo/vue2vecty/vue2vecty"
"io/ioutil"
"path/filepath"
"strings"
)
func Component() {
var templateHome = "templates"
xcmds.AddCommand(func(cmd *xcmds.Command) *xcmds.Command {
cmd.Flags().StringVar(&templateHome, "dir", templateHome, "模板目录位置")
return cmd
}(&xcmds.Command{
Use: "component",
Aliases: []string{"c", "comp"},
Short: "create component",
RunE: func(cmd *xcmds.Command, args []string) (err error) {
defer xerror.RespErr(&err)
xerror.PanicT(len(args) == 0, "component name must be specified")
names := strings.Split(args[0], "/")
_file := filepath.Join(templateHome, "components", args[0])
if len(names) == 1 {
xerror.Panic(vue2vecty.CreateComponent("components", names[0]).Save(_file + ".go"))
} else {
xerror.Panic(fileutil.IsNotExistMkDir(filepath.Dir(_file + ".go")))
xerror.Panic(vue2vecty.CreateComponent(names[len(names)-2], names[len(names)-1]).Save(_file + ".go"))
}
xerror.Panic(ioutil.WriteFile(_file+".html", []byte(`<p>{{0==0?"world":"hello"}}</p>`), 0644))
return
}},
))
xcmds.AddCommand(func(cmd *xcmds.Command) *xcmds.Command {
cmd.Flags().StringVar(&templateHome, "dir", templateHome, "模板目录位置")
return cmd
}(&xcmds.Command{
Use: "page",
Aliases: []string{"p", "view", "route", "r"},
Short: "create page",
RunE: func(cmd *xcmds.Command, args []string) (err error) {
defer xerror.RespErr(&err)
xerror.PanicT(len(args) == 0, "page name must be specified")
names := strings.Split(args[0], "/")
_file := filepath.Join(templateHome, "views", args[0]+".go")
if len(names) == 1 {
xerror.Panic(vue2vecty.CreateComponent("views", names[0]).Save(_file))
} else {
xerror.Panic(fileutil.IsNotExistMkDir(filepath.Dir(_file)))
xerror.Panic(vue2vecty.CreateComponent(names[len(names)-2], names[len(names)-1]).Save(_file))
}
xerror.Panic(ioutil.WriteFile(_file+".html", []byte(`<p>{{0==0?"world":"hello"}}</p>`), 0644))
return
}},
))
}