import { Command } from "@commander-js/extra-typings"; import { App, apps } from "./app"; import fetch from "./actions/fetch"; import add from "./actions/add"; import remove from "./actions/delete"; export const bot = new Command("bot").description( "Creates, deletes, and list users on different apps.", ); type Options = { email?: string; password?: string; }; const getUser = ( options: Options, app: App, ) => { let email = options.email; if (!email) { email = process.env[app.userAuthEnvVars.email]; if (!email) { throw `No email was provided. You can either pass one through the --password option, or set it up in the ${app.userAuthEnvVars.email} environment variable`; } else { console.log( `No email was provided to the CLI, falling back on using the ${app.userAuthEnvVars.email} environment variable`, ); } } let password = options.password; if (!password) { password = process.env[app.userAuthEnvVars.password]; if (!password) { throw `No password was provided. You can either pass one through the --password option, or set it up in the ${app.userAuthEnvVars.password} environment variable`; } else { console.log( `No password was provided to the CLI, falling back on using the ${app.userAuthEnvVars.password} environment variable`, ); } } return { email, password }; }; bot .command("list") .description("Fetches a list of users.") .argument("app") .option("--email ", "The email used to log into the app") .option("--password ", "The password used to log into the app") .action(async (appName, options) => { if (appName in apps) { const app = apps[appName as keyof typeof apps]; const user = getUser(options, app); await fetch(app, user, { log: console.log }); } else { throw [ `${appName} is not an available app. Available apps are:`, ...Object.keys(apps).map((app) => `- ${app}`), ].join("\n"); } }); bot .command("add") .description("Adds a user to the app.") .argument("app") .argument("email", "The email of the user to add") .argument("fullName", "The full name of the user to add") .option("--email ", "The email used to log into the app") .option("--password ", "The password used to log into the app") .action(async (appName, email, fullName, options) => { if (appName in apps) { const app = apps[appName as keyof typeof apps]; const user = getUser(options, app); await add( app, user, { fullName, email, }, { log: console.log }, ); } else { throw [ `${appName} is not an available app. Available apps are:`, ...Object.keys(apps).map((app) => `- ${app}`), ].join("\n"); } }); bot .command("remove") .description("Removes a user from the app.") .argument("app") .argument("email", "The email of the user to remove") .option("--email ", "The email used to log into the app") .option("--password ", "The password used to log into the app") .action(async (appName, email, options) => { if (appName in apps) { const app = apps[appName as keyof typeof apps]; const user = getUser(options, app); await remove( app, user, { email, }, { log: console.log }, ); } else { throw [ `${appName} is not an available app. Available apps are:`, ...Object.keys(apps).map((app) => `- ${app}`), ].join("\n"); } }); bot .command("test") .description("Tests an app.") .argument("app") .option("--email ", "The email used to log into the app") .option("--password ", "The password used to log into the app") .action(async (appName, options) => { if (appName in apps) { const app = apps[appName as keyof typeof apps]; const user = getUser(options, app); const collaborators = await fetch(app, user, { log: console.log, }); const testCollaborator = { fullName: "Test User", email: `test+${collaborators.length + 1}@test.com`, }; await add(app, user, testCollaborator, { log: console.log }); await remove(app, user, testCollaborator, { log: console.log }); } else { throw [ `${appName} is not an available app. Available apps are:`, ...Object.keys(apps).map((app) => `- ${app}`), ].join("\n"); } });