refactor and add list_snippets

This commit is contained in:
pablof7z
2025-03-31 12:26:50 +01:00
parent 6d0e5a8e79
commit f74736191a
15 changed files with 494 additions and 190 deletions

View File

@@ -1,43 +1,32 @@
import { Command } from 'commander';
import { ndk } from '../ndk.js';
import { knownUsers } from '../users.js';
import { identifierToPubkeys } from '../lib/nostr/utils.js';
import { Command } from "commander";
import { toPubkeys, formatUser } from "../lib/converters/index.js";
export function registerFindUserCommand(program: Command): void {
program
.command('find-user')
.description('Find a user by identifier')
.argument('<query>', 'User identifier to search for')
.action(async (query: string) => {
try {
const pubkeys = identifierToPubkeys(query);
if (pubkeys.length > 0) {
const result = pubkeys.map(formatUser).join('\n\n---\n\n');
console.log(result);
} else {
console.log("No user found matching the query.");
}
} catch (error) {
console.error('Error executing find-user command:', error);
process.exit(1);
// Create a command for finding a user
const findUserCommand = new Command("find-user")
.description("Find a user by name, npub, or other profile information")
.argument("<query>", "The search query to find a user")
.action(async (query: string) => {
try {
// Find matching pubkeys
const pubkeys = toPubkeys(query);
if (pubkeys.length === 0) {
console.log(`No users found matching query: ${query}`);
return;
}
});
}
// Helper function to format user profiles
function formatUser(pubkey: string) {
const profile = knownUsers[pubkey]?.profile;
const user = ndk.getUser({ pubkey });
const keys: Record<string, string> = {
Npub: user.npub,
};
// Format and display each matching user
console.log(`Found ${pubkeys.length} matching users:`);
for (let i = 0; i < pubkeys.length; i++) {
if (i > 0) console.log("\n---\n");
const pubkey = pubkeys[i];
if (pubkey) {
console.log(formatUser(pubkey));
}
}
} catch (error) {
console.error("Error:", error);
}
});
if (profile?.name) keys.Name = profile.name;
if (profile?.about) keys.About = profile.about;
if (profile?.picture) keys.Picture = profile.picture;
return Object.entries(keys)
.map(([key, value]) => `${key}: ${value}`)
.join("\n");
}
export default findUserCommand;