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

Jes*_*ord 5 terminal continuous-integration node.js npm yarnpkg

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

$ printf "jesstelford\n<password>\nexample@email.com\n" | npm login
Run Code Online (Sandbox Code Playgroud)

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

$ printf "jesstelford\nexample@email.com\n" | yarn login
yarn login v0.21.3
question npm username: jesstelford
question npm email:
Run Code Online (Sandbox Code Playgroud)

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

$ yarn login
yarn login v0.21.3
question npm username: jesstelford
question npm email: example@email.com
?  Done in 22.53s.
Run Code Online (Sandbox Code Playgroud)

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

Jes*_*ord 5

yarn输入用户名后似乎暂停。在非交互模式下,您还需要暂停:

$ { echo "jesstelford"; sleep 1; echo "example@email.com"; } | yarn login
Run Code Online (Sandbox Code Playgroud)

这将为您提供以下输出:

yarn login v0.21.3
question npm username: jesstelford
question npm email: example@email.com
?  Done in 0.84s.
Run Code Online (Sandbox Code Playgroud)

这个怎么运作

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

sleep 1 将在输入用户名后插入 1 秒暂停,然后继续输入电子邮件:

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


szn*_*cki 5

我想出了如何让yarn在Dockerfile中使用nexus凭证。如果这可以通过 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
Run Code Online (Sandbox Code Playgroud)

这整件事让我太受不了了。如果有人像我(或未来的我)一样遇到这个问题,我希望这篇文章能有所帮助。