Skip to content

Instantly share code, notes, and snippets.

@stavxyz
Last active September 27, 2019 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stavxyz/b6498f4cea50b915bc767f2400b2a8a3 to your computer and use it in GitHub Desktop.
Save stavxyz/b6498f4cea50b915bc767f2400b2a8a3 to your computer and use it in GitHub Desktop.
read a gatsby config with golang
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
)
func main() {
fmt.Printf("Running node command.")
out, err := exec.Command(
"node", "-e", "process.stdout.write(JSON.stringify(require(\"./gatsby-config.js\")))",
).Output()
if err != nil {
fmt.Printf("Node command failed.")
log.Fatal(err)
}
fmt.Printf("Node command succeeded.\n")
gatsbyConfig := make(map[string]interface{})
fmt.Printf("Decoding json.\n")
err = json.Unmarshal(out, &gatsbyConfig)
fmt.Printf("conf --> %+v \n", gatsbyConfig)
if err != nil {
fmt.Printf("Failed to decode json.\n")
log.Fatal(err)
}
fmt.Printf("JSON decoding succeeded.\n")
b, err := json.MarshalIndent(gatsbyConfig, "", " ")
os.Stdout.Write(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment