Skip to content

Commit

Permalink
feat: support deleting multiple custom registries
Browse files Browse the repository at this point in the history
  • Loading branch information
chouchouji committed Dec 10, 2024
1 parent 3089f30 commit df2c456
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Usage: nrm [options] [command]
set <registryName> Set custom registry attribute
-a --attr <attr> Set custom registry attribute
-v --value <value> Set custom registry value
del <registry> Delete one custom registry
del [registry] Delete one custom registry
rename <registryName> <newName> Set custom registry name
home <registry> [browser] Open the homepage of registry with optional browser
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
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"scripts": {
"clean": "rimraf node_modules dist",
"test": "pnpm build && vitest run",
"dev": "tsc --watch",
"build": "rimraf dist && tsc && rimraf dist/types.js",
"test:watch": "vitest",
"changeset:version": "changeset version",
Expand All @@ -33,6 +34,7 @@
},
"homepage": "https://github.com/Pana/nrm",
"dependencies": {
"@inquirer/checkbox": "^4.0.3",
"@inquirer/select": "^4.0.2",
"chalk": "4.1.2",
"commander": "^8.3.0",
Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 49 additions & 18 deletions src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import checkbox from '@inquirer/checkbox';
import select from '@inquirer/select';
import chalk from 'chalk';
import open from 'open';
Expand Down Expand Up @@ -56,14 +57,18 @@ export async function onCurrent({ showUrl }: { showUrl: boolean }) {
if (!matchedRegistry) {
printMessages([
`Your current registry(${currentRegistry}) is not included in the nrm registries.`,
`Use the ${chalk.green('nrm add <registry> <url> [home]')} command to add your registry.`,
`Use the ${chalk.green(
'nrm add <registry> <url> [home]',
)} command to add your registry.`,
]);
return;
}

const [name, registry] = matchedRegistry;
printMessages([
`You are using ${chalk.green(showUrl ? registry[REGISTRY] : name)} registry.`,
`You are using ${chalk.green(
showUrl ? registry[REGISTRY] : name,
)} registry.`,
]);
}

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

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

export async function onDelete(name: string) {
if (
(await isRegistryNotFound(name)) ||
(await isInternalRegistry(name, 'delete'))
) {
export async function onDelete(name: string | undefined) {
const customRegistries = await readFile(NRMRC);

const deleteKeys: string[] = [];
if (name) {
deleteKeys.push(name);
}

const choices = Object.keys(customRegistries);
if (name === undefined && !choices.length) {
printMessages(['No any custom registries can be deleted.']);
return;
}

const customRegistries = await readFile(NRMRC);
const registry = customRegistries[name];
delete customRegistries[name];
await writeFile(NRMRC, customRegistries);
printSuccess(`The registry '${name}' has been deleted successfully.`);
if (name === undefined) {
const selectedKeys = await checkbox<string>({
message: 'Please select the registries you want to delete',
choices,
});
deleteKeys.push(...selectedKeys);
}

const currentRegistry = await getCurrentRegistry();
if (currentRegistry === registry[REGISTRY]) {
await onUse('npm');
for (const key of deleteKeys) {
if (
(await isRegistryNotFound(key)) ||
(await isInternalRegistry(key, 'delete'))
) {
continue;
}

const registry = customRegistries[key];
delete customRegistries[key];
await writeFile(NRMRC, customRegistries);
printSuccess(`The registry '${key}' has been deleted successfully.`);

const currentRegistry = await getCurrentRegistry();
if (currentRegistry === registry[REGISTRY]) {
await onUse('npm');
}
}
}

Expand Down Expand Up @@ -135,7 +162,9 @@ export async function onAdd(name: string, url: string, home?: string) {
});
await writeFile(NRMRC, newCustomRegistries);
printSuccess(
`Add registry ${name} success, run ${chalk.green(`nrm use ${name}`)} command to use ${name} registry.`,
`Add registry ${name} success, run ${chalk.green(
`nrm use ${name}`,
)} command to use ${name} registry.`,
);
}

Expand Down Expand Up @@ -250,7 +279,9 @@ export async function onSetAttribute(

if (REPOSITORY === attr) {
return exit(
`Use the ${chalk.green('nrm set-hosted-repo <name> <repo>')} command to set repository.`,
`Use the ${chalk.green(
'nrm set-hosted-repo <name> <repo>',
)} command to set repository.`,
);
}
const customRegistries = await readFile(NRMRC);
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ program
.action(onRename);

program
.command('del <name>')
.command('del [name]')
.description('Delete custom registry')
.action(onDelete);

Expand Down
18 changes: 14 additions & 4 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
} from 'vitest';

import { onHome, onTest } from '../src/actions';
import { readFile, writeFile } from '../src/helpers';
import { NPMRC, REGISTRIES } from '../src/constants';
import { readFile, writeFile } from '../src/helpers';

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

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

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

await coffee.spawn('nrm', ['current'], { shell: isWin }).expect('stdout', /cnpm/g).expect('code', 0).end();
await coffee
.spawn('nrm', ['current'], { shell: isWin })
.expect('stdout', /cnpm/g)
.expect('code', 0)
.end();
});

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

await coffee.spawn('nrm', ['current'], { shell: isWin }).expect('stdout', /cnpm/g).expect('code', 0).end();
await coffee
.spawn('nrm', ['current'], { shell: isWin })
.expect('stdout', /cnpm/g)
.expect('code', 0)
.end();
});

it('nrm use without argument', async () => {
Expand All @@ -127,7 +135,9 @@ it('nrm use without argument', async () => {
});
});

expect(message).toBe(`? Please select the registry you want to use (Use arrow keys)
expect(
message,
).toBe(`? Please select the registry you want to use (Use arrow keys)
${isWin ? '>' : '❯'} npm
yarn
tencent
Expand Down

0 comments on commit df2c456

Please sign in to comment.