Skip to content

Commit

Permalink
Merge pull request #191 from chouchouji/feat-use-select
Browse files Browse the repository at this point in the history
feat: allow to select from registry list if name is undefined
  • Loading branch information
iosh authored Nov 24, 2024
2 parents 193b1bf + 84f7e95 commit da2d723
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-peas-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nrm': minor
---

Added select npm registry by keyboard
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ $ nrm use cnpm //switch registry to cnpm

## Usage

> [!TIP]
> The lowest node version is ***18*** from ***v1.3.2***.
```
Usage: nrm [options] [command]
Expand All @@ -49,7 +52,7 @@ Usage: nrm [options] [command]
ls List all the registries
current Show current registry name
-u --show-url Show the registry URL instead of the name
use <registry> Change registry to registry
use [registry] Change registry to registry
add <registry> <url> [home] Add one custom registry
login <registry> [value] Set authorize information for a registry with a base64 encoded string or username and password
-a --always-auth Set is always auth
Expand Down
12 changes: 11 additions & 1 deletion actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const open = require('open');
const chalk = require('chalk');
const select = require('@inquirer/select').default;
const { fetch } = require('undici');
const {
exit,
Expand Down Expand Up @@ -54,11 +55,20 @@ async function onCurrent({ showUrl }) {
}

async function onUse(name) {
const registries = await getRegistries();

// if name is undefined, select the registry alias from list
if (name === undefined) {
name = await select({
message: 'Please select the registry you want to use',
choices: Object.keys(registries),
});
}

if (await isRegistryNotFound(name)) {
return;
}

const registries = await getRegistries();
const registry = registries[name];
const npmrc = await readFile(NPMRC);
await writeFile(NPMRC, Object.assign(npmrc, registry));
Expand Down
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ program
.description('Show current registry name or URL')
.action(actions.onCurrent);

program.command('use <name>').description('Change current registry').action(actions.onUse);
program.command('use [name]').description('Change current registry').action(actions.onUse);

program.command('add <name> <url> [home]').description('Add custom registry').action(actions.onAdd);

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"homepage": "https://github.com/Pana/nrm",
"dependencies": {
"@inquirer/select": "^4.0.2",
"chalk": "^4.1.2",
"commander": "^8.3.0",
"ini": "^2.0.0",
Expand All @@ -38,5 +39,8 @@
"coffee": "^5.4.0",
"jest": "^27.4.0",
"prettier": "^3.3.3"
},
"engines": {
"node": ">=18"
}
}
24 changes: 22 additions & 2 deletions tests/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const coffee = require('coffee');
const open = require('open');
const chalk = require('chalk');

const { onHome, onTest } = require('.././actions.js');

const { spawn } = require('node:child_process');
const stripAnsi = require('strip-ansi');
const isWin = process.platform === 'win32';

jest.setTimeout(20000);
Expand Down Expand Up @@ -58,6 +58,26 @@ it('nrm use <registry>', async () => {
.end();
});

it('nrm use without argument', async () => {
const { stdout } = spawn('nrm', ['use'], { shell: isWin });

const message = await new Promise((resolve) => {
stdout.on('data', (data) => {
resolve(stripAnsi(data.toString()).trim());
});
});

expect(message).toBe(`? Please select the registry you want to use (Use arrow keys)
${isWin ? '>' : '❯'} npm
yarn
tencent
cnpm
taobao
npmMirror
huawei`);

});

it('nrm current', async () => {
await coffee
.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
Expand Down
67 changes: 66 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,42 @@
resolved "https://r.cnpmjs.org/@fastify/busboy/-/busboy-2.1.1.tgz#b9da6a878a371829a0502c9b6c1c143ef6663f4d"
integrity sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==

"@inquirer/core@^10.1.0":
version "10.1.0"
resolved "https://registry.yarnpkg.com/@inquirer/core/-/core-10.1.0.tgz#c5fdc34c4cafd7248da29a3c3b3120fe6e1c45be"
integrity sha512-I+ETk2AL+yAVbvuKx5AJpQmoaWhpiTFOg/UJb7ZkMAK4blmtG8ATh5ct+T/8xNld0CZG/2UhtkdMwpgvld92XQ==
dependencies:
"@inquirer/figures" "^1.0.8"
"@inquirer/type" "^3.0.1"
ansi-escapes "^4.3.2"
cli-width "^4.1.0"
mute-stream "^2.0.0"
signal-exit "^4.1.0"
strip-ansi "^6.0.1"
wrap-ansi "^6.2.0"
yoctocolors-cjs "^2.1.2"

"@inquirer/figures@^1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@inquirer/figures/-/figures-1.0.8.tgz#d9e414a1376a331a0e71b151fea27c48845788b0"
integrity sha512-tKd+jsmhq21AP1LhexC0pPwsCxEhGgAkg28byjJAd+xhmIs8LUX8JbUc3vBf3PhLxWiB5EvyBE5X7JSPAqMAqg==

"@inquirer/select@^4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@inquirer/select/-/select-4.0.2.tgz#c38ef154524a6859de4a1af11a90ad3f9638c9f2"
integrity sha512-uSWUzaSYAEj0hlzxa1mUB6VqrKaYx0QxGBLZzU4xWFxaSyGaXxsSE4OSOwdU24j0xl8OajgayqFXW0l2bkl2kg==
dependencies:
"@inquirer/core" "^10.1.0"
"@inquirer/figures" "^1.0.8"
"@inquirer/type" "^3.0.1"
ansi-escapes "^4.3.2"
yoctocolors-cjs "^2.1.2"

"@inquirer/type@^3.0.1":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@inquirer/type/-/type-3.0.1.tgz#619ce9f65c3e114d8e39c41822bed3440d20b478"
integrity sha512-+ksJMIy92sOAiAccGpcKZUc3bYO07cADnscIxHBknEm3uNts3movSmBofc1908BNy5edKscxYeAdaX1NXkHS6A==

"@istanbuljs/load-nyc-config@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
Expand Down Expand Up @@ -911,7 +947,7 @@ ansi-colors@^4.1.1, ansi-colors@^4.1.3:
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.3.tgz#37611340eb2243e70cc604cad35d63270d48781b"
integrity sha512-/6w/C21Pm1A7aZitlI5Ni/2J6FFQN8i1Cvz3kHABAAbw93v/NlvKdVOqz7CCWz/3iv/JplRSEEZ83XION15ovw==

ansi-escapes@^4.2.1:
ansi-escapes@^4.2.1, ansi-escapes@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-4.3.2.tgz#6b2291d1db7d98b6521d5f1efa42d0f3a9feb65e"
integrity sha512-gKXj5ALrKWQLsYG9jlTRmR/xKluxHV+Z9QEwNIgCfM1/uwPMCuzVVnh5mwTd+OuBZcwSIMbqssNWRm1lE51QaQ==
Expand Down Expand Up @@ -1142,6 +1178,11 @@ cjs-module-lexer@^1.0.0:
resolved "https://registry.yarnpkg.com/cjs-module-lexer/-/cjs-module-lexer-1.4.1.tgz#707413784dbb3a72aa11c2f2b042a0bef4004170"
integrity sha512-cuSVIHi9/9E/+821Qjdvngor+xpnlwnuwIyZOaLmHBVdXL+gP+I6QQB9VkO7RI77YIcTV+S1W9AreJ5eN63JBA==

cli-width@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-4.1.0.tgz#42daac41d3c254ef38ad8ac037672130173691c5"
integrity sha512-ouuZd4/dm2Sw5Gmqy6bGyNNNe1qt9RpmxveLSO7KcgsTnU7RXfsw+/bukWGo1abgBiMAic068rclZsO4IWmmxQ==

cliui@^7.0.2:
version "7.0.4"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-7.0.4.tgz#a0265ee655476fc807aea9df3df8df7783808b4f"
Expand Down Expand Up @@ -2434,6 +2475,11 @@ ms@^2.1.3:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

mute-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-2.0.0.tgz#a5446fc0c512b71c83c44d908d5c7b7b4c493b2b"
integrity sha512-WWdIxpyjEn+FhQJQQv9aQAYlHoNVdzIzUySNV1gHUPDSdZJ3yZn7pAAbQcV7B56Mvu881q9FZV+0Vx2xC44VWA==

natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
Expand Down Expand Up @@ -2795,6 +2841,11 @@ signal-exit@^3.0.2, signal-exit@^3.0.3:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9"
integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==

signal-exit@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-4.1.0.tgz#952188c1cbd546070e2dd20d0f41c0ae0530cb04"
integrity sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==

sisteransi@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"
Expand Down Expand Up @@ -3124,6 +3175,15 @@ which@^2.0.1:
dependencies:
isexe "^2.0.0"

wrap-ansi@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-6.2.0.tgz#e9393ba07102e6c91a3b221478f0257cd2856e53"
integrity sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
Expand Down Expand Up @@ -3195,3 +3255,8 @@ yargs@^16.2.0:
string-width "^4.2.0"
y18n "^5.0.5"
yargs-parser "^20.2.2"

yoctocolors-cjs@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/yoctocolors-cjs/-/yoctocolors-cjs-2.1.2.tgz#f4b905a840a37506813a7acaa28febe97767a242"
integrity sha512-cYVsTjKl8b+FrnidjibDWskAv7UKOfcwaVZdp/it9n1s9fU3IkgDbhdIRKCW4JDsAlECJY0ytoVPT3sK6kideA==

0 comments on commit da2d723

Please sign in to comment.