Created
June 25, 2024 03:35
-
-
Save halfnibble/8af45d5c14dd7b25f4118339731da335 to your computer and use it in GitHub Desktop.
Rush.js multiple project `npm start` runner
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
// I place this in ./cli/main.go, whereas rush.json is at the root of my repo. | |
// Then run from within ./cli with `$ go run main.go` | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"os" | |
"os/exec" | |
"path/filepath" | |
"sync" | |
"github.com/tidwall/jsonc" | |
) | |
// Project represents the structure of each project in rush.json | |
type Project struct { | |
PackageName string `json:"packageName"` | |
ProjectFolder string `json:"projectFolder"` | |
ReviewCategory string `json:"reviewCategory"` | |
Tags []string `json:"tags"` | |
} | |
// Config represents the JSON structure of rush.json | |
type Config struct { | |
Projects []Project `json:"projects"` | |
} | |
func main() { | |
// // Base directory where projects are located | |
baseDir := "../" // Adjust this path based on the actual relative path from the Go app to the projects | |
// Load the configuration from rush.json | |
rushPath := filepath.Join(baseDir, "rush.json") | |
config, err := loadConfig(rushPath) | |
if err != nil { | |
fmt.Println("Error loading configuration:", err) | |
return | |
} | |
var wg sync.WaitGroup | |
for _, project := range config.Projects { | |
wg.Add(1) | |
go func(project Project) { | |
defer wg.Done() | |
runStart(baseDir, project.ProjectFolder) | |
}(project) | |
} | |
wg.Wait() | |
} | |
func loadConfig(filePath string) (*Config, error) { | |
file, err := os.ReadFile(filePath) | |
if err != nil { | |
return nil, err | |
} | |
var config Config | |
err = json.Unmarshal(jsonc.ToJSON(file), &config) | |
if err != nil { | |
return nil, err | |
} | |
return &config, nil | |
} | |
func runStart(baseDir, project string) { | |
projectPath := filepath.Join(baseDir, project) // Full path to the project | |
fmt.Printf("Starting project: %s\n", projectPath) | |
cmd := exec.Command("npm", "start") // Prepare the npm start command | |
cmd.Dir = projectPath // Set the working directory to the project folder | |
cmd.Stdout = os.Stdout | |
cmd.Stderr = os.Stderr | |
err := cmd.Run() // Run the command | |
if err != nil { | |
fmt.Printf("Error running project %s: %s\n", projectPath, err) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment