Skip to content

Commit

Permalink
feat: 新增NPM账号管理的_auth字段支持
Browse files Browse the repository at this point in the history
  • Loading branch information
beezen committed Apr 10, 2024
1 parent 83eb612 commit 95a02f6
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ $ nucm use beezen

在后续的使用过程中,您可以通过 `nucm use <name>` 的方式快速切换各种账号的访问令牌,从而实现对 npm 包使用不同账号进行发布。

如果想进一步了解 NPM 登录相关的配置,可以参考[.npmrc 配置说明](https://docs.npmjs.com/cli/v9/configuring-npm/npmrc#auth-related-configuration)

## 许可证

MIT
Expand Down
2 changes: 2 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ Note: If you have logged in through npm login or npm adduser, you can preserve t

During subsequent use, you can swiftly switch between accounts using the nucm use <name> command, enabling quick switching of the associated Access Token. This allows seamless release of NPM packages with different account credentials.

If you want to learn more about the configuration of NPM login, you can refer to the [.npmrc configuration instructions](https://docs.npmjs.com/cli/v9/configuring-npm/npmrc#auth-related-configuration).

## LICENSE

MIT
Expand Down
3 changes: 2 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = {
"/use",
"/localize",
"/update",
"/registry"
"/registry",
"/more"
],
nav: [
{
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ title: 介绍

NUCM 的全称为 NPM User Change Manager,是一款高效而直观的 NPM 账号切换管理工具,为开发者提供了简便的方法来轻松切换和管理不同的 NPM 用户账号。无论是在开发多个项目,协作开发,还是在不同的工作环境中切换,该工具都能帮助用户保持无缝的 NPM 包管理体验。

![An image](./static/nucm.gif)

**主要特性包括:**

1. 快速账号切换: 使用"NPM User Change Manager",用户可以通过简单的命令或图形界面迅速切换到他们的不同 NPM 账号,无需反复输入认证信息。
Expand Down
29 changes: 29 additions & 0 deletions docs/more.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: 了解更多...
---

# 了解更多

如果你恰好看到了这篇文章,你一定是希望对这个项目贡献自己的一份力量。

欢迎任何形式的贡献,不管是一个错别字的修改,还是一次友好的建议,不管是通过提交 [Issue](https://github.com/beezen/nucm/issues), 还是一个帅气 [pull request](https://github.com/beezen/nucm/pulls)

## NPM auth related configuration

The settings `_auth`, `_authToken`, `username` and `_password` must all be scoped to a specific registry. This ensures that `npm` will never send credentials to the wrong host.

The full list is:

- `_auth` (base64 authentication string)
- `_authToken` (authentication token)
- `username`
- `_password`
- `email`
- `certfile` (path to certificate file)
- `keyfile` (path to key file)

## 参考链接

- [npmrc](https://docs.npmjs.com/cli/v9/configuring-npm/npmrc)
- [about-access-tokens](https://docs.npmjs.com/about-access-tokens)
- [nrm](https://www.npmjs.com/package/nrm)
Binary file added docs/static/nucm.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 11 additions & 2 deletions src/actions/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ interface LOptions {
all?: boolean;
}

// 账号类型配置 _authToken|_auth|_password
interface AuthOptions {
type?: "auth";
}

/**
* 获取用户列表
*/
Expand Down Expand Up @@ -55,7 +60,7 @@ export function getUserList(options: LOptions) {
}

/** 变更用户 */
export function changeUser(name: string) {
export function changeUser(name: string, options?: AuthOptions) {
const { fileConfig, registryConfig } = baseInitConfig;
let npmrcConfig = fileConfig.npm;
let nucmrcConfig = fileConfig.nucm;
Expand All @@ -64,7 +69,11 @@ export function changeUser(name: string) {
printLog("account.notFound", { type: "error" });
return;
}
npmrcConfig[`${registryConfig.registry.replace(/^https?:/, "")}:_authToken`] =
let authStr = "_authToken";
if (["auth", "authToken"].includes(options?.type)) {
authStr = `_${options.type}`;
}
npmrcConfig[`${registryConfig.registry.replace(/^https?:/, "")}:${authStr}`] =
accountList[name]["access-tokens"];
Object.keys(accountList).forEach((key) => {
if (accountList[key]["is-current"]) {
Expand Down
4 changes: 3 additions & 1 deletion src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function getRegistryConfig(config: BaseConfig): RegistryConfig | {} {
let registriesList = { ...registries, ...config.nrm }; // 源注册表
let registryName = "";
let _authtoken = config.npm[`${registry.replace(/^https?:/, "")}:_authToken`]; // 当前源的用户账号令牌
let _auth = config.npm[`${registry.replace(/^https?:/, "")}:_auth`]; // 当前源的用户账号
// 获取当前源别名
for (let key in registriesList) {
let currentRegistry = registriesList[key]?.registry?.replace(/^https?:\/\/|\/*$/g, "");
Expand All @@ -111,7 +112,8 @@ export function getRegistryConfig(config: BaseConfig): RegistryConfig | {} {
return {
registry,
registryName,
_authtoken
_authtoken,
_auth
};
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ program
.action(prepareEnv(getUserList));
program
.command("use <name>")
.option("-t,--type <type>", printLog("command.switchAccountUseType", { isPrint: false }))
.description(printLog("command.switchAccount", { isPrint: false }))
.action(prepareEnv(changeUser));
program
Expand Down
1 change: 1 addition & 0 deletions src/lang/default/en/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"listLs": "show detail",
"listAll": "show all account details",
"switchAccount": "switch account",
"switchAccountUseType": "switch account use a type",
"addAccount": "add account",
"removeAccount": "remove account",
"localizedLang": "use localized languages",
Expand Down
1 change: 1 addition & 0 deletions src/lang/default/zh/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"listLs": "显示详细",
"listAll": "显示所有账号详细",
"switchAccount": "切换账号",
"switchAccountUseType": "切换账号用某种类型",
"addAccount": "添加账号",
"removeAccount": "移除账号",
"localizedLang": "使用本地化语言",
Expand Down

0 comments on commit 95a02f6

Please sign in to comment.