summaryrefslogtreecommitdiffhomepage
path: root/cmd/diff/main.go
blob: da93e4660069912f4081495ab3d2eac8d93c0729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main

import (
	"fmt"
	"io"
	"os"
	"os/exec"
	"path/filepath"
)

func main() {
	// Create a temporary directory
	tempDir, err := os.MkdirTemp("", "git-split-diffs")
	if err != nil {
		fmt.Printf("Error creating temp directory: %v\n", err)
		os.Exit(1)
	}
	defer func() {
		fmt.Printf("Cleaning up temporary directory: %s\n", tempDir)
		os.RemoveAll(tempDir)
	}()
	fmt.Printf("Created temporary directory: %s\n", tempDir)

	// Clone the repository with minimum depth
	fmt.Println("Cloning git-split-diffs repository with minimum depth...")
	cmd := exec.Command("git", "clone", "--depth=1", "https://github.com/kujtimiihoxha/git-split-diffs", tempDir)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		fmt.Printf("Error cloning repository: %v\n", err)
		os.Exit(1)
	}

	// Run npm install
	fmt.Println("Running npm install...")
	cmdNpmInstall := exec.Command("npm", "install")
	cmdNpmInstall.Dir = tempDir
	cmdNpmInstall.Stdout = os.Stdout
	cmdNpmInstall.Stderr = os.Stderr
	if err := cmdNpmInstall.Run(); err != nil {
		fmt.Printf("Error running npm install: %v\n", err)
		os.Exit(1)
	}

	// Run npm run build
	fmt.Println("Running npm run build...")
	cmdNpmBuild := exec.Command("npm", "run", "build")
	cmdNpmBuild.Dir = tempDir
	cmdNpmBuild.Stdout = os.Stdout
	cmdNpmBuild.Stderr = os.Stderr
	if err := cmdNpmBuild.Run(); err != nil {
		fmt.Printf("Error running npm run build: %v\n", err)
		os.Exit(1)
	}

	destDir := filepath.Join(".", "internal", "assets", "diff")
	destFile := filepath.Join(destDir, "index.mjs")

	// Make sure the destination directory exists
	if err := os.MkdirAll(destDir, 0o755); err != nil {
		fmt.Printf("Error creating destination directory: %v\n", err)
		os.Exit(1)
	}

	// Copy the file
	srcFile := filepath.Join(tempDir, "build", "index.mjs")
	fmt.Printf("Copying %s to %s\n", srcFile, destFile)
	if err := copyFile(srcFile, destFile); err != nil {
		fmt.Printf("Error copying file: %v\n", err)
		os.Exit(1)
	}

	fmt.Println("Successfully completed the process!")
}

// copyFile copies a file from src to dst
func copyFile(src, dst string) error {
	sourceFile, err := os.Open(src)
	if err != nil {
		return err
	}
	defer sourceFile.Close()

	destFile, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer destFile.Close()

	_, err = io.Copy(destFile, sourceFile)
	if err != nil {
		return err
	}

	// Make sure the file is written to disk
	err = destFile.Sync()
	if err != nil {
		return err
	}

	return nil
}