Skip to content

Commit

Permalink
Merge pull request #195 from chouchouji/refactor-current-command
Browse files Browse the repository at this point in the history
refactor: optimize onCurrent function
  • Loading branch information
iosh authored Nov 23, 2024
2 parents 04dbe9d + 157b451 commit 193b1bf
Showing 1 changed file with 11 additions and 9 deletions.
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

0 comments on commit 193b1bf

Please sign in to comment.