Skip to content

Commit

Permalink
If COMPONENTS doesn't match package.json's name, shim it for NODE_PAT…
Browse files Browse the repository at this point in the history
…H with a symlink
  • Loading branch information
wegry committed Oct 16, 2021
1 parent ccfbc33 commit 82741e7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/dsk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func main() {

if componentsPath != "" {
if err := app.OpenComponents(ctx); err != nil {
log.Print(red.Sprintf("Failed to start application: %s", err))
log.Fatal(red.Sprintf("Failed to start application: %s", err))
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/plex/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ func (app *App) OpenVersions(ctx context.Context) error {

func (app *App) OpenComponents(ctx context.Context) error {
cmps, err := NewComponents(app.componentsPath)
if err != nil {
return err
}

cmps.Detect()
app.Components = cmps
return err
Expand Down
61 changes: 49 additions & 12 deletions internal/plex/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package plex

import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
Expand All @@ -24,23 +26,58 @@ var (
}
)

func NewComponents(path string) (*Components, error) {
log.Printf("Initializing components from path %s...", path)
// Allow node module resolution for component paths.
orgFp := filepath.Dir(path)
type packageJsonFields struct {
Name string `json:"name"`
}

// NewComponents sniffs around to make sure that a package.json is found
// at the path provided by env var. If it is, and its name doesn't match
// the path (in node module's resolution), we fudge the path by
// dropping it at a symlinked path instead.
func NewComponents(pathEnvVar string) (*Components, error) {
log.Printf("Initializing components from path %s...", pathEnvVar)
nodePath := filepath.Clean(pathEnvVar)

rawPkgJson, err := ioutil.ReadFile(filepath.Join(nodePath, packageJson))
if err != nil {
return nil, err
}

var pkgJson packageJsonFields
json.Unmarshal(rawPkgJson, &pkgJson)

if err != nil {
return nil, err
}

nodePath := path
if filepath.Base(nodePath) != pkgJson.Name {
dir := filepath.Join("dist", pkgJson.Name)

// given <path>/@rundsk/example-component-library, this sets NODE_PATH to be <path>
if strings.HasPrefix(filepath.Base(orgFp), "@") {
nodePath = filepath.Dir(orgFp)
nodePath = "dist"
if err := os.Remove(dir); err != nil && !os.IsNotExist(err) {
return nil, err
}

splitName := strings.Split(pkgJson.Name, "/")

if len(splitName) > 1 {
os.MkdirAll(filepath.Dir(dir), os.ModePerm)
}

err = os.Symlink(filepath.Clean(pathEnvVar), dir)

if err != nil {
return nil, err
}
}

path, err := filepath.Abs(path)
path, err := filepath.Abs(nodePath)
log.Printf("Using path %s as JS entry point", path)
return &Components{
FS: http.Dir(path),
Path: path,
JSEntryPoint: nodePath,
FS: http.Dir(pathEnvVar),
// This is different because the CSS entrypoint doesn't use ESBuild's package.json lookup and NODE_ENV.
Path: filepath.Clean(pathEnvVar),
JSEntryPoint: path,
}, err
}

Expand Down

0 comments on commit 82741e7

Please sign in to comment.