Skip to content

Commit

Permalink
Make node version customizable (QubitPi#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi committed Jul 22, 2024
1 parent 6229985 commit 1f5e0f4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions .web-docs/components/provisioner/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The `react` provisioner is used to install a compiled React-based APP in AWS AMI

**Optional**

- `nodeVersion` (string) - The Node.js version running the React app; default to "18"
- `homeDir` (string) - The `$Home` directory in AMI image; default to `/home/ubuntu`

<!--
Expand Down
1 change: 1 addition & 0 deletions docs/provisioners/react.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The `react` provisioner is used to install a compiled React-based APP in AWS AMI

**Optional**

- `nodeVersion` (string) - The Node.js version running the React app; default to "18"
- `homeDir` (string) - The `$Home` directory in AMI image; default to `/home/ubuntu`

<!--
Expand Down
21 changes: 13 additions & 8 deletions provisioner/react/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ import (
"text/template"
)

// PORT Default port of Sonatype Nexus
// PORT Default port of React app
const PORT string = "3000"

// NODE_VERSION Default node version running the React app
const NODE_VERSION = "18"

type Config struct {
DistSource string `mapstructure:"distSource" required:"true"`
SslCertBase64 string `mapstructure:"sslCertBase64" required:"true"`
SslCertKeyBase64 string `mapstructure:"sslCertKeyBase64" required:"true"`
AppDomain string `mapstructure:"appDomain" required:"true"`
NodeVersion string `mapstructure:"nodeVersion" required:"false"`
HomeDir string `mapstructure:"homeDir" required:"false"`

ctx interpolate.Context
Expand Down Expand Up @@ -59,7 +63,10 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, communicat
return err
}

err = shell.Provision(ctx, ui, communicator, getCommands())
if p.config.NodeVersion == "" {
p.config.NodeVersion = NODE_VERSION
}
err = shell.Provision(ctx, ui, communicator, getCommands(p.config.NodeVersion))
if err != nil {
return err
}
Expand Down Expand Up @@ -125,8 +132,8 @@ server {
return buf.String()
}

func getCommands() []string {
return append(getCommandsUpdatingUbuntu(), getCommandsInstallingNode()...)
func getCommands(nodeVersion string) []string {
return append(getCommandsUpdatingUbuntu(), getCommandsInstallingNode(nodeVersion)...)
}

func getCommandsUpdatingUbuntu() []string {
Expand All @@ -136,14 +143,12 @@ func getCommandsUpdatingUbuntu() []string {
}
}

func getCommandsInstallingNode() []string {
func getCommandsInstallingNode(nodeVersion string) []string {
return []string{
"sudo apt install -y curl",
"curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -",
fmt.Sprintf("curl -fsSL https://deb.nodesource.com/setup_%s.x | sudo -E bash -", nodeVersion),
"sudo apt install -y nodejs",

"sudo npm install -g yarn",

"sudo npm install -g serve",
}
}
2 changes: 2 additions & 0 deletions provisioner/react/provisioner.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions provisioner/react/provisioner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Jiaqi Liu
// SPDX-License-Identifier: MPL-2.0

package react

import (
"reflect"
"testing"
)

func Test_getCommandsInstallingNode(t *testing.T) {
actualCommands := getCommandsInstallingNode("18")

expectedCommands := []string{
"sudo apt install -y curl",
"curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -",
"sudo apt install -y nodejs",

"sudo npm install -g yarn",

"sudo npm install -g serve",
}

if !reflect.DeepEqual(expectedCommands, actualCommands) {
t.Errorf("Expected and actual commands do not match: %s\n\n%s", expectedCommands, actualCommands)
}
}

0 comments on commit 1f5e0f4

Please sign in to comment.