pulumi/sdk/nodejs/npm/npm.go

90 lines
2.4 KiB
Go
Raw Permalink Normal View History

package npm
import (
"bytes"
"context"
"errors"
2023-05-24 17:18:22 +00:00
"fmt"
"io"
"os"
"os/exec"
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
"path/filepath"
"strings"
)
// NPM is the canonical "Node Package Manager".
2023-05-24 17:18:22 +00:00
type npmManager struct {
executable string
}
// Assert that NPM is an instance of PackageManager.
2023-05-24 17:18:22 +00:00
var _ PackageManager = &npmManager{}
2023-05-24 17:18:22 +00:00
func newNPM() (*npmManager, error) {
npmPath, err := exec.LookPath("npm")
if err != nil {
if errors.Is(err, exec.ErrNotFound) {
return nil, errors.New("Could not find `npm` executable.\n" +
"Install npm and make sure it is in your PATH.")
}
return nil, err
}
return &npmManager{
executable: npmPath,
}, nil
}
2023-05-24 17:18:22 +00:00
func (node *npmManager) Name() string {
return "npm"
}
2023-05-24 17:18:22 +00:00
func (node *npmManager) Install(ctx context.Context, dir string, production bool, stdout, stderr io.Writer) error {
command := node.installCmd(ctx, production)
command.Dir = dir
command.Stdout = stdout
command.Stderr = stderr
return command.Run()
}
2023-05-24 17:18:22 +00:00
func (node *npmManager) installCmd(ctx context.Context, production bool) *exec.Cmd {
// We pass `--loglevel=error` to prevent `npm` from printing warnings about missing
// `description`, `repository`, and `license` fields in the package.json file.
args := []string{"install", "--loglevel=error"}
if production {
args = append(args, "--production")
}
//nolint:gosec // False positive on tained command execution. We aren't accepting input from the user here.
return exec.CommandContext(ctx, node.executable, args...)
}
2023-05-24 17:18:22 +00:00
func (node *npmManager) Pack(ctx context.Context, dir string, stderr io.Writer) ([]byte, error) {
//nolint:gosec // False positive on tained command execution. We aren't accepting input from the user here.
command := exec.CommandContext(ctx, node.executable, "pack", "--loglevel=error")
command.Dir = dir
// We have to read the name of the file from stdout.
var stdout bytes.Buffer
command.Stdout = &stdout
command.Stderr = stderr
err := command.Run()
if err != nil {
return nil, err
}
// Next, we try to read the name of the file from stdout.
// packfile is the name of the file containing the tarball,
// as produced by `npm pack`.
Use pnpm as package manager if we find a pnpm-lock.yaml file (#15456) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Initial support pnpm. Note that this does not support pnpm workspaces yet. This also does not handle passing the package manager through from `pulumi new`. Once a user manually runs pnpm, creating a pnpm-lock.yaml, we'll detect that and pnpm. Fixes https://github.com/pulumi/pulumi/issues/15455 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-02-21 13:41:21 +00:00
packFilename := strings.TrimSpace(stdout.String())
packfile := filepath.Join(dir, packFilename)
defer os.Remove(packfile)
packTarball, err := os.ReadFile(packfile)
if err != nil {
newErr := fmt.Errorf("'npm pack' completed successfully but the package .tgz file was not generated: %w", err)
return nil, newErr
}
return packTarball, nil
}