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

NPM: Version packages using Lerna or Nx #294

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion artifacts/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (f *NPMPackages) BuildFile(ctx context.Context, builder *dagger.Container,
}

func (f *NPMPackages) BuildDir(ctx context.Context, builder *dagger.Container, opts *pipeline.ArtifactContainerOpts) (*dagger.Directory, error) {
return frontend.NPMPackages(builder, f.Src, strings.TrimPrefix(f.Version, "v")), nil
return frontend.NPMPackages(ctx, builder, opts.Log, f.Src, strings.TrimPrefix(f.Version, "v"))
}

func (f *NPMPackages) Publisher(ctx context.Context, opts *pipeline.ArtifactContainerOpts) (*dagger.Container, error) {
Expand Down
16 changes: 16 additions & 0 deletions daggerutil/fileexists.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package daggerutil

import (
"context"

"dagger.io/dagger"
)

func FileExists(ctx context.Context, dir *dagger.Directory, path string) bool {
_, err := dir.File(path).Contents(ctx)
if err == nil {
return true
jackw marked this conversation as resolved.
Show resolved Hide resolved
}

return false
}
35 changes: 27 additions & 8 deletions frontend/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,38 @@ package frontend
import (
"context"
"fmt"
"log/slog"

"dagger.io/dagger"
"github.com/grafana/grafana-build/containers"
"github.com/grafana/grafana-build/daggerutil"
)

// NPMPackages returns a dagger.Directory which contains the Grafana NPM packages from the grafana source code.
func NPMPackages(builder *dagger.Container, src *dagger.Directory, ersion string) *dagger.Directory {
return builder.WithExec([]string{"mkdir", "npm-packages"}).
WithExec([]string{"yarn", "run", "packages:build"}).
// TODO: We should probably start reusing the yarn pnp map if we can figure that out instead of rerunning yarn install everywhere.
WithExec([]string{"yarn", "run", "lerna", "version", ersion, "--exact", "--no-git-tag-version", "--no-push", "--force-publish", "-y"}).
WithExec([]string{"yarn", "lerna", "exec", "--no-private", "--", "yarn", "pack", "--out", fmt.Sprintf("/src/npm-packages/%%s-%v.tgz", "v"+ersion)}).
Directory("./npm-packages")
// NPMPackages versions and packs the npm packages into tarballs into `npm-packages` directory.
// It then returns the npm-packages directory as a dagger.Directory.
func NPMPackages(ctx context.Context, builder *dagger.Container, log *slog.Logger, src *dagger.Directory, ersion string) (*dagger.Directory, error) {
// Check if the version of Grafana uses lerna or nx to manage package versioning.
hasLerna := daggerutil.FileExists(ctx, src, "./lerna.json")
hasNx := daggerutil.FileExists(ctx, src, "./nx.json")

if hasLerna {
return builder.WithExec([]string{"mkdir", "npm-packages"}).
WithExec([]string{"yarn", "packages:build"}).
WithExec([]string{"yarn", "lerna", "version", ersion, "--exact", "--no-git-tag-version", "--no-push", "--force-publish", "-y"}).
WithExec([]string{"yarn", "lerna", "exec", "--no-private", "--", "yarn", "pack", "--out", fmt.Sprintf("/src/npm-packages/%%s-%v.tgz", "v"+ersion)}).
Directory("./npm-packages"), nil
}

if hasNx {
return builder.WithExec([]string{"mkdir", "npm-packages"}).
WithExec([]string{"yarn", "packages:build"}).
WithExec([]string{"yarn", "nx", "release", "version", ersion, "--no-git-commit", "--no-git-tag", "--no-stage-changes", "--group", "fixed"}).
WithExec([]string{"yarn", "workspaces", "foreach", "--no-private", "--include='@grafana/*'", "-A", "exec", "yarn", "pack", "--out", fmt.Sprintf("/src/npm-packages/%%s-%v.tgz", "v"+ersion)}).
Directory("./npm-packages"), nil
}

log.Error("No lerna.json or nx.json found in source directory")
return nil, fmt.Errorf("no lerna.json or nx.json found in source directory")
}

// PublishNPM publishes a npm package to the given destination.
Expand Down