mirror of
https://github.com/opbnq-q/nto-cli.git
synced 2025-12-06 16:20:34 +07:00
feat: path auto resolution
This commit is contained in:
@@ -1,16 +1,16 @@
|
|||||||
package generation
|
package generation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"nto_cli/entities"
|
"nto_cli/entities"
|
||||||
"nto_cli/utils"
|
"nto_cli/utils"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Generate(structName string, fields []entities.Field) {
|
func Generate(structName string, fields []entities.Field) {
|
||||||
mkPath := fmt.Sprintf("%s/frontend/src/%s", utils.FindFrontendPath(), strings.ToLower(structName))
|
mkPath := filepath.Join(utils.FindFrontendPath(), strings.ToLower(structName))
|
||||||
if err := os.Mkdir(mkPath, 0755); err != nil {
|
if err := os.Mkdir(mkPath, 0755); err != nil {
|
||||||
log.Fatalf("Failed to mkdir for model: %s", err)
|
log.Fatalf("Failed to mkdir for model: %s", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FindFrontendPath() string {
|
func FindFrontendPath() string {
|
||||||
@@ -12,33 +13,22 @@ func FindFrontendPath() string {
|
|||||||
log.Fatalf("Failed to find path for frontend: %s", err)
|
log.Fatalf("Failed to find path for frontend: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var dirs []string
|
frontendSuffix := filepath.Join("frontend")
|
||||||
for currentPath != filepath.VolumeName(currentPath)+string(os.PathSeparator) {
|
frontendSourceSuffix := filepath.Join("frontend", "src")
|
||||||
dir, file := filepath.Split(currentPath)
|
if strings.HasSuffix(currentPath, frontendSourceSuffix) {
|
||||||
if file != "" {
|
return currentPath
|
||||||
dirs = append([]string{file}, dirs...)
|
|
||||||
}
|
|
||||||
currentPath = filepath.Clean(dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(dirs) < 2 || filepath.Join(dirs[len(dirs)-2], dirs[len(dirs)-1]) != filepath.Join("frontend", "src") {
|
|
||||||
log.Fatalf("Run this program in frontend/ directory")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var path string
|
var path string
|
||||||
for i, dir := range dirs {
|
|
||||||
if dir == "frontend" {
|
if strings.HasSuffix(currentPath, frontendSuffix) {
|
||||||
break
|
path = filepath.Join(currentPath, "src")
|
||||||
}
|
} else {
|
||||||
if i > 0 {
|
path = filepath.Join(currentPath, frontendSourceSuffix)
|
||||||
path = filepath.Join(path, dir)
|
|
||||||
} else {
|
|
||||||
path = dir
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if filepath.VolumeName(path) == "" {
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
path = filepath.Join(string(os.PathSeparator), path)
|
log.Fatalf("Frontend source directory not found at %s", path)
|
||||||
}
|
}
|
||||||
|
|
||||||
return path
|
return path
|
||||||
|
|||||||
Reference in New Issue
Block a user