Skip to content

Commit

Permalink
chore:added functionality to automatically create types definition an…
Browse files Browse the repository at this point in the history
…d folders
  • Loading branch information
urizennnn committed Jun 10, 2024
1 parent 070258e commit 59b3483
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
61 changes: 61 additions & 0 deletions lib/Scripts/Type.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions lib/cmd/addType.go
Original file line number Diff line number Diff line change
@@ -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 <type-name>",
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)

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@urizen/express-cli",
"version": "2.3.4",
"version": "2.4.4",
"license": "MIT",
"bin": {
"express": "bin/linux"
Expand Down

0 comments on commit 59b3483

Please sign in to comment.