commit fd0ba1690455d1840dc1c75af6b0c4171cdfd0c9 Author: andres Date: Sun Dec 10 12:58:10 2023 +0100 initial commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/create-react-component.iml b/.idea/create-react-component.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/create-react-component.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1c582df --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..92c77fb --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module create-react-component + +go 1.21 diff --git a/main.go b/main.go new file mode 100644 index 0000000..ad87f32 --- /dev/null +++ b/main.go @@ -0,0 +1,159 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "os/exec" + "strings" +) + +func main() { + args := os.Args + if len(args) < 3 { + fmt.Println("Please provide the component name and directory") + os.Exit(1) + } + + name := args[1] + currentDir := args[2] + + err := createComponent(name, currentDir) + if err != nil { + fmt.Println("Error creating component:", err) + os.Exit(1) + } + + err = updateMainIndex(name, currentDir) + if err != nil { + fmt.Println("Error updating main index:", err) + os.Exit(1) + } +} + +func capitalizeFirstLetter(s string) string { + return strings.ToUpper(s[:1]) + s[1:] +} + +func createComponent(name, currentDir string) error { + capitalizedName := capitalizeFirstLetter(name) + dirPath := fmt.Sprintf("%s/%s", currentDir, name) + componentPath := fmt.Sprintf("%s/%s.tsx", dirPath, name) + sassPath := fmt.Sprintf("%s/%s.module.scss", dirPath, name) + indexPath := fmt.Sprintf("%s/index.ts", dirPath) + storyPath := fmt.Sprintf("%s/%s.stories.tsx", dirPath, name) + + // Create directory if not exists + if _, err := os.Stat(dirPath); os.IsNotExist(err) { + err := os.Mkdir(dirPath, 0755) + if err != nil { + return err + } + } + + // Component content + componentContent := fmt.Sprintf(`import React from 'react' +import s from './%s.module.scss' +export type %sProps = {} + +export const %s: React.FC<%sProps> = ({}) => { + return
%s
+} +`, name, capitalizedName, capitalizedName, capitalizedName, capitalizedName) + + // SASS content + sassContent := `.container { + // styles go here +}` + + // Index content + indexContent := fmt.Sprintf(`export * from './%s'`, name) + + // Story content + storyContent := fmt.Sprintf(`import type { Meta, StoryObj } from '@storybook/react' +import { %s } from './' + +const meta = { + component: %s, + tags: ['autodocs'], + title: 'Components/%s', +} satisfies Meta + +export default meta +type Story = StoryObj + +export const Default: Story = { + args: {}, +} +`, capitalizedName, capitalizedName, capitalizedName, capitalizedName) + + // Write files + err := writeToFile(componentPath, componentContent) + if err != nil { + return err + } + err = writeToFile(sassPath, sassContent) + if err != nil { + return err + } + err = writeToFile(indexPath, indexContent) + if err != nil { + return err + } + err = writeToFile(storyPath, storyContent) + if err != nil { + return err + } + + // Execute formatting and linting commands + runCommand("pnpm", "run", "format:file", dirPath) + runCommand("pnpm", "run", "lint:file", dirPath+"/**") + + return nil +} + +func writeToFile(path, content string) error { + file, err := os.Create(path) + if err != nil { + return err + } + defer file.Close() + + writer := bufio.NewWriter(file) + _, err = writer.WriteString(content) + if err != nil { + return err + } + + return writer.Flush() +} + +func updateMainIndex(name, currentDir string) error { + mainIndexPath := fmt.Sprintf("%s/index.ts", currentDir) + lineToAdd := fmt.Sprintf(`export * from './%s'`, name) + + // Read and update file content + content, err := os.ReadFile(mainIndexPath) + if err != nil { + return err + } + + if !strings.Contains(string(content), lineToAdd) { + newContent := lineToAdd + "\n" + string(content) + err = os.WriteFile(mainIndexPath, []byte(newContent), 0644) + if err != nil { + return err + } + } + + return nil +} + +func runCommand(command string, args ...string) { + cmd := exec.Command(command, args...) + output, err := cmd.CombinedOutput() + if err != nil { + fmt.Println("Error executing command:", err) + } + fmt.Println(string(output)) +}