Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zeroconfig] Default nodejs to corepack enabled #2396

Merged
merged 2 commits into from
Nov 4, 2024

Conversation

mikeland73
Copy link
Contributor

Summary

  • Adds DEVBOX_COREPACK_ENABLED: "1" to nodejs detector.
  • Adds ability to set env programmatically on config. (This probably doesn't work with interspaced comments)

How was it tested?

  • Unit tests
  • Manually created project with package.json and then did devbox init --auto

Members: members,
}
}
c.root.Format()
Copy link
Collaborator

@LucilleH LucilleH Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above logic seems so convoluted - what's hujson.ObjectMember?

Is there a simpler representation? Something like

env := [key, val, key, val]
members := make(map[string]string)

for i := 0; i < len(env); i += 2 {
    members[env[i]] = env[i+1]
}

I'm not sure about this part of the logic as I don't understand it. Maybe @gcurtis knows better.

Approving for the rest of the NodeJS logic 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we can get rid of any of the code because hujson api is pretty generic so everything needs to be explicit. Maybe I can refactor some portions out:

func (c *configAST) createMemberIfMissing(key string) *hujson.ObjectMember {
	i := c.memberIndex(c.root.Value.(*hujson.Object), key)
	if i == -1 {
		c.root.Value.(*hujson.Object).Members = append(c.root.Value.(*hujson.Object).Members, hujson.ObjectMember{
			Name: hujson.Value{Value: hujson.String(key)},
		})
		i = len(c.root.Value.(*hujson.Object).Members) - 1
	}
	return &c.root.Value.(*hujson.Object).Members[i]
}

func mapToObjectMembers(env map[string]string) []hujson.ObjectMember {
	members := make([]hujson.ObjectMember, 0, len(env))
	for k, v := range env {
		members = append(members, hujson.ObjectMember{
			Name:  hujson.Value{Value: hujson.String(k)},
			Value: hujson.Value{Value: hujson.String(v)},
		})
	}
	return members
}

func (c *configAST) setEnv(env map[string]string) {
	c.createMemberIfMissing("env").Value.Value = &hujson.Object{
		Members: mapToObjectMembers(env),
	}
	c.root.Format()
}

@mikeland73 mikeland73 merged commit cba645a into main Nov 4, 2024
29 checks passed
@mikeland73 mikeland73 deleted the landau/autodetect-env branch November 4, 2024 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants