npm adduser via bash

For people working with a private registry (typically for CI purpose), reaching directly the Rest API may be a solution :

curl -XPUT -H "Content-type: application/json" -d '{ "name": "your_username", "password": "your_password" }' 'http://localhost:4873/-/user/org.couchdb.user:your_username'

This is what npm adduser is doing behind the scene.


This way works and with a more elegant expect:

/usr/bin/expect <<EOD
spawn npm adduser
expect {
  "Username:" {send "$USERNAME\r"; exp_continue}
  "Password:" {send "$PASSWORD\r"; exp_continue}
  "Email: (this IS public)" {send "$EMAIL\r"; exp_continue}
}
EOD

@Aurélien Thieriot: thanks for the hint.

I have two solutions for my problem:

Solution 1

export NPM_AUTH_TOKEN=myToken
export NPM_EMAIL=myEmail

create/override ~/.npmrc by following shell script:

echo "_auth = $NPM_AUTH_TOKEN" > ~/.npmrc
echo "email = $NPM_EMAIL" >> ~/.npmrc

Solution 2

export NPM_USERNAME=myUsername
export NPM_PASSWORD=myPassword
export NPM_EMAIL=myEmail

I know the order of the questions. So I can do the following:

npm adduser <<!
$NPM_USERNAME
$NPM_PASSWORD
$NPM_EMAIL
!

Note: solution 2 works only when the user isn't added yet
Otherwise the $NPM_PASSWORD is not necessary