|
| 1 | +import { and, eq, like } from "drizzle-orm"; |
| 2 | +import { Users } from "~drizzle/schema"; |
| 3 | +import type { User } from "~packages/database-interface/user"; |
| 4 | +import { BaseCommand } from "./base"; |
| 5 | +import { Args, Flags, type Command, type Interfaces } from "@oclif/core"; |
| 6 | + |
| 7 | +export type FlagsType<T extends typeof Command> = Interfaces.InferredFlags< |
| 8 | + (typeof BaseCommand)["baseFlags"] & T["flags"] |
| 9 | +>; |
| 10 | +export type ArgsType<T extends typeof Command> = Interfaces.InferredArgs< |
| 11 | + T["args"] |
| 12 | +>; |
| 13 | + |
| 14 | +export abstract class UserFinderCommand< |
| 15 | + T extends typeof BaseCommand, |
| 16 | +> extends BaseCommand<typeof UserFinderCommand> { |
| 17 | + static baseFlags = { |
| 18 | + pattern: Flags.boolean({ |
| 19 | + char: "p", |
| 20 | + description: |
| 21 | + "Process as a wildcard pattern (don't forget to escape)", |
| 22 | + }), |
| 23 | + type: Flags.string({ |
| 24 | + char: "t", |
| 25 | + description: "Type of identifier", |
| 26 | + options: ["id", "username", "note", "display-name", "email"], |
| 27 | + default: "id", |
| 28 | + }), |
| 29 | + limit: Flags.integer({ |
| 30 | + char: "n", |
| 31 | + description: "Limit the number of users", |
| 32 | + default: 100, |
| 33 | + }), |
| 34 | + print: Flags.boolean({ |
| 35 | + allowNo: true, |
| 36 | + default: true, |
| 37 | + char: "P", |
| 38 | + description: "Print user(s) found before processing", |
| 39 | + }), |
| 40 | + }; |
| 41 | + |
| 42 | + static baseArgs = { |
| 43 | + identifier: Args.string({ |
| 44 | + description: |
| 45 | + "Identifier of the user (by default this must be an ID)", |
| 46 | + required: true, |
| 47 | + }), |
| 48 | + }; |
| 49 | + |
| 50 | + protected flags!: FlagsType<T>; |
| 51 | + protected args!: ArgsType<T>; |
| 52 | + |
| 53 | + public async init(): Promise<void> { |
| 54 | + await super.init(); |
| 55 | + const { args, flags } = await this.parse({ |
| 56 | + flags: this.ctor.flags, |
| 57 | + baseFlags: (super.ctor as typeof BaseCommand).baseFlags, |
| 58 | + args: this.ctor.args, |
| 59 | + strict: this.ctor.strict, |
| 60 | + }); |
| 61 | + this.flags = flags as FlagsType<T>; |
| 62 | + this.args = args as ArgsType<T>; |
| 63 | + } |
| 64 | + |
| 65 | + public async findUsers(): Promise<User[]> { |
| 66 | + const operator = this.flags.pattern ? like : eq; |
| 67 | + // Replace wildcards with an SQL LIKE pattern |
| 68 | + const identifier = this.flags.pattern |
| 69 | + ? this.args.identifier.replace(/\*/g, "%") |
| 70 | + : this.args.identifier; |
| 71 | + |
| 72 | + const { User } = await import("~packages/database-interface/user"); |
| 73 | + |
| 74 | + return await User.manyFromSql( |
| 75 | + and( |
| 76 | + this.flags.type === "id" |
| 77 | + ? operator(Users.id, identifier) |
| 78 | + : undefined, |
| 79 | + this.flags.type === "username" |
| 80 | + ? operator(Users.username, identifier) |
| 81 | + : undefined, |
| 82 | + this.flags.type === "note" |
| 83 | + ? operator(Users.note, identifier) |
| 84 | + : undefined, |
| 85 | + this.flags.type === "display-name" |
| 86 | + ? operator(Users.displayName, identifier) |
| 87 | + : undefined, |
| 88 | + this.flags.type === "email" |
| 89 | + ? operator(Users.email, identifier) |
| 90 | + : undefined, |
| 91 | + ), |
| 92 | + undefined, |
| 93 | + this.flags.limit, |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
0 commit comments