2019-08-01 20:05:57 +02:00
|
|
|
package npm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-01-30 01:02:44 +01:00
|
|
|
"path/filepath"
|
2019-08-01 20:05:57 +02:00
|
|
|
"strings"
|
|
|
|
|
2019-11-21 21:59:48 +01:00
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
|
2019-08-01 20:05:57 +02:00
|
|
|
"github.com/pkg/errors"
|
2019-11-21 21:59:48 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/util/cmdutil"
|
2020-03-18 23:34:58 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/util/logging"
|
2019-08-01 20:05:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Pack runs `npm pack` in the given directory, packaging the Node.js app located there into a
|
|
|
|
// tarball and returning it as `[]byte`. `stdout` is ignored for the command, as it does not
|
2019-11-21 21:59:48 +01:00
|
|
|
// generate useful data. If the `PULUMI_PREFER_YARN` environment variable is set, `yarn pack` is run
|
|
|
|
// instead of `npm pack`.
|
2019-08-01 20:05:57 +02:00
|
|
|
func Pack(dir string, stderr io.Writer) ([]byte, error) {
|
2019-11-21 21:59:48 +01:00
|
|
|
c, npm, bin, err := getCmd("pack")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-01 20:05:57 +02:00
|
|
|
c.Dir = dir
|
|
|
|
|
2019-11-21 21:59:48 +01:00
|
|
|
// Note that `npm pack` doesn't have the ability to specify the resulting filename, since
|
|
|
|
// it's meant to be uploaded directly to npm, which means we have to get that information
|
|
|
|
// by parsing the output of the command. However, if we're using `yarn`, we can specify a
|
|
|
|
// filename.
|
|
|
|
var packfile string
|
|
|
|
if !npm {
|
|
|
|
packfile = fmt.Sprintf("%s.tgz", uuid.NewV4().String())
|
|
|
|
c.Args = append(c.Args, "--filename", packfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the command.
|
|
|
|
// `stdout` is ignored for the command, as it does not generate useful data.
|
2019-08-01 20:05:57 +02:00
|
|
|
var stdout bytes.Buffer
|
2019-11-21 21:59:48 +01:00
|
|
|
if err = runCmd(c, npm, &stdout, stderr); err != nil {
|
2019-08-01 20:05:57 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-21 21:59:48 +01:00
|
|
|
|
|
|
|
// If `npm` was used, parse the filename from the output.
|
|
|
|
if npm {
|
|
|
|
packfile = strings.TrimSpace(stdout.String())
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:05:57 +02:00
|
|
|
defer os.Remove(packfile)
|
|
|
|
|
|
|
|
packTarball, err := ioutil.ReadFile(packfile)
|
|
|
|
if err != nil {
|
2019-11-21 21:59:48 +01:00
|
|
|
return nil, fmt.Errorf("%s pack completed successfully but the packed .tgz file was not generated", bin)
|
2019-08-01 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return packTarball, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install runs `npm install` in the given directory, installing the dependencies for the Node.js
|
2019-11-21 21:59:48 +01:00
|
|
|
// app located there. If the `PULUMI_PREFER_YARN` environment variable is set, `yarn install` is used
|
|
|
|
// instead of `npm install`.
|
|
|
|
func Install(dir string, stdout, stderr io.Writer) (string, error) {
|
|
|
|
c, npm, bin, err := getCmd("install")
|
|
|
|
if err != nil {
|
|
|
|
return bin, err
|
|
|
|
}
|
2019-08-01 20:05:57 +02:00
|
|
|
c.Dir = dir
|
|
|
|
|
|
|
|
// Run the command.
|
2019-11-21 21:59:48 +01:00
|
|
|
if err = runCmd(c, npm, stdout, stderr); err != nil {
|
|
|
|
return bin, err
|
2019-08-01 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the "node_modules" directory exists.
|
2020-01-30 01:02:44 +01:00
|
|
|
nodeModulesPath := filepath.Join(dir, "node_modules")
|
|
|
|
if _, err := os.Stat(nodeModulesPath); os.IsNotExist(err) {
|
2019-11-21 21:59:48 +01:00
|
|
|
return bin, errors.Errorf("%s install reported success, but node_modules directory is missing", bin)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCmd returns the exec.Cmd used to install NPM dependencies. It will either use `npm` or `yarn` depending
|
|
|
|
// on what is available on the current path, and if `PULUMI_PREFER_YARN` is truthy.
|
|
|
|
// The boolean return parameter indicates if `npm` is chosen or not (instead of `yarn`).
|
|
|
|
func getCmd(command string) (*exec.Cmd, bool, string, error) {
|
|
|
|
if preferYarn() {
|
|
|
|
const file = "yarn"
|
|
|
|
yarnPath, err := exec.LookPath(file)
|
|
|
|
if err == nil {
|
|
|
|
return exec.Command(yarnPath, command), false, file, nil
|
|
|
|
}
|
|
|
|
logging.Warningf("could not find yarn on the $PATH, trying npm instead: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const file = "npm"
|
|
|
|
npmPath, err := exec.LookPath(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, file, errors.Wrapf(err, "could not find npm on the $PATH; npm is installed with Node.js "+
|
|
|
|
"available at https://nodejs.org/")
|
|
|
|
}
|
|
|
|
// We pass `--loglevel=error` to prevent `npm` from printing warnings about missing
|
|
|
|
// `description`, `repository`, and `license` fields in the package.json file.
|
|
|
|
return exec.Command(npmPath, command, "--loglevel=error"), true, file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runCmd handles hooking up `stdout` and `stderr` and then runs the command.
|
|
|
|
func runCmd(c *exec.Cmd, npm bool, stdout, stderr io.Writer) error {
|
|
|
|
// Setup `stdout` and `stderr`.
|
|
|
|
// `stderr` is ignored when `yarn` is used because it outputs warnings like "package.json: No license field"
|
|
|
|
// to `stderr` that we don't need to show.
|
|
|
|
c.Stdout = stdout
|
|
|
|
var stderrBuffer bytes.Buffer
|
|
|
|
if npm {
|
|
|
|
c.Stderr = stderr
|
|
|
|
} else {
|
|
|
|
c.Stderr = &stderrBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the command.
|
|
|
|
if err := c.Run(); err != nil {
|
|
|
|
// If we failed, and we're using `yarn`, write out any bytes that were written to `stderr`.
|
|
|
|
if !npm {
|
|
|
|
stderr.Write(stderrBuffer.Bytes())
|
|
|
|
}
|
|
|
|
return err
|
2019-08-01 20:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-21 21:59:48 +01:00
|
|
|
|
|
|
|
// preferYarn returns true if the `PULUMI_PREFER_YARN` environment variable is set.
|
|
|
|
func preferYarn() bool {
|
|
|
|
return cmdutil.IsTruthy(os.Getenv("PULUMI_PREFER_YARN"))
|
|
|
|
}
|