|
| 1 | +package nodejs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + |
| 10 | + "fastcat.org/go/gdev/addons/build" |
| 11 | + "fastcat.org/go/gdev/internal" |
| 12 | + "fastcat.org/go/gdev/shx" |
| 13 | +) |
| 14 | + |
| 15 | +func detectRush(root string) (build.Builder, error) { |
| 16 | + rjPath := filepath.Join(root, "rush.json") |
| 17 | + if _, err := os.Stat(rjPath); err != nil { |
| 18 | + return nil, nil // no rush.json, not a Rush project |
| 19 | + } |
| 20 | + rj, err := internal.ReadJSONFile[RushJSON](rjPath) |
| 21 | + if err != nil { |
| 22 | + return nil, fmt.Errorf("failed to read %s: %w", rjPath, err) |
| 23 | + } |
| 24 | + return &rushBuilder{ |
| 25 | + root: root, |
| 26 | + rj: rj, |
| 27 | + }, nil |
| 28 | +} |
| 29 | + |
| 30 | +// Root implements build.Builder. |
| 31 | +func (b *rushBuilder) Root() string { |
| 32 | + return b.root |
| 33 | +} |
| 34 | + |
| 35 | +type rushBuilder struct { |
| 36 | + root string |
| 37 | + rj RushJSON |
| 38 | + |
| 39 | + // populated on demand |
| 40 | + dirToPkg map[string]string |
| 41 | +} |
| 42 | + |
| 43 | +func (b *rushBuilder) withExtra() { |
| 44 | + if b.dirToPkg == nil { |
| 45 | + b.dirToPkg = make(map[string]string, len(b.rj.Projects)) |
| 46 | + for _, p := range b.rj.Projects { |
| 47 | + b.dirToPkg[p.ProjectFolder] = p.PackageName |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (b *rushBuilder) build( |
| 53 | + ctx context.Context, |
| 54 | + args []string, |
| 55 | + opts build.Options, |
| 56 | +) error { |
| 57 | + shOpts := []shx.Option{shx.WithCwd(b.root)} |
| 58 | + shOpts = append(shOpts, opts.ShellOpts()...) |
| 59 | + // always tell rush to emit verbose output so we can emit it on errors, rush's |
| 60 | + // tendency to only emit the tail of a build error often doesn't include |
| 61 | + // enough context |
| 62 | + cna := []string{"rush", "build", "--verbose"} |
| 63 | + cna = append(cna, args...) |
| 64 | + res, err := shx.Run(ctx, cna, shOpts...) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("failed to start rush build: %w", err) |
| 67 | + } |
| 68 | + defer res.Close() //nolint:errcheck |
| 69 | + if err = res.Err(); err != nil { |
| 70 | + if !opts.Verbose { |
| 71 | + _, _ = io.Copy(os.Stderr, res.Stdout()) |
| 72 | + } |
| 73 | + return fmt.Errorf("rush build failed: %w", err) |
| 74 | + } |
| 75 | + if err := res.Close(); err != nil { |
| 76 | + return fmt.Errorf("error cleaning up after rush build: %w", err) |
| 77 | + } |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// BuildAll implements build.Builder. |
| 82 | +func (b *rushBuilder) BuildAll(ctx context.Context, opts build.Options) error { |
| 83 | + return b.build(ctx, nil, opts) |
| 84 | +} |
| 85 | + |
| 86 | +// BuildDirs implements build.Builder. |
| 87 | +func (b *rushBuilder) BuildDirs(ctx context.Context, dirs []string, opts build.Options) error { |
| 88 | + args := make([]string, 0, len(dirs)*2) |
| 89 | + b.withExtra() |
| 90 | + for _, dir := range dirs { |
| 91 | + if pkg, ok := b.dirToPkg[dir]; ok { |
| 92 | + args = append(args, "--to", pkg) |
| 93 | + } else { |
| 94 | + return fmt.Errorf("unknown directory %s to build for %s", dir, b.root) |
| 95 | + } |
| 96 | + } |
| 97 | + return b.build(ctx, args, opts) |
| 98 | +} |
| 99 | + |
| 100 | +// ValidSubdirs implements build.Builder. |
| 101 | +func (b *rushBuilder) ValidSubdirs(context.Context) ([]string, error) { |
| 102 | + ret := make([]string, 0, len(b.rj.Projects)) |
| 103 | + for _, p := range b.rj.Projects { |
| 104 | + ret = append(ret, p.ProjectFolder) |
| 105 | + } |
| 106 | + return ret, nil |
| 107 | +} |
0 commit comments