Created
August 23, 2017 18:12
-
-
Save jameynakama/9d7c66e0f60e75be7a031fcccc445c09 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"os/exec" | |
"os/user" | |
"path/filepath" | |
"strings" | |
) | |
func buildDist(dir string) string { | |
log.Println("Building dist...") | |
cmd := exec.Command("npm", "run", "dist") | |
cmd.Dir = dir | |
out, err := cmd.Output() | |
handleErr(err, "Error building dist"+dir) | |
return string(out) | |
} | |
func copyNpmrc(dir string) string { | |
log.Println("Copying .npmrc into dist...") | |
cmd := exec.Command("cp", ".npmrc", "dist") | |
cmd.Dir = dir | |
out, err := cmd.Output() | |
handleErr(err, "Error copying .npmrc into dist"+dir) | |
return string(out) | |
} | |
func updateNpmrc(dir string) string { | |
log.Println("Updating location of cafile...") | |
npmrc := fmt.Sprintf("%s/dist/.npmrc", dir) | |
input, err := ioutil.ReadFile(npmrc) | |
handleErr(err, "Error reading .npmrc") | |
lines := strings.Split(string(input), "\n") | |
for i, l := range lines { | |
if strings.HasPrefix(l, "cafile") { | |
lines[i] = "cafile=../../cafile.pem" | |
break | |
} | |
} | |
output := strings.Join(lines, "\n") | |
err = ioutil.WriteFile(npmrc, []byte(output), 0644) | |
handleErr(err, "Error writing to .npmrc"+dir) | |
return "Updated .npmrc" | |
} | |
func makeLink(dir string) string { | |
log.Printf("Creating npm link to %s...\n", dir) | |
cmd := exec.Command("npm", "link") | |
cmd.Dir = dir | |
out, err := cmd.Output() | |
handleErr(err, "Error making link for project at "+dir) | |
return string(out) | |
} | |
func link(dir string, project string) string { | |
log.Printf("Linking %s to %s...\n", dir, project) | |
cmd := exec.Command("npm", "link", "@oreilly/"+project) | |
cmd.Dir = dir | |
out, err := cmd.Output() | |
handleErr(err, "Error linking project at "+dir) | |
return string(out) | |
} | |
func handleErr(err error, s string) { | |
if err != nil { | |
log.Println(s) | |
log.Fatal(err) | |
} | |
} | |
func main() { | |
var basePath string | |
if len(os.Args) == 4 { | |
p, err := filepath.Abs(os.Args[3]) | |
handleErr(err, "Error resolving path") | |
basePath = p | |
} else { | |
u, err := user.Current() | |
handleErr(err, "Error obtaining user") | |
h := u.HomeDir | |
basePath = fmt.Sprintf("%s/src/frontend", h) | |
} | |
projectLink := fmt.Sprintf("%s/%s", basePath, os.Args[1]) | |
projectDest := fmt.Sprintf("%s/%s", basePath, os.Args[2]) | |
buildDist(projectLink) | |
copyNpmrc(projectLink) | |
updateNpmrc(projectLink) | |
makeLink(projectLink + "/dist") | |
link(projectDest, os.Args[1]) | |
log.Println("Did the thing.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment