如何以非交互方式登录纱线?

时间:2017-04-07 05:51:35

标签: node.js npm terminal continuous-integration yarnpkg

使用npm时,我可以通过以下方式非交互式登录:

$ printf "jesstelford\n<password>\nexample@email.com\n" | npm login

但是,yarn的类似命令会挂起:

$ printf "jesstelford\nexample@email.com\n" | yarn login
yarn login v0.21.3
question npm username: jesstelford
question npm email:

在交互模式下,我可以成功运行:

$ yarn login
yarn login v0.21.3
question npm username: jesstelford
question npm email: example@email.com
✨  Done in 22.53s.

如何以非交互方式运行yarn login

2 个答案:

答案 0 :(得分:4)

输入用户名后,

yarn似乎暂停。在非交互模式下,您还需要暂停:

$ { echo "jesstelford"; sleep 1; echo "example@email.com"; } | yarn login

这将为您提供以下输出:

yarn login v0.21.3
question npm username: jesstelford
question npm email: example@email.com
✨  Done in 0.84s.

如何运作

echo "jesstelford"输入字符串,然后输入换行符

在继续输入电子邮件之前,

sleep 1会在输入用户名后插入1秒暂停:

echo "example@email.com"输入第二个字符串,后跟一个换行符来结束命令。

答案 1 :(得分:1)

我弄清楚了如何在Dockerfile中使用yarn来使用nexus凭证。 如果可以通过链接完成此操作,那么我相信它可以在有问题的其他所有用例中使用。

以下是代码段:

# Install necessary package to login automatically
RUN npm install -g npm-cli-adduser

# Whenever auth changes, this has to be incremented,
# so following calls are not cached by Docker (or always build with --no-cache).
ARG FORCE_NO_CACHE_FLAG=2

# Authenticate to Nexus via npm package that does the job
RUN npm-cli-adduser -u ${NPM_USER} -p ${NPM_PASS} -e ${NPM_EMAIL} -r ${NPM_REGISTRY} -s ${NPM_SCOPE} -a

# Now, the fun / missing part I struggled with
# To fix yarn being silly aby not using token from npm-cli-adduser
# You need to set it explicitly.
# I found this by comparing my local `yarn config list` with a docker one.
# On local yarn was able to talk to nexus, not on docker. This piece one missing somehow.
RUN npm config set ${NPM_SCOPE}:registry ${NPM_REGISTRY}

# Now this will work, yarn will auth properly with your register.
RUN yarn install

这整个事情让我太过困惑了。如果有人像我(或未来的我)遇到这个问题,希望这篇文章对您有所帮助。