diff --git a/cmd/root.go b/cmd/root.go index 7d0f7a497..596186043 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,6 +8,7 @@ import ( "github.com/spf13/cobra" "github.com/yaoapp/kun/exception" "github.com/yaoapp/yao/cmd/studio" + "github.com/yaoapp/yao/cmd/sui" "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/pack" "github.com/yaoapp/yao/share" @@ -62,6 +63,7 @@ var langs = map[string]string{ "Error occurred while updating binary: %s": "更新二进制文件时出错: %s", "🎉Successfully updated to version: %s🎉": "🎉成功更新到版本: %s🎉", "Print all version information": "显示详细版本信息", + "SUI Template Engine": "SUI 模板引擎命令", } // L 多语言切换 @@ -110,9 +112,26 @@ var studioCmd = &cobra.Command{ }, } +var suiCmd = &cobra.Command{ + Use: "sui", + Short: L("SUI Template Engine"), + Long: L("SUI Template Engine"), + Args: cobra.MinimumNArgs(1), + CompletionOptions: cobra.CompletionOptions{ + DisableDefaultCmd: true, + }, + Run: func(cmd *cobra.Command, args []string) { + fmt.Fprintln(os.Stderr, L("One or more arguments are not correct"), args) + os.Exit(1) + }, +} + // 加载命令 func init() { + studioCmd.AddCommand(studio.RunCmd) + suiCmd.AddCommand(sui.WatchCmd) + rootCmd.AddCommand( versionCmd, migrateCmd, @@ -126,6 +145,7 @@ func init() { // websocketCmd, packCmd, studioCmd, + suiCmd, upgradeCmd, ) // rootCmd.SetHelpCommand(helpCmd) diff --git a/cmd/sui/utils.go b/cmd/sui/utils.go new file mode 100644 index 000000000..1ecd6a1df --- /dev/null +++ b/cmd/sui/utils.go @@ -0,0 +1,54 @@ +package sui + +import ( + "os" + "path/filepath" + + "github.com/yaoapp/kun/exception" + "github.com/yaoapp/yao/config" +) + +var appPath string +var envFile string + +var langs = map[string]string{ + "Auto-build when the template file changes": "模板文件变化时自动构建", + "Session Data": "会话数据", +} + +// L 多语言切换 +func L(words string) string { + + var lang = os.Getenv("YAO_LANG") + if lang == "" { + return words + } + + if trans, has := langs[words]; has { + return trans + } + return words +} + +// Boot 设定配置 +func Boot() { + root := config.Conf.Root + if appPath != "" { + r, err := filepath.Abs(appPath) + if err != nil { + exception.New("Root error %s", 500, err.Error()).Throw() + } + root = r + } + if envFile != "" { + config.Conf = config.LoadFrom(envFile) + } else { + config.Conf = config.LoadFrom(filepath.Join(root, ".env")) + } + + if config.Conf.Mode == "production" { + config.Production() + } else if config.Conf.Mode == "development" { + config.Development() + } +} diff --git a/cmd/sui/watch.go b/cmd/sui/watch.go new file mode 100644 index 000000000..ccdfdeeac --- /dev/null +++ b/cmd/sui/watch.go @@ -0,0 +1,223 @@ +package sui + +import ( + "fmt" + "io/fs" + "os" + "os/signal" + "path/filepath" + "strings" + "sync" + "syscall" + + "github.com/fatih/color" + "github.com/fsnotify/fsnotify" + "github.com/google/uuid" + jsoniter "github.com/json-iterator/go" + "github.com/spf13/cobra" + "github.com/yaoapp/gou/session" + "github.com/yaoapp/kun/log" + "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/engine" + "github.com/yaoapp/yao/sui/core" +) + +var data string + +var watched sync.Map + +// WatchCmd command +var WatchCmd = &cobra.Command{ + Use: "watch", + Short: L("Auto-build when the template file changes"), + Long: L("Auto-build when the template file changes"), + Run: func(cmd *cobra.Command, args []string) { + if len(args) < 2 { + fmt.Fprintln(os.Stderr, color.RedString(L("yao cui watch