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

Merged
merged 12 commits into from
Jan 16, 2025
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(builder, opts.Client, opts.Log, f.Src, strings.TrimPrefix(f.Version, "v"))
}

func (f *NPMPackages) Publisher(ctx context.Context, opts *pipeline.ArtifactContainerOpts) (*dagger.Container, error) {
Expand Down
26 changes: 19 additions & 7 deletions frontend/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@ package frontend
import (
"context"
"fmt"
"log/slog"

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

// 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 {
// 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(builder *dagger.Container, d *dagger.Client, 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.
var (
out = fmt.Sprintf("/src/npm-packages/%%s-%v.tgz", "v"+ersion)

lernaBuild = fmt.Sprintf("yarn lerna version %s --exact --no-git-tag-version --no-push --force-publish -y", ersion)
lernaPack = fmt.Sprintf("yarn lerna exec --no-private -- yarn pack --out %s", out)

nxBuild = fmt.Sprintf("yarn nx release version %s --no-git-commit --no-git-tag --no-stage-changes --group grafanaPackages", ersion)
nxPack = fmt.Sprintf("yarn nx exec --projects=$(cat nx.json | jq -r '.relase.groups.grafanaPackages.projects | join(\",\")') -- yarn pack --out %s", out)
)

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")
WithExec([]string{"yarn", "packages:build"}).
WithExec([]string{"/bin/sh", "-c", fmt.Sprintf("if [ -f .nx ]; then %s; else %s; fi", nxBuild, lernaBuild)}).
kminehart marked this conversation as resolved.
Show resolved Hide resolved
WithExec([]string{"/bin/sh", "-c", fmt.Sprintf("if [ -f .nx ]; then %s; else %s; fi", nxPack, lernaPack)}).
Directory("./npm-packages"), nil
}

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