如何运行AWS CodeArtifact登录并保持默认注册表

6

我在 package.json 文件中的 scripts > preinstall 或者 scripts > prepare 中运行了这个命令:

aws codeartifact login --tool npm --repository my-repo --domain my-domain --domain-owner <123456789> --profile <me>

完整文件:<123456789><me> 已修改为stackoverflow)

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "preinstall": "npm run co:login",
    "co:login": "aws codeartifact login --tool npm --repository my-repo --domain my-domain --domain-owner <123456789> --profile <me>",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "uuid": "^3.3.2",
    "@my-npm/my-common": "1.0.0"
  }
}

在运行aws codeartifact login ..命令之前,我的.npmrc文件如下:

registry=https://registry.npmjs.org
@my-npm:registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/

我的 .npmrc 文件已修改:

registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/
@my-npm:registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:always-auth=true
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:_authToken=eyJ2ZXIiOjEsIml....

但我需要保留这个:

registry=https://registry.npmjs.org
@my-npm:registry=https://my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/-repo/
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:always-auth=true
//my-domain-<123456789>.d.codeartifact.eu-west-1.amazonaws.com/npm/my-repo/:_authToken=eyJ2ZXIiOjEsIml....
2个回答

14

在命令行末尾添加--namespace @my-npm参数。

这个参数只会改变.npmrc文件中@my-npm:registry=...的作用域。


这并没有提供问题的答案。如果要批评或请求作者澄清,请在他们的帖子下留言。-【来自审查】 - Marek Urbanowicz
我希望在答案中看到那个解释。我们需要提供完整的答案和解释,这样人们才能理解,而不是简单地复制粘贴。 - Marek Urbanowicz
@MarekUrbanowicz,是的,这回答了问题。如果您在命令行末尾添加--namespace @my-npm,则默认注册表不受影响。 - Stéphane GRILLON

2
我一直在使用的解决方案是,不使用 aws codeartifact login --tool npm --repository my-repo --domain my-domain 来登录 AWS,而是使用更细粒度的方法,使用以下命令:
# get endpoint 
endpoint = aws codeartifact get-repository-endpoint --domain my_domain --domain-owner 111122223333 --repository my_repo --format npm

# set a scoped registry
npm config set registry endpoint --scope=@my-package

# get token
token = aws codeartifact get-authorization-token --domain my_domain --domain-owner 111122223333 --repository my_repo

# set token
npm config set //my_domain-111122223333.d.codeartifact.region.amazonaws.com/npm/my_repo/:_authToken=token

# always truth
npm config set //my_domain-111122223333.d.codeartifact.region.amazonaws.com/npm/my_repo/:always-auth=true

这些命令是对 aws codeartifact login --tool npm --repository my-repo --domain my-domain更多信息)进行了分解,不同之处在于,不是在您的 .npmrc 文件中设置通用的 registry(用于设置您的 npm 配置),而是设置了一个 作用域 registry更多信息)。 这样,您就可以从您想要的来源获取软件包。 将此应用于 package.json,您可以将这些命令插入到 co:login 的值中,甚至将这些命令隔离到一个脚本中,并将这些脚本作为值调用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接