gradle-node-plugin icon indicating copy to clipboard operation
gradle-node-plugin copied to clipboard

npm or yarn login to registry

Open Ramblurr opened this issue 8 years ago • 2 comments

Using the yarn/npm login commands, one can fetch the auth token needed to authenticate to a private npm registry.

The problem is that in both yarn and npm these commands require some sort of TTY.

I've tried a few ways, but I can't get it to work.

Any idea how to login to a private npm registry with this plugin?

Attempt 1

task npmLogin(type: NpmTask) {
    args = ["login",  "--registry=https://private.registry/npm"]
    def stdOut = new ByteArrayOutputStream()
    execOverrides {
      it.ignoreExitValue = true

      it.workingDir = frontendDir
      it.standardInput = new ByteArrayInputStream('user\npass\[email protected]\n'.getBytes("UTF-8"))
      it.standardOutput = System.out
    }
}

fails with:

Username: Password: npm ERR! cb() never called!

Attempt 2

add to scripts in package.json:

  "login": "echo \"$NPM_USER\n$NPM_PASS\n$NPM_EMAIL\" | npm login --registry=$NPM_CONFIG_REGISTRY"

gradle

task npmLogin(type: NpmTask) {
  args = ['run', 'login']
}

run with:

env NPM_USER=user NPM_PASS=pass [email protected] NPM_CONFIG_REGISTRY=https://private.registry/npm gradle npmLogin

fails with:

Username: Password: npm ERR! cb() never called!

Ramblurr avatar May 18 '17 15:05 Ramblurr

any plans or updates on the issue?

stoetti avatar Dec 22 '17 10:12 stoetti

yeah this one is definitely tricky - idk if gradle provides something like expect, but i've basically made a little set of scripts to do this:

npmLogin

#!/bin/bash

registry=$1
username=$2
password=$3
email=$4

npm config set registry ${registry}
npm config set always-auth true
/usr/bin/expect <<EOD
spawn npm adduser --registry ${registry} --auth-type=legacy --always-auth
expect {
  "Username:" {send "${username}\r"; exp_continue}
  "Password:" {send "${password}\r"; exp_continue}
  "Email: (this IS public)" {send "${email}\r"; exp_continue}
}
EOD

npmLogout

#!/bin/bash
registry=$1

npm logout --registry ${registry}
npm config delete always-auth
npm config delete registry

flyinprogrammer avatar May 02 '19 16:05 flyinprogrammer