Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimize onCurrent function #195

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ async function onList() {

async function onCurrent({ showUrl }) {
const currentRegistry = await getCurrentRegistry();
let usingUnknownRegistry = true;
const registries = await getRegistries();
for (const name in registries) {
const registry = registries[name];
if (isLowerCaseEqual(registry[REGISTRY], currentRegistry)) {
usingUnknownRegistry = false;
printMessages([`You are using ${chalk.green(showUrl ? registry[REGISTRY] : name)} registry.`]);
}
}
if (usingUnknownRegistry) {

const matchedRegistry = Object.entries(registries).find(([_name, registry]) =>
isLowerCaseEqual(registry[REGISTRY], currentRegistry),
);

// not find equal registry
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.`,
]);
return;
}

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

async function onUse(name) {
Expand Down