Skip to content

Commit

Permalink
feat: add npm and yarn based build auto-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishb committed Nov 13, 2023
1 parent 15f6d0d commit 888df44
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/gabo/internal/generator/all_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ func GetOptions() []Option {
newPatternMatcher("docker build ", "docker buildx"),
newGenerator2(generateBuildDockerYaml), "build-docker.yaml",
},
_Option{
"NPM Builder", "build-npm", newFileMatcher("package-lock.json"),
newPatternMatcher("npm install "),
newGenerator2(generateBuildNpmYaml), "build-npm.yaml",
},
_Option{
"Yarn Builder", "build-yarn", newFileMatcher("yarn.lock"),
newPatternMatcher("yarn build"),
newGenerator2(generateBuildYarnYaml), "build-yarn.yaml",
},
_Option{
"Docker Linter", "lint-docker", newFileMatcher("Dockerfile"),
newPatternMatcher("hadolint "),
Expand Down
31 changes: 31 additions & 0 deletions src/gabo/internal/generator/build_npm_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package generator

import "fmt"

const _setupNodeJsTask = `
- name: "Setup Node.js for %s"
uses: actions/setup-node@v4
with:
node-version: "latest"
cache: "npm"
cache-dependency-path: "%s/package-lock.json"
`

const _buildNpmTask = `
- name: "Build %s using NPM"
working-directory: %s
run: npm install && npm run build
`

func generateBuildNpmYaml(repoDir string) (*string, error) {
dirs, err := getDirsContaining(repoDir, "package-lock.json")
if err != nil {
return nil, err
}
str := _buildNpmTemplate
for _, dir := range dirs {
str += fmt.Sprintf(_setupNodeJsTask, dir, dir)
str += fmt.Sprintf(_buildNpmTask, dir, dir)
}
return &str, nil
}
31 changes: 31 additions & 0 deletions src/gabo/internal/generator/build_yarn_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package generator

import "fmt"

const _setupYarnTask = `
- name: "Setup Node.js for %s"
uses: actions/setup-node@v4
with:
node-version: "latest"
cache: "yarn"
cache-dependency-path: "%s/yarn.lock"
`

const _buildYarnTask = `
- name: "Build %s using Yarn"
working-directory: %s
run: yarn && yarn build
`

func generateBuildYarnYaml(repoDir string) (*string, error) {
dirs, err := getDirsContaining(repoDir, "yarn.lock")
if err != nil {
return nil, err
}
str := _buildYarnTemplate
for _, dir := range dirs {
str += fmt.Sprintf(_setupYarnTask, dir, dir)
str += fmt.Sprintf(_buildYarnTask, dir, dir)
}
return &str, nil
}
27 changes: 27 additions & 0 deletions src/gabo/internal/generator/data/build-npm-incomplete.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: Build Node.js package(s)

on: # yamllint disable-line rule:truthy
push:
branches: [main, master]
paths:
- "**/package-lock.json"
- ".github/workflows/build-npm.yaml"
pull_request:
branches: [main, master]
paths:
- "**/package-lock.json"
- ".github/workflows/build-npm.yaml"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
buildNpm:
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- name: Checkout repository
uses: actions/checkout@v4
27 changes: 27 additions & 0 deletions src/gabo/internal/generator/data/build-yarn-incomplete.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: Build Node.js package(s)

on: # yamllint disable-line rule:truthy
push:
branches: [main, master]
paths:
- "**/yarn-lock.json"
- ".github/workflows/build-yarn.yaml"
pull_request:
branches: [main, master]
paths:
- "**/yarn-lock.json"
- ".github/workflows/build-yarn.yaml"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
buildYarn:
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- name: Checkout repository
uses: actions/checkout@v4
4 changes: 4 additions & 0 deletions src/gabo/internal/generator/yaml_embed_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var (
_buildAndroidTemplate string
//go:embed data/build-docker-incomplete.yaml
_buildDockerTemplate string
//go:embed data/build-npm-incomplete.yaml
_buildNpmTemplate string
//go:embed data/build-yarn-incomplete.yaml
_buildYarnTemplate string
//go:embed data/check-goreleaser-config.yaml
_checkGoReleaserConfigTemplate string
//go:embed data/compress-images.yaml
Expand Down

0 comments on commit 888df44

Please sign in to comment.