Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Feb 28, 2024
1 parent f6f6c55 commit a040600
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 44 deletions.
5 changes: 0 additions & 5 deletions server/cjs_lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ func initCJSLexerWorkDirectory() (err error) {
panic(err)
}
err = os.WriteFile(path.Join(wd, "cjs_lexer.js"), js, 0644)
if err != nil {
return
}

log.Infof("cjs lexer initialized")
return
}

Expand Down
60 changes: 28 additions & 32 deletions server/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,51 +94,44 @@ var polyfilledInternalNodeModules = map[string]string{
"zlib": "browserify-zlib@0.2.0",
}

func checkNodejs(installDir string) (nodeVer string, pnpmVer string, err error) {
var installed bool
CheckNodejs:
nodeVer, major, err := getNodejsVersion()
if err != nil || major < nodejsMinVersion {
func checkNodejs(installDir string) (nodeVersion string, pnpmVersion string, err error) {
nodeVersion, major, err := getNodejsVersion()
usingSystemNodejs := err == nil && major >= nodejsMinVersion

if !usingSystemNodejs {
PATH := os.Getenv("PATH")
nodeBinDir := path.Join(installDir, "bin")
if !strings.Contains(PATH, nodeBinDir) {
os.Setenv("PATH", fmt.Sprintf("%s%c%s", nodeBinDir, os.PathListSeparator, PATH))
goto CheckNodejs
} else if !installed {
err = os.RemoveAll(installDir)
if err != nil {
return
}
}
nodeVersion, major, err = getNodejsVersion()
if err != nil || major < nodejsMinVersion {
err = installNodejs(installDir, nodejsLatestLTS)
if err != nil {
return
}
log.Infof("nodejs %s installed", nodejsLatestLTS)
installed = true
goto CheckNodejs
} else {
if err == nil {
err = fmt.Errorf("bad nodejs version %s need %d+", nodeVer, nodejsMinVersion)
}
return
}
nodeVersion, major, err = getNodejsVersion()
}
if err == nil && major < nodejsMinVersion {
err = fmt.Errorf("bad nodejs version %s need %d+", nodeVersion, nodejsMinVersion)
}

CheckPnpm:
output, err := exec.Command("pnpm", "-v").CombinedOutput()
if err != nil {
if errors.Is(err, exec.ErrNotFound) {
output, err = exec.Command("npm", "install", "pnpm", "-g").CombinedOutput()
if err != nil {
err = fmt.Errorf("install pnpm: %s", strings.TrimSpace(string(output)))
return
}
goto CheckPnpm
return
}

pnpmOutput, err := exec.Command("pnpm", "-v").CombinedOutput()
if err != nil && errors.Is(err, exec.ErrNotFound) {
out, e := exec.Command("npm", "install", "pnpm", "-g").CombinedOutput()
if e != nil {
err = fmt.Errorf("failed to install pnpm: %v", string(out))
return
}
err = fmt.Errorf("bad pnpm version: %s", strings.TrimSpace(string(output)))
pnpmOutput, err = exec.Command("pnpm", "-v").CombinedOutput()
}
if err == nil {
pnpmVer = strings.TrimSpace(string(output))
pnpmVersion = strings.TrimSpace(string(pnpmOutput))
}
return
}
Expand All @@ -155,7 +148,7 @@ func getNodejsVersion() (version string, major int, err error) {
return
}

func installNodejs(dir string, version string) (err error) {
func installNodejs(installDir string, version string) (err error) {
arch := runtime.GOARCH
switch arch {
case "amd64":
Expand Down Expand Up @@ -189,7 +182,10 @@ func installNodejs(dir string, version string) (err error) {
return
}

cmd = exec.Command("mv", "-f", strings.TrimSuffix(path.Base(dlURL), ".tar.xz"), dir)
// remove old installation if exists
os.RemoveAll(installDir)

cmd = exec.Command("mv", "-f", strings.TrimSuffix(path.Base(dlURL), ".tar.xz"), installDir)
cmd.Dir = os.TempDir()
output, err = cmd.CombinedOutput()
if err != nil {
Expand Down
9 changes: 2 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func Serve(efs EmbedFS) {
cfg.NpmRegistry = strings.TrimRight(strings.TrimSpace(string(output)), "/") + "/"
}
}
log.Infof("nodejs v%s installed, registry: %s, pnpm: %s", nodeVer, cfg.NpmRegistry, pnpmVer)
log.Infof("nodejs: v%s, pnpm: %s, registry: %s", nodeVer, pnpmVer, cfg.NpmRegistry)

err = initCJSLexerWorkDirectory()
if err != nil {
Expand Down Expand Up @@ -161,12 +161,7 @@ func Serve(efs EmbedFS) {
},
})

if isDev {
log.Debugf("Server is ready on http://localhost:%d", cfg.Port)
log.Debugf("Testing page at http://localhost:%d?test", cfg.Port)
} else {
log.Info("Server is ready")
}
log.Infof("Server is ready on http://localhost:%d", cfg.Port)

c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGHUP, syscall.SIGABRT)
Expand Down

0 comments on commit a040600

Please sign in to comment.