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

How to run AWS codeartifact login and keep default registry

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

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

完整文件:<123456789><me> 针对 Whosebug 进行了修改)

{
  "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"
  }
}

我的 .npmrc 文件在 aws codeartifact login .. 命令之前:

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....

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

添加的此参数仅更改 .npmrc 文件中的范围 @my-npm:registry=...

我一直在使用的解决方案不是使用 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 (more info), with the difference that instead of setting a general registry at your .npmrc file (used to set configurations for your npm) will set a scoped registry (more info) 的解构。 通过这种方式,您将能够从您想要的来源获取您的包。 将此应用于 package.json,您可以将这些命令插入 co:login 的值,甚至可以将命令隔离在脚本中并将这些脚本作为值调用。