diff --git a/lib/Scripts/Type.go b/lib/Scripts/Type.go new file mode 100644 index 0000000..e03d958 --- /dev/null +++ b/lib/Scripts/Type.go @@ -0,0 +1,61 @@ +package scripts + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/urizennnn/express-cli/errors" +) + +func checkIfTSEnabled(cwd string) bool { + rootDir := filepath.VolumeName(cwd) + string(os.PathSeparator) + + for { + entries, err := os.ReadDir(cwd) + errors.Check_Err(err) + + for _, entry := range entries { + if !entry.IsDir() && entry.Name() == "tsconfig.json" { + fmt.Printf("TypeScript enabled: tsconfig.json found in %s\n", cwd) + return true + } + } + + parentDir := filepath.Dir(cwd) + + if parentDir == cwd || parentDir == rootDir { + break + } + + cwd = parentDir + } + + fmt.Println("TypeScript not enabled: tsconfig.json not found") + return false +} +func AddTypeDefinition(cwd, name string) error { + if !checkIfTSEnabled(cwd) { + return fmt.Errorf("TypeScript must be enabled to add type definition") + } + + typesDir := filepath.Join(cwd, "types") + err := os.MkdirAll(typesDir, os.ModePerm) + if err != nil { + return fmt.Errorf("failed to create types directory: %w", err) + } + + file := filepath.Join(typesDir, name+".dto.ts") + newFile, err := os.Create(file) + errors.Check_Err(err) + defer newFile.Close() + if err != nil { + return fmt.Errorf("failed to create type definition file: %w", err) + } + + typeDef := `export type Test={}` + _, err = newFile.Write([]byte(typeDef)) + errors.Check_Err(err) + fmt.Printf("Type definition file created: %s\n", file) + return nil +} diff --git a/lib/cmd/addType.go b/lib/cmd/addType.go new file mode 100644 index 0000000..ada0643 --- /dev/null +++ b/lib/cmd/addType.go @@ -0,0 +1,30 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" + "github.com/urizennnn/express-cli/errors" + scripts "github.com/urizennnn/express-cli/lib/Scripts" +) + +var addTypeCmd = &cobra.Command{ + Use: "type ", + Short: "A brief description of your command", + Long: `Add type definition to your code.`, + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + err := cmd.Help() + errors.Check_Err(err) + } + cwd, err := os.Getwd() + errors.Check_Err(err) + err = scripts.AddTypeDefinition(cwd, args[0]) + errors.Check_Err(err) + }, +} + +func init() { + rootCmd.AddCommand(addTypeCmd) + +} diff --git a/package.json b/package.json index 983f74c..b3a2c1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@urizen/express-cli", - "version": "2.3.4", + "version": "2.4.4", "license": "MIT", "bin": { "express": "bin/linux"