github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/yarn/builder.go (about)

     1  package yarn
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  )
     9  
    10  // RunBeforeBuild attempts to run yarn install if it finds yarn.lock
    11  func (p *Plugin) RunBeforeBuild(ctx context.Context, root string, args []string) error {
    12  	cmd := p.buildCmd(ctx)
    13  	if cmd == nil {
    14  		return nil
    15  	}
    16  
    17  	return cmd.Run()
    18  }
    19  
    20  // build command will return the command if yarn.lock is found
    21  // otherwise returns nil
    22  func (p *Plugin) buildCmd(ctx context.Context) *exec.Cmd {
    23  	_, err := os.Stat("yarn.lock")
    24  	if os.IsNotExist(err) {
    25  		return nil
    26  	}
    27  
    28  	fmt.Println(">> Running yarn install <<<")
    29  
    30  	c := exec.CommandContext(ctx, "yarn", "install", "--no-progress")
    31  	c.Stdin = os.Stdin
    32  	c.Stderr = os.Stderr
    33  	c.Stdout = os.Stdout
    34  
    35  	return c
    36  }