很难相信,经过这么长时间,npm 登录仍然没有解决方案。当然,您可以一次获取令牌并将其用于所有 CI 需求,但是永不过期令牌的安全隐患呢?如果有一天管理员决定令牌应该过期怎么办?
下面是我使用包的 hacky javascript 解决npm-registry-client方案。只需传递一个 json 字符串参数,它就会登录并将.npmrc文件写入您当前的目录。npm logout像往常一样注销使用。
var client = new (require('npm-registry-client'))({});
var std_in = JSON.parse(process.argv[2]);
if (std_in.uri === undefined) {
console.error('Must input registry uri!');
return;
}
// fix annoying trailing '/' thing in registry uri
if (std_in.uri[std_in.uri.length - 1] !== '/') {
std_in.uri = std_in.uri + '/';
}
if (std_in.scope === undefined) {
console.error('Must input scope!');
return;
//std_in.scope = '@my-scope'; // or add default scope of your own
}
if (std_in.scope[0] !== '@') {
std_in.scope = '@' + std_in.scope;
}
client.adduser(std_in.uri, std_in.params, function(err, data, raw, res) {
if (err) {
console.error(err);
return;
}
require('fs').writeFileSync('.npmrc', `${std_in.scope}:registry=${std_in.uri}\n//${(std_in.uri.split('//'))[1]}:_authToken=${data.token}`);
});
示例输入:
{
"uri": "https://my-nmp.reg",
"scope": "@my-scope",
"params": {
"auth": {
"username": "secret-agent",
"password": "12345",
"email": "secret-agent@007.com"
}
}
}