Skip to content

Commit df2c456

Browse files
committed
feat: support deleting multiple custom registries
1 parent 3089f30 commit df2c456

File tree

6 files changed

+103
-24
lines changed

6 files changed

+103
-24
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Usage: nrm [options] [command]
7171
set <registryName> Set custom registry attribute
7272
-a --attr <attr> Set custom registry attribute
7373
-v --value <value> Set custom registry value
74-
del <registry> Delete one custom registry
74+
del [registry] Delete one custom registry
7575
rename <registryName> <newName> Set custom registry name
7676
home <registry> [browser] Open the homepage of registry with optional browser
7777
publish [<tarball>|<folder>] Publish package to current registry if current registry is a custom registry. The field 'repository' of current custom registry is required running this command. If you're not using custom registry, this command will run npm publish directly

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"scripts": {
1717
"clean": "rimraf node_modules dist",
1818
"test": "pnpm build && vitest run",
19+
"dev": "tsc --watch",
1920
"build": "rimraf dist && tsc && rimraf dist/types.js",
2021
"test:watch": "vitest",
2122
"changeset:version": "changeset version",
@@ -33,6 +34,7 @@
3334
},
3435
"homepage": "https://github.com/Pana/nrm",
3536
"dependencies": {
37+
"@inquirer/checkbox": "^4.0.3",
3638
"@inquirer/select": "^4.0.2",
3739
"chalk": "4.1.2",
3840
"commander": "^8.3.0",

pnpm-lock.yaml

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions.ts

+49-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import checkbox from '@inquirer/checkbox';
12
import select from '@inquirer/select';
23
import chalk from 'chalk';
34
import open from 'open';
@@ -56,14 +57,18 @@ export async function onCurrent({ showUrl }: { showUrl: boolean }) {
5657
if (!matchedRegistry) {
5758
printMessages([
5859
`Your current registry(${currentRegistry}) is not included in the nrm registries.`,
59-
`Use the ${chalk.green('nrm add <registry> <url> [home]')} command to add your registry.`,
60+
`Use the ${chalk.green(
61+
'nrm add <registry> <url> [home]',
62+
)} command to add your registry.`,
6063
]);
6164
return;
6265
}
6366

6467
const [name, registry] = matchedRegistry;
6568
printMessages([
66-
`You are using ${chalk.green(showUrl ? registry[REGISTRY] : name)} registry.`,
69+
`You are using ${chalk.green(
70+
showUrl ? registry[REGISTRY] : name,
71+
)} registry.`,
6772
]);
6873
}
6974

@@ -73,7 +78,7 @@ export async function onUse(name: string) {
7378

7479
// if alias is undefined, select the registry alias from list
7580
if (alias === undefined) {
76-
alias = await select({
81+
alias = await select<string>({
7782
message: 'Please select the registry you want to use',
7883
choices: Object.keys(registries),
7984
pageSize: 10,
@@ -91,23 +96,45 @@ export async function onUse(name: string) {
9196
printSuccess(`The registry has been changed to '${alias}'.`);
9297
}
9398

94-
export async function onDelete(name: string) {
95-
if (
96-
(await isRegistryNotFound(name)) ||
97-
(await isInternalRegistry(name, 'delete'))
98-
) {
99+
export async function onDelete(name: string | undefined) {
100+
const customRegistries = await readFile(NRMRC);
101+
102+
const deleteKeys: string[] = [];
103+
if (name) {
104+
deleteKeys.push(name);
105+
}
106+
107+
const choices = Object.keys(customRegistries);
108+
if (name === undefined && !choices.length) {
109+
printMessages(['No any custom registries can be deleted.']);
99110
return;
100111
}
101112

102-
const customRegistries = await readFile(NRMRC);
103-
const registry = customRegistries[name];
104-
delete customRegistries[name];
105-
await writeFile(NRMRC, customRegistries);
106-
printSuccess(`The registry '${name}' has been deleted successfully.`);
113+
if (name === undefined) {
114+
const selectedKeys = await checkbox<string>({
115+
message: 'Please select the registries you want to delete',
116+
choices,
117+
});
118+
deleteKeys.push(...selectedKeys);
119+
}
107120

108-
const currentRegistry = await getCurrentRegistry();
109-
if (currentRegistry === registry[REGISTRY]) {
110-
await onUse('npm');
121+
for (const key of deleteKeys) {
122+
if (
123+
(await isRegistryNotFound(key)) ||
124+
(await isInternalRegistry(key, 'delete'))
125+
) {
126+
continue;
127+
}
128+
129+
const registry = customRegistries[key];
130+
delete customRegistries[key];
131+
await writeFile(NRMRC, customRegistries);
132+
printSuccess(`The registry '${key}' has been deleted successfully.`);
133+
134+
const currentRegistry = await getCurrentRegistry();
135+
if (currentRegistry === registry[REGISTRY]) {
136+
await onUse('npm');
137+
}
111138
}
112139
}
113140

@@ -135,7 +162,9 @@ export async function onAdd(name: string, url: string, home?: string) {
135162
});
136163
await writeFile(NRMRC, newCustomRegistries);
137164
printSuccess(
138-
`Add registry ${name} success, run ${chalk.green(`nrm use ${name}`)} command to use ${name} registry.`,
165+
`Add registry ${name} success, run ${chalk.green(
166+
`nrm use ${name}`,
167+
)} command to use ${name} registry.`,
139168
);
140169
}
141170

@@ -250,7 +279,9 @@ export async function onSetAttribute(
250279

251280
if (REPOSITORY === attr) {
252281
return exit(
253-
`Use the ${chalk.green('nrm set-hosted-repo <name> <repo>')} command to set repository.`,
282+
`Use the ${chalk.green(
283+
'nrm set-hosted-repo <name> <repo>',
284+
)} command to set repository.`,
254285
);
255286
}
256287
const customRegistries = await readFile(NRMRC);

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ program
8282
.action(onRename);
8383

8484
program
85-
.command('del <name>')
85+
.command('del [name]')
8686
.description('Delete custom registry')
8787
.action(onDelete);
8888

tests/cli.test.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
} from 'vitest';
1616

1717
import { onHome, onTest } from '../src/actions';
18-
import { readFile, writeFile } from '../src/helpers';
1918
import { NPMRC, REGISTRIES } from '../src/constants';
19+
import { readFile, writeFile } from '../src/helpers';
2020

2121
const isWin = process.platform === 'win32';
2222

@@ -98,7 +98,11 @@ it('nrm use <registry> local', async () => {
9898

9999
expect(npmrc.registry).toBe(REGISTRIES.cnpm.registry);
100100

101-
await coffee.spawn('nrm', ['current'], { shell: isWin }).expect('stdout', /cnpm/g).expect('code', 0).end();
101+
await coffee
102+
.spawn('nrm', ['current'], { shell: isWin })
103+
.expect('stdout', /cnpm/g)
104+
.expect('code', 0)
105+
.end();
102106
});
103107

104108
it('nrm use <registry> local with user config', async () => {
@@ -115,7 +119,11 @@ it('nrm use <registry> local with user config', async () => {
115119
expect(npmrc.registry).toBe(REGISTRIES.cnpm.registry);
116120
expect(npmrc.abc).toBe('123');
117121

118-
await coffee.spawn('nrm', ['current'], { shell: isWin }).expect('stdout', /cnpm/g).expect('code', 0).end();
122+
await coffee
123+
.spawn('nrm', ['current'], { shell: isWin })
124+
.expect('stdout', /cnpm/g)
125+
.expect('code', 0)
126+
.end();
119127
});
120128

121129
it('nrm use without argument', async () => {
@@ -127,7 +135,9 @@ it('nrm use without argument', async () => {
127135
});
128136
});
129137

130-
expect(message).toBe(`? Please select the registry you want to use (Use arrow keys)
138+
expect(
139+
message,
140+
).toBe(`? Please select the registry you want to use (Use arrow keys)
131141
${isWin ? '>' : '❯'} npm
132142
yarn
133143
tencent

0 commit comments

Comments
 (0)