skip installing dev dependencies for nodejs plugin setup (#7188)

This commit is contained in:
Evan Boyle 2021-06-02 11:09:03 -07:00 committed by GitHub
parent bd6410e2fb
commit 6ae6cc45b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 13 deletions

View file

@ -311,7 +311,7 @@ func installRequiredPolicy(finalDir string, tgz io.ReadCloser) error {
} }
func completeNodeJSInstall(finalDir string) error { func completeNodeJSInstall(finalDir string) error {
if bin, err := npm.Install(finalDir, nil, os.Stderr); err != nil { if bin, err := npm.Install(finalDir, false /*production*/, nil, os.Stderr); err != nil {
return errors.Wrapf( return errors.Wrapf(
err, err,
"failed to install dependencies of policy pack; you may need to re-run `%s install` "+ "failed to install dependencies of policy pack; you may need to re-run `%s install` "+

View file

@ -599,7 +599,7 @@ func nodeInstallDependencies() (string, error) {
fmt.Println("Installing dependencies...") fmt.Println("Installing dependencies...")
fmt.Println() fmt.Println()
bin, err := npm.Install("", os.Stdout, os.Stderr) bin, err := npm.Install("", false /*production*/, os.Stdout, os.Stderr)
if err != nil { if err != nil {
return bin, err return bin, err
} }

View file

@ -383,7 +383,7 @@ func (info PluginInfo) Install(tgz io.ReadCloser) error {
switch runtime { switch runtime {
case "nodejs": case "nodejs":
var b bytes.Buffer var b bytes.Buffer
if _, err := npm.Install(finalDir, &b, &b); err != nil { if _, err := npm.Install(finalDir, true /* production */, &b, &b); err != nil {
os.Stderr.Write(b.Bytes()) os.Stderr.Write(b.Bytes())
return errors.Wrap(err, "installing plugin dependencies") return errors.Wrap(err, "installing plugin dependencies")
} }

View file

@ -36,7 +36,7 @@ import (
// generate useful data. If the `PULUMI_PREFER_YARN` environment variable is set, `yarn pack` is run // generate useful data. If the `PULUMI_PREFER_YARN` environment variable is set, `yarn pack` is run
// instead of `npm pack`. // instead of `npm pack`.
func Pack(dir string, stderr io.Writer) ([]byte, error) { func Pack(dir string, stderr io.Writer) ([]byte, error) {
c, npm, bin, err := getCmd("pack") c, npm, bin, err := getCmd("pack", false /*production*/)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -83,8 +83,8 @@ func Pack(dir string, stderr io.Writer) ([]byte, error) {
// Install runs `npm install` in the given directory, installing the dependencies for the Node.js // Install runs `npm install` in the given directory, installing the dependencies for the Node.js
// app located there. If the `PULUMI_PREFER_YARN` environment variable is set, `yarn install` is used // app located there. If the `PULUMI_PREFER_YARN` environment variable is set, `yarn install` is used
// instead of `npm install`. // instead of `npm install`.
func Install(dir string, stdout, stderr io.Writer) (string, error) { func Install(dir string, production bool, stdout, stderr io.Writer) (string, error) {
c, npm, bin, err := getCmd("install") c, npm, bin, err := getCmd("install", production)
if err != nil { if err != nil {
return bin, err return bin, err
} }
@ -107,12 +107,16 @@ func Install(dir string, stdout, stderr io.Writer) (string, error) {
// getCmd returns the exec.Cmd used to install NPM dependencies. It will either use `npm` or `yarn` depending // 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. // 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`). // The boolean return parameter indicates if `npm` is chosen or not (instead of `yarn`).
func getCmd(command string) (*exec.Cmd, bool, string, error) { func getCmd(command string, production bool) (*exec.Cmd, bool, string, error) {
args := []string{command}
if production {
args = append(args, "--production")
}
if preferYarn() { if preferYarn() {
const file = "yarn" const file = "yarn"
yarnPath, err := exec.LookPath(file) yarnPath, err := exec.LookPath(file)
if err == nil { if err == nil {
return exec.Command(yarnPath, command), false, file, nil return exec.Command(yarnPath, args...), false, file, nil
} }
logging.Warningf("could not find yarn on the $PATH, trying npm instead: %v", err) logging.Warningf("could not find yarn on the $PATH, trying npm instead: %v", err)
} }
@ -125,7 +129,8 @@ func getCmd(command string) (*exec.Cmd, bool, string, error) {
} }
// We pass `--loglevel=error` to prevent `npm` from printing warnings about missing // We pass `--loglevel=error` to prevent `npm` from printing warnings about missing
// `description`, `repository`, and `license` fields in the package.json file. // `description`, `repository`, and `license` fields in the package.json file.
return exec.Command(npmPath, command, "--loglevel=error"), true, file, nil args = append(args, "--loglevel=error")
return exec.Command(npmPath, args...), true, file, nil
} }
// runCmd handles hooking up `stdout` and `stderr` and then runs the command. // runCmd handles hooking up `stdout` and `stderr` and then runs the command.

View file

@ -24,15 +24,17 @@ import (
) )
func TestNPMInstall(t *testing.T) { func TestNPMInstall(t *testing.T) {
testInstall(t, "npm") testInstall(t, "npm", false /*production*/)
testInstall(t, "npm", true /*production*/)
} }
func TestYarnInstall(t *testing.T) { func TestYarnInstall(t *testing.T) {
os.Setenv("PULUMI_PREFER_YARN", "true") os.Setenv("PULUMI_PREFER_YARN", "true")
testInstall(t, "yarn") testInstall(t, "yarn", false /*production*/)
testInstall(t, "yarn", true /*production*/)
} }
func testInstall(t *testing.T, expectedBin string) { func testInstall(t *testing.T, expectedBin string, production bool) {
// Skip during short test runs since this test involves downloading dependencies. // Skip during short test runs since this test involves downloading dependencies.
if testing.Short() { if testing.Short() {
t.Skip("Skipped in short test run") t.Skip("Skipped in short test run")
@ -59,7 +61,7 @@ func testInstall(t *testing.T, expectedBin string) {
// Install dependencies, passing nil for stdout and stderr, which connects // Install dependencies, passing nil for stdout and stderr, which connects
// them to the file descriptor for the null device (os.DevNull). // them to the file descriptor for the null device (os.DevNull).
bin, err := Install(pkgdir, nil, nil) bin, err := Install(pkgdir, production, nil, nil)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedBin, bin) assert.Equal(t, expectedBin, bin)
} }