-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore:added functionality to automatically create types definition an…
…d folders
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters