gradle-node-plugin
gradle-node-plugin copied to clipboard
npm or yarn login to registry
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!
any plans or updates on the issue?
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