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

feat: frontend js project type #135

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ Dockerfile.initium
*.init_test*
initium_onmain.yaml
initium_onbranch.yaml
dist/
/dist

node_modules
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Following we have a matrix related to which application runtime our CLI is curre
|----------------------|:------------------:|
| [Nodejs](https://github.com/nearform/initium-cli/blob/main/assets/docker/Dockerfile.node.tmpl) | :white_check_mark: |
| [GoLang](https://github.com/nearform/initium-cli/blob/main/assets/docker/Dockerfile.go.tmpl) | :white_check_mark: |
| [JS Frontend](https://github.com/nearform/initium-cli/blob/main/assets/docker/Dockerfile.frontend-js.tmpl) | :white_check_mark: |
| Python | Coming Soon |
| More will be added...| |

Expand Down
24 changes: 24 additions & 0 deletions assets/docker/Dockerfile.frontend-js.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM node:{{ or .RuntimeVersion .DefaultRuntimeVersion }} AS build-env

ENV CI=true

WORKDIR /app

COPY package*.json tsconfig*.json ./

RUN {{ .NodeInstallCommand }}

COPY . .

RUN npm run build --if-present
RUN npm test

FROM node:{{ or .RuntimeVersion .DefaultRuntimeVersion }}

COPY --from=build-env /app /app

WORKDIR /app

USER node

ENTRYPOINT npx http-server ./build
2 changes: 1 addition & 1 deletion assets/docker/Dockerfile.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ RUN CGO_ENABLED=0 go build -o /go/bin/app
FROM gcr.io/distroless/static-debian11

COPY --from=build /go/bin/app /
ENTRYPOINT ["/app"]
ENTRYPOINT ["/app"]
2 changes: 1 addition & 1 deletion assets/docker/Dockerfile.node.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ FROM gcr.io/distroless/nodejs20-debian11
COPY --from=build-env /app /app
WORKDIR /app
USER nonroot
CMD ["index.js"]
CMD ["index.js"]
14 changes: 14 additions & 0 deletions example/frontend-js/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should add an actual basic react code here?

<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Initium Frontend Sample</title>
</head>

<body>
Welcome to Initium!
</body>

</html>
38 changes: 38 additions & 0 deletions example/frontend-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "frontend-js",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 32 additions & 2 deletions src/cli/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ FROM gcr.io/distroless/static-debian11

COPY --from=build /go/bin/app /
ENTRYPOINT ["/app"]

`

const expectedNodeAppDockerTemplate = `FROM node:20.2.0 AS build-env
Expand All @@ -49,11 +50,40 @@ COPY --from=build-env /app /app
WORKDIR /app
USER nonroot
CMD ["index.js"]

`

const expectedFrontendJsAppDockerTemplate = `FROM node:20.2.0 AS build-env

ENV CI=true

WORKDIR /app

COPY package*.json tsconfig*.json ./

RUN npm i

COPY . .

RUN npm run build --if-present
RUN npm test

FROM node:20.2.0

COPY --from=build-env /app /app

WORKDIR /app

USER node

CMD npx http-server -p 8080 ./build

`

var projects = map[project.ProjectType]map[string]string{
project.NodeProject: {"directory": "example", "expectedTemplate": expectedNodeAppDockerTemplate},
project.GoProject: {"directory": ".", "expectedTemplate": expectedGoAppDockerTemplate},
project.NodeProject: {"directory": "example/node", "expectedTemplate": expectedNodeAppDockerTemplate},
project.GoProject: {"directory": ".", "expectedTemplate": expectedGoAppDockerTemplate},
project.FrontendJsProject: {"directory": "example/frontend-js", "expectedTemplate": expectedFrontendJsAppDockerTemplate},
}

func TestShouldRenderDockerTemplate(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions src/services/k8s/knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestLoadManifestForPrivateService(t *testing.T) {
Tag: "v1.1.0",
}

serviceManifest, err := LoadManifest(namespace, commitSha, proj, dockerImage, path.Join(root, "example/.env.sample"))
serviceManifest, err := LoadManifest(namespace, commitSha, proj, dockerImage, path.Join(root, "example/node/.env.sample"))

if err != nil {
t.Fatalf(fmt.Sprintf("Error: %v", err))
Expand All @@ -93,7 +93,7 @@ func TestLoadManifestForPublicService(t *testing.T) {
imagePullSecrets := []string{"secretPassword123"}

proj := &project.Project{Name: "knative_test",
Directory: path.Join(root, "example"),
Directory: path.Join(root, "example/node"),
Resources: os.DirFS(root),
ImagePullSecrets: imagePullSecrets,
IsPrivate: true,
Expand All @@ -106,7 +106,7 @@ func TestLoadManifestForPublicService(t *testing.T) {
Tag: "v1.1.0",
}

serviceManifest, err := LoadManifest(namespace, commitSha, proj, dockerImage, path.Join(root, "example/.env.sample"))
serviceManifest, err := LoadManifest(namespace, commitSha, proj, dockerImage, path.Join(root, "example/node/.env.sample"))

if err != nil {
t.Fatalf(fmt.Sprintf("Error: %v", err))
Expand Down
36 changes: 32 additions & 4 deletions src/services/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package project

import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"os"
"path"
"strings"
"text/template"

"github.com/nearform/initium-cli/src/services/git"
Expand All @@ -15,8 +17,9 @@ import (
type ProjectType string

const (
NodeProject ProjectType = "node"
GoProject ProjectType = "go"
NodeProject ProjectType = "node"
GoProject ProjectType = "go"
FrontendJsProject ProjectType = "frontend-js"
)

type Project struct {
Expand Down Expand Up @@ -65,6 +68,8 @@ func IsValidProjectType(projectType string) bool {
return true
case string(GoProject):
return true
case string(FrontendJsProject):
return true
default:
return false
}
Expand All @@ -74,8 +79,28 @@ func DetectType(directory string) (ProjectType, error) {
var detectedRuntimes []ProjectType
var projectType ProjectType
if _, err := os.Stat(path.Join(directory, "package.json")); err == nil {
detectedRuntimes = append(detectedRuntimes, NodeProject)
projectType = NodeProject
bytes, err := os.ReadFile(path.Join(directory, "package.json"))
var result map[string]any
json.Unmarshal(bytes, &result)
Copy link
Contributor

Choose a reason for hiding this comment

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

Lack error validation

dependencies := result["dependencies"].(map[string]any)

if err != nil {
fmt.Print(err)
}

frontendDetected := false
for dependency := range dependencies {
if strings.Contains(string(dependency), "react") || strings.Contains(string(dependency), "angular") || strings.Contains(string(dependency), "vue") {
frontendDetected = true
}
}
if frontendDetected {
detectedRuntimes = append(detectedRuntimes, FrontendJsProject)
projectType = FrontendJsProject
} else {
detectedRuntimes = append(detectedRuntimes, NodeProject)
projectType = NodeProject
}
}
if _, err := os.Stat(path.Join(directory, "go.mod")); err == nil {
detectedRuntimes = append(detectedRuntimes, GoProject)
Expand All @@ -98,6 +123,9 @@ func (proj *Project) setRuntimeVersion() error {
case GoProject:
proj.DefaultRuntimeVersion = defaults.DefaultGoRuntimeVersion
return nil
case FrontendJsProject:
proj.DefaultRuntimeVersion = defaults.DefaultFrontendJsRuntimeVersion
return nil
default:
return fmt.Errorf("cannot detect runtime version for project type %s", proj.Type)
}
Expand Down
9 changes: 5 additions & 4 deletions src/services/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

var projects = map[ProjectType]map[string]string{
NodeProject: {"directory": "example"},
GoProject: {"directory": "."},
"invalid": {"directory": "src"},
NodeProject: {"directory": "example/node"},
FrontendJsProject: {"directory": "example/frontend-js"},
GoProject: {"directory": "."},
"invalid": {"directory": "src"},
}

var root = "../../../"
Expand Down Expand Up @@ -139,7 +140,7 @@ func TestNodeInstallCommand(t *testing.T) {
Resources: os.DirFS(root),
}

if project_type == NodeProject {
if project_type == NodeProject || project_type == FrontendJsProject {
expected := "npm i"
installCommand := proj.NodeInstallCommand()
if installCommand != expected {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ const DefaultNodeRuntimeVersion string = "20.2.0"

// renovate: datasource=docker depName=golang
const DefaultGoRuntimeVersion string = "1.20.4"

const DefaultFrontendJsRuntimeVersion string = "20.2.0"
Loading