main.go

  1package main
  2
  3import (
  4	"fmt"
  5	"io"
  6	"os"
  7	"os/exec"
  8	"path/filepath"
  9)
 10
 11func main() {
 12	// Create a temporary directory
 13	tempDir, err := os.MkdirTemp("", "git-split-diffs")
 14	if err != nil {
 15		fmt.Printf("Error creating temp directory: %v\n", err)
 16		os.Exit(1)
 17	}
 18	defer func() {
 19		fmt.Printf("Cleaning up temporary directory: %s\n", tempDir)
 20		os.RemoveAll(tempDir)
 21	}()
 22	fmt.Printf("Created temporary directory: %s\n", tempDir)
 23
 24	// Clone the repository with minimum depth
 25	fmt.Println("Cloning git-split-diffs repository with minimum depth...")
 26	cmd := exec.Command("git", "clone", "--depth=1", "https://github.com/kujtimiihoxha/git-split-diffs", tempDir)
 27	cmd.Stdout = os.Stdout
 28	cmd.Stderr = os.Stderr
 29	if err := cmd.Run(); err != nil {
 30		fmt.Printf("Error cloning repository: %v\n", err)
 31		os.Exit(1)
 32	}
 33
 34	// Run npm install
 35	fmt.Println("Running npm install...")
 36	cmdNpmInstall := exec.Command("npm", "install")
 37	cmdNpmInstall.Dir = tempDir
 38	cmdNpmInstall.Stdout = os.Stdout
 39	cmdNpmInstall.Stderr = os.Stderr
 40	if err := cmdNpmInstall.Run(); err != nil {
 41		fmt.Printf("Error running npm install: %v\n", err)
 42		os.Exit(1)
 43	}
 44
 45	// Run npm run build
 46	fmt.Println("Running npm run build...")
 47	cmdNpmBuild := exec.Command("npm", "run", "build")
 48	cmdNpmBuild.Dir = tempDir
 49	cmdNpmBuild.Stdout = os.Stdout
 50	cmdNpmBuild.Stderr = os.Stderr
 51	if err := cmdNpmBuild.Run(); err != nil {
 52		fmt.Printf("Error running npm run build: %v\n", err)
 53		os.Exit(1)
 54	}
 55
 56	destDir := filepath.Join(".", "internal", "assets", "diff")
 57	destFile := filepath.Join(destDir, "index.mjs")
 58
 59	// Make sure the destination directory exists
 60	if err := os.MkdirAll(destDir, 0o755); err != nil {
 61		fmt.Printf("Error creating destination directory: %v\n", err)
 62		os.Exit(1)
 63	}
 64
 65	// Copy the file
 66	srcFile := filepath.Join(tempDir, "build", "index.mjs")
 67	fmt.Printf("Copying %s to %s\n", srcFile, destFile)
 68	if err := copyFile(srcFile, destFile); err != nil {
 69		fmt.Printf("Error copying file: %v\n", err)
 70		os.Exit(1)
 71	}
 72
 73	fmt.Println("Successfully completed the process!")
 74}
 75
 76// copyFile copies a file from src to dst
 77func copyFile(src, dst string) error {
 78	sourceFile, err := os.Open(src)
 79	if err != nil {
 80		return err
 81	}
 82	defer sourceFile.Close()
 83
 84	destFile, err := os.Create(dst)
 85	if err != nil {
 86		return err
 87	}
 88	defer destFile.Close()
 89
 90	_, err = io.Copy(destFile, sourceFile)
 91	if err != nil {
 92		return err
 93	}
 94
 95	// Make sure the file is written to disk
 96	err = destFile.Sync()
 97	if err != nil {
 98		return err
 99	}
100
101	return nil
102}